Git Product home page Git Product logo

teapot's Introduction

Teapot

Teapot is micro web framework for Pharo Smalltalk on top of the Zinc HTTP components, that focuses on simplicity and ease of use. It's around 600 lines of code, not counting the tests.

Explore the docs

Unit Tests Coverage Status Baseline Groups Markdown Lint

GitHub release Pharo 9.0 Pharo 10 Pharo 11

Name origin: 418 I'm a teapot (RFC 2324) is an HTTP status code.

This code was defined in 1998 as one of the traditional IETF April Fools' jokes, in RFC 2324, Hyper Text Coffee Pot Control Protocol. The RFC specifies this code should be returned by tea pots requested to brew coffee.

License

  • The code is licensed under MIT.
  • The documentation is licensed under CC BY-SA 4.0.

Quick Start

Metacello new
  baseline: 'Teapot';
  repository: 'github://zeroflag/Teapot/source';
  load.

Installation

To load the project in a Pharo image, or declare it as a dependency of your own project follow this instructions

Contributing

Check the Contribution Guidelines

Other

If you want to lively work with Teapot or quickly implement REST services with it, we recommend having a look at the Tealight project a thin layer on top of Teapot to quickly experiment and deliver

teapot's People

Contributors

astares avatar demarey avatar ducasse avatar fortizpenaloza avatar gcotelli avatar zeroflag avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

teapot's Issues

POST/PUT Request Bodies

How can one access the request body in a POST or PUT block? The following only seems to work for x-www-form-urlencoded parameters:

POST: '/' -> [ :req | ^(req at: #foo)]

if I POST: foo: something as x-www-form-urlencoded.

But what if I want to post a JSON body, like {"foo": "something"}? I can't seem to find a way to access that.

404 response instead of 405

If you define a route and do a request against this route but using an unexpected HTTP method, Teapot is responding with 404 Not Found.

As far as I understand in that case the response should be 405 Method Not Allowed, for example:

Teapot on
    GET: '/welcome' -> 'Hello World!';
    start.
	
ZnClient new
	beOneShot;
	enforceHttpSuccess: true;
	options: 'http://localhost:1701/welcome' asZnUrl  

Here the resource exists, but the HTTP method used is invalid, so I think this should return 405.

I think part of the problem is that TeaDynamicRouter assume that if no route can handle the request then the resource is not found; but in this case the resource exists, what it's invalid is the Method used.

References:

Baseline dependency with ZincComponents

The baseline conflicts with the ZincHTTPComponent baseline that is loaded in the Pharo 11 base image. The dependency for this project is pointing to an incorrect repo path. According to the Zinc project it should say:

Metacello new
repository: 'github://svenvc/zinc/repository';
baseline: 'ZincHTTPComponents';
load.

this line in the baseline: 'ZincHTTPComponents' with: [ spec repository: 'github://svenvc/zinc' ];
should probably be replaced with: 'ZincHTTPComponents' with: [ spec repository: 'github://svenvc/zinc/repository' ];

TeaPadding does not understand #readStream

Hi,

I registered the following routes:

GET -> '/data-streams/identifier:IsUUID/data-points'
GET -> '/data-streams'
GET -> '/data-streams/identifier:IsUUID'

and performing a GET on '/data-streams' I've got a MNU from TeaPadding.

IsUUID implementation of #matchesTo: and IsInteger look alike, I changed the regex pattern.

In the stack I found a TeaRequestMatcher trying to match the request's URL '/data-streams' to '/data-streams/identifier:IsUUID/data-points' by padding the segments of the first one and getting the following collection ('data-streams', TeaPadding, TeaPadding).

Then tries to match them from left to right until it gets the MNU trying to match the type constraint IsUUID to the first TeaPadding.

I think I can easily fix it by changing the IsUUID class>>#matchesTo: implementation taking into account that a TeaPadding can show up but I find hard to image that I'm the first one running into this issue.

Am I missing something?

testTralingSlash is failing

testTralingSlash
	self pattern: '/a/b' matches: '/a/b/'.

It looks like the trimTrailingSlash method fails to trim the ending / because the last segment of the url is no longer a string but a character ($/).

trimTrailingSlash: urlSegments 
	^ (urlSegments isNotEmpty and: [ urlSegments last = '/' ])
		ifTrue: [ urlSegments allButLast ]
		ifFalse: [ urlSegments ]

World Menu Error on Pharo 7

The method Base64MimeConverter class>>#mimeDecodeToBytes: called from TeaIconProvider class>>#teapotIcon has been deprecated.
Please use #base64Decoded now. See issue #21937

Used teapots doesn't are GC collected

After execute the test on package Teapot-Test, there are 11 teapot instances that doesn't are GC collected. All server are closed, as can be see in the teapot inspector,
Captura de pantalla de 2020-08-07 19-51-45
but remains in the image.
Captura de pantalla de 2020-08-07 19-53-25

Is there a method to remove them from the image?

Default exception handler can't be set with custom headers

Hi Attila, I've started to use Teapot for a backend and it feels really great. Thanks for sharing all this.

While working with the authorization of this API, I've faced an issue on exceptions. I've noticed that any answer that was using abort: was having CORS issues.

Digging into it I've came across the initialization of Teapot itself:

Teapot>>initializeOptions: optionsDictionary
	dynamicRouter := TeaDynamicRouter new.
	staticRouter := TeaStaticRouter new.
	compositeRouter := TeaCompositeRouter routers: {dynamicRouter. staticRouter}.	
	defaultOutput := self responseTransformer: (optionsDictionary at: #defaultOutput ifAbsent: #html).									
	self exception: TeaAbort -> [ :abort :req | abort response teaTransform: [ :same | same ] request: req].

That exception: TeaAbort is deciding on the answer and because is the first error handler, even if you app tunes it to add headers, this one will be performed.

For these interested in a solution right now:

It doesn't have accessors to manipulate that from the outside so for now in my app I've solved it with this horrible hack:

App>>newHttpServer

	| newServer |
	newServer := Teapot configure: { 
			             (#defaultOutput -> #json).
			             (#port -> self class port).
			             (#debugMode -> self class debugMode).
			             (#bindAddress
			              -> (NetNameResolver addressForName: self class bind)) }.
	((newServer instVarNamed: 'compositeRouter') instVarNamed:
		 'errorhandlers') removeLast.
	^ newServer

So later the App can:

App>>setExceptionHandlers

	| origin |
	self teapot exception: TeaAbort -> [ :abort :req | 
		origin := req headers at: 'origin' ifAbsent: [ '*' ].

		abort response headers
			at: 'Access-Control-Allow-Credentials' put: 'true';
			at: 'Access-Control-Allow-Origin' put: origin;
			yourself.
		abort response teaTransform: [ :same | same ] request: req ].

With this, the frontend will not be blocking the 401 because of CORS.

I'd be happy to open a pull request with a change to make this but without needing to rely on hacking Teapot. Let me know if you have any suggestion on how. Thanks again.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.