aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_handler.erl
AgeCommit message (Collapse)Author
2019-04-03Bind erlang:get_stacktrace prior to making other callsSteve Strong
If we bind too late there might be an exception triggered in the terminate function and we will not get the correct stacktrace as a result.
2018-06-27Disable warnings for erlang:get_stacktrace/0 in OTP-21+Loïc Hoguin
2017-09-04Fix more documentation todosLoïc Hoguin
I have decided not to include a manual page for cowboy_stream_h at this point because it clashes with the cowboy_stream manual page. This decision will be revisited in the future.
2017-02-18Allow passing options to sub protocolsLoïc Hoguin
Before this commit we had an issue where configuring a Websocket connection was simply not possible without doing magic, adding callbacks or extra return values. The init/2 function only allowed setting hibernate and timeout options. After this commit, when switching to a different type of handler you can either return {module, Req, State} or {module, Req, State, Opts} where Opts is any value (as far as the sub protocol interface is concerned) and is ultimately checked by the custom handlers. A large protocol like Websocket would accept only a map there, with many different options, while a small interface like loop handlers would allow passing hibernate and nothing else. For Websocket, hibernate must be set from the websocket_init/1 callback, because init/2 executes in a separate process. Sub protocols now have two callbacks: one with the Opts value, one without. The loop handler code was largely reworked and simplified. It does not need to manage a timeout or read from the socket anymore, it's the job of the protocol code. A lot of unnecessary stuff was therefore removed. Websocket compression must now be enabled from the handler options instead of per listener. This means that a project can have two separate Websocket handlers with different options. Compression is still disabled by default, and the idle_timeout value was changed from inifnity to 60000 (60 seconds), as that's safer and is also a good value for mobile devices.
2017-01-02Welcome to 2017Loïc Hoguin
2017-01-02Numerous Dialyzer fixesLoïc Hoguin
2016-08-10Let the stream handler take care of crash handling and loggingLoïc Hoguin
2016-06-22Remove cowboy_req:lock/1Loïc Hoguin
2016-03-05Initial commit with connection/streamsLoïc Hoguin
Breaking changes with previous commit. This is a very large change, and I am giving up on making a single commit that fixes everything. More commits will follow slowly adding back features, introducing new tests and fixing the documentation. This change contains most of the work toward unifying the interface for handling both HTTP/1.1 and HTTP/2. HTTP/1.1 connections are now no longer 1 process per connection; instead by default 1 process per request is also created. This has a number of pros and cons. Because it has cons, we also allow users to use a lower-level API that acts on "streams" (requests/responses) directly at the connection process-level. If performance is a concern, one can always write a stream handler. The performance in this case will be even greater than with Cowboy 1, although all the special handlers are unavailable. When switching to Websocket, after the handler returns from init/2, Cowboy stops the stream and the Websocket protocol takes over the connection process. Websocket then calls websocket_init/2 for any additional initialization such as timers, because the process is different in init/2 and websocket_*/* functions. This however would allow us to use websocket_init/2 for sending messages on connect, instead of sending ourselves a message and be subject to races. Note that websocket_init/2 is optional. This is all a big change and while most of the tests pass, some functionality currently doesn't. SPDY is broken and will be removed soon in favor of HTTP/2. Automatic compression is currently disabled. The cowboy_req interface probably still have a few functions that need to be updated. The docs and examples do not refer the current functionality anymore. Everything will be fixed over time. Feedback is more than welcome. Open a ticket!
2015-07-27Add optional callbacksLoïc Hoguin
Mostly useful for REST, which has a ton. This is an initial commit, it still needs to be tested, but it's time to sleep.
2015-05-07Exit with exit/1 on handler exception and include class in reasonJames Fish
2014-09-30Improve handler interface and documentationLoïc Hoguin
This change simplifies a little more the sub protocols mechanism. Aliases have been removed. The renaming of loop handlers as long polling handlers has been reverted. Plain HTTP handlers now simply do their work in the init/2 callback. There is no specific code for them. Loop handlers now follow the same return value as Websocket, they use ok to continue and shutdown to stop. Terminate reasons for all handler types have been documented. The terminate callback is now appropriately called in all cases (or should be). Behaviors for all handler types have been moved in the module that implement them. This means that cowboy_handler replaces the cowboy_http_handler behavior, and similarly cowboy_loop replaces cowboy_loop_handler, cowboy_websocket replaces cowboy_websocket_handler. Finally cowboy_rest now has the start of a behavior in it and will have the full list of optional callbacks defined once Erlang 18.0 gets released. The guide has been reorganized and should be easier to follow.
2014-09-26Unify the init and terminate callbacksLoïc Hoguin
This set of changes is the first step to simplify the writing of handlers, by removing some extraneous callbacks and making others optional. init/3 is now init/2, its first argument being removed. rest_init/2 and rest_terminate/2 have been removed. websocket_init/3 and websocket_terminate/3 have been removed. terminate/3 is now optional. It is called regardless of the type of handler, including rest and websocket. The return value of init/2 changed. It now returns {Mod, Req, Opts} with Mod being either one of the four handler type or a custom module. It can also return extra timeout and hibernate options. The signature for sub protocols has changed, they now receive these extra timeout and hibernate options. Loop handlers are now implemented in cowboy_long_polling, and will be renamed throughout the project in a future commit.
2014-09-24Remove the error tuple return value for middlewaresLoïc Hoguin
It wasn't interesting compared to simply returning a halt tuple with an explicit reply.
2014-07-12Remove useless todo commentsLoïc Hoguin
2014-07-12Reply with 400 on header parsing crashLoïc Hoguin
This is a first step to improve the HTTP status codes returned by Cowboy on crashes. We will tweak it over time. Also fixes a small bug where two replies may have been sent when using loop handlers under rare conditions.
2014-06-30Update specs that were too wideLoïc Hoguin
2014-03-26Remove outdated comments, all edoc, plus a few minor tweaksLoïc Hoguin
2014-02-06Update copyright yearsLoïc Hoguin
2013-11-18Fix loop handler keepalive race conditionJames Fish
Previously if a loop handler received the timeout message from a previous request on the same connection the socket would be set to {active, once} incorrectly - when a socket packet was already in the message queue. This second packet would not be added to the buffer before a Handler:info/3 call if a user message was in the message queue before both socket packets.
2013-08-24Crash on failure, don't report errorsLoïc Hoguin
When something went wrong in a handler we used to report errors and then terminate the process normally. This doesn't work so well with links which won't detect failure. Now we still catch the error, but throw another one with more details on why it happened, including the Req object information and the stacktrace. Ranch will then print an error message with all this information. Because we crash directly, this also means that we will not hog resources unnecessarily for too long when something bad happens.
2013-05-16Move cowboy_http:status() to cowboy:http_status()Loïc Hoguin
2013-04-12Don't receive data from the socket only once in the loop handlerLoïc Hoguin
2013-04-12Ensure we can fetch the body in the info/3 function of loop handlersLoïc Hoguin
2013-02-22Fix to prevent loop handler awakening immediately after response sentJames Fish
If a loop handler sent a response (e.g. cowboy_req:chunked_reply/2,/3) and then returns {loop, Req, HandlerState, hibernate} it would have a {cowboy_req, resp_sent} message in its message queue. This message would cause the process to immediately awaken, so it is flushed before hibernation.
2013-02-21Added warn compile options. Fixed compile warnings.Egobrain
2013-02-17Fix {suspend, ...} specs to use atom() for function nameJames Fish
2013-02-15Make sure socket is passive once we've done with loop handlerAndrew Majorov
It is sometimes important to make a socket passive as it was initially and as it is expected to be by cowboy_protocol, right after we've done with loop handling.
2013-02-11Better handle socket closing with loop handlersLoïc Hoguin
We now read from the socket to be able to detect errors or TCP close events, and buffer the data if any. Once the data receive goes over a certain limit, which defaults to 5000 bytes, we simply close the connection with an {error, overflow} reason.
2013-01-29Fix {cowboy_req, resp_sent} potentially leaking in loop handlersLoïc Hoguin
2013-01-22Replace terminate/2 with terminate/3, adding a ReasonLoïc Hoguin
This should have been done a *long* time ago, back when I initially added Websocket support. This is the first part of two in improving loop handler support with regards to socket closure. Reason may include: {normal, shutdown} for the most normal shutdown, {normal, timeout} for a loop handler timeout shutdown, or {error, _} if an error occured.
2013-01-03Add middleware supportLoïc Hoguin
Middlewares allow customizing the request processing. All existing Cowboy project are incompatible with this commit. You need to change `{dispatch, Dispatch}` in the protocol options to `{env, [{dispatch, Dispatch}]}` to fix your code.