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

652 commits

Author SHA1 Message Date
Loïc Hoguin
b650ef8907 Merge branch 'multipart' of https://github.com/nox/cowboy
Conflicts:
	src/cowboy_http_req.erl
	test/http_SUITE.erl
2012-01-23 07:37:49 +01:00
Anthony Ramine
528507c7de Add multipart support 2012-01-23 07:20:35 +01:00
Magnus Klaar
a7334d55c0 Add etag option to cowboy_http_static handler. 2012-01-07 23:07:45 +01:00
Loïc Hoguin
d2f13366a9 Fix the stream_body_set_resp test
It was failing from time to time due to the response being sent
as two separate packets.
2012-01-06 20:49:31 +01:00
Loïc Hoguin
5095c27c65 Merge branch 'issue-114-tests' of https://github.com/klaar/cowboy into gracefully-handle-crashes
Conflicts:
	test/http_SUITE.erl
2012-01-06 19:19:34 +01:00
Magnus Klaar
1592adcd4e only run end_static_dir for http and https 2012-01-05 01:01:03 +01:00
Magnus Klaar
54d16c14ad Add test and doc for content types function. 2012-01-05 01:01:03 +01:00
Magnus Klaar
50578254d5 Add tests for #114 2011-12-29 00:06:22 +01:00
Magnus Klaar
ea7ae14df8 Add built-in cowboy_http_static handler. 2011-12-28 18:17:15 +01:00
Magnus Klaar
937a2b0326 Add cowboy_http_req:set_resp_body_fun/3. 2011-12-28 18:17:10 +01:00
Loïc Hoguin
72d91583b9 Add a max_keepalive HTTP protocol option
Based on the patch by Louis-Philippe Gauthier.
2011-12-22 21:35:40 +01:00
Loïc Hoguin
7a68a38e5a Add ct tests for binary websocket frames 2011-12-22 20:19:05 +01:00
Loïc Hoguin
9800348c21 Move the websocket tests in a separate suite 2011-12-22 20:06:58 +01:00
Loïc Hoguin
8d2102fe11 Allow HTTP protocol upgrades to use keepalive
REST needed this to be allowed to chain requests on the same connection.
2011-12-08 18:30:13 +01:00
Loïc Hoguin
ce92ab1e63 Add cowboy_http_req:set_resp_cookie/4
Pretty much just an alias for a combination of set_resp_header and cookie.
2011-12-07 11:54:57 +01:00
Loïc Hoguin
0201f7f2b2 cowboy_http_protocol shouldn't crash if the client sends HTTP responses
It was replying back the correct error, but with a crash message in
the console. This patch prevents it from crashing.

Fixes issue #94 reported by oribrost.
2011-12-06 12:22:36 +01:00
Loïc Hoguin
aab1587a4b Add experimental Webmachine based REST protocol support
As with everything experimental, it probably has a lot of bugs and
may not even work.

Like Websocket, REST must be upgraded from a standard resource through
the init/3 function.

A key difference between Webmachine and Cowboy's REST protocol handler
is that in Cowboy the resource has direct access to the request object.
This makes a small change in a few places where you were expected to
return headers or body in Webmachine and are now expected to set them
directly yourself if needed (options/2, for example).

Another difference is that the functions rest_init/2 will always be
called when starting to process a request. Similarly, rest_terminate/2
will be called when the process completes successfully.

The Cowboy REST support also includes automatic language selection,
thanks to the languages_provided/2 callback.

Finally, Cowboy REST expects full URIs to be given at all times, and
will not try to reconstruct URIs from fragments.

Note that REST requests cannot be chained (keepalive) at this time.
This is a design issue in cowboy_http_protocol that will be fixed soon.

Check out the source for more details. It has been designed to be very
easy to read and understand so if you don't understand something,
it's probably a bug. Thanks in advance for all the great bug reports,
pull requests and comments you'll forward my way!
2011-12-05 23:05:32 +01:00
Loïc Hoguin
ad87fee0af Fix a websocket test handler that was throwing an unwanted error
The test still worked because we expect the websocket connection
to fail, but it didn't fail exactly the way we wanted it to.
2011-11-28 09:14:07 +01:00
Loïc Hoguin
64a40cb479 Add set_resp_header/3 and set_resp_body/2 to cowboy_http_req
These functions allow to set response headers and body in advance,
before calling any of the reply functions.

