0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-16 05:00:24 +00:00
Commit graph

58 commits

Author SHA1 Message Date
Loïc Hoguin
b669b1b5a3 Reset the max number of empty lines between keepalive requests
Fixes issue #47.
2011-09-13 23:41:34 +02:00
Loïc Hoguin
24bf2c54d0 Ensure header names are handled in a case insensitive manner
To this end we are formatting the header names just like OTP does
except we do it for names of up to 32 characters, as there are
widely used header names of more than 20 characters, the limit that
OTP follows currently. An example of such header name would be
Sec-Websocket-Version.

The formatting itself is fairly simple: an uppercase character at
the start and after dashes, everything else lowercase.
2011-08-23 16:20:53 +02:00
Loïc Hoguin
43d14b52cd Give the ListenerPid to the protocol on startup
Also sends a message 'shoot' that can be received by the protocol
to make sure Cowboy has had enough time to fully initialize the
socket. This message should be received before any socket-related
operations are performed.

WebSocket request connections are now moved from the pool 'default'
to the pool 'websocket', meaning we can have a lot of running
WebSockets despite having a low 'max_connections' setting.
2011-08-10 20:28:30 +02:00
Loïc Hoguin
474f4e0bfa Call Handler:terminate/2 even on error in Handler:handle/2
This ensures we can cleanup what we did in Handler:init/3.
2011-07-07 17:45:28 +02:00
Loïc Hoguin
aa0a66749e Move recursion out of a try .. catch block.
Fixes issue #31.

Recursion shouldn't happen in a single catch statement or inside
a try .. catch statement. The only safe construct for catching
exceptions and perform recursion when everything goes well is
to put the recursive call inside a try .. of .. catch construct
in the of .. catch block.

Otherwise the stack gets filled with exception-related information
since they can still be caught if we were to send them and unfold
the whole thing.

Thanks go to lpgauth for reporting the issue and people on IRC
for explaining the hows and whys.
2011-07-06 20:00:08 +02:00
Loïc Hoguin
108a491f55 Add documentation for the public interface.
This is probably not perfect yet but it should be better than
nothing. We'll improve things with feedback received from the
many users.
2011-07-06 17:42:20 +02:00
Loïc Hoguin
3e55cb62c9 Refresh the type specifications.
Following discussions on #erlounge.

Also fixes compilation in R14B03 and fixes a few underspecs
dialyzer warnings.
2011-05-25 23:02:40 +02:00
Loïc Hoguin
4c4030a792 Send a meaningful error to error_logger on handler crashes.
Inspired by gen_server and friends. Should fix issue #13.
2011-05-20 01:28:55 +02:00
Anthony Ramine
0ca8f1364b Implement path_info feature
The dispatcher now accepts '...' as the leading segment of Host and the
trailing segment of Path, this special atom matches any remaining path tail.

When given "cowboy.bugs.dev-extend.eu", host rule ['...', <<"dev-extend">>,
<<"eu">>] matches and fills host_info with [<<"cowboy">>, <<"bugs">>].

When given "/a/b/c/d", path rule [<<"a">>, <<"b">>, '...'] matches and fills
path_info with [<<"c">>, <<"d">>].
2011-05-09 15:14:38 +02:00
Loïc Hoguin
420f5baf98 Add chunked reply support.
Send the status line and headers using
cowboy_http_req:chunked_reply/3, and
individual chunks with cowboy_http_req:chunk/2.
2011-05-08 17:26:21 +02:00
Loïc Hoguin
29e71cf4da Switch the HTTP protocol to use binary packets instead of lists.
The server now does a single recv (or more, but only if needed)
which is then sent to erlang:decode_packet/3 multiple times. Since
most requests are smaller than the default MTU on many platforms,
we benefit from this greatly.

In the case of requests with a body, the server usually read at
least part of the body on the first recv. This is bufferized
properly and used when later retrieving the body.

In the case of pipelined requests, we can end up reading many
requests in a single recv, which are then handled properly using
only the buffer containing the received data.
2011-05-05 14:03:39 +02:00
Loïc Hoguin
6c1f73c53c Add cowboy_http_req:port/1.
Returns the port given in the Host header if present,
otherwise the default port of 443 for HTTPS and 80 for HTTP
is returned.
2011-05-04 12:52:13 +02:00
Loïc Hoguin
c044fa9602 Remove the next_request function always matching its first clause. 2011-04-30 01:14:57 +02:00
Loïc Hoguin
e8286e18e6 Remove the include/types.hrl file. 2011-04-18 13:56:38 +02:00
Loïc Hoguin
2beb5c8206 Move the dispatcher related types into cowboy_dispatcher. 2011-04-18 13:24:27 +02:00
Loïc Hoguin
ef2673b61e Rename dispatch() into dispatch_rules(). 2011-04-18 12:14:13 +02:00
Loïc Hoguin
15dc645596 Use the inet:socket() type instead of the user-defined one. 2011-04-18 00:06:26 +02:00
Loïc Hoguin
73b120b68e Fix a pattern matching bug in cowboy_http_protocol:handler_init/2. 2011-04-12 16:22:46 +02:00
Loïc Hoguin
6ec20b736e Limit the number of empty lines to allow before the request-line.
Defaults to 5. Prevents someone from indefinitely sending empty lines.
2011-04-09 15:28:41 +02:00
Loïc Hoguin
0fad6c6fde Don't crash on errors while receiving headers; throw an error 500 instead.
The server shouldn't crash the request process when we have an error
while receiving headers. A case where this could happen is if the header
line is too long.

