aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md276
-rw-r--r--README.md18
-rw-r--r--ROADMAP.md22
-rw-r--r--examples/README.md30
-rw-r--r--examples/cookie/README.md18
-rw-r--r--examples/cookie/rebar.config6
-rw-r--r--examples/cookie/src/cookie.app.src15
-rw-r--r--examples/cookie/src/cookie.erl14
-rw-r--r--examples/cookie/src/cookie_app.erl25
-rw-r--r--examples/cookie/src/cookie_sup.erl23
-rw-r--r--examples/cookie/src/toppage_handler.erl29
-rwxr-xr-xexamples/cookie/start.sh3
-rw-r--r--examples/cookie/templates/toppage.dtl24
-rw-r--r--examples/echo_get/src/toppage_handler.erl2
-rw-r--r--examples/echo_post/src/toppage_handler.erl2
-rw-r--r--guide/toc.md3
-rw-r--r--rebar.config2
-rw-r--r--src/cowboy_clock.erl54
-rw-r--r--src/cowboy_cookies.erl416
-rw-r--r--src/cowboy_dispatcher.erl43
-rw-r--r--src/cowboy_http.erl228
-rw-r--r--src/cowboy_multipart.erl92
-rw-r--r--src/cowboy_protocol.erl38
-rw-r--r--src/cowboy_req.erl84
-rw-r--r--src/cowboy_rest.erl57
-rw-r--r--src/cowboy_static.erl53
-rw-r--r--src/cowboy_websocket.erl159
-rw-r--r--src/cowboy_websocket_handler.erl8
-rw-r--r--test/http_SUITE.erl45
-rw-r--r--test/rest_expires.erl22
-rw-r--r--test/rest_missing_callbacks.erl21
-rw-r--r--test/ws_SUITE.erl208
-rw-r--r--test/ws_send_many_handler.erl12
-rw-r--r--test/ws_timeout_cancel_handler.erl33
-rw-r--r--test/ws_upgrade_with_opts_handler.erl28
35 files changed, 1285 insertions, 828 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4545ff1..2f4fc12 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,25 +4,28 @@ CHANGELOG
next
----
-* This release drops R14 compatibility
+ * This release drops R14 compatibility
-* Add CONTRIBUTING.md file
+ * Add CONTRIBUTING.md file
-* Use Ranch for connection handling
+ * Use Ranch for connection handling
To start listeners you can now use cowboy:start_http/4 for HTTP,
and cowboy:start_https/4 for HTTPS. The proper transport and
protocol modules will be used.
-* Shorten the name of many modules
+ * Add a dependency on crypto
- * cowboy_http_protocol becomes cowboy_protocol.
- * cowboy_http_req becomes cowboy_req.
- * cowboy_http_rest becomes cowboy_rest.
- * cowboy_http_static becomes cowboy_static.
- * cowboy_http_websocket becomes cowboy_websocket.
+ * Remove implicit dependency on inets
-* Introduce the cowboy_req:req() opaque type
+ * Shorten the name of many modules
+ * cowboy_http_protocol becomes cowboy_protocol
+ * cowboy_http_req becomes cowboy_req
+ * cowboy_http_rest becomes cowboy_rest
+ * cowboy_http_static becomes cowboy_static
+ * cowboy_http_websocket becomes cowboy_websocket
+
+ * Introduce the cowboy_req:req() opaque type
The include/http.hrl file was removed. Users are expected to use
the cowboy_req API to access or modify the Req object.
@@ -30,84 +33,95 @@ next
This required a lot of changes so cleanup and optimization were
performed where possible.
-* Add many cowboy_req functions
-
- * cowboy_req:delete_resp_header/2 deletes a previously set resp header.
- * cowboy_req:set_meta/3 sets metadata in the Req object.
- * cowboy_req:to_list/1 converts the Req object to a list of key/values.
- * cowboy_req:fragment/1 returns the request URL fragment.
- * cowboy_req:host_url/1 returns the request URL without the path or qs.
- * cowboy_req:url/1 returns the full request URL.
+ * Add many cowboy_req functions
+ * cowboy_req:delete_resp_header/2 deletes a previously set resp header
+ * cowboy_req:set_meta/3 sets metadata in the Req object
+ * cowboy_req:to_list/1 converts the Req object to a list of key/values
+ * cowboy_req:fragment/1 returns the request URL fragment
+ * cowboy_req:host_url/1 returns the request URL without the path or qs
+ * cowboy_req:url/1 returns the full request URL
+
+ * Rename or drop many cowboy_req functions
+ * Replace cowboy_req:host/1 with cowboy_req:raw_host/1
+ * Replace cowboy_req:path/1 with cowboy_req:raw_path/1
+ * cowboy_req:raw_qs/1 becomes cowboy_req:qs/1
+
+ * Change the signature of many cowboy_req functions
+ * parse_header now returns {ok, any(), Req} instead of {any(), Req}
+ * body_qs now returns {ok, QsVals, Req} instead of {QsVals, Req}
+ * multipart_data now returns {headers, Headers, Req} instead of
+ {{headers, Headers}, Req} and {body, Body, Req} instead of
+ {{body, Body}, Req}
+ * set_resp_* functions now return Req instead of {ok, Req}
-* Rename or drop many cowboy_req functions
+ * Fix consistency issues caused by erlang:decode_packet/3
+ * The method is now always a case sensitive binary string
+ * Note that standard method names are uppercase (e.g. <<"GET">>)
+ * Header names are now always lowercase binary string
- * Replace cowboy_req:host/1 with cowboy_req:raw_host/1.
- * Replace cowboy_req:path/1 with cowboy_req:raw_path/1.
- * cowboy_req:raw_qs/1 becomes cowboy_req:qs/1.
+ * The max_line_length cowboy_protocol option was replaced by 3 new options:
+ * max_request_line_length, defaults to 4096 bytes
+ * max_header_name_length, defaults to 64 bytes
+ * max_header_value_length, defaults to 4096 bytes
-* Change the signature of many cowboy_req functions
+ * Add max_headers option, limiting the number of headers; defaults to 100
- * parse_header now returns {ok, any(), Req} instead of {any(), Req}.
- * body_qs now returns {ok, QsVals, Req} instead of {QsVals, Req}.
- * multipart_data now returns {headers, Headers, Req} instead of
- {{headers, Headers}, Req} and {body, Body, Req} instead of
- {{body, Body}, Req}.
- * set_resp_* functions now return Req instead of {ok, Req}.
+ * Enhance the websocket API
+ * Change a websocket error from {error, protocol} to {error, badframe}
+ * Allow websocket handlers to reply more than one frame
+ * Check for errors when calling Transport:send/2 to avoid crashes
+ * Add close, {close, Payload}, ping, pong frame types for replies
-* Fix consistency issues caused by erlang:decode_packet/3
+ * Use -callback in behaviours
- * The method is now always a case sensitive binary string.
- * Note that standard method names are uppercase (e.g. <<"GET">>).
- * Header names are now always lowercase binary string.
+ * Add cowboy_protocol:onrequest_fun/0 and :onresponse_fun/0 types
-* The max_line_length cowboy_protocol option was replaced by 3 new options:
+ * Add the body data to onresponse_fun/0 callback
- * max_request_line_length, defaults to 4096 bytes
- * max_header_name_length, defaults to 64 bytes
- * max_header_value_length, defaults to 4096 bytes
+ * Remove the urldecode cowboy_protocol option
-* Add max_headers option, limiting the number of headers; defaults to 100
+ * Isolate multipart from body reading to fix an issue
-* Use -callback in behaviours
+ * Avoid a duplicate HTTP reply in cowboy_websocket:upgrade_error/1
-* Add cowboy_protocol:onrequest_fun/0 and :onresponse_fun/0 types
+ * Fix use of the Vary header, was named Variances in the previous code
-* Add the body data to onresponse_fun/0 callback
+ * Improve returned status code for HTTP and REST
-* Remove the urldecode cowboy_protocol option
+ * Fix charsets_provided return value
-* Isolate multipart from body reading to fix an issue
+ * Allow passing {M, F} for the mimetype function to cowboy_static
-* Change a websocket error from {error, protocol} to {error, badframe}
+ * Can now upgrade protocols with {upgrade, protocol, P, Req, Opts}
-* Avoid a duplicate HTTP reply in cowboy_websocket:upgrade_error/1
+ * Cowboy now only expects universal time, never local time
-* Many, many optimizations for the most critical code path
+ * Many, many optimizations for the most critical code path
0.6.1
-----
-* Add hello_world, rest_hello_world, chunked_hello_world,
+ * Add hello_world, rest_hello_world, chunked_hello_world,
echo_get, echo_post and static examples.
-* Add support for the "Expect: 100-continue" header.
+ * Add support for the "Expect: 100-continue" header.
-* Keep the original 'Host' header value instead of modifying it.
+ * Keep the original 'Host' header value instead of modifying it.
-* Fix use of parsed headers cache.
+ * Fix use of parsed headers cache.
-* REST: fix the matching of charsets.
+ * REST: fix the matching of charsets.
-* REST: allow <<"type/subtype">> format for content_types_accepted.
+ * REST: allow <<"type/subtype">> format for content_types_accepted.
-* Improve typespecs.
+ * Improve typespecs.
0.6.0
-----
-* Add multipart support
+ * Add multipart support
-* Add chunked transfer decoding support
+ * Add chunked transfer decoding support
Done by reworking the body reading API. Now all the body
reading goes through the cowboy_http_req:stream_body/1
@@ -115,14 +129,14 @@ next
Transfer-Encoding and the Content-Encoding, returning
properly decoded data ready for consumption.
-* Add fragmented websocket messages support
+ * Add fragmented websocket messages support
Properly tested by the addition of the Autobahn websocket
test suite to our toolbox. All tests pass except a few
related to UTF-8 handling, as Cowboy does no checks on that
end at this point.
-* Add 'onrequest' and 'onresponse' hooks
+ * Add 'onrequest' and 'onresponse' hooks
The first can be used for all the special cases you may have
that can't be dealt with otherwise. It's also pretty good for
@@ -131,76 +145,76 @@ next
The second can be used for logging errors or replacing error
pages, amongst others.
-* Add cowboy:get_protocol_options/1 and cowboy:set_protocol_options/2
+ * Add cowboy:get_protocol_options/1 and cowboy:set_protocol_options/2
These functions allow for retrieving a listener's protocol options,
and for modifying them while the listener is running. This is
most useful to upgrade the dispatch list. The upgrade applies
to all the future connections.
-* Add the sockname/1 function to TCP and SSL transports
+ * Add the sockname/1 function to TCP and SSL transports
-* Improve SSL transport support
+ * Improve SSL transport support
Add support for specifying the ciphers. Add CA support. Make
specifying the password optional.
-* Add new HTTP status codes from RFC 6585
+ * Add new HTTP status codes from RFC 6585
-* Add a 'file' option to cowboy_http_static
+ * Add a 'file' option to cowboy_http_static
This allows for mapping /folder/ paths to a /folder/index.html file.
-* Add the '*' catch all Content-Type for REST
+ * Add the '*' catch all Content-Type for REST
-* Add {halt, Req, State} as a possible return value for REST
+ * Add {halt, Req, State} as a possible return value for REST
-* Add absolute URI support for requests
+ * Add absolute URI support for requests
-* Add cowboy_http:x_www_form_urlencoded/2
+ * Add cowboy_http:x_www_form_urlencoded/2
-* Various REST bug fixes
+ * Various REST bug fixes
-* Do not send chunked replies for HTTP/1.0 connections
+ * Do not send chunked replies for HTTP/1.0 connections
-* Fix a DST bug in the cookies code
+ * Fix a DST bug in the cookies code
-* Fix a bug with setting cookie values containing slashes
+ * Fix a bug with setting cookie values containing slashes
-* Fix a small timer leak when using loop/websocket timeouts
+ * Fix a small timer leak when using loop/websocket timeouts
-* Make charset and media type parsing more relaxed
+ * Make charset and media type parsing more relaxed
This is to accomodate some widely used broken clients.
-* Make error messages more readable
+ * Make error messages more readable
-* Fix and improve type specifications
+ * Fix and improve type specifications
-* Fix a bug preventing documentation from being generated
+ * Fix a bug preventing documentation from being generated
-* Small improvements to the documentation
+ * Small improvements to the documentation
-* Rework the HTTP test suite
+ * Rework the HTTP test suite
The suite now uses an integrated Cowboy HTTP client. The client
is currently experimental and shouldn't be used.
-* Add many many tests.
+ * Add many many tests.
0.4.0
-----
-* Set the cowboy_listener process priority to high
+ * Set the cowboy_listener process priority to high
As it is the central process used by all incoming requests
we need to set its priority to high to avoid timeouts that
would happen otherwise when reaching a huge number of
concurrent requests.
-* Add cowboy:child_spec/6 for embedding in other applications
+ * Add cowboy:child_spec/6 for embedding in other applications
-* Add cowboy_http_rest, an experimental REST protocol support
+ * Add cowboy_http_rest, an experimental REST protocol support
Based on the Webmachine diagram and documentation. It is a
new implementation, not a port, therefore a few changes have
@@ -211,7 +225,7 @@ next
resource documentation and the comments found in cowboy_http_rest,
which itself should be fairly easy to read and understand.
-* Add cowboy_http_static, an experimental static file handler
+ * Add cowboy_http_static, an experimental static file handler
Makes use of the aforementioned REST protocol support to
deliver files with proper content type and cache headers.
@@ -220,22 +234,22 @@ next
appropriate, which currently requires the VM to be started
with the +A option defined, else errors may randomly appear.
-* Add cowboy_bstr module for binary strings related functions
+ * Add cowboy_bstr module for binary strings related functions
-* Add cowboy_http module for HTTP parsing functions
+ * Add cowboy_http module for HTTP parsing functions
This module so far contains various functions for HTTP header
parsing along with URL encoding and decoding.
-* Remove quoted from the default dependencies
+ * Remove quoted from the default dependencies
This should make Cowboy much easier to compile and use by default.
It is of course still possible to use quoted as your URL decoding
library in Cowboy thanks to the newly added urldecode option.
-* Fix supervisor spec for non dynamic modules to allow upgrades to complete
+ * Fix supervisor spec for non dynamic modules to allow upgrades to complete
-* Add cowboy:accept_ack/1 for a cleaner handling of the shoot message
+ * Add cowboy:accept_ack/1 for a cleaner handling of the shoot message
Before, when the listener accepted a connection, the newly created
process was waiting for a message containing the atom 'shoot' before
@@ -245,81 +259,81 @@ next
contents of the message have changed (and could change again in the
distant future).
-* Update binary parsing expressions to avoid hype crashes
+ * Update binary parsing expressions to avoid hype crashes
More specifically, /bits was replaced by /binary.
-* Rename the type cowboy_dispatcher:path_tokens/0 to tokens/0
+ * Rename the type cowboy_dispatcher:path_tokens/0 to tokens/0
-* Remove the cowboy_clock:date/0, time/0 and datetime/0 types
+ * Remove the cowboy_clock:date/0, time/0 and datetime/0 types
The calendar module exports those same types properly since R14B04.
-* Add cacertfile configuration option to cowboy_ssl_transport
+ * Add cacertfile configuration option to cowboy_ssl_transport
-* Add cowboy_protocol behaviour
+ * Add cowboy_protocol behaviour
-* Remove -Wbehaviours dialyzer option unavailable in R15B
+ * Remove -Wbehaviours dialyzer option unavailable in R15B
-* Many tests and specs improvements
+ * Many tests and specs improvements
### cowboy_http_req
-* Fix a crash when reading the request body
+ * Fix a crash when reading the request body
-* Add parse_header/2 and parse_header/3
+ * Add parse_header/2 and parse_header/3
The following headers can now be semantically parsed: Connection, Accept,
Accept-Charset, Accept-Encoding, Accept-Language, Content-Length,
Content-Type, If-Match, If-None-Match, If-Modified-Since,
If-Unmodified-Since, Upgrade
-* Add set_resp_header/3, set_resp_cookie/4 and set_resp_body/2
+ * Add set_resp_header/3, set_resp_cookie/4 and set_resp_body/2
These functions allow handlers to set response headers and body
without having to reply directly.
-* Add set_resp_body_fun/3
+ * Add set_resp_body_fun/3
This function allows handlers to stream the body of the response
using the given fun. The size of the response must be known beforehand.
-* Add transport/1 to obtain the transport and socket for the request
+ * Add transport/1 to obtain the transport and socket for the request
This allows handlers to have low-level socket access in those cases
where they do need it, like when streaming a response body with
set_resp_body_fun/3.
-* Add peer_addr/1
+ * Add peer_addr/1
This function tries to guess the real peer IP based on the HTTP
headers received.
-* Add meta/2 and meta/3 to save useful protocol information
+ * Add meta/2 and meta/3 to save useful protocol information
Currently used to save the Websocket protocol version currently used,
and to save request information in the REST protocol handler.
-* Add reply/2 and reply/3 aliases to reply/4
+ * Add reply/2 and reply/3 aliases to reply/4
-* Add upgrade_reply/3 for protocol upgrades
+ * Add upgrade_reply/3 for protocol upgrades
### cowboy_http_protocol
-* Add the {urldecode, fun urldecode/2} option
+ * Add the {urldecode, fun urldecode/2} option
Added when quoted was removed from the default build. Can be used to
tell Cowboy to use quoted or any other URL decoding routine.
-* Add the max_keepalive option
+ * Add the max_keepalive option
-* Add the max_line_length option
+ * Add the max_line_length option
-* Allow HTTP handlers to stop during init/3
+ * Allow HTTP handlers to stop during init/3
To do so they can return {shutdown, Req, State}.
-* Add loops support in HTTP handlers for proper long-polling support
+ * Add loops support in HTTP handlers for proper long-polling support
A loop can be entered by returning either of {loop, Req, State},
{loop, Req, State, hibernate}, {loop, Req, State, Timeout} or
@@ -341,60 +355,60 @@ next
Like in OTP, you do need to set timeout and hibernate again when
returning from info/3 to enable them until the next call.
-* Fix the sending of 500 errors when handlers crash
+ * Fix the sending of 500 errors when handlers crash
Now we send an error response when no response has been sent,
and do nothing more than close the connection if anything
did get sent.
-* Fix a crash when the server is sent HTTP responses
+ * Fix a crash when the server is sent HTTP responses
-* Fix HTTP timeouts handling when the Request-Line wasn't received
+ * Fix HTTP timeouts handling when the Request-Line wasn't received
-* Fix the handling of the max number of empty lines between requests
+ * Fix the handling of the max number of empty lines between requests
-* Fix the handling of HEAD requests
+ * Fix the handling of HEAD requests
-* Fix HTTP/1.0 Host header handling
+ * Fix HTTP/1.0 Host header handling
-* Reply status 400 if we receive an unexpected value or error for headers
+ * Reply status 400 if we receive an unexpected value or error for headers
-* Properly close when the application sends "Connection: close" header
+ * Properly close when the application sends "Connection: close" header
-* Close HTTP connections on all errors
+ * Close HTTP connections on all errors
-* Improve the error message for HTTP handlers
+ * Improve the error message for HTTP handlers
### cowboy_http_websocket
-* Add websocket support for all versions up to RFC 6455
+ * Add websocket support for all versions up to RFC 6455
Support isn't perfect yet according to the specifications, but
is working against all currently known client implementations.
-* Allow websocket_init/3 to return with the hibernate option set
+ * Allow websocket_init/3 to return with the hibernate option set
-* Add {shutdown, Req} return value to websocket_init/3 to fail an upgrade
+ * Add {shutdown, Req} return value to websocket_init/3 to fail an upgrade
-* Fix websocket timeout handling
+ * Fix websocket timeout handling
-* Fix error messages: wrong callback name was reported on error
+ * Fix error messages: wrong callback name was reported on error
-* Fix byte-by-byte websocket handling
+ * Fix byte-by-byte websocket handling
-* Fix an issue when using hixie-76 with certain proxies
+ * Fix an issue when using hixie-76 with certain proxies
-* Fix a crash in the hixie-76 handshake
+ * Fix a crash in the hixie-76 handshake
-* Fix the handshake when SSL is used on port 443
+ * Fix the handshake when SSL is used on port 443
-* Fix a crash in the handshake when cowboy_http_req:compact/1 is used
+ * Fix a crash in the handshake when cowboy_http_req:compact/1 is used
-* Fix handshake when a query string is present
+ * Fix handshake when a query string is present
-* Fix a crash when the Upgrade header contains more than one token
+ * Fix a crash when the Upgrade header contains more than one token
0.2.0
-----
-* Initial release.
+ * Initial release.
diff --git a/README.md b/README.md
index 4ff5ea4..b850083 100644
--- a/README.md
+++ b/README.md
@@ -18,13 +18,6 @@ Because it uses Ranch for managing connections, Cowboy can easily be
No parameterized module. No process dictionary. **Clean** Erlang code.
-Quick start
------------
-
- * Add Cowboy as a rebar dependency to your application.
- * Start Cowboy and add one or more listeners.
- * Write handlers for your application.
-
Getting Started
---------------
@@ -32,10 +25,17 @@ Getting Started
* Look at the examples in the `examples/` directory
* Build API documentation with `make docs`; open `doc/index.html`
+Support
+-------
+ * Official IRC Channel: #ninenines on irc.freenode.net
+ * [Mailing Lists](http://lists.ninenines.eu)
+ * [Commercial Support](http://ninenines.eu/support)
-Old README
-----------
+
+
+Old README (deprecated)
+-----------------------
This and all following sections will be removed as soon as their
equivalent appear in the Cowboy guide.
diff --git a/ROADMAP.md b/ROADMAP.md
index a409af0..7dc19af 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -6,7 +6,7 @@ list of planned changes and work to be done on the Cowboy
server. It is non-exhaustive and subject to change. Items
are not ordered.
-* Write more, better examples.
+ * Write more, better examples.
The first step would be to port misultin's examples
to Cowboy. Then these examples could be completed with
@@ -20,12 +20,12 @@ are not ordered.
Examples should be commented. They may or may not be
used for writing the user guides.
-* Write user guides.
+ * Write user guides.
We currently have good API documentation, but no step
by step user guides.
-* Write more, better tests.
+ * Write more, better tests.
Amongst the areas less tested there is protocol upgrades
and the REST handler.
@@ -37,7 +37,7 @@ are not ordered.
While eunit and ct tests are fine, some parts of the
code could benefit from PropEr tests.
-* Continuous performance testing.
+ * Continuous performance testing.
Initially dubbed the Horse project, Cowboy could benefit
from a continuous performance testing tool that would
@@ -49,37 +49,37 @@ are not ordered.
Cowboy to other servers and eventually take ideas from
the servers that outperform Cowboy for the task being tested.
-* Improve HTTP/1.0 support.
+ * Improve HTTP/1.0 support.
Most of the work on Cowboy has been done with HTTP/1.1
in mind. But there is still a need for HTTP/1.0 code in
Cowboy. The server code should be reviewed and tested
to ensure compatibility with remaining HTTP/1.0 products.
-* Complete the work on Websockets.
+ * Complete the work on Websockets.
Now that the Autobahn test suite is available (make inttests),
we have a definite way to know whether Cowboy's implementation
of Websockets is right. The work can thus be completed. The
remaining task is proper UTF8 handling.
-* SPDY support.
+ * SPDY support.
While SPDY probably won't be added directly to Cowboy, work
has been started on making Cowboy use SPDY.
-* Transport upgrades.
+ * Transport upgrades.
Some protocols allow an upgrade from TCP to SSL without
closing the connection. This is currently not possible
through the Cowboy API.
-* Resizing the acceptor pool.
+ * Resizing the acceptor pool.
We should be able to add more acceptors to a pool but also
to remove some of them as needed.
-* Simplified dispatch list.
+ * Simplified dispatch list.
For convenience purposes, the dispatch list should allow
lists instead of binaries. The lists can be converted to
@@ -88,7 +88,7 @@ are not ordered.
There has also been discussion on allowing the dispatch
list to be hierarchical.
-* Add Transport:secure/0.
+ * Add Transport:secure/0.
Currently Cowboy checks if a connection is secure by
checking if its name is 'ssl'. This isn't a very modular
diff --git a/examples/README.md b/examples/README.md
index 0289754..c0e1f41 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -1,20 +1,24 @@
-Cowboy examples
+Cowboy Examples
===============
-* [hello_world](./examples/hello_world):
-simplest example application
+ * [chunked_hello_world](./examples/chunked_hello_world):
+ demonstrates chunked data transfer with two one-second delays
-* [echo_get](./examples/echo_get):
-parse and echo a GET query string
+ * [cookie](./examples/cookie):
+ set cookies from server and client side
-* [echo_post](./examples/echo_post):
-parse and echo a POST parameter
+ * [echo_get](./examples/echo_get):
+ parse and echo a GET query string
-* [rest_hello_world](./examples/rest_hello_world):
-return the data type that matches the request type (ex: html, text, json...)
+ * [echo_post](./examples/echo_post):
+ parse and echo a POST parameter
-* [chunked_hello_world](./examples/chunked_hello_world):
-demonstrates chunked data transfer with two one-second delays
-* [static](./examples/static):
-an example file server
+ * [hello_world](./examples/hello_world):
+ simplest example application
+
+ * [rest_hello_world](./examples/rest_hello_world):
+ return the data type that matches the request type (ex: html, text, json)
+
+ * [static](./examples/static):
+ an example file server
diff --git a/examples/cookie/README.md b/examples/cookie/README.md
new file mode 100644
index 0000000..cb6465f
--- /dev/null
+++ b/examples/cookie/README.md
@@ -0,0 +1,18 @@
+Cowboy Cookie
+=============
+
+To compile this example you need rebar in your PATH.
+
+Type the following command:
+```
+$ rebar get-deps compile
+```
+
+You can then start the Erlang node with the following command:
+```
+./start.sh
+```
+
+Then point your browser to the indicated URL. This example allows
+you to use any path you want to try to show that cookies are defined
+site-wide. Try it in your browser!
diff --git a/examples/cookie/rebar.config b/examples/cookie/rebar.config
new file mode 100644
index 0000000..d1fecef
--- /dev/null
+++ b/examples/cookie/rebar.config
@@ -0,0 +1,6 @@
+{deps, [
+ {cowboy, ".*",
+ {git, "git://github.com/extend/cowboy.git", "master"}},
+ {erlydtl, ".*",
+ {git, "https://github.com/evanmiller/erlydtl.git", "master"}}
+]}.
diff --git a/examples/cookie/src/cookie.app.src b/examples/cookie/src/cookie.app.src
new file mode 100644
index 0000000..1e9cd3d
--- /dev/null
+++ b/examples/cookie/src/cookie.app.src
@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, cookie, [
+ {description, "Cowboy Cookie example."},
+ {vsn, "1"},
+ {modules, []},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ cowboy
+ ]},
+ {mod, {cookie_app, []}},
+ {env, []}
+]}.
diff --git a/examples/cookie/src/cookie.erl b/examples/cookie/src/cookie.erl
new file mode 100644
index 0000000..455fcb0
--- /dev/null
+++ b/examples/cookie/src/cookie.erl
@@ -0,0 +1,14 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(cookie).
+
+%% API.
+-export([start/0]).
+
+%% API.
+
+start() ->
+ ok = application:start(crypto),
+ ok = application:start(ranch),
+ ok = application:start(cowboy),
+ ok = application:start(cookie).
diff --git a/examples/cookie/src/cookie_app.erl b/examples/cookie/src/cookie_app.erl
new file mode 100644
index 0000000..195d6b6
--- /dev/null
+++ b/examples/cookie/src/cookie_app.erl
@@ -0,0 +1,25 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(cookie_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+
+start(_Type, _Args) ->
+ Dispatch = [
+ {'_', [
+ {'_', toppage_handler, []}
+ ]}
+ ],
+ {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
+ {dispatch, Dispatch}
+ ]),
+ cookie_sup:start_link().
+
+stop(_State) ->
+ ok.
diff --git a/examples/cookie/src/cookie_sup.erl b/examples/cookie/src/cookie_sup.erl
new file mode 100644
index 0000000..5ba4e53
--- /dev/null
+++ b/examples/cookie/src/cookie_sup.erl
@@ -0,0 +1,23 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @private
+-module(cookie_sup).
+-behaviour(supervisor).
+
+%% API.
+-export([start_link/0]).
+
+%% supervisor.
+-export([init/1]).
+
+%% API.
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% supervisor.
+
+init([]) ->
+ Procs = [],
+ {ok, {{one_for_one, 10, 10}, Procs}}.
diff --git a/examples/cookie/src/toppage_handler.erl b/examples/cookie/src/toppage_handler.erl
new file mode 100644
index 0000000..783cda6
--- /dev/null
+++ b/examples/cookie/src/toppage_handler.erl
@@ -0,0 +1,29 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+%% @doc Cookie handler.
+-module(toppage_handler).
+
+-export([init/3]).
+-export([handle/2]).
+-export([terminate/2]).
+
+init(_Transport, Req, []) ->
+ {ok, Req, undefined}.
+
+handle(Req, State) ->
+ NewValue = integer_to_list(random:uniform(1000000)),
+ Req2 = cowboy_req:set_resp_cookie(
+ <<"server">>, NewValue, [{path, <<"/">>}], Req),
+ {ClientCookie, Req3} = cowboy_req:cookie(<<"client">>, Req2),
+ {ServerCookie, Req4} = cowboy_req:cookie(<<"server">>, Req3),
+ {ok, Body} = toppage_dtl:render([
+ {client, ClientCookie},
+ {server, ServerCookie}
+ ]),
+ {ok, Req5} = cowboy_req:reply(200,
+ [{<<"content-type">>, <<"text/html">>}],
+ Body, Req4),
+ {ok, Req5, State}.
+
+terminate(_Req, _State) ->
+ ok.
diff --git a/examples/cookie/start.sh b/examples/cookie/start.sh
new file mode 100755
index 0000000..7b1d728
--- /dev/null
+++ b/examples/cookie/start.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+erl -pa ebin deps/*/ebin -s cookie \
+ -eval "io:format(\"Point your browser at http://localhost:8080~n\")."
diff --git a/examples/cookie/templates/toppage.dtl b/examples/cookie/templates/toppage.dtl
new file mode 100644
index 0000000..e6faa2b
--- /dev/null
+++ b/examples/cookie/templates/toppage.dtl
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title>Cowboy Cookie Example</title>
+</head>
+
+<body>
+ <h1>Cowboy Cookie Example</h1>
+ <p>Refresh the page to see the next cookie.</p>
+
+ <h2>Cookie Set Server-Side</h2>
+ <p>{{ server }}</p>
+
+ <h2>Cookie Set Client-Side</h2>
+ <p>{{ client }}</p>
+
+ <script type="text/javascript">
+// <![CDATA[
+document.cookie="client=test=";
+// ]]>
+ </script>
+</body>
+</html>
diff --git a/examples/echo_get/src/toppage_handler.erl b/examples/echo_get/src/toppage_handler.erl
index 6d914ec..86433cb 100644
--- a/examples/echo_get/src/toppage_handler.erl
+++ b/examples/echo_get/src/toppage_handler.erl
@@ -20,7 +20,7 @@ echo(<<"GET">>, undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(<<"GET">>, Echo, Req) ->
cowboy_req:reply(200,
- [{<<"Content-Encoding">>, <<"utf-8">>}], Echo, Req);
+ [{<<"content-encoding">>, <<"utf-8">>}], Echo, Req);
echo(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req).
diff --git a/examples/echo_post/src/toppage_handler.erl b/examples/echo_post/src/toppage_handler.erl
index 69aeb9f..808ba8e 100644
--- a/examples/echo_post/src/toppage_handler.erl
+++ b/examples/echo_post/src/toppage_handler.erl
@@ -30,7 +30,7 @@ echo(undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(Echo, Req) ->
cowboy_req:reply(200,
- [{<<"Content-Encoding">>, <<"utf-8">>}], Echo, Req).
+ [{<<"content-encoding">>, <<"utf-8">>}], Echo, Req).
terminate(_Req, _State) ->
ok.
diff --git a/guide/toc.md b/guide/toc.md
index ea543d1..b57b92e 100644
--- a/guide/toc.md
+++ b/guide/toc.md
@@ -8,9 +8,10 @@ Cowboy User Guide
* Getting started
* Routing
* Purpose
- * Dispatch rule
+ * Dispatch list
* Match rules
* Bindings
+ * Constraints
* Handlers
* Purpose
* Protocol upgrades
diff --git a/rebar.config b/rebar.config
index d35044d..0c0af46 100644
--- a/rebar.config
+++ b/rebar.config
@@ -1,6 +1,6 @@
{deps, [
{ranch, "0\\.4\\.0.*", {git, "git://github.com/extend/ranch.git",
- "cd099983b1b807b87fa050d1e4ff0a13aba25b49"}}
+ "fb7ed3807620f7534c617789e7347192838a419a"}}
]}.
{erl_opts, [
%% bin_opt_info,
diff --git a/src/cowboy_clock.erl b/src/cowboy_clock.erl
index 5e2bf44..b439bb1 100644
--- a/src/cowboy_clock.erl
+++ b/src/cowboy_clock.erl
@@ -25,6 +25,7 @@
-export([start_link/0]).
-export([stop/0]).
-export([rfc1123/0]).
+-export([rfc1123/1]).
-export([rfc2109/1]).
%% gen_server.
@@ -61,50 +62,26 @@ stop() ->
gen_server:call(?SERVER, stop).
%% @doc Return the current date and time formatted according to RFC-1123.
-%%
-%% This format is used in the <em>date</em> header sent with HTTP responses.
-spec rfc1123() -> binary().
rfc1123() ->
ets:lookup_element(?TABLE, rfc1123, 2).
+%% @doc Return the given date and time formatted according to RFC-1123.
+-spec rfc1123(calendar:datetime()) -> binary().
+rfc1123(DateTime) ->
+ update_rfc1123(<<>>, undefined, DateTime).
+
%% @doc Return the current date and time formatted according to RFC-2109.
%%
%% This format is used in the <em>set-cookie</em> header sent with
%% HTTP responses.
-spec rfc2109(calendar:datetime()) -> binary().
-rfc2109(LocalTime) ->
- {{YYYY,MM,DD},{Hour,Min,Sec}} =
- case calendar:local_time_to_universal_time_dst(LocalTime) of
- [Gmt] -> Gmt;
- [_,Gmt] -> Gmt;
- [] ->
- %% The localtime generated by cowboy_cookies may fall within
- %% the hour that is skipped by daylight savings time. If this
- %% is such a localtime, increment the localtime with one hour
- %% and try again, if this succeeds, subtracting the max_age
- %% from the resulting universaltime and converting to a local
- %% time will yield the original localtime.
- {Date, {Hour1, Min1, Sec1}} = LocalTime,
- LocalTime2 = {Date, {Hour1 + 1, Min1, Sec1}},
- case calendar:local_time_to_universal_time_dst(LocalTime2) of
- [Gmt] -> Gmt;
- [_,Gmt] -> Gmt
- end
- end,
- Wday = calendar:day_of_the_week({YYYY,MM,DD}),
- DayBin = pad_int(DD),
- YearBin = list_to_binary(integer_to_list(YYYY)),
- HourBin = pad_int(Hour),
- MinBin = pad_int(Min),
- SecBin = pad_int(Sec),
- WeekDay = weekday(Wday),
- Month = month(MM),
- <<WeekDay/binary, ", ",
- DayBin/binary, " ", Month/binary, " ",
- YearBin/binary, " ",
- HourBin/binary, ":",
- MinBin/binary, ":",
- SecBin/binary, " GMT">>.
+rfc2109({Date = {Y, Mo, D}, {H, Mi, S}}) ->
+ Wday = calendar:day_of_the_week(Date),
+ << (weekday(Wday))/binary, ", ", (pad_int(D))/binary, "-",
+ (month(Mo))/binary, "-", (list_to_binary(integer_to_list(Y)))/binary,
+ " ", (pad_int(H))/binary, $:, (pad_int(Mi))/binary,
+ $:, (pad_int(S))/binary, " GMT" >>.
%% gen_server.
@@ -215,6 +192,13 @@ month(12) -> <<"Dec">>.
-ifdef(TEST).
+rfc2109_test_() ->
+ Tests = [
+ {<<"Sat, 14-May-2011 14:25:33 GMT">>, {{2011, 5, 14}, {14, 25, 33}}},
+ {<<"Sun, 01-Jan-2012 00:00:00 GMT">>, {{2012, 1, 1}, { 0, 0, 0}}}
+ ],
+ [{R, fun() -> R = rfc2109(D) end} || {R, D} <- Tests].
+
update_rfc1123_test_() ->
Tests = [
{<<"Sat, 14 May 2011 14:25:33 GMT">>, undefined,
diff --git a/src/cowboy_cookies.erl b/src/cowboy_cookies.erl
deleted file mode 100644
index d10c848..0000000
--- a/src/cowboy_cookies.erl
+++ /dev/null
@@ -1,416 +0,0 @@
-%% Copyright 2007 Mochi Media, Inc.
-%% Copyright 2011 Thomas Burdick <[email protected]>
-%%
-%% Permission to use, copy, modify, and/or distribute this software for any
-%% purpose with or without fee is hereby granted, provided that the above
-%% copyright notice and this permission notice appear in all copies.
-%%
-%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-
-%% @doc HTTP Cookie parsing and generating (RFC 2965).
-
--module(cowboy_cookies).
-
-%% API.
--export([parse_cookie/1]).
--export([cookie/3]).
--export([cookie/2]).
-
-%% Types.
--type kv() :: {Name::binary(), Value::binary()}.
--type kvlist() :: [kv()].
--type cookie_option() :: {max_age, integer()}
- | {local_time, calendar:datetime()}
- | {domain, binary()} | {path, binary()}
- | {secure, true | false} | {http_only, true | false}.
-
--export_type([kv/0]).
--export_type([kvlist/0]).
--export_type([cookie_option/0]).
-
--define(QUOTE, $\").
-
--ifdef(TEST).
--include_lib("eunit/include/eunit.hrl").
--endif.
-
-%% API.
-
-%% @doc Parse the contents of a Cookie header field, ignoring cookie
-%% attributes, and return a simple property list.
--spec parse_cookie(binary()) -> kvlist().
-parse_cookie(<<>>) ->
- [];
-parse_cookie(Cookie) when is_binary(Cookie) ->
- parse_cookie(Cookie, []).
-
-%% @equiv cookie(Key, Value, [])
--spec cookie(binary(), binary()) -> kv().
-cookie(Key, Value) when is_binary(Key) andalso is_binary(Value) ->
- cookie(Key, Value, []).
-
-%% @doc Generate a Set-Cookie header field tuple.
--spec cookie(binary(), binary(), [cookie_option()]) -> kv().
-cookie(Key, Value, Options) when is_binary(Key)
- andalso is_binary(Value) andalso is_list(Options) ->
- Cookie = <<(any_to_binary(Key))/binary, "=",
- (quote(Value))/binary, "; Version=1">>,
- %% Set-Cookie:
- %% Comment, Domain, Max-Age, Path, Secure, Version
- ExpiresPart =
- case proplists:get_value(max_age, Options) of
- undefined ->
- <<"">>;
- RawAge ->
- When = case proplists:get_value(local_time, Options) of
- undefined ->
- calendar:local_time();
- LocalTime ->
- LocalTime
- end,
- Age = case RawAge < 0 of
- true ->
- 0;
- false ->
- RawAge
- end,
- AgeBinary = quote(Age),
- CookieDate = age_to_cookie_date(Age, When),
- <<"; Expires=", CookieDate/binary,
- "; Max-Age=", AgeBinary/binary>>
- end,
- SecurePart =
- case proplists:get_value(secure, Options) of
- true ->
- <<"; Secure">>;
- _ ->
- <<"">>
- end,
- DomainPart =
- case proplists:get_value(domain, Options) of
- undefined ->
- <<"">>;
- Domain ->
- <<"; Domain=", (quote(Domain))/binary>>
- end,
- PathPart =
- case proplists:get_value(path, Options) of
- undefined ->
- <<"">>;
- Path ->
- <<"; Path=", (quote(Path, true))/binary>>
- end,
- HttpOnlyPart =
- case proplists:get_value(http_only, Options) of
- true ->
- <<"; HttpOnly">>;
- _ ->
- <<"">>
- end,
- CookieParts = <<Cookie/binary, ExpiresPart/binary, SecurePart/binary,
- DomainPart/binary, PathPart/binary, HttpOnlyPart/binary>>,
- {<<"Set-Cookie">>, CookieParts}.
-
-%% Internal.
-
-%% @doc Check if a character is a white space character.
--spec is_whitespace(char()) -> boolean().
-is_whitespace($\s) -> true;
-is_whitespace($\t) -> true;
-is_whitespace($\r) -> true;
-is_whitespace($\n) -> true;
-is_whitespace(_) -> false.
-
-%% @doc Check if a character is a separator.
--spec is_separator(char()) -> boolean().
-is_separator(C) when C < 32 -> true;
-is_separator($\s) -> true;
-is_separator($\t) -> true;
-is_separator($() -> true;
-is_separator($)) -> true;
-is_separator($<) -> true;
-is_separator($>) -> true;
-is_separator($@) -> true;
-is_separator($,) -> true;
-is_separator($;) -> true;
-is_separator($:) -> true;
-is_separator($\\) -> true;
-is_separator(?QUOTE) -> true;
-is_separator($/) -> true;
-is_separator($[) -> true;
-is_separator($]) -> true;
-is_separator($?) -> true;
-is_separator($=) -> true;
-is_separator(${) -> true;
-is_separator($}) -> true;
-is_separator(_) -> false.
-
-%% @doc Check if a binary has an ASCII separator character.
--spec has_separator(binary(), boolean()) -> boolean().
-has_separator(<<>>, _) ->
- false;
-has_separator(<<$/, Rest/binary>>, true) ->
- has_separator(Rest, true);
-has_separator(<<C, Rest/binary>>, IgnoreSlash) ->
- case is_separator(C) of
- true ->
- true;
- false ->
- has_separator(Rest, IgnoreSlash)
- end.
-
-%% @doc Convert to a binary and raise an error if quoting is required. Quoting
-%% is broken in different ways for different browsers. Its better to simply
-%% avoiding doing it at all.
-%% @end
--spec quote(term(), boolean()) -> binary().
-quote(V0, IgnoreSlash) ->
- V = any_to_binary(V0),
- case has_separator(V, IgnoreSlash) of
- true ->
- erlang:error({cookie_quoting_required, V});
- false ->
- V
- end.
-
-%% @equiv quote(Bin, false)
--spec quote(term()) -> binary().
-quote(V0) ->
- quote(V0, false).
-
--spec add_seconds(integer(), calendar:datetime()) -> calendar:datetime().
-add_seconds(Secs, LocalTime) ->
- Greg = calendar:datetime_to_gregorian_seconds(LocalTime),
- calendar:gregorian_seconds_to_datetime(Greg + Secs).
-
--spec age_to_cookie_date(integer(), calendar:datetime()) -> binary().
-age_to_cookie_date(Age, LocalTime) ->
- cowboy_clock:rfc2109(add_seconds(Age, LocalTime)).
-
--spec parse_cookie(binary(), kvlist()) -> kvlist().
-parse_cookie(<<>>, Acc) ->
- lists:reverse(Acc);
-parse_cookie(String, Acc) ->
- {{Token, Value}, Rest} = read_pair(String),
- Acc1 = case Token of
- <<"">> ->
- Acc;
- <<"$", _R/binary>> ->
- Acc;
- _ ->
- [{Token, Value} | Acc]
- end,
- parse_cookie(Rest, Acc1).
-
--spec read_pair(binary()) -> {{binary(), binary()}, binary()}.
-read_pair(String) ->
- {Token, Rest} = read_token(skip_whitespace(String)),
- {Value, Rest1} = read_value(skip_whitespace(Rest)),
- {{Token, Value}, skip_past_separator(Rest1)}.
-
--spec read_value(binary()) -> {binary(), binary()}.
-read_value(<<"=", Value/binary>>) ->
- Value1 = skip_whitespace(Value),
- case Value1 of
- <<?QUOTE, _R/binary>> ->
- read_quoted(Value1);
- _ ->
- read_token(Value1)
- end;
-read_value(String) ->
- {<<"">>, String}.
-
--spec read_quoted(binary()) -> {binary(), binary()}.
-read_quoted(<<?QUOTE, String/binary>>) ->
- read_quoted(String, <<"">>).
-
--spec read_quoted(binary(), binary()) -> {binary(), binary()}.
-read_quoted(<<"">>, Acc) ->
- {Acc, <<"">>};
-read_quoted(<<?QUOTE, Rest/binary>>, Acc) ->
- {Acc, Rest};
-read_quoted(<<$\\, Any, Rest/binary>>, Acc) ->
- read_quoted(Rest, <<Acc/binary, Any>>);
-read_quoted(<<C, Rest/binary>>, Acc) ->
- read_quoted(Rest, <<Acc/binary, C>>).
-
-%% @doc Drop characters while a function returns true.
--spec binary_dropwhile(fun((char()) -> boolean()), binary()) -> binary().
-binary_dropwhile(_F, <<"">>) ->
- <<"">>;
-binary_dropwhile(F, String) ->
- <<C, Rest/binary>> = String,
- case F(C) of
- true ->
- binary_dropwhile(F, Rest);
- false ->
- String
- end.
-
-%% @doc Remove leading whitespace.
--spec skip_whitespace(binary()) -> binary().
-skip_whitespace(String) ->
- binary_dropwhile(fun is_whitespace/1, String).
-
-%% @doc Split a binary when the current character causes F to return true.
--spec binary_splitwith(fun((char()) -> boolean()), binary(), binary())
- -> {binary(), binary()}.
-binary_splitwith(_F, Head, <<>>) ->
- {Head, <<>>};
-binary_splitwith(F, Head, Tail) ->
- <<C, NTail/binary>> = Tail,
- case F(C) of
- true ->
- {Head, Tail};
- false ->
- binary_splitwith(F, <<Head/binary, C>>, NTail)
- end.
-
-%% @doc Split a binary with a function returning true or false on each char.
--spec binary_splitwith(fun((char()) -> boolean()), binary())
- -> {binary(), binary()}.
-binary_splitwith(F, String) ->
- binary_splitwith(F, <<>>, String).
-
-%% @doc Split the binary when the next separator is found.
--spec read_token(binary()) -> {binary(), binary()}.
-read_token(String) ->
- binary_splitwith(fun is_separator/1, String).
-
-%% @doc Return string after ; or , characters.
--spec skip_past_separator(binary()) -> binary().
-skip_past_separator(<<"">>) ->
- <<"">>;
-skip_past_separator(<<";", Rest/binary>>) ->
- Rest;
-skip_past_separator(<<",", Rest/binary>>) ->
- Rest;
-skip_past_separator(<<_C, Rest/binary>>) ->
- skip_past_separator(Rest).
-
--spec any_to_binary(binary() | string() | atom() | integer()) -> binary().
-any_to_binary(V) when is_binary(V) ->
- V;
-any_to_binary(V) when is_list(V) ->
- erlang:list_to_binary(V);
-any_to_binary(V) when is_atom(V) ->
- erlang:atom_to_binary(V, latin1);
-any_to_binary(V) when is_integer(V) ->
- list_to_binary(integer_to_list(V)).
-
-%% Tests.
-
--ifdef(TEST).
-
-quote_test() ->
- %% ?assertError eunit macro is not compatible with coverage module
- _ = try quote(<<":wq">>)
- catch error:{cookie_quoting_required, <<":wq">>} -> ok
- end,
- ?assertEqual(<<"foo">>,quote(foo)),
- _ = try quote(<<"/test/slashes/">>)
- catch error:{cookie_quoting_required, <<"/test/slashes/">>} -> ok
- end,
- ok.
-
-parse_cookie_test() ->
- %% RFC example
- C1 = <<"$Version=\"1\"; Customer=\"WILE_E_COYOTE\"; $Path=\"/acme\";
- Part_Number=\"Rocket_Launcher_0001\"; $Path=\"/acme\";
- Shipping=\"FedEx\"; $Path=\"/acme\"">>,
- ?assertEqual(
- [{<<"Customer">>,<<"WILE_E_COYOTE">>},
- {<<"Part_Number">>,<<"Rocket_Launcher_0001">>},
- {<<"Shipping">>,<<"FedEx">>}],
- parse_cookie(C1)),
- %% Potential edge cases
- ?assertEqual(
- [{<<"foo">>, <<"x">>}],
- parse_cookie(<<"foo=\"\\x\"">>)),
- ?assertEqual(
- [],
- parse_cookie(<<"=">>)),
- ?assertEqual(
- [{<<"foo">>, <<"">>}, {<<"bar">>, <<"">>}],
- parse_cookie(<<" foo ; bar ">>)),
- ?assertEqual(
- [{<<"foo">>, <<"">>}, {<<"bar">>, <<"">>}],
- parse_cookie(<<"foo=;bar=">>)),
- ?assertEqual(
- [{<<"foo">>, <<"\";">>}, {<<"bar">>, <<"">>}],
- parse_cookie(<<"foo = \"\\\";\";bar ">>)),
- ?assertEqual(
- [{<<"foo">>, <<"\";bar">>}],
- parse_cookie(<<"foo=\"\\\";bar">>)),
- ?assertEqual(
- [],
- parse_cookie(<<"">>)),
- ?assertEqual(
- [{<<"foo">>, <<"bar">>}, {<<"baz">>, <<"wibble">>}],
- parse_cookie(<<"foo=bar , baz=wibble ">>)),
- ok.
-
-domain_test() ->
- ?assertEqual(
- {<<"Set-Cookie">>,
- <<"Customer=WILE_E_COYOTE; "
- "Version=1; "
- "Domain=acme.com; "
- "HttpOnly">>},
- cookie(<<"Customer">>, <<"WILE_E_COYOTE">>,
- [{http_only, true}, {domain, <<"acme.com">>}])),
- ok.
-
-local_time_test() ->
- {<<"Set-Cookie">>, B} = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>,
- [{max_age, 111}, {secure, true}]),
-
- ?assertMatch(
- [<<"Customer=WILE_E_COYOTE">>,
- <<" Version=1">>,
- <<" Expires=", _R/binary>>,
- <<" Max-Age=111">>,
- <<" Secure">>],
- binary:split(B, <<";">>, [global])),
- ok.
-
--spec cookie_test() -> no_return(). %% Not actually true, just a bad option.
-cookie_test() ->
- C1 = {<<"Set-Cookie">>,
- <<"Customer=WILE_E_COYOTE; "
- "Version=1; "
- "Path=/acme">>},
- C1 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>, [{path, <<"/acme">>}]),
-
- C1 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>,
- [{path, <<"/acme">>}, {badoption, <<"negatory">>}]),
-
- {<<"Set-Cookie">>,<<"=NoKey; Version=1">>}
- = cookie(<<"">>, <<"NoKey">>, []),
- {<<"Set-Cookie">>,<<"=NoKey; Version=1">>}
- = cookie(<<"">>, <<"NoKey">>),
- LocalTime = calendar:universal_time_to_local_time(
- {{2007, 5, 15}, {13, 45, 33}}),
- C2 = {<<"Set-Cookie">>,
- <<"Customer=WILE_E_COYOTE; "
- "Version=1; "
- "Expires=Tue, 15 May 2007 13:45:33 GMT; "
- "Max-Age=0">>},
- C2 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>,
- [{max_age, -111}, {local_time, LocalTime}]),
- C3 = {<<"Set-Cookie">>,
- <<"Customer=WILE_E_COYOTE; "
- "Version=1; "
- "Expires=Wed, 16 May 2007 13:45:50 GMT; "
- "Max-Age=86417">>},
- C3 = cookie(<<"Customer">>, <<"WILE_E_COYOTE">>,
- [{max_age, 86417}, {local_time, LocalTime}]),
- ok.
-
--endif.
diff --git a/src/cowboy_dispatcher.erl b/src/cowboy_dispatcher.erl
index cfb8fb6..ef6e8ac 100644
--- a/src/cowboy_dispatcher.erl
+++ b/src/cowboy_dispatcher.erl
@@ -21,7 +21,7 @@
-type bindings() :: [{atom(), binary()}].
-type tokens() :: [binary()].
--type match_rule() :: '_' | '*' | [binary() | '_' | '...' | atom()].
+-type match_rule() :: '_' | <<_:8>> | [binary() | '_' | '...' | atom()].
-type dispatch_path() :: [{match_rule(), module(), any()}].
-type dispatch_rule() :: {Host::match_rule(), Path::dispatch_path()}.
-type dispatch_rules() :: [dispatch_rule()].
@@ -45,9 +45,10 @@
%% <em>PathRules</em> being a list of <em>{Path, HandlerMod, HandlerOpts}</em>.
%%
%% <em>Hostname</em> and <em>Path</em> are match rules and can be either the
-%% atom <em>'_'</em>, which matches everything for a single token, the atom
-%% <em>'*'</em>, which matches everything for the rest of the tokens, or a
-%% list of tokens. Each token can be either a binary, the atom <em>'_'</em>,
+%% atom <em>'_'</em>, which matches everything, `<<"*">>', which match the
+%% wildcard path, or a list of tokens.
+%%
+%% Each token can be either a binary, the atom <em>'_'</em>,
%% the atom '...' or a named atom. A binary token must match exactly,
%% <em>'_'</em> matches everything for a single token, <em>'...'</em> matches
%% everything for the rest of the tokens and a named atom will bind the
@@ -67,7 +68,8 @@
-> {ok, module(), any(), bindings(),
HostInfo::undefined | tokens(),
PathInfo::undefined | tokens()}
- | {error, notfound, host} | {error, notfound, path}.
+ | {error, notfound, host} | {error, notfound, path}
+ | {error, badrequest, path}.
match([], _, _) ->
{error, notfound, host};
match([{'_', PathMatchs}|_Tail], _, Path) ->
@@ -91,12 +93,12 @@ match(Dispatch, Host, Path) ->
-> {ok, module(), any(), bindings(),
HostInfo::undefined | tokens(),
PathInfo::undefined | tokens()}
- | {error, notfound, path}.
+ | {error, notfound, path} | {error, badrequest, path}.
match_path([], _, _, _) ->
{error, notfound, path};
match_path([{'_', Handler, Opts}|_Tail], HostInfo, _, Bindings) ->
{ok, Handler, Opts, Bindings, HostInfo, undefined};
-match_path([{'*', Handler, Opts}|_Tail], HostInfo, '*', Bindings) ->
+match_path([{<<"*">>, Handler, Opts}|_Tail], HostInfo, <<"*">>, Bindings) ->
{ok, Handler, Opts, Bindings, HostInfo, undefined};
match_path([{PathMatch, Handler, Opts}|Tail], HostInfo, Tokens,
Bindings) when is_list(Tokens) ->
@@ -106,6 +108,8 @@ match_path([{PathMatch, Handler, Opts}|Tail], HostInfo, Tokens,
{true, PathBinds, PathInfo} ->
{ok, Handler, Opts, Bindings ++ PathBinds, HostInfo, PathInfo}
end;
+match_path(_Dispatch, _HostInfo, badrequest, _Bindings) ->
+ {error, badrequest, path};
match_path(Dispatch, HostInfo, Path, Bindings) ->
match_path(Dispatch, HostInfo, split_path(Path), Bindings).
@@ -135,17 +139,24 @@ split_host(Host, Acc) ->
%% and part of a path segment.
-spec split_path(binary()) -> tokens().
split_path(<< $/, Path/bits >>) ->
- split_path(Path, []).
+ split_path(Path, []);
+split_path(_) ->
+ badrequest.
split_path(Path, Acc) ->
- case binary:match(Path, <<"/">>) of
- nomatch when Path =:= <<>> ->
- lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
- nomatch ->
- lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]);
- {Pos, _} ->
- << Segment:Pos/binary, _:8, Rest/bits >> = Path,
- split_path(Rest, [Segment|Acc])
+ try
+ case binary:match(Path, <<"/">>) of
+ nomatch when Path =:= <<>> ->
+ lists:reverse([cowboy_http:urldecode(S) || S <- Acc]);
+ nomatch ->
+ lists:reverse([cowboy_http:urldecode(S) || S <- [Path|Acc]]);
+ {Pos, _} ->
+ << Segment:Pos/binary, _:8, Rest/bits >> = Path,
+ split_path(Rest, [Segment|Acc])
+ end
+ catch
+ error:badarg ->
+ badrequest
end.
-spec list_match(tokens(), match_rule(), bindings())
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index e0b1632..66383cb 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -19,6 +19,7 @@
%% Parsing.
-export([list/2]).
-export([nonempty_list/2]).
+-export([cookie_list/1]).
-export([content_type/1]).
-export([media_range/2]).
-export([conneg/2]).
@@ -42,6 +43,7 @@
-export([ce_identity/1]).
%% Interpretation.
+-export([cookie_to_iodata/3]).
-export([version_to_binary/1]).
-export([urldecode/1]).
-export([urldecode/2]).
@@ -100,6 +102,77 @@ list(Data, Fun, Acc) ->
end)
end).
+%% @doc Parse a list of cookies.
+%%
+%% We need a special function for this because we need to support both
+%% $; and $, as separators as per RFC2109.
+-spec cookie_list(binary()) -> [{binary(), binary()}] | {error, badarg}.
+cookie_list(Data) ->
+ case cookie_list(Data, []) of
+ {error, badarg} -> {error, badarg};
+ [] -> {error, badarg};
+ L -> lists:reverse(L)
+ end.
+
+-spec cookie_list(binary(), Acc) -> Acc | {error, badarg}
+ when Acc::[{binary(), binary()}].
+cookie_list(Data, Acc) ->
+ whitespace(Data,
+ fun (<<>>) -> Acc;
+ (<< $,, Rest/binary >>) -> cookie_list(Rest, Acc);
+ (<< $;, Rest/binary >>) -> cookie_list(Rest, Acc);
+ (Rest) -> cookie(Rest,
+ fun (Rest2, << $$, _/bits >>, _) ->
+ cookie_list(Rest2, Acc);
+ (Rest2, Name, Value) ->
+ cookie_list(Rest2, [{Name, Value}|Acc])
+ end)
+ end).
+
+-spec cookie(binary(), fun()) -> any().
+cookie(Data, Fun) ->
+ whitespace(Data,
+ fun (Rest) ->
+ cookie_name(Rest,
+ fun (_Rest2, <<>>) -> {error, badarg};
+ (<< $=, Rest2/binary >>, Name) ->
+ cookie_value(Rest2,
+ fun (Rest3, Value) ->
+ Fun(Rest3, Name, Value)
+ end);
+ (_Rest2, _Attr) -> {error, badarg}
+ end)
+ end).
+
+-spec cookie_name(binary(), fun()) -> any().
+cookie_name(Data, Fun) ->
+ cookie_name(Data, Fun, <<>>).
+
+-spec cookie_name(binary(), fun(), binary()) -> any().
+cookie_name(<<>>, Fun, Acc) ->
+ Fun(<<>>, Acc);
+cookie_name(Data = << C, _Rest/binary >>, Fun, Acc)
+ when C =:= $=; C =:= $,; C =:= $;; C =:= $\s; C =:= $\t;
+ C =:= $\r; C =:= $\n; C =:= $\013; C =:= $\014 ->
+ Fun(Data, Acc);
+cookie_name(<< C, Rest/binary >>, Fun, Acc) ->
+ C2 = cowboy_bstr:char_to_lower(C),
+ cookie_name(Rest, Fun, << Acc/binary, C2 >>).
+
+-spec cookie_value(binary(), fun()) -> any().
+cookie_value(Data, Fun) ->
+ cookie_value(Data, Fun, <<>>).
+
+-spec cookie_value(binary(), fun(), binary()) -> any().
+cookie_value(<<>>, Fun, Acc) ->
+ Fun(<<>>, Acc);
+cookie_value(Data = << C, _Rest/binary >>, Fun, Acc)
+ when C =:= $,; C =:= $;; C =:= $\s; C =:= $\t;
+ C =:= $\r; C =:= $\n; C =:= $\013; C =:= $\014 ->
+ Fun(Data, Acc);
+cookie_value(<< C, Rest/binary >>, Fun, Acc) ->
+ cookie_value(Rest, Fun, << Acc/binary, C >>).
+
%% @doc Parse a content type.
-spec content_type(binary()) -> any().
content_type(Data) ->
@@ -341,12 +414,17 @@ params(Data, Fun) ->
-spec params(binary(), fun(), [{binary(), binary()}]) -> any().
params(Data, Fun, Acc) ->
whitespace(Data,
- fun (<< $;, Rest/binary >>) -> param(Rest, Fun, Acc);
- (Rest) -> Fun(Rest, lists:reverse(Acc))
+ fun (<< $;, Rest/binary >>) ->
+ param(Rest,
+ fun (Rest2, Attr, Value) ->
+ params(Rest2, Fun, [{Attr, Value}|Acc])
+ end);
+ (Rest) ->
+ Fun(Rest, lists:reverse(Acc))
end).
--spec param(binary(), fun(), [{binary(), binary()}]) -> any().
-param(Data, Fun, Acc) ->
+-spec param(binary(), fun()) -> any().
+param(Data, Fun) ->
whitespace(Data,
fun (Rest) ->
token_ci(Rest,
@@ -354,8 +432,7 @@ param(Data, Fun, Acc) ->
(<< $=, Rest2/binary >>, Attr) ->
word(Rest2,
fun (Rest3, Value) ->
- params(Rest3, Fun,
- [{Attr, Value}|Acc])
+ Fun(Rest3, Attr, Value)
end);
(_Rest2, _Attr) -> {error, badarg}
end)
@@ -772,6 +849,55 @@ ce_identity(Data) ->
%% Interpretation.
+%% @doc Convert a cookie name, value and options to its iodata form.
+%% @end
+%%
+%% Initially from Mochiweb:
+%% * Copyright 2007 Mochi Media, Inc.
+%% Initial binary implementation:
+%% * Copyright 2011 Thomas Burdick <[email protected]>
+-spec cookie_to_iodata(iodata(), iodata(), cowboy_req:cookie_opts())
+ -> iodata().
+cookie_to_iodata(Name, Value, Opts) ->
+ case binary:match(iolist_to_binary(Name), [<<$=>>, <<$,>>, <<$;>>,
+ <<$\s>>, <<$\t>>, <<$\r>>, <<$\n>>, <<$\013>>, <<$\014>>]) of
+ nomatch -> ok
+ end,
+ case binary:match(iolist_to_binary(Value), [<<$,>>, <<$;>>,
+ <<$\s>>, <<$\t>>, <<$\r>>, <<$\n>>, <<$\013>>, <<$\014>>]) of
+ nomatch -> ok
+ end,
+ MaxAgeBin = case lists:keyfind(max_age, 1, Opts) of
+ false -> <<>>;
+ {_, 0} ->
+ %% MSIE requires an Expires date in the past to delete a cookie.
+ <<"; Expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0">>;
+ {_, MaxAge} when is_integer(MaxAge), MaxAge > 0 ->
+ UTC = calendar:universal_time(),
+ Secs = calendar:datetime_to_gregorian_seconds(UTC),
+ Expires = calendar:gregorian_seconds_to_datetime(Secs + MaxAge),
+ [<<"; Expires=">>, cowboy_clock:rfc2109(Expires),
+ <<"; Max-Age=">>, integer_to_list(MaxAge)]
+ end,
+ DomainBin = case lists:keyfind(domain, 1, Opts) of
+ false -> <<>>;
+ {_, Domain} -> [<<"; Domain=">>, Domain]
+ end,
+ PathBin = case lists:keyfind(path, 1, Opts) of
+ false -> <<>>;
+ {_, Path} -> [<<"; Path=">>, Path]
+ end,
+ SecureBin = case lists:keyfind(secure, 1, Opts) of
+ false -> <<>>;
+ {_, true} -> <<"; Secure">>
+ end,
+ HttpOnlyBin = case lists:keyfind(http_only, 1, Opts) of
+ false -> <<>>;
+ {_, true} -> <<"; HttpOnly">>
+ end,
+ [Name, <<"=">>, Value, <<"; Version=1">>,
+ MaxAgeBin, DomainBin, PathBin, SecureBin, HttpOnlyBin].
+
%% @doc Convert an HTTP version tuple to its binary form.
-spec version_to_binary(version()) -> binary().
version_to_binary({1, 1}) -> <<"HTTP/1.1">>;
@@ -927,6 +1053,38 @@ nonempty_token_list_test_() ->
],
[{V, fun() -> R = nonempty_list(V, fun token/2) end} || {V, R} <- Tests].
+cookie_list_test_() ->
+ %% {Value, Result}.
+ Tests = [
+ {<<"name=value; name2=value2">>, [
+ {<<"name">>, <<"value">>},
+ {<<"name2">>, <<"value2">>}
+ ]},
+ {<<"$Version=1; Customer=WILE_E_COYOTE; $Path=/acme">>, [
+ {<<"customer">>, <<"WILE_E_COYOTE">>}
+ ]},
+ {<<"$Version=1; Customer=WILE_E_COYOTE; $Path=/acme; "
+ "Part_Number=Rocket_Launcher_0001; $Path=/acme; "
+ "Shipping=FedEx; $Path=/acme">>, [
+ {<<"customer">>, <<"WILE_E_COYOTE">>},
+ {<<"part_number">>, <<"Rocket_Launcher_0001">>},
+ {<<"shipping">>, <<"FedEx">>}
+ ]},
+ %% Potential edge cases (initially from Mochiweb).
+ {<<"foo=\\x">>, [{<<"foo">>, <<"\\x">>}]},
+ {<<"=">>, {error, badarg}},
+ {<<" foo ; bar ">>, {error, badarg}},
+ {<<"foo=;bar=">>, [{<<"foo">>, <<>>}, {<<"bar">>, <<>>}]},
+ {<<"foo=\\\";;bar ">>, {error, badarg}},
+ {<<"foo=\\\";;bar=good ">>,
+ [{<<"foo">>, <<"\\\"">>}, {<<"bar">>, <<"good">>}]},
+ {<<"foo=\"\\\";bar">>, {error, badarg}},
+ {<<"">>, {error, badarg}},
+ {<<"foo=bar , baz=wibble ">>,
+ [{<<"foo">>, <<"bar">>}, {<<"baz">>, <<"wibble">>}]}
+ ],
+ [{V, fun() -> R = cookie_list(V) end} || {V, R} <- Tests].
+
media_range_list_test_() ->
%% {Tokens, Result}
Tests = [
@@ -1040,6 +1198,64 @@ digits_test_() ->
],
[{V, fun() -> R = digits(V) end} || {V, R} <- Tests].
+cookie_to_iodata_test_() ->
+ %% {Name, Value, Opts, Result}
+ Tests = [
+ {<<"Customer">>, <<"WILE_E_COYOTE">>,
+ [{http_only, true}, {domain, <<"acme.com">>}],
+ <<"Customer=WILE_E_COYOTE; Version=1; "
+ "Domain=acme.com; HttpOnly">>},
+ {<<"Customer">>, <<"WILE_E_COYOTE">>,
+ [{path, <<"/acme">>}],
+ <<"Customer=WILE_E_COYOTE; Version=1; Path=/acme">>},
+ {<<"Customer">>, <<"WILE_E_COYOTE">>,
+ [{path, <<"/acme">>}, {badoption, <<"negatory">>}],
+ <<"Customer=WILE_E_COYOTE; Version=1; Path=/acme">>}
+ ],
+ [{R, fun() -> R = iolist_to_binary(cookie_to_iodata(N, V, O)) end}
+ || {N, V, O, R} <- Tests].
+
+cookie_to_iodata_max_age_test() ->
+ F = fun(N, V, O) ->
+ binary:split(iolist_to_binary(
+ cookie_to_iodata(N, V, O)), <<";">>, [global])
+ end,
+ [<<"Customer=WILE_E_COYOTE">>,
+ <<" Version=1">>,
+ <<" Expires=", _/binary>>,
+ <<" Max-Age=111">>,
+ <<" Secure">>] = F(<<"Customer">>, <<"WILE_E_COYOTE">>,
+ [{max_age, 111}, {secure, true}]),
+ case catch F(<<"Customer">>, <<"WILE_E_COYOTE">>, [{max_age, -111}]) of
+ {'EXIT', {{case_clause, {max_age, -111}}, _}} -> ok
+ end,
+ [<<"Customer=WILE_E_COYOTE">>,
+ <<" Version=1">>,
+ <<" Expires=", _/binary>>,
+ <<" Max-Age=86417">>] = F(<<"Customer">>, <<"WILE_E_COYOTE">>,
+ [{max_age, 86417}]),
+ ok.
+
+cookie_to_iodata_failures_test_() ->
+ F = fun(N, V) ->
+ try cookie_to_iodata(N, V, []) of
+ _ ->
+ false
+ catch _:_ ->
+ true
+ end
+ end,
+ Tests = [
+ {<<"Na=me">>, <<"Value">>},
+ {<<"Name;">>, <<"Value">>},
+ {<<"\r\name">>, <<"Value">>},
+ {<<"Name">>, <<"Value;">>},
+ {<<"Name">>, <<"\value">>}
+ ],
+ [{iolist_to_binary(io_lib:format("{~p, ~p} failure", [N, V])),
+ fun() -> true = F(N, V) end}
+ || {N, V} <- Tests].
+
x_www_form_urlencoded_test_() ->
%% {Qs, Result}
Tests = [
diff --git a/src/cowboy_multipart.erl b/src/cowboy_multipart.erl
index fc889ef..7363054 100644
--- a/src/cowboy_multipart.erl
+++ b/src/cowboy_multipart.erl
@@ -74,17 +74,51 @@ parse(Bin, Boundary) ->
more(Bin, fun (NewBin) -> parse(NewBin, Boundary) end).
-type pattern() :: {binary:cp(), non_neg_integer()}.
+-type patterns() :: {pattern(), pattern()}.
-%% @doc Return a compiled binary pattern with its size in bytes.
-%% The pattern is the boundary prepended with "\r\n--".
--spec pattern(binary()) -> pattern().
+%% @doc Return two compiled binary patterns with their sizes in bytes.
+%% The boundary pattern is the boundary prepended with "\r\n--".
+%% The boundary suffix pattern matches all prefixes of the boundary.
+-spec pattern(binary()) -> patterns().
pattern(Boundary) ->
MatchPattern = <<"\r\n--", Boundary/binary>>,
- {binary:compile_pattern(MatchPattern), byte_size(MatchPattern)}.
+ MatchPrefixes = prefixes(MatchPattern),
+ {{binary:compile_pattern(MatchPattern), byte_size(MatchPattern)},
+ {binary:compile_pattern(MatchPrefixes), byte_size(MatchPattern)}}.
+
+%% @doc Return all prefixes of a binary string.
+%% The list of prefixes includes the full string.
+-spec prefixes(binary()) -> [binary()].
+prefixes(<<C, Rest/binary>>) ->
+ prefixes(Rest, <<C>>).
+
+-spec prefixes(binary(), binary()) -> [binary()].
+prefixes(<<C, Rest/binary>>, Acc) ->
+ [Acc|prefixes(Rest, <<Acc/binary, C>>)];
+prefixes(<<>>, Acc) ->
+ [Acc].
+
+%% @doc Test if a boundary is a possble suffix.
+%% The patterns are expected to have been returned from `pattern/1'.
+-spec suffix_match(binary(), patterns()) -> nomatch | {integer(), integer()}.
+suffix_match(Bin, {_Boundary, {Pat, Len}}) ->
+ Size = byte_size(Bin),
+ suffix_match(Bin, Pat, Size, max(-Size, -Len)).
+
+-spec suffix_match(binary(), tuple(), non_neg_integer(), 0|neg_integer()) ->
+ nomatch | {integer(), integer()}.
+suffix_match(_Bin, _Pat, _Size, _Match=0) ->
+ nomatch;
+suffix_match(Bin, Pat, Size, Match) when Match < 0 ->
+ case binary:match(Bin, Pat, [{scope, {Size, Match}}]) of
+ {Pos, Len}=Part when Pos + Len =:= Size -> Part;
+ {_, Len} -> suffix_match(Bin, Pat, Size, Match + Len);
+ nomatch -> nomatch
+ end.
%% @doc Parse remaining characters of a line beginning with the boundary.
%% If followed by "--", <em>eof</em> is returned and parsing is finished.
--spec parse_boundary_tail(binary(), pattern()) -> more(part_result()).
+-spec parse_boundary_tail(binary(), patterns()) -> more(part_result()).
parse_boundary_tail(Bin, Pattern) when byte_size(Bin) >= 2 ->
case Bin of
<<"--", _Rest/binary>> ->
@@ -100,7 +134,7 @@ parse_boundary_tail(Bin, Pattern) ->
more(Bin, fun (NewBin) -> parse_boundary_tail(NewBin, Pattern) end).
%% @doc Skip whitespace and unknown chars until CRLF.
--spec parse_boundary_eol(binary(), pattern()) -> more(part_result()).
+-spec parse_boundary_eol(binary(), patterns()) -> more(part_result()).
parse_boundary_eol(Bin, Pattern) ->
case binary:match(Bin, <<"\r\n">>) of
{CrlfStart, _Length} ->
@@ -115,7 +149,7 @@ parse_boundary_eol(Bin, Pattern) ->
more(Rest, fun (NewBin) -> parse_boundary_eol(NewBin, Pattern) end)
end.
--spec parse_boundary_crlf(binary(), pattern()) -> more(part_result()).
+-spec parse_boundary_crlf(binary(), patterns()) -> more(part_result()).
parse_boundary_crlf(<<"\r\n", Rest/binary>>, Pattern) ->
% The binary is at least 2 bytes long as this function is only called by
% parse_boundary_eol/3 when CRLF has been found so a more tuple will never
@@ -127,11 +161,11 @@ parse_boundary_crlf(Bin, Pattern) ->
% considered part of the boundary so EOL needs to be searched again.
parse_boundary_eol(Bin, Pattern).
--spec parse_headers(binary(), pattern()) -> more(part_result()).
+-spec parse_headers(binary(), patterns()) -> more(part_result()).
parse_headers(Bin, Pattern) ->
parse_headers(Bin, Pattern, []).
--spec parse_headers(binary(), pattern(), http_headers()) -> more(part_result()).
+-spec parse_headers(binary(), patterns(), http_headers()) -> more(part_result()).
parse_headers(Bin, Pattern, Acc) ->
case erlang:decode_packet(httph_bin, Bin, []) of
{ok, {http_header, _, Name, _, Value}, Rest} ->
@@ -150,8 +184,8 @@ parse_headers(Bin, Pattern, Acc) ->
more(Bin, fun (NewBin) -> parse_headers(NewBin, Pattern, Acc) end)
end.
--spec parse_body(binary(), pattern()) -> more(body_result()).
-parse_body(Bin, Pattern = {P, PSize}) when byte_size(Bin) >= PSize ->
+-spec parse_body(binary(), patterns()) -> more(body_result()).
+parse_body(Bin, Pattern = {{P, PSize}, _}) when byte_size(Bin) >= PSize ->
case binary:match(Bin, P) of
{0, _Length} ->
<<_:PSize/binary, Rest/binary>> = Bin,
@@ -163,19 +197,27 @@ parse_body(Bin, Pattern = {P, PSize}) when byte_size(Bin) >= PSize ->
FResult = end_of_part(Rest, Pattern),
{body, PBody, fun () -> FResult end};
nomatch ->
- PartialLength = byte_size(Bin) - PSize + 1,
- <<PBody:PartialLength/binary, Rest/binary>> = Bin,
- {body, PBody, fun () -> parse_body(Rest, Pattern) end}
+ case suffix_match(Bin, Pattern) of
+ nomatch ->
+ %% Prefix of boundary not found at end of input. it's
+ %% safe to return the whole binary. Saves copying of
+ %% next input onto tail of current input binary.
+ {body, Bin, fun () -> parse_body(<<>>, Pattern) end};
+ {BoundaryStart, Len} ->
+ PBody = binary:part(Bin, 0, BoundaryStart),
+ Rest = binary:part(Bin, BoundaryStart, Len),
+ {body, PBody, fun () -> parse_body(Rest, Pattern) end}
+ end
end;
parse_body(Bin, Pattern) ->
more(Bin, fun (NewBin) -> parse_body(NewBin, Pattern) end).
--spec end_of_part(binary(), pattern()) -> end_of_part().
+-spec end_of_part(binary(), patterns()) -> end_of_part().
end_of_part(Bin, Pattern) ->
{end_of_part, fun () -> parse_boundary_tail(Bin, Pattern) end}.
--spec skip(binary(), pattern()) -> more(part_result()).
-skip(Bin, Pattern = {P, PSize}) ->
+-spec skip(binary(), patterns()) -> more(part_result()).
+skip(Bin, Pattern = {{P, PSize}, _}) ->
case binary:match(Bin, P) of
{BoundaryStart, _Length} ->
% Boundary found, proceed with parsing of the next part.
@@ -255,4 +297,20 @@ title(Bin) ->
),
iolist_to_binary(Title).
+suffix_test_() ->
+ [?_assertEqual(Part, suffix_match(Packet, pattern(Boundary))) ||
+ {Part, Packet, Boundary} <- [
+ {nomatch, <<>>, <<"ABC">>},
+ {{0, 1}, <<"\r">>, <<"ABC">>},
+ {{0, 2}, <<"\r\n">>, <<"ABC">>},
+ {{0, 4}, <<"\r\n--">>, <<"ABC">>},
+ {{0, 5}, <<"\r\n--A">>, <<"ABC">>},
+ {{0, 6}, <<"\r\n--AB">>, <<"ABC">>},
+ {{0, 7}, <<"\r\n--ABC">>, <<"ABC">>},
+ {nomatch, <<"\r\n--AB1">>, <<"ABC">>},
+ {{1, 1}, <<"1\r">>, <<"ABC">>},
+ {{2, 2}, <<"12\r\n">>, <<"ABC">>},
+ {{3, 4}, <<"123\r\n--">>, <<"ABC">>}
+ ]].
+
-endif.
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl
index c5ea561..7ab66a9 100644
--- a/src/cowboy_protocol.erl
+++ b/src/cowboy_protocol.erl
@@ -379,7 +379,9 @@ request(B, State=#state{transport=Transport}, M, P, Q, F, Version, Headers) ->
request(B, State, M, P, Q, F, Version, Headers,
<<>>, default_port(Transport:name()));
{_, RawHost} ->
- case parse_host(RawHost, <<>>) of
+ case catch parse_host(RawHost, <<>>) of
+ {'EXIT', _} ->
+ error_terminate(400, State);
{Host, undefined} ->
request(B, State, M, P, Q, F, Version, Headers,
Host, default_port(Transport:name()));
@@ -440,19 +442,19 @@ request(Buffer, State=#state{socket=Socket, transport=Transport,
Req = cowboy_req:new(Socket, Transport, Method, Path, Query, Fragment,
Version, Headers, Host, Port, Buffer, ReqKeepalive < MaxKeepalive,
OnResponse),
- onrequest(Req, State, Host, Path).
+ onrequest(Req, State, Host).
%% Call the global onrequest callback. The callback can send a reply,
%% in which case we consider the request handled and move on to the next
%% one. Note that since we haven't dispatched yet, we don't know the
%% handler, host_info, path_info or bindings yet.
--spec onrequest(cowboy_req:req(), #state{}, binary(), binary()) -> ok.
-onrequest(Req, State=#state{onrequest=undefined}, Host, Path) ->
- dispatch(Req, State, Host, Path);
-onrequest(Req, State=#state{onrequest=OnRequest}, Host, Path) ->
+-spec onrequest(cowboy_req:req(), #state{}, binary()) -> ok.
+onrequest(Req, State=#state{onrequest=undefined}, Host) ->
+ dispatch(Req, State, Host, cowboy_req:get(path, Req));
+onrequest(Req, State=#state{onrequest=OnRequest}, Host) ->
Req2 = OnRequest(Req),
case cowboy_req:get(resp_state, Req2) of
- waiting -> dispatch(Req2, State, Host, Path);
+ waiting -> dispatch(Req2, State, Host, cowboy_req:get(path, Req2));
_ -> next_request(Req2, State, ok)
end.
@@ -464,6 +466,8 @@ dispatch(Req, State=#state{dispatch=Dispatch}, Host, Path) ->
handler_init(Req2, State, Handler, Opts);
{error, notfound, host} ->
error_terminate(400, State);
+ {error, badrequest, path} ->
+ error_terminate(400, State);
{error, notfound, path} ->
error_terminate(404, State)
end.
@@ -489,16 +493,18 @@ handler_init(Req, State=#state{transport=Transport}, Handler, Opts) ->
handler_terminate(Req2, Handler, HandlerState);
%% @todo {upgrade, transport, Module}
{upgrade, protocol, Module} ->
- upgrade_protocol(Req, State, Handler, Opts, Module)
+ upgrade_protocol(Req, State, Handler, Opts, Module);
+ {upgrade, protocol, Module, Req2, Opts2} ->
+ upgrade_protocol(Req2, State, Handler, Opts2, Module)
catch Class:Reason ->
error_terminate(500, State),
error_logger:error_msg(
- "** Handler ~p terminating in init/3~n"
+ "** Cowboy handler ~p terminating in ~p/~p~n"
" for the reason ~p:~p~n"
"** Options were ~p~n"
"** Request was ~p~n"
"** Stacktrace: ~p~n~n",
- [Handler, Class, Reason, Opts,
+ [Handler, init, 3, Class, Reason, Opts,
cowboy_req:to_list(Req), erlang:get_stacktrace()])
end.
@@ -518,12 +524,12 @@ handler_handle(Req, State, Handler, HandlerState) ->
terminate_request(Req2, State, Handler, HandlerState2)
catch Class:Reason ->
error_logger:error_msg(
- "** Handler ~p terminating in handle/2~n"
+ "** Cowboy handler ~p terminating in ~p/~p~n"
" for the reason ~p:~p~n"
"** Handler state was ~p~n"
"** Request was ~p~n"
"** Stacktrace: ~p~n~n",
- [Handler, Class, Reason, HandlerState,
+ [Handler, handle, 2, Class, Reason, HandlerState,
cowboy_req:to_list(Req), erlang:get_stacktrace()]),
handler_terminate(Req, Handler, HandlerState),
error_terminate(500, State)
@@ -576,12 +582,12 @@ handler_call(Req, State, Handler, HandlerState, Message) ->
Handler, HandlerState2)
catch Class:Reason ->
error_logger:error_msg(
- "** Handler ~p terminating in info/3~n"
+ "** Cowboy handler ~p terminating in ~p/~p~n"
" for the reason ~p:~p~n"
"** Handler state was ~p~n"
"** Request was ~p~n"
"** Stacktrace: ~p~n~n",
- [Handler, Class, Reason, HandlerState,
+ [Handler, info, 3, Class, Reason, HandlerState,
cowboy_req:to_list(Req), erlang:get_stacktrace()]),
handler_terminate(Req, Handler, HandlerState),
error_terminate(500, State)
@@ -593,12 +599,12 @@ handler_terminate(Req, Handler, HandlerState) ->
Handler:terminate(cowboy_req:lock(Req), HandlerState)
catch Class:Reason ->
error_logger:error_msg(
- "** Handler ~p terminating in terminate/2~n"
+ "** Cowboy handler ~p terminating in ~p/~p~n"
" for the reason ~p:~p~n"
"** Handler state was ~p~n"
"** Request was ~p~n"
"** Stacktrace: ~p~n~n",
- [Handler, Class, Reason, HandlerState,
+ [Handler, terminate, 2, Class, Reason, HandlerState,
cowboy_req:to_list(Req), erlang:get_stacktrace()])
end.
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 33aaa33..7f3b566 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -118,6 +118,12 @@
-include_lib("eunit/include/eunit.hrl").
-endif.
+-type cookie_option() :: {max_age, non_neg_integer()}
+ | {domain, binary()} | {path, binary()}
+ | {secure, boolean()} | {http_only, boolean()}.
+-type cookie_opts() :: [cookie_option()].
+-export_type([cookie_opts/0]).
+
-type resp_body_fun() :: fun(() -> {sent, non_neg_integer()}).
-record(http_req, {
@@ -183,15 +189,13 @@ new(Socket, Transport, Method, Path, Query, Fragment,
method=Method, path=Path, qs=Query, fragment=Fragment, version=Version,
headers=Headers, host=Host, port=Port, buffer=Buffer,
onresponse=OnResponse},
- case CanKeepalive of
+ case CanKeepalive and (Version =:= {1, 1}) of
false ->
Req#http_req{connection=close};
true ->
case lists:keyfind(<<"connection">>, 1, Headers) of
- false when Version =:= {1, 1} ->
- Req; %% keepalive
false ->
- Req#http_req{connection=close};
+ Req; %% keepalive
{_, ConnectionHeader} ->
Tokens = parse_connection_before(ConnectionHeader, []),
Connection = connection_to_atom(Tokens),
@@ -335,11 +339,11 @@ host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
-spec url(Req) -> {undefined | binary(), Req} when Req::req().
url(Req=#http_req{}) ->
{HostURL, Req2} = host_url(Req),
- url2(HostURL, Req2).
+ url(HostURL, Req2).
-url2(undefined, Req=#http_req{}) ->
+url(undefined, Req=#http_req{}) ->
{undefined, Req};
-url2(HostURL, Req=#http_req{path=Path, qs=QS, fragment=Fragment}) ->
+url(HostURL, Req=#http_req{path=Path, qs=QS, fragment=Fragment}) ->
QS2 = case QS of
<<>> -> <<>>;
_ -> << "?", QS/binary >>
@@ -439,6 +443,8 @@ parse_header(Name, Req, Default) when Name =:= <<"content-length">> ->
parse_header(Name, Req, Default, fun cowboy_http:digits/1);
parse_header(Name, Req, Default) when Name =:= <<"content-type">> ->
parse_header(Name, Req, Default, fun cowboy_http:content_type/1);
+parse_header(Name = <<"cookie">>, Req, Default) ->
+ parse_header(Name, Req, Default, fun cowboy_http:cookie_list/1);
parse_header(Name, Req, Default) when Name =:= <<"expect">> ->
parse_header(Name, Req, Default,
fun (Value) ->
@@ -490,11 +496,10 @@ cookie(Name, Req) when is_binary(Name) ->
-spec cookie(binary(), Req, Default)
-> {binary() | true | Default, Req} when Req::req(), Default::any().
cookie(Name, Req=#http_req{cookies=undefined}, Default) when is_binary(Name) ->
- case header(<<"cookie">>, Req) of
- {undefined, Req2} ->
+ case parse_header(<<"cookie">>, Req) of
+ {ok, undefined, Req2} ->
{Default, Req2#http_req{cookies=[]}};
- {RawCookie, Req2} ->
- Cookies = cowboy_cookies:parse_cookie(RawCookie),
+ {ok, Cookies, Req2} ->
cookie(Name, Req2#http_req{cookies=Cookies}, Default)
end;
cookie(Name, Req, Default) ->
@@ -506,11 +511,10 @@ cookie(Name, Req, Default) ->
%% @doc Return the full list of cookie values.
-spec cookies(Req) -> {list({binary(), binary() | true}), Req} when Req::req().
cookies(Req=#http_req{cookies=undefined}) ->
- case header(<<"cookie">>, Req) of
- {undefined, Req2} ->
+ case parse_header(<<"cookie">>, Req) of
+ {ok, undefined, Req2} ->
{[], Req2#http_req{cookies=[]}};
- {RawCookie, Req2} ->
- Cookies = cowboy_cookies:parse_cookie(RawCookie),
+ {ok, Cookies, Req2} ->
cookies(Req2#http_req{cookies=Cookies})
end;
cookies(Req=#http_req{cookies=Cookies}) ->
@@ -540,7 +544,7 @@ meta(Name, Req, Default) ->
%% If the value already exists it will be overwritten.
-spec set_meta(atom(), any(), Req) -> Req when Req::req().
set_meta(Name, Value, Req=#http_req{meta=Meta}) ->
- Req#http_req{meta=[{Name, Value}|lists:keydelete(Name, 1, Meta)]}.
+ Req#http_req{meta=lists:keyreplace(Name, 1, Meta, {Name, Value})}.
%% Request Body API.
@@ -803,11 +807,17 @@ multipart_skip(Req) ->
%% Response API.
%% @doc Add a cookie header to the response.
--spec set_resp_cookie(binary(), binary(),
- [cowboy_cookies:cookie_option()], Req) -> Req when Req::req().
-set_resp_cookie(Name, Value, Options, Req) ->
- {HeaderName, HeaderValue} = cowboy_cookies:cookie(Name, Value, Options),
- set_resp_header(HeaderName, HeaderValue, Req).
+%%
+%% The cookie name cannot contain any of the following characters:
+%% =,;\s\t\r\n\013\014
+%%
+%% The cookie value cannot contain any of the following characters:
+%% ,; \t\r\n\013\014
+-spec set_resp_cookie(iodata(), iodata(), cookie_opts(), Req)
+ -> Req when Req::req().
+set_resp_cookie(Name, Value, Opts, Req) ->
+ Cookie = cowboy_http:cookie_to_iodata(Name, Value, Opts),
+ set_resp_header(<<"set-cookie">>, Cookie, Req).
%% @doc Add a header to the response.
-spec set_resp_header(binary(), iodata(), Req)
@@ -1145,8 +1155,18 @@ response_merge_headers(Headers, RespHeaders, DefaultHeaders) ->
-spec merge_headers(cowboy_http:headers(), cowboy_http:headers())
-> cowboy_http:headers().
+
+%% Merge headers by prepending the tuples in the second list to the
+%% first list. It also handles Set-Cookie properly, which supports
+%% duplicated entries. Notice that, while the RFC2109 does allow more
+%% than one cookie to be set per Set-Cookie header, we are following
+%% the implementation of common web servers and applications which
+%% return many distinct headers per each Set-Cookie entry to avoid
+%% issues with clients/browser which may not support it.
merge_headers(Headers, []) ->
Headers;
+merge_headers(Headers, [{<<"set-cookie">>, Value}|Tail]) ->
+ merge_headers([{<<"set-cookie">>, Value}|Headers], Tail);
merge_headers(Headers, [{Name, Value}|Tail]) ->
Headers2 = case lists:keymember(Name, 1, Headers) of
true -> Headers;
@@ -1343,4 +1363,26 @@ connection_to_atom_test_() ->
[{lists:flatten(io_lib:format("~p", [T])),
fun() -> R = connection_to_atom(T) end} || {T, R} <- Tests].
+merge_headers_test() ->
+ Left0 = [{<<"content-length">>,<<"13">>},{<<"server">>,<<"Cowboy">>}],
+ Right0 = [{<<"set-cookie">>,<<"foo=bar">>},{<<"content-length">>,<<"11">>}],
+
+ ?assertMatch(
+ [{<<"set-cookie">>,<<"foo=bar">>},
+ {<<"content-length">>,<<"13">>},
+ {<<"server">>,<<"Cowboy">>}],
+ merge_headers(Left0, Right0)),
+
+ Left1 = [{<<"content-length">>,<<"13">>},{<<"server">>,<<"Cowboy">>}],
+ Right1 = [{<<"set-cookie">>,<<"foo=bar">>},{<<"set-cookie">>,<<"bar=baz">>}],
+
+ ?assertMatch(
+ [{<<"set-cookie">>,<<"bar=baz">>},
+ {<<"set-cookie">>,<<"foo=bar">>},
+ {<<"content-length">>,<<"13">>},
+ {<<"server">>,<<"Cowboy">>}],
+ merge_headers(Left1, Right1)),
+
+ ok.
+
-endif.
diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl
index 1c0554a..c6b53bd 100644
--- a/src/cowboy_rest.erl
+++ b/src/cowboy_rest.erl
@@ -40,7 +40,7 @@
language_a :: undefined | binary(),
%% Charset.
- charsets_p = [] :: [{binary(), atom()}],
+ charsets_p = [] :: [{binary(), integer()}],
charset_a :: undefined | binary(),
%% Cached resource calls.
@@ -73,10 +73,10 @@ upgrade(_ListenerPid, Handler, Opts, Req) ->
catch Class:Reason ->
PLReq = cowboy_req:to_list(Req),
error_logger:error_msg(
- "** Handler ~p terminating in rest_init/2~n"
+ "** Cowboy handler ~p terminating in ~p/~p~n"
" for the reason ~p:~p~n** Options were ~p~n"
"** Request was ~p~n** Stacktrace: ~p~n~n",
- [Handler, Class, Reason, Opts, PLReq, erlang:get_stacktrace()]),
+ [Handler, rest_init, 2, Class, Reason, Opts, PLReq, erlang:get_stacktrace()]),
{ok, _Req2} = cowboy_req:reply(500, Req),
close
end.
@@ -195,8 +195,9 @@ options(Req, State) ->
%%
%% Note that it is also possible to return a binary content type that will
%% then be parsed by Cowboy. However note that while this may make your
-%% resources a little more readable, this is a lot less efficient. An example
-%% of such a return value would be:
+%% resources a little more readable, this is a lot less efficient.
+%%
+%% An example of such return value would be:
%% {<<"text/html">>, to_html}
content_types_provided(Req, State) ->
case call(Req, State, content_types_provided) of
@@ -210,14 +211,15 @@ content_types_provided(Req, State) ->
CTP2 = [normalize_content_types(P) || P <- CTP],
State2 = State#state{
handler_state=HandlerState, content_types_p=CTP2},
- {ok, Accept, Req3} = cowboy_req:parse_header(<<"accept">>, Req2),
- case Accept of
- undefined ->
+ case cowboy_req:parse_header(<<"accept">>, Req2) of
+ {error, badarg} ->
+ respond(Req2, State2, 400);
+ {ok, undefined, Req3} ->
{PMT, _Fun} = HeadCTP = hd(CTP2),
languages_provided(
cowboy_req:set_meta(media_type, PMT, Req3),
State2#state{content_type_a=HeadCTP});
- Accept ->
+ {ok, Accept, Req3} ->
Accept2 = prioritize_accept(Accept),
choose_media_type(Req3, State2, Accept2)
end
@@ -725,9 +727,19 @@ put_resource(Req, State, OnTrue) ->
%% list of content types, otherwise it'll shadow the ones following.
choose_content_type(Req, State, _OnTrue, _ContentType, []) ->
respond(Req, State, 415);
-choose_content_type(Req, State, OnTrue, ContentType, [{Accepted, Fun}|_Tail])
+choose_content_type(Req,
+ State=#state{handler=Handler, handler_state=HandlerState},
+ OnTrue, ContentType, [{Accepted, Fun}|_Tail])
when Accepted =:= '*' orelse Accepted =:= ContentType ->
case call(Req, State, Fun) of
+ no_call ->
+ error_logger:error_msg(
+ "** Cowboy handler ~p terminating; "
+ "function ~p/~p was not exported~n"
+ "** Request was ~p~n** State was ~p~n~n",
+ [Handler, Fun, 2, cowboy_req:to_list(Req), HandlerState]),
+ {ok, _} = cowboy_req:reply(500, Req),
+ close;
{halt, Req2, HandlerState} ->
terminate(Req2, State#state{handler_state=HandlerState});
{true, Req2, HandlerState} ->
@@ -758,19 +770,28 @@ has_resp_body(Req, State) ->
%% Set the response headers and call the callback found using
%% content_types_provided/2 to obtain the request body and add
%% it to the response.
-set_resp_body(Req, State=#state{content_type_a={_Type, Fun}}) ->
+set_resp_body(Req, State=#state{handler=Handler, handler_state=HandlerState,
+ content_type_a={_Type, Fun}}) ->
{Req2, State2} = set_resp_etag(Req, State),
{LastModified, Req3, State3} = last_modified(Req2, State2),
- case LastModified of
+ Req4 = case LastModified of
LastModified when is_atom(LastModified) ->
- Req4 = Req3;
+ Req3;
LastModified ->
- LastModifiedStr = httpd_util:rfc1123_date(LastModified),
- Req4 = cowboy_req:set_resp_header(
- <<"last-modified">>, LastModifiedStr, Req3)
+ LastModifiedBin = cowboy_clock:rfc1123(LastModified),
+ cowboy_req:set_resp_header(
+ <<"last-modified">>, LastModifiedBin, Req3)
end,
{Req5, State4} = set_resp_expires(Req4, State3),
case call(Req5, State4, Fun) of
+ no_call ->
+ error_logger:error_msg(
+ "** Cowboy handler ~p terminating; "
+ "function ~p/~p was not exported~n"
+ "** Request was ~p~n** State was ~p~n~n",
+ [Handler, Fun, 2, cowboy_req:to_list(Req5), HandlerState]),
+ {ok, _} = cowboy_req:reply(500, Req5),
+ close;
{halt, Req6, HandlerState} ->
terminate(Req6, State4#state{handler_state=HandlerState});
{Body, Req6, HandlerState} ->
@@ -810,9 +831,9 @@ set_resp_expires(Req, State) ->
Expires when is_atom(Expires) ->
{Req2, State2};
Expires ->
- ExpiresStr = httpd_util:rfc1123_date(Expires),
+ ExpiresBin = cowboy_clock:rfc1123(Expires),
Req3 = cowboy_req:set_resp_header(
- <<"expires">>, ExpiresStr, Req2),
+ <<"expires">>, ExpiresBin, Req2),
{Req3, State2}
end.
diff --git a/src/cowboy_static.erl b/src/cowboy_static.erl
index 724bf33..55d01c7 100644
--- a/src/cowboy_static.erl
+++ b/src/cowboy_static.erl
@@ -158,7 +158,7 @@
%% {file, <<"index.html">>}]}
%%
%% %% Serve cowboy/priv/www/page.html under http://example.com/*/page
-%% {['*', <<"page">>], cowboy_static,
+%% {['_', <<"page">>], cowboy_static,
%% [{directory, {priv_dir, cowboy, [<<"www">>]}}
%% {file, <<"page.html">>}]}.
%%
@@ -217,6 +217,7 @@ rest_init(Req, Opts) ->
Directory1 = directory_path(Directory),
Mimetypes = proplists:get_value(mimetypes, Opts, []),
Mimetypes1 = case Mimetypes of
+ {{M, F}, E} -> {fun M:F/2, E};
{_, _} -> Mimetypes;
[] -> {fun path_to_mimetypes/2, []};
[_|_] -> {fun path_to_mimetypes/2, Mimetypes}
@@ -321,58 +322,10 @@ content_types_provided(Req, #state{filepath=Filepath,
file_contents(Req, #state{filepath=Filepath,
fileinfo={ok, #file_info{size=Filesize}}}=State) ->
{ok, Transport, Socket} = cowboy_req:transport(Req),
- Writefile = content_function(Transport, Socket, Filepath),
+ Writefile = fun() -> Transport:sendfile(Socket, Filepath) end,
{{stream, Filesize, Writefile}, Req, State}.
-%% @private Return a function writing the contents of a file to a socket.
-%% The function returns the number of bytes written to the socket to enable
-%% the calling function to determine if the expected number of bytes were
-%% written to the socket.
--spec content_function(module(), inet:socket(), binary()) ->
- fun(() -> {sent, non_neg_integer()}).
-content_function(Transport, Socket, Filepath) ->
- %% `file:sendfile/2' will only work with the `ranch_tcp'
- %% transport module. SSL or future SPDY transports that require the
- %% content to be encrypted or framed as the content is sent
- %% will use the fallback mechanism.
- case erlang:function_exported(file, sendfile, 2) of
- false ->
- fun() -> sfallback(Transport, Socket, Filepath) end;
- _ when Transport =/= ranch_tcp ->
- fun() -> sfallback(Transport, Socket, Filepath) end;
- true ->
- fun() -> sendfile(Socket, Filepath) end
- end.
-
-
-%% @private Sendfile fallback function.
--spec sfallback(module(), inet:socket(), binary()) -> {sent, non_neg_integer()}.
-sfallback(Transport, Socket, Filepath) ->
- {ok, File} = file:open(Filepath, [read,binary,raw]),
- sfallback(Transport, Socket, File, 0).
-
--spec sfallback(module(), inet:socket(), file:io_device(),
- non_neg_integer()) -> {sent, non_neg_integer()}.
-sfallback(Transport, Socket, File, Sent) ->
- case file:read(File, 16#1FFF) of
- eof ->
- ok = file:close(File),
- {sent, Sent};
- {ok, Bin} ->
- case Transport:send(Socket, Bin) of
- ok -> sfallback(Transport, Socket, File, Sent + byte_size(Bin));
- {error, closed} -> {sent, Sent}
- end
- end.
-
-
-%% @private Wrapper for sendfile function.
--spec sendfile(inet:socket(), binary()) -> {sent, non_neg_integer()}.
-sendfile(Socket, Filepath) ->
- {ok, Sent} = file:sendfile(Filepath, Socket),
- {sent, Sent}.
-
-spec directory_path(dirspec()) -> dirpath().
directory_path({priv_dir, App, []}) ->
priv_dir_path(App);
diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl
index 1c6d20c..8c02ac7 100644
--- a/src/cowboy_websocket.erl
+++ b/src/cowboy_websocket.erl
@@ -21,6 +21,11 @@
%% Internal.
-export([handler_loop/4]).
+-type frame() :: close | ping | pong
+ | {text | binary | close | ping | pong, binary()}
+ | {close, 1000..4999, binary()}.
+-export_type([frame/0]).
+
-type opcode() :: 0 | 1 | 2 | 8 | 9 | 10.
-type mask_key() :: 0..16#ffffffff.
@@ -125,12 +130,12 @@ handler_init(State=#state{transport=Transport, handler=Handler, opts=Opts},
closed
catch Class:Reason ->
upgrade_error(Req),
- PLReq = cowboy_req:to_list(Req),
error_logger:error_msg(
- "** Handler ~p terminating in websocket_init/3~n"
+ "** Cowboy handler ~p terminating in ~p/~p~n"
" for the reason ~p:~p~n** Options were ~p~n"
"** Request was ~p~n** Stacktrace: ~p~n~n",
- [Handler, Class, Reason, Opts, PLReq, erlang:get_stacktrace()])
+ [Handler, websocket_init, 3, Class, Reason, Opts,
+ cowboy_req:to_list(Req),erlang:get_stacktrace()])
end.
-spec upgrade_error(cowboy_req:req()) -> closed.
@@ -149,9 +154,9 @@ websocket_handshake(State=#state{socket=Socket, transport=Transport,
{<< "http", Location/binary >>, Req1} = cowboy_req:url(Req),
{ok, Req2} = cowboy_req:upgrade_reply(
<<"101 WebSocket Protocol Handshake">>,
- [{<<"Upgrade">>, <<"WebSocket">>},
- {<<"Sec-Websocket-Location">>, << "ws", Location/binary >>},
- {<<"Sec-Websocket-Origin">>, Origin}],
+ [{<<"upgrade">>, <<"WebSocket">>},
+ {<<"sec-websocket-location">>, << "ws", Location/binary >>},
+ {<<"sec-websocket-origin">>, Origin}],
Req1),
%% Flush the resp_sent message before moving on.
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
@@ -171,18 +176,20 @@ websocket_handshake(State=#state{socket=Socket, transport=Transport,
handler_before_loop(State#state{messages=Transport:messages()},
Req4, HandlerState, <<>>);
_Any ->
- closed %% If an error happened reading the body, stop there.
+ %% If an error happened reading the body, stop there.
+ handler_terminate(State, Req3, HandlerState, {error, closed})
end;
websocket_handshake(State=#state{transport=Transport, challenge=Challenge},
Req, HandlerState) ->
{ok, Req2} = cowboy_req:upgrade_reply(
101,
- [{<<"Upgrade">>, <<"websocket">>},
- {<<"Sec-Websocket-Accept">>, Challenge}],
+ [{<<"upgrade">>, <<"websocket">>},
+ {<<"sec-websocket-accept">>, Challenge}],
Req),
%% Flush the resp_sent message before moving on.
receive {cowboy_req, resp_sent} -> ok after 0 -> ok end,
- handler_before_loop(State#state{messages=Transport:messages()},
+ State2 = handler_loop_timeout(State),
+ handler_before_loop(State2#state{messages=Transport:messages()},
Req2, HandlerState, <<>>).
-spec handler_before_loop(#state{}, cowboy_req:req(), any(), binary()) -> closed.
@@ -190,15 +197,13 @@ handler_before_loop(State=#state{
socket=Socket, transport=Transport, hibernate=true},
Req, HandlerState, SoFar) ->
Transport:setopts(Socket, [{active, once}]),
- State2 = handler_loop_timeout(State),
catch erlang:hibernate(?MODULE, handler_loop,
- [State2#state{hibernate=false}, Req, HandlerState, SoFar]),
+ [State#state{hibernate=false}, Req, HandlerState, SoFar]),
closed;
handler_before_loop(State=#state{socket=Socket, transport=Transport},
Req, HandlerState, SoFar) ->
Transport:setopts(Socket, [{active, once}]),
- State2 = handler_loop_timeout(State),
- handler_loop(State2, Req, HandlerState, SoFar).
+ handler_loop(State, Req, HandlerState, SoFar).
-spec handler_loop_timeout(#state{}) -> #state{}.
handler_loop_timeout(State=#state{timeout=infinity}) ->
@@ -216,7 +221,8 @@ handler_loop(State=#state{
Req, HandlerState, SoFar) ->
receive
{OK, Socket, Data} ->
- websocket_data(State, Req, HandlerState,
+ State2 = handler_loop_timeout(State),
+ websocket_data(State2, Req, HandlerState,
<< SoFar/binary, Data/binary >>);
{Closed, Socket} ->
handler_terminate(State, Req, HandlerState, {error, closed});
@@ -452,38 +458,76 @@ handler_call(State=#state{handler=Handler, opts=Opts}, Req, HandlerState,
Req2, HandlerState2, RemainingData);
{reply, Payload, Req2, HandlerState2}
when is_tuple(Payload) ->
- ok = websocket_send(Payload, State),
- NextState(State, Req2, HandlerState2, RemainingData);
+ case websocket_send(Payload, State) of
+ ok ->
+ State2 = handler_loop_timeout(State),
+ NextState(State2, Req2, HandlerState2, RemainingData);
+ shutdown ->
+ handler_terminate(State, Req2, HandlerState,
+ {normal, shutdown});
+ {error, _} = Error ->
+ handler_terminate(State, Req2, HandlerState2, Error)
+ end;
{reply, Payload, Req2, HandlerState2, hibernate}
when is_tuple(Payload) ->
- ok = websocket_send(Payload, State),
- NextState(State#state{hibernate=true},
- Req2, HandlerState2, RemainingData);
+ case websocket_send(Payload, State) of
+ ok ->
+ State2 = handler_loop_timeout(State),
+ NextState(State2#state{hibernate=true},
+ Req2, HandlerState2, RemainingData);
+ shutdown ->
+ handler_terminate(State, Req2, HandlerState,
+ {normal, shutdown});
+ {error, _} = Error ->
+ handler_terminate(State, Req2, HandlerState2, Error)
+ end;
{reply, Payload, Req2, HandlerState2}
when is_list(Payload) ->
- ok = websocket_send_many(Payload, State),
- NextState(State, Req2, HandlerState2, RemainingData);
+ case websocket_send_many(Payload, State) of
+ ok ->
+ State2 = handler_loop_timeout(State),
+ NextState(State2, Req2, HandlerState2, RemainingData);
+ shutdown ->
+ handler_terminate(State, Req2, HandlerState,
+ {normal, shutdown});
+ {error, _} = Error ->
+ handler_terminate(State, Req2, HandlerState2, Error)
+ end;
{reply, Payload, Req2, HandlerState2, hibernate}
when is_list(Payload) ->
- ok = websocket_send_many(Payload, State),
- NextState(State#state{hibernate=true},
- Req2, HandlerState2, RemainingData);
+ case websocket_send_many(Payload, State) of
+ ok ->
+ State2 = handler_loop_timeout(State),
+ NextState(State2#state{hibernate=true},
+ Req2, HandlerState2, RemainingData);
+ shutdown ->
+ handler_terminate(State, Req2, HandlerState,
+ {normal, shutdown});
+ {error, _} = Error ->
+ handler_terminate(State, Req2, HandlerState2, Error)
+ end;
{shutdown, Req2, HandlerState2} ->
websocket_close(State, Req2, HandlerState2, {normal, shutdown})
catch Class:Reason ->
PLReq = cowboy_req:to_list(Req),
error_logger:error_msg(
- "** Handler ~p terminating in ~p/3~n"
+ "** Cowboy handler ~p terminating in ~p/~p~n"
" for the reason ~p:~p~n** Message was ~p~n"
"** Options were ~p~n** Handler state was ~p~n"
"** Request was ~p~n** Stacktrace: ~p~n~n",
- [Handler, Callback, Class, Reason, Message, Opts,
+ [Handler, Callback, 3, Class, Reason, Message, Opts,
HandlerState, PLReq, erlang:get_stacktrace()]),
websocket_close(State, Req, HandlerState, {error, handler})
end.
--spec websocket_send({text | binary | ping | pong, binary()}, #state{})
- -> ok | {error, atom()}.
+websocket_opcode(text) -> 1;
+websocket_opcode(binary) -> 2;
+websocket_opcode(close) -> 8;
+websocket_opcode(ping) -> 9;
+websocket_opcode(pong) -> 10.
+
+-spec websocket_send(frame(), #state{})
+ -> ok | shutdown | {error, atom()}.
%% hixie-76 text frame.
websocket_send({text, Payload}, #state{
socket=Socket, transport=Transport, version=0}) ->
@@ -491,24 +535,52 @@ websocket_send({text, Payload}, #state{
%% Ignore all unknown frame types for compatibility with hixie 76.
websocket_send(_Any, #state{version=0}) ->
ok;
+websocket_send(Type, #state{socket=Socket, transport=Transport})
+ when Type =:= close ->
+ Opcode = websocket_opcode(Type),
+ case Transport:send(Socket, << 1:1, 0:3, Opcode:4, 0:8 >>) of
+ ok -> shutdown;
+ Error -> Error
+ end;
+websocket_send(Type, #state{socket=Socket, transport=Transport})
+ when Type =:= ping; Type =:= pong ->
+ Opcode = websocket_opcode(Type),
+ Transport:send(Socket, << 1:1, 0:3, Opcode:4, 0:8 >>);
+websocket_send({close, Payload}, State) ->
+ websocket_send({close, 1000, Payload}, State);
+websocket_send({Type = close, StatusCode, Payload}, #state{
+ socket=Socket, transport=Transport}) ->
+ Opcode = websocket_opcode(Type),
+ Len = 2 + iolist_size(Payload),
+ %% Control packets must not be > 125 in length.
+ true = Len =< 125,
+ BinLen = hybi_payload_length(Len),
+ Transport:send(Socket,
+ [<< 1:1, 0:3, Opcode:4, 0:1, BinLen/bits, StatusCode:16 >>, Payload]),
+ shutdown;
websocket_send({Type, Payload}, #state{socket=Socket, transport=Transport}) ->
- Opcode = case Type of
- text -> 1;
- binary -> 2;
- ping -> 9;
- pong -> 10
+ Opcode = websocket_opcode(Type),
+ Len = iolist_size(Payload),
+ %% Control packets must not be > 125 in length.
+ true = if Type =:= ping; Type =:= pong ->
+ Len =< 125;
+ true ->
+ true
end,
- Len = hybi_payload_length(iolist_size(Payload)),
- Transport:send(Socket, [<< 1:1, 0:3, Opcode:4, 0:1, Len/bits >>,
- Payload]).
+ BinLen = hybi_payload_length(Len),
+ Transport:send(Socket,
+ [<< 1:1, 0:3, Opcode:4, 0:1, BinLen/bits >>, Payload]).
--spec websocket_send_many([{text | binary | ping | pong, binary()}], #state{})
- -> ok | {error, atom()}.
+-spec websocket_send_many([frame()], #state{})
+ -> ok | shutdown | {error, atom()}.
websocket_send_many([], _) ->
ok;
websocket_send_many([Frame|Tail], State) ->
- ok = websocket_send(Frame, State),
- websocket_send_many(Tail, State).
+ case websocket_send(Frame, State) of
+ ok -> websocket_send_many(Tail, State);
+ shutdown -> shutdown;
+ Error -> Error
+ end.
-spec websocket_close(#state{}, cowboy_req:req(), any(), {atom(), atom()})
-> closed.
@@ -516,7 +588,6 @@ websocket_close(State=#state{socket=Socket, transport=Transport, version=0},
Req, HandlerState, Reason) ->
Transport:send(Socket, << 255, 0 >>),
handler_terminate(State, Req, HandlerState, Reason);
-%% @todo Send a Payload? Using Reason is usually good but we're quite careless.
websocket_close(State=#state{socket=Socket, transport=Transport},
Req, HandlerState, Reason) ->
Transport:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>),
@@ -531,11 +602,11 @@ handler_terminate(#state{handler=Handler, opts=Opts},
catch Class:Reason ->
PLReq = cowboy_req:to_list(Req),
error_logger:error_msg(
- "** Handler ~p terminating in websocket_terminate/3~n"
+ "** Cowboy handler ~p terminating in ~p/~p~n"
" for the reason ~p:~p~n** Initial reason was ~p~n"
"** Options were ~p~n** Handler state was ~p~n"
"** Request was ~p~n** Stacktrace: ~p~n~n",
- [Handler, Class, Reason, TerminateReason, Opts,
+ [Handler, websocket_terminate, 3, Class, Reason, TerminateReason, Opts,
HandlerState, PLReq, erlang:get_stacktrace()])
end,
closed.
diff --git a/src/cowboy_websocket_handler.erl b/src/cowboy_websocket_handler.erl
index 34749ba..6d7f9de 100644
--- a/src/cowboy_websocket_handler.erl
+++ b/src/cowboy_websocket_handler.erl
@@ -66,15 +66,15 @@
-callback websocket_handle({text | binary | ping | pong, binary()}, Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
- | {reply, {text | binary | ping | pong, binary()}, Req, State}
- | {reply, {text | binary | ping | pong, binary()}, Req, State, hibernate}
+ | {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State}
+ | {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State, hibernate}
| {shutdown, Req, State}
when Req::cowboy_req:req(), State::state().
-callback websocket_info(any(), Req, State)
-> {ok, Req, State}
| {ok, Req, State, hibernate}
- | {reply, {text | binary | ping | pong, binary()}, Req, State}
- | {reply, {text | binary | ping | pong, binary()}, Req, State, hibernate}
+ | {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State}
+ | {reply, cowboy_websocket:frame() | [cowboy_websocket:frame()], Req, State, hibernate}
| {shutdown, Req, State}
when Req::cowboy_req:req(), State::state().
-callback websocket_terminate(terminate_reason(), cowboy_req:req(), state())
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 4820f89..c90e585 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -48,8 +48,12 @@
-export([onresponse_crash/1]).
-export([onresponse_reply/1]).
-export([pipeline/1]).
+-export([rest_bad_accept/1]).
+-export([rest_expires/1]).
-export([rest_keepalive/1]).
-export([rest_keepalive_post/1]).
+-export([rest_missing_get_callbacks/1]).
+-export([rest_missing_put_callbacks/1]).
-export([rest_nodelete/1]).
-export([rest_resource_etags/1]).
-export([rest_resource_etags_if_none_match/1]).
@@ -93,8 +97,12 @@ groups() ->
nc_rand,
nc_zero,
pipeline,
+ rest_bad_accept,
+ rest_expires,
rest_keepalive,
rest_keepalive_post,
+ rest_missing_get_callbacks,
+ rest_missing_put_callbacks,
rest_nodelete,
rest_resource_etags,
rest_resource_etags_if_none_match,
@@ -247,11 +255,15 @@ init_dispatch(Config) ->
{file, <<"test_file.css">>}]},
{[<<"multipart">>], http_handler_multipart, []},
{[<<"echo">>, <<"body">>], http_handler_echo_body, []},
+ {[<<"bad_accept">>], rest_simple_resource, []},
{[<<"simple">>], rest_simple_resource, []},
{[<<"forbidden_post">>], rest_forbidden_resource, [true]},
{[<<"simple_post">>], rest_forbidden_resource, [false]},
+ {[<<"missing_get_callbacks">>], rest_missing_callbacks, []},
+ {[<<"missing_put_callbacks">>], rest_missing_callbacks, []},
{[<<"nodelete">>], rest_nodelete_resource, []},
{[<<"resetags">>], rest_resource_etags, []},
+ {[<<"rest_expires">>], rest_expires, []},
{[<<"loop_timeout">>], http_handler_loop_timeout, []},
{[], http_handler, []}
]}
@@ -354,6 +366,7 @@ The document has moved
{400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
{400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
{400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
+ {400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
{505, ResponsePacket},
{408, "GET / HTTP/1.1\r\n"},
{408, "GET / HTTP/1.1\r\nHost: localhost"},
@@ -647,6 +660,24 @@ pipeline(Config) ->
{ok, 200, _, Client11} = cowboy_client:response(Client10),
{error, closed} = cowboy_client:response(Client11).
+rest_bad_accept(Config) ->
+ Client = ?config(client, Config),
+ {ok, Client2} = cowboy_client:request(<<"GET">>,
+ build_url("/bad_accept", Config),
+ [{<<"accept">>, <<"1">>}],
+ Client),
+ {ok, 400, _, _} = cowboy_client:response(Client2).
+
+rest_expires(Config) ->
+ Client = ?config(client, Config),
+ {ok, Client2} = cowboy_client:request(<<"GET">>,
+ build_url("/rest_expires", Config), Client),
+ {ok, 200, RespHeaders, _} = cowboy_client:response(Client2),
+ {_, Expires} = lists:keyfind(<<"expires">>, 1, RespHeaders),
+ {_, LastModified} = lists:keyfind(<<"last-modified">>, 1, RespHeaders),
+ Expires = LastModified = <<"Fri, 21 Sep 2012 22:36:14 GMT">>,
+ ok.
+
rest_keepalive(Config) ->
Client = ?config(client, Config),
URL = build_url("/simple", Config),
@@ -691,6 +722,20 @@ rest_keepalive_post_loop(Config, Client, forbidden_post, N) ->
= lists:keyfind(<<"connection">>, 1, RespHeaders),
rest_keepalive_post_loop(Config, Client3, simple_post, N - 1).
+rest_missing_get_callbacks(Config) ->
+ Client = ?config(client, Config),
+ {ok, Client2} = cowboy_client:request(<<"GET">>,
+ build_url("/missing_get_callbacks", Config), Client),
+ {ok, 500, _, _} = cowboy_client:response(Client2).
+
+rest_missing_put_callbacks(Config) ->
+ Client = ?config(client, Config),
+ {ok, Client2} = cowboy_client:request(<<"PUT">>,
+ build_url("/missing_put_callbacks", Config),
+ [{<<"content-type">>, <<"application/json">>}],
+ <<"{}">>, Client),
+ {ok, 500, _, _} = cowboy_client:response(Client2).
+
rest_nodelete(Config) ->
Client = ?config(client, Config),
{ok, Client2} = cowboy_client:request(<<"DELETE">>,
diff --git a/test/rest_expires.erl b/test/rest_expires.erl
new file mode 100644
index 0000000..4209041
--- /dev/null
+++ b/test/rest_expires.erl
@@ -0,0 +1,22 @@
+-module(rest_expires).
+
+-export([init/3]).
+-export([content_types_provided/2]).
+-export([get_text_plain/2]).
+-export([expires/2]).
+-export([last_modified/2]).
+
+init(_Transport, _Req, _Opts) ->
+ {upgrade, protocol, cowboy_rest}.
+
+content_types_provided(Req, State) ->
+ {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
+
+get_text_plain(Req, State) ->
+ {<<"This is REST!">>, Req, State}.
+
+expires(Req, State) ->
+ {{{2012, 9, 21}, {22, 36, 14}}, Req, State}.
+
+last_modified(Req, State) ->
+ {{{2012, 9, 21}, {22, 36, 14}}, Req, State}.
diff --git a/test/rest_missing_callbacks.erl b/test/rest_missing_callbacks.erl
new file mode 100644
index 0000000..171c856
--- /dev/null
+++ b/test/rest_missing_callbacks.erl
@@ -0,0 +1,21 @@
+-module(rest_missing_callbacks).
+-export([init/3]).
+-export([allowed_methods/2]).
+-export([content_types_accepted/2]).
+-export([content_types_provided/2]).
+
+init(_Transport, _Req, _Opts) ->
+ {upgrade, protocol, cowboy_rest}.
+
+allowed_methods(Req, State) ->
+ {[<<"GET">>, <<"PUT">>], Req, State}.
+
+content_types_accepted(Req, State) ->
+ {[
+ {<<"application/json">>, put_application_json}
+ ], Req, State}.
+
+content_types_provided(Req, State) ->
+ {[
+ {<<"text/plain">>, get_text_plain}
+ ], Req, State}.
diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl
index 9741329..34befda 100644
--- a/test/ws_SUITE.erl
+++ b/test/ws_SUITE.erl
@@ -30,9 +30,14 @@
-export([ws8_init_shutdown/1]).
-export([ws8_single_bytes/1]).
-export([ws13/1]).
+-export([ws_send_close/1]).
+-export([ws_send_close_payload/1]).
-export([ws_send_many/1]).
-export([ws_text_fragments/1]).
-export([ws_timeout_hibernate/1]).
+-export([ws_timeout_cancel/1]).
+-export([ws_timeout_reset/1]).
+-export([ws_upgrade_with_opts/1]).
%% ct.
@@ -46,9 +51,14 @@ groups() ->
ws8_init_shutdown,
ws8_single_bytes,
ws13,
+ ws_send_close,
+ ws_send_close_payload,
ws_send_many,
ws_text_fragments,
- ws_timeout_hibernate
+ ws_timeout_hibernate,
+ ws_timeout_cancel,
+ ws_timeout_reset,
+ ws_upgrade_with_opts
],
[{ws, [], BaseTests}].
@@ -85,8 +95,28 @@ init_dispatch() ->
{[<<"websocket">>], websocket_handler, []},
{[<<"ws_echo_handler">>], websocket_echo_handler, []},
{[<<"ws_init_shutdown">>], websocket_handler_init_shutdown, []},
- {[<<"ws_send_many">>], ws_send_many_handler, []},
- {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []}
+ {[<<"ws_send_many">>], ws_send_many_handler, [
+ {sequence, [
+ {text, <<"one">>},
+ {text, <<"two">>},
+ {text, <<"seven!">>}]}
+ ]},
+ {[<<"ws_send_close">>], ws_send_many_handler, [
+ {sequence, [
+ {text, <<"send">>},
+ close,
+ {text, <<"won't be received">>}]}
+ ]},
+ {[<<"ws_send_close_payload">>], ws_send_many_handler, [
+ {sequence, [
+ {text, <<"send">>},
+ {close, 1001, <<"some text!">>},
+ {text, <<"won't be received">>}]}
+ ]},
+ {[<<"ws_timeout_hibernate">>], ws_timeout_hibernate_handler, []},
+ {[<<"ws_timeout_cancel">>], ws_timeout_cancel_handler, []},
+ {[<<"ws_upgrade_with_opts">>], ws_upgrade_with_opts_handler,
+ <<"failure">>}
]}
].
@@ -310,6 +340,64 @@ ws13(Config) ->
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
+ws_send_close(Config) ->
+ {port, Port} = lists:keyfind(port, 1, Config),
+ {ok, Socket} = gen_tcp:connect("localhost", Port,
+ [binary, {active, false}, {packet, raw}]),
+ ok = gen_tcp:send(Socket, [
+ "GET /ws_send_close HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "Connection: Upgrade\r\n"
+ "Upgrade: websocket\r\n"
+ "Sec-WebSocket-Origin: http://localhost\r\n"
+ "Sec-WebSocket-Version: 8\r\n"
+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
+ "\r\n"]),
+ {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
+ {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
+ = erlang:decode_packet(http, Handshake, []),
+ [Headers, <<>>] = websocket_headers(
+ erlang:decode_packet(httph, Rest, []), []),
+ {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
+ {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
+ {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
+ = lists:keyfind("sec-websocket-accept", 1, Headers),
+ %% We catch all frames at once and check them directly.
+ {ok, Many} = gen_tcp:recv(Socket, 8, 6000),
+ << 1:1, 0:3, 1:4, 0:1, 4:7, "send",
+ 1:1, 0:3, 8:4, 0:8 >> = Many,
+ {error, closed} = gen_tcp:recv(Socket, 0, 6000),
+ ok.
+
+ws_send_close_payload(Config) ->
+ {port, Port} = lists:keyfind(port, 1, Config),
+ {ok, Socket} = gen_tcp:connect("localhost", Port,
+ [binary, {active, false}, {packet, raw}]),
+ ok = gen_tcp:send(Socket, [
+ "GET /ws_send_close_payload HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "Connection: Upgrade\r\n"
+ "Upgrade: websocket\r\n"
+ "Sec-WebSocket-Origin: http://localhost\r\n"
+ "Sec-WebSocket-Version: 8\r\n"
+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
+ "\r\n"]),
+ {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
+ {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
+ = erlang:decode_packet(http, Handshake, []),
+ [Headers, <<>>] = websocket_headers(
+ erlang:decode_packet(httph, Rest, []), []),
+ {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
+ {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
+ {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
+ = lists:keyfind("sec-websocket-accept", 1, Headers),
+ %% We catch all frames at once and check them directly.
+ {ok, Many} = gen_tcp:recv(Socket, 20, 6000),
+ << 1:1, 0:3, 1:4, 0:1, 4:7, "send",
+ 1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >> = Many,
+ {error, closed} = gen_tcp:recv(Socket, 0, 6000),
+ ok.
+
ws_send_many(Config) ->
{port, Port} = lists:keyfind(port, 1, Config),
{ok, Socket} = gen_tcp:connect("localhost", Port,
@@ -332,12 +420,11 @@ ws_send_many(Config) ->
{'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
{"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
= lists:keyfind("sec-websocket-accept", 1, Headers),
- {ok, << 1:1, 0:3, 1:4, 0:1, 3:7, "one" >>}
- = gen_tcp:recv(Socket, 0, 6000),
- {ok, << 1:1, 0:3, 1:4, 0:1, 3:7, "two" >>}
- = gen_tcp:recv(Socket, 0, 6000),
- {ok, << 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >>}
- = gen_tcp:recv(Socket, 0, 6000),
+ %% We catch all frames at once and check them directly.
+ {ok, Many} = gen_tcp:recv(Socket, 18, 6000),
+ << 1:1, 0:3, 1:4, 0:1, 3:7, "one",
+ 1:1, 0:3, 1:4, 0:1, 3:7, "two",
+ 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >> = Many,
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
@@ -424,6 +511,109 @@ ws_timeout_hibernate(Config) ->
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
+ws_timeout_cancel(Config) ->
+ %% Erlang messages to a socket should not cancel the timeout
+ {port, Port} = lists:keyfind(port, 1, Config),
+ {ok, Socket} = gen_tcp:connect("localhost", Port,
+ [binary, {active, false}, {packet, raw}]),
+ ok = gen_tcp:send(Socket, [
+ "GET /ws_timeout_cancel HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "Connection: Upgrade\r\n"
+ "Upgrade: websocket\r\n"
+ "Sec-WebSocket-Origin: http://localhost\r\n"
+ "Sec-WebSocket-Version: 8\r\n"
+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
+ "\r\n"]),
+ {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
+ {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
+ = erlang:decode_packet(http, Handshake, []),
+ [Headers, <<>>] = websocket_headers(
+ erlang:decode_packet(httph, Rest, []), []),
+ {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
+ {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
+ {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
+ = lists:keyfind("sec-websocket-accept", 1, Headers),
+ {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
+ {error, closed} = gen_tcp:recv(Socket, 0, 6000),
+ ok.
+
+ws_timeout_reset(Config) ->
+ %% Erlang messages across a socket should reset the timeout
+ {port, Port} = lists:keyfind(port, 1, Config),
+ {ok, Socket} = gen_tcp:connect("localhost", Port,
+ [binary, {active, false}, {packet, raw}]),
+ ok = gen_tcp:send(Socket, [
+ "GET /ws_timeout_cancel HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "Connection: Upgrade\r\n"
+ "Upgrade: WebSocket\r\n"
+ "Origin: http://localhost\r\n"
+ "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
+ "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
+ "\r\n"]),
+ {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
+ {ok, {http_response, {1, 1}, 101, "WebSocket Protocol Handshake"}, Rest}
+ = erlang:decode_packet(http, Handshake, []),
+ [Headers, <<>>] = websocket_headers(
+ erlang:decode_packet(httph, Rest, []), []),
+ {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
+ {'Upgrade', "WebSocket"} = lists:keyfind('Upgrade', 1, Headers),
+ {"sec-websocket-location", "ws://localhost/ws_timeout_cancel"}
+ = lists:keyfind("sec-websocket-location", 1, Headers),
+ {"sec-websocket-origin", "http://localhost"}
+ = lists:keyfind("sec-websocket-origin", 1, Headers),
+ ok = gen_tcp:send(Socket, <<15,245,8,18,2,204,133,33>>),
+ {ok, Body} = gen_tcp:recv(Socket, 0, 6000),
+ <<169,244,191,103,146,33,149,59,74,104,67,5,99,118,171,236>> = Body,
+ ok = gen_tcp:send(Socket, << 0, "msg sent", 255 >>),
+ {ok, << 0, "msg sent", 255 >>}
+ = gen_tcp:recv(Socket, 0, 6000),
+ ok = timer:sleep(500),
+ ok = gen_tcp:send(Socket, << 0, "msg sent", 255 >>),
+ {ok, << 0, "msg sent", 255 >>}
+ = gen_tcp:recv(Socket, 0, 6000),
+ ok = timer:sleep(500),
+ ok = gen_tcp:send(Socket, << 0, "msg sent", 255 >>),
+ {ok, << 0, "msg sent", 255 >>}
+ = gen_tcp:recv(Socket, 0, 6000),
+ ok = timer:sleep(500),
+ ok = gen_tcp:send(Socket, << 0, "msg sent", 255 >>),
+ {ok, << 0, "msg sent", 255 >>}
+ = gen_tcp:recv(Socket, 0, 6000),
+ {ok, << 255, 0 >>} = gen_tcp:recv(Socket, 0, 6000),
+ {error, closed} = gen_tcp:recv(Socket, 0, 6000),
+ ok.
+
+ws_upgrade_with_opts(Config) ->
+ {port, Port} = lists:keyfind(port, 1, Config),
+ {ok, Socket} = gen_tcp:connect("localhost", Port,
+ [binary, {active, false}, {packet, raw}]),
+ ok = gen_tcp:send(Socket, [
+ "GET /ws_upgrade_with_opts HTTP/1.1\r\n"
+ "Host: localhost\r\n"
+ "Connection: Upgrade\r\n"
+ "Upgrade: websocket\r\n"
+ "Sec-WebSocket-Origin: http://localhost\r\n"
+ "Sec-WebSocket-Version: 8\r\n"
+ "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
+ "\r\n"]),
+ {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
+ {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest}
+ = erlang:decode_packet(http, Handshake, []),
+ [Headers, <<>>] = websocket_headers(
+ erlang:decode_packet(httph, Rest, []), []),
+ {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers),
+ {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers),
+ {"sec-websocket-accept", "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="}
+ = lists:keyfind("sec-websocket-accept", 1, Headers),
+ {ok, Response} = gen_tcp:recv(Socket, 9, 6000),
+ << 1:1, 0:3, 1:4, 0:1, 7:7, "success" >> = Response,
+ ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 0:8 >>), %% close
+ {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
+ {error, closed} = gen_tcp:recv(Socket, 0, 6000),
+ ok.
+
%% Internal.
websocket_headers({ok, http_eoh, Rest}, Acc) ->
diff --git a/test/ws_send_many_handler.erl b/test/ws_send_many_handler.erl
index ee386ba..bd67814 100644
--- a/test/ws_send_many_handler.erl
+++ b/test/ws_send_many_handler.erl
@@ -12,20 +12,16 @@
init(_Any, _Req, _Opts) ->
{upgrade, protocol, cowboy_websocket}.
-websocket_init(_TransportName, Req, _Opts) ->
+websocket_init(_TransportName, Req, Sequence) ->
Req2 = cowboy_req:compact(Req),
erlang:send_after(10, self(), send_many),
- {ok, Req2, undefined}.
+ {ok, Req2, Sequence}.
websocket_handle(_Frame, Req, State) ->
{ok, Req, State}.
-websocket_info(send_many, Req, State) ->
- {reply, [
- {text, <<"one">>},
- {text, <<"two">>},
- {text, <<"seven!">>}
- ], Req, State}.
+websocket_info(send_many, Req, State = [{sequence, Sequence}]) ->
+ {reply, Sequence, Req, State}.
websocket_terminate(_Reason, _Req, _State) ->
ok.
diff --git a/test/ws_timeout_cancel_handler.erl b/test/ws_timeout_cancel_handler.erl
new file mode 100644
index 0000000..ee75d9b
--- /dev/null
+++ b/test/ws_timeout_cancel_handler.erl
@@ -0,0 +1,33 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(ws_timeout_cancel_handler).
+-behaviour(cowboy_http_handler).
+-behaviour(cowboy_websocket_handler).
+-export([init/3, handle/2, terminate/2]).
+-export([websocket_init/3, websocket_handle/3,
+ websocket_info/3, websocket_terminate/3]).
+
+init(_Any, _Req, _Opts) ->
+ {upgrade, protocol, cowboy_websocket}.
+
+handle(_Req, _State) ->
+ exit(badarg).
+
+terminate(_Req, _State) ->
+ exit(badarg).
+
+websocket_init(_TransportName, Req, _Opts) ->
+ erlang:start_timer(500, self(), should_not_cancel_timer),
+ {ok, Req, undefined, 1000}.
+
+websocket_handle({text, Data}, Req, State) ->
+ {reply, {text, Data}, Req, State};
+websocket_handle({binary, Data}, Req, State) ->
+ {reply, {binary, Data}, Req, State}.
+
+websocket_info(_Info, Req, State) ->
+ erlang:start_timer(500, self(), should_not_cancel_timer),
+ {ok, Req, State}.
+
+websocket_terminate(_Reason, _Req, _State) ->
+ ok.
diff --git a/test/ws_upgrade_with_opts_handler.erl b/test/ws_upgrade_with_opts_handler.erl
new file mode 100644
index 0000000..02d755e
--- /dev/null
+++ b/test/ws_upgrade_with_opts_handler.erl
@@ -0,0 +1,28 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+-module(ws_upgrade_with_opts_handler).
+-behaviour(cowboy_websocket_handler).
+
+-export([init/3]).
+-export([websocket_init/3]).
+-export([websocket_handle/3]).
+-export([websocket_info/3]).
+-export([websocket_terminate/3]).
+
+init(_Any, Req, _Opts) ->
+ {upgrade, protocol, cowboy_websocket, Req, <<"success">>}.
+
+websocket_init(_TransportName, Req, Response) ->
+ Req2 = cowboy_req:compact(Req),
+ erlang:send_after(10, self(), send_response),
+ {ok, Req2, Response}.
+
+websocket_handle(_Frame, Req, State) ->
+ {ok, Req, State}.
+
+websocket_info(send_response, Req, State = Response)
+ when is_binary(Response) ->
+ {reply, {text, Response}, Req, State}.
+
+websocket_terminate(_Reason, _Req, _State) ->
+ ok.