Also add has_resp_header/2 and has_resp_body/1 to check if the given
response headers have already been set.
2011-11-28 09:09:41 +01:00
Paul Oliver
30c3c75bbc Accept Sec-WebSocket-Version: 13 header on Chrome 15 through 17 2011-10-24 15:23:36 +01:00
Loïc Hoguin
70d28ff64d Make sure the hixie-76 websocket code works properly with proxies 2011-10-20 01:36:23 +02:00
Loïc Hoguin
fe5f0ca539 Add a max_line_length to the HTTP protocol
Allows to limit the size of request and header lines, thus preventing
Cowboy from infinitely reading from the socket and never finding an
end of line.

Defaults to 4096 bytes.
2011-10-19 20:35:55 +02:00
Loïc Hoguin
97df2a2d79 Fix the init_shutdown test 2011-10-19 18:50:06 +02:00
Loïc Hoguin
81cc99d10b Add shortcuts to reply functions
New functions are reply/2, reply/3, chunked_reply/2 in cowboy_http_req.
2011-10-13 16:16:53 +02:00
Loïc Hoguin
5e006be01f Add support for loops in standard HTTP handlers
Now init/3 can return one of the following values to enable loops:
 - {loop, Req, State}
 - {loop, Req, State, hibernate}
 - {loop, Req, State, Timeout}
 - {loop, Req, State, Timeout, hibernate}

Returning one of these tuples will activate looping in the HTTP handler.
When looping, handle/2 is never called. Instead, Cowboy will listen
for Erlang messages and forward them to the info/3 function of the
handler. If a timeout is defined, Cowboy will also close the connection
when no message has been received for Timeout milliseconds.

The info/3 function is defined as info(Msg, Req, State). It can return
either of the following tuples:
 - {ok, Req, State}
 - {loop, Req, State}
 - {loop, Req, State, hibernate}

The first one ends the connection, calling terminate/2 before closing.
The others continue the loop.

Loops are useful when writing long-polling handlers that need to wait
and don't expect to receive anything. Therefore it is recommended to
set a timeout to close the connection if nothing arrives after a while
and to enable hibernate everywhere.

Normal HTTP handlers shouldn't need to use this and as such info/3
was made optional.
2011-10-10 17:27:52 +02:00
Loïc Hoguin
25ae2028d6 Add {shutdown, Req} to websocket_init/3 to fail a websocket upgrade
This change allows application developers to refuse websocket upgrades
by returning {shutdown, Req}. The application can also send a reply
with a custom error before returning from websocket_init/3, otherwise
an error 400 is sent.

Note that right now Cowboy closes the connection immediately. Also note
that neither terminate/3 nor websocket_terminate/3 will be called when
the connection is shutdown by websocket_init/3.
2011-10-10 09:09:15 +02:00
Loïc Hoguin
138cccb4f9 Allow HTTP handlers to skip the handle/2 step in init/3
You can now return {shutdown, Req, State} from Handler:init/3
to skip the handle/2 step.

Also allow init/3 function to send responses.
2011-10-06 15:54:37 +02:00
Loïc Hoguin
fe41f2944b Merge branch 'hybi-framing-fix' of https://github.com/smarkets/cowboy 2011-10-06 13:01:27 +02:00
Loïc Hoguin
8e835bce9f Close the connection when the application sends Connection: close
Now Cowboy checks headers sent to the client for the 'Connection'
header value, parses it, and checks whether it contains a 'close'
or 'keep-alive' value. It makes sure to close or keep the connection
alive depending on the value found there, if any.