See also bfrog's report on ticket #3 on github.
2011-04-07 00:54:32 +02:00
Loïc Hoguin
d8a2fcf258 Don't automatically retrieve the peer information for the 'OPTIONS' method. 2011-04-05 20:09:49 +02:00
Loïc Hoguin
44e6f60b91 Handle properly the default Connection value for HTTP/1.0.
Fixes a bug reported by evaxsoftware on IRC.
2011-04-05 20:07:52 +02:00
Loïc Hoguin
d0d9b0e8b3 Use a more efficient variant of string:to_lower to improve performance.
After much testing and experimentation of all kinds I find lists to be both
faster and using less memory than binaries for request-line and headers
handling. This is more than likely due to the fact that headers are very
short and thus do not benefit from the advantages of refc binaries, meaning
they're copied, just like lists. The memory usage discrepancy is still a
mystery for the most part, although the hoops needed to perform operations
on the binaries are probably responsible for the extra memory use.

I'm thus giving up on trying to use binaries for request-line and headers.
Instead, this commit improves performances even more to the lists code,
making lists 5% faster than binaries. Lists are easier to work with too,
so I guess it's all a big win for everyone.

Of course the request body is still read as a binary, we're using the
binary type where it performs best.
2011-03-27 01:16:11 +01:00
Loïc Hoguin
5726aa3d85 Match early in connection_to_atom for a small performance improvement. 2011-03-27 01:11:33 +01:00
Loïc Hoguin
9fe8141d2a Allow Handler:init/3 to request a protocol upgrade. 2011-03-22 23:03:43 +01:00
Loïc Hoguin
7888be00d2 Forward transport and protocol name to Handler:init. 2011-03-22 19:50:02 +01:00
Loïc Hoguin
c3a36246b5 Explicitly ignore the return value of cowboy_http_req:reply in error_response. 2011-03-22 12:27:34 +01:00
Loïc Hoguin
c366343ac6 Fix spec for cowboy_http_protocol:init/3. 2011-03-21 22:22:30 +01:00
Loïc Hoguin
7cacb88fec Introduce cowboy_http_req:body/1 to read the full request body. 2011-03-21 22:08:27 +01:00
Loïc Hoguin
e9781e77f1 Make sure error_response always returns ok. 2011-03-21 17:52:27 +01:00
Loïc Hoguin
e3dc9b2694 Add specs to ensure_response and change the clauses order. 2011-03-21 17:51:21 +01:00
Loïc Hoguin
8b02992e6a Skip the request body if it hasn't been read by the handler. 2011-03-21 17:47:17 +01:00
Loïc Hoguin
e40001a884 Ensure a response is sent when the handler doesn't reply anything. 2011-03-20 19:38:45 +01:00
Loïc Hoguin
a1e56a2fba Move the error response code into a separate function. 2011-03-20 19:29:32 +01:00
Loïc Hoguin
71b31cee92 Make sure we can only reply to an HTTP request inside Handler:handle.
Of course since requests are a record the response state can be explicitly
overriden, but standard use prevents errors by making sure only one reply
is sent.
2011-03-20 18:03:36 +01:00
Loïc Hoguin
d69d0adfa7 Lazy-retrieve the peer name and port to avoid wasting time each request. 2011-03-20 16:09:05 +01:00
Loïc Hoguin
f5e7178651 Change a @todo for Handler:init possible return values. 2011-03-20 15:30:29 +01:00
Loïc Hoguin
a3fff2f5b0 Rename a variable in cowboy_http_protocol for clarity. 2011-03-20 15:10:58 +01:00
Loïc Hoguin
df35916d2a Allow code reloading inside the cowboy_http_protocol module during keep-alive. 2011-03-20 14:24:43 +01:00
Loïc Hoguin
6fad3f7824 Default the connection to keep-alive on HTTP/1.1 and close on 1.0. 2011-03-20 00:09:15 +01:00
Loïc Hoguin
b874b28561 Save the raw path string in the request. 2011-03-20 00:01:29 +01:00
Loïc Hoguin
8085529f48 Save the raw host string in the request. 2011-03-19 23:57:23 +01:00
Loïc Hoguin
bd3a646316 Protect the calls to the handler using catch.
* Handler:init shouldn't reply anything; send an error 500.
* Handler:handle may have sent something to the client; close the socket.
* Handler:terminate failed to clean itself up. Close the socket.
2011-03-19 19:51:44 +01:00
Loïc Hoguin
a4f8bb6573 Add support for the '*' path.
Mostly used by the following request: OPTIONS * HTTP/1.1
2011-03-19 18:53:59 +01:00
Loïc Hoguin
2c52a30b0a Rewrite the dispatcher to take a list of host each having a list of paths.
* Makes more sense to parse the host only once instead of for each path.
* Allows proper handling of: If the host is not a valid host on the server,
  the response MUST be a 400 (Bad Request) error.
2011-03-19 17:42:03 +01:00
Loïc Hoguin
ebe638165e Ignore all extra Host values sent in the request. 2011-03-19 16:49:06 +01:00
Loïc Hoguin
673c7e2cb5 Reply with error 501 on all non absolute path URIs for now. 2011-03-19 14:46:45 +01:00
Loïc Hoguin
7ef67d08fe Reply with error 400 on all bad Request-Lines received. 2011-03-19 14:40:39 +01:00
Loïc Hoguin
c9eb3ce5fc Ignore empty lines when expecting the Request-Line.
In the interest of robustness, servers SHOULD ignore any empty
line(s) received where a Request-Line is expected.  In other words,
if the server is reading the protocol stream at the beginning of a
message and receives a CRLF first, it should ignore the CRLF.
2011-03-19 14:38:31 +01:00
Loïc Hoguin
db715a3eb1 Comparisons of host names MUST be case-insensitive. 2011-03-19 14:16:17 +01:00