Also change chunked replies to not close the connection by default
unless the application requests it.
2011-10-06 12:40:04 +02:00
Loïc Hoguin
97460a5993 Fix a crash in websocket_handshake when cowboy_http_req:compact/1 is used
Also add a call to compact/1 in the websocket test handler so we may
catch bugs related to it faster later on.
2011-10-06 01:07:49 +02:00
Loïc Hoguin
d25c30790c Do not send a 408 response if the Request-Line wasn't fully received
The server should not send a response if there wasn't at least
the beginning of a request sent (the Request-Line).
2011-10-04 10:54:30 +02:00
Hunter Morris
6250b22384 Fix byte-by-byte Websocket handling
If the websocket frame handling code in cowboy_http_websocket receives
only 1 byte at a time, it fails with a badmatch in
cowboy_http_websocket:websocket_data/4. This commit fixes the problem
and introduces a test of the correct behaviour.
2011-10-03 14:58:12 +01:00
Loïc Hoguin
d0f711a61d Add a test for websocket hibernate + timeout and fix this use case
The issue was that we were calling erlang:hibernate before a
receive .. after .. end call. Erlang hibernates the process before
reaching the receive instruction and we therefore couldn't enter
the after clause when hibernating.

This is now fixed by using erlang:send_after instead and receiving
that message instead of using an after clause.
2011-09-22 23:15:54 +02:00
Loïc Hoguin
89ae3c8cad 'Host' header is optional in HTTP/1.0
Krishnamurthy, Kristol, Mogul: "Key Differences between HTTP/1.0
and HTTP/1.1", "Internet address conservation".
http://www8.org/w8-papers/5c-protocols/key/key.html

Fixes issue #35 reported by Alex Kropivny.
2011-09-14 01:45:12 +02:00
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
2374aa7e07 Add WebSocket drafts 7, 8, 9 and 10 implementation
The implementation is only partial for now but should work for
all browsers implementing it.
2011-08-23 23:49:58 +02:00
Loïc Hoguin
e5d4c1f22f Add a PropEr test for cowboy_dispatcher:split_host/1
Mostly thanks to Magnus Klaar as it took me a while to figure
out how PropEr tests had to be written.
2011-07-26 10:46:06 +02:00
Loïc Hoguin
293cf33702 Separate message and packet handling for websockets
Improves the readability of websocket handler code by having
two functions: websocket_handle/3 handles the packets received
from the socket, removing the tuple construct that was otherwise
needed, so only websocket_handle(Data, Req, State) is needed now;
websocket_info/3 handles the messages that the websocket handler
process received, as websocket_info(Info, Req, State).

Both functions return values are handled identically by Cowboy
so nothing changes on that end.
2011-07-19 12:20:16 +02:00
Steven Gravell
bebe3dc5d2 do not send ports 80 and 443 - browsers get mad
Browsers get mad that the returned location address is not the same
as what they sent, since the :(80|443) is stripped.

Add a simple eunit test due to existing ct websockets tests not
covering this case.
2011-06-27 15:25:30 +01: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
470baff61f Add headers_huge test, demonstrating issue #3 is fixed.
The previous commit switching to raw recv + erlang:decode_packet/3
works around the OTP bug regarding headers size in http recv.
2011-05-05 17:11:27 +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
734f57aa8a ct: Throw garbage at the server then check if it's still up.
Basically:
cat /dev/urandom | nc host port

Only run this test if cat and nc are available.
2011-04-17 13:36:51 +02:00
Loïc Hoguin
cb60d18e82 Initial draft-hixie-thewebsocketprotocol-76 support. 2011-04-14 21:21:17 +02:00
Loïc Hoguin
c32db277c8 Fix a bug where dupe headers were sent in cowboy_http_req:reply/4.
Now the server defines default headers that can be overwritten by the
handler simply by passing them to the reply/4 function. Default headers
include, for now, Connection and Content-Length headers. Note that it isn't
enough to change the Connection header to close a keep-alive connection
server-side.
2011-04-14 01:32:02 +02:00
Loïc Hoguin
0ef66b78f7 ct: Add a test for requests pipelining. 2011-04-10 02:43:30 +02:00
Loïc Hoguin
f05953516b ct: Add a test for \n which throws an error 400. 2011-04-09 15:45:25 +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
c79df567be ct: Add tests for incomplete requests leading to a timeout. 2011-04-09 13:53:22 +02:00