| 1 |
[appendix] |
| 2 |
== Migrating from Cowboy 1.0 to 2.0 |
| 3 |
|
| 4 |
A lot has changed between Cowboy 1.0 and 2.0. The `cowboy_req` |
| 5 |
interface in particular has seen a massive revamp. Hooks are |
| 6 |
gone, their functionality can now be achieved via stream |
| 7 |
handlers. |
| 8 |
|
| 9 |
The documentation has seen great work, in particular the |
| 10 |
manual. Each module and each function now has its own dedicated |
| 11 |
manual page with full details and examples. |
| 12 |
|
| 13 |
=== Compatibility |
| 14 |
|
| 15 |
Compatibility with Erlang/OTP R16, 17 and 18 has been dropped. |
| 16 |
Erlang/OTP 19.0 or above is required. It is non-trivial to |
| 17 |
make Cowboy 2.0 work with older Erlang/OTP versions. |
| 18 |
|
| 19 |
Cowboy 2.0 is not compatible with Cowlib versions older than |
| 20 |
2.0. It should be compatible with Ranch 1.0 or above, however |
| 21 |
it has not been tested with Ranch versions older than 1.4. |
| 22 |
|
| 23 |
Cowboy 2.0 is tested on Arch Linux, Ubuntu, FreeBSD, Windows |
| 24 |
and OSX. It is tested with every point release (latest patch |
| 25 |
release) and also with HiPE on the most recent release. |
| 26 |
|
| 27 |
Cowboy 2.0 now comes with Erlang.mk templates. |
| 28 |
|
| 29 |
=== Features added |
| 30 |
|
| 31 |
* The HTTP/2 protocol is now supported. |
| 32 |
|
| 33 |
* Cowboy no longer uses only one process per connection. |
| 34 |
It now uses one process per connection plus one process |
| 35 |
per request by default. This is necessary for HTTP/2. |
| 36 |
There might be a slight drop in performance for HTTP/1.1 |
| 37 |
connections due to this change. |
| 38 |
|
| 39 |
* Cowboy internals have largely been reworked in order to |
| 40 |
support HTTP/2. This opened the way to stream handlers, |
| 41 |
which are a chain of modules that are called whenever |
| 42 |
something happens relating to a request/response. |
| 43 |
|
| 44 |
* The `cowboy_stream_h` stream handler has been added. |
| 45 |
It provides most of Cowboy's default behavior. |
| 46 |
|
| 47 |
* The `cowboy_compress_h` stream handler has been added. |
| 48 |
It compresses responses when possible. It's worth noting |
| 49 |
that it compresses in more cases than Cowboy 1.0 ever did. |
| 50 |
|
| 51 |
* Because of the many changes in the internals of Cowboy, |
| 52 |
many options have been added or modified. Of note is that |
| 53 |
the Websocket options are now given per handler rather |
| 54 |
than for the entire listener. |
| 55 |
|
| 56 |
* Websocket permessage-deflate compression is now supported |
| 57 |
via the `compress` option. |
| 58 |
|
| 59 |
* Static file handlers will now correctly find files found |
| 60 |
in '.ez' archives. |
| 61 |
|
| 62 |
* Constraints have been generalized and are now used not only |
| 63 |
in the router but also in some `cowboy_req` functions. Their |
| 64 |
interface has also been modified to allow for reverse |
| 65 |
operations and formatting of errors. |
| 66 |
|
| 67 |
=== Features removed |
| 68 |
|
| 69 |
* SPDY support has been removed. Use HTTP/2 instead. |
| 70 |
|
| 71 |
* Hooks have been removed. Use xref:streams[stream handlers] instead. |
| 72 |
|
| 73 |
* The undocumented `waiting_stream` hack has been removed. |
| 74 |
It allowed disabling chunked transfer-encoding for HTTP/1.1. |
| 75 |
It has no equivalent in Cowboy 2.0. Open a ticket if necessary. |
| 76 |
|
| 77 |
* Sub protocols still exist, but their interface has largely changed |
| 78 |
and they are no longer documented for the time being. |
| 79 |
|
| 80 |
=== Changed behaviors |
| 81 |
|
| 82 |
* The handler behaviors have been renamed and are now `cowboy_handler`, |
| 83 |
`cowboy_loop`, `cowboy_rest` and `cowboy_websocket`. |
| 84 |
|
| 85 |
* Plain HTTP, loop, REST and Websocket handlers have had their |
| 86 |
init and terminate callbacks unified. They now all use the |
| 87 |
`init/2` and `terminate/3` callbacks. The latter is now optional. |
| 88 |
The terminate reason has now been documented for all handlers. |
| 89 |
|
| 90 |
* The tuple returned to switch to a different handler type has |
| 91 |
changed. It now takes the form `{Module, Req, State}` or |
| 92 |
`{Module, Req, State, Opts}`, where `Opts` is a map of options |
| 93 |
to configure the handler. The timeout and hibernate options |
| 94 |
must now be specified using this map, where applicable. |
| 95 |
|
| 96 |
* All behaviors that used to accept `halt` or `shutdown` tuples |
| 97 |
as a return value no longer do so. The return value is now |
| 98 |
a `stop` tuple, consistent across Cowboy. |
| 99 |
|
| 100 |
* Middlewares can no longer return an `error` tuple. They have |
| 101 |
to send the response and return a `stop` tuple instead. |
| 102 |
|
| 103 |
* The `known_content_type` REST handler callback has been removed |
| 104 |
as it was found unnecessary. |
| 105 |
|
| 106 |
* Websocket handlers have both the normal `init/2` and |
| 107 |
an optional `websocket_init/1` function. The reason for |
| 108 |
that exception is that the `websocket_*` callbacks execute |
| 109 |
in a separate process from the `init/2` callback, and it |
| 110 |
was therefore not obvious how timers or monitors should |
| 111 |
be setup properly. They are effectively initializing the |
| 112 |
handler before and after the HTTP/1.1 upgrade. |
| 113 |
|
| 114 |
* Websocket handlers can now send frames directly from |
| 115 |
`websocket_init/1`. The frames will be sent immediately |
| 116 |
after the handshake. |
| 117 |
|
| 118 |
* Websocket handler callbacks no longer receive the Req |
| 119 |
argument. The `init/2` callback still receives it and |
| 120 |
can be used to extract relevant information. The `terminate/3` |
| 121 |
callback, if implemented, may still receive the Req |
| 122 |
(see next bullet point). |
| 123 |
|
| 124 |
* Websocket handlers have a new `req_filter` option that |
| 125 |
can be used to customize how much information should be |
| 126 |
discarded from the Req object after the handshake. Note |
| 127 |
that the Req object is only available in `terminate/3` |
| 128 |
past that point. |
| 129 |
|
| 130 |
* Websocket handlers have their timeout default changed |
| 131 |
from infinity to 60 seconds. |
| 132 |
|
| 133 |
=== New functions |
| 134 |
|
| 135 |
* The `cowboy_req:scheme/1` function has been added. |
| 136 |
|
| 137 |
* The `cowboy_req:uri/1,2` function has been added, replacing the |
| 138 |
less powerful functions `cowboy_req:url/1` and `cowboy_req:host_url/1`. |
| 139 |
|
| 140 |
* The functions `cowboy_req:match_qs/2` and `cowboy_req:match_cookies/2` |
| 141 |
allow matching query string and cookies against constraints. |
| 142 |
|
| 143 |
* The function `cowboy_req:set_resp_cookie/3` has been added to |
| 144 |
complement `cowboy_req:set_resp_cookie/4`. |
| 145 |
|
| 146 |
* The functions `cowboy_req:resp_header/2,3` and `cowboy_req:resp_headers/1` |
| 147 |
have been added. They can be used to retrieve response headers |
| 148 |
that were previously set. |
| 149 |
|
| 150 |
* The function `cowboy_req:set_resp_headers/2` has been added. It |
| 151 |
allows setting many response headers at once. |
| 152 |
|
| 153 |
* The functions `cowboy_req:push/3,4` can be used to push resources |
| 154 |
for protocols that support it (by default only HTTP/2). |
| 155 |
|
| 156 |
=== Changed functions |
| 157 |
|
| 158 |
* The `cowboy:start_http/4` function was renamed to `cowboy:start_clear/3`. |
| 159 |
|
| 160 |
* The `cowboy:start_https/4` function was renamed to `cowboy:start_tls/3`. |
| 161 |
|
| 162 |
* Most, if not all, functions in the `cowboy_req` module have been modified. |
| 163 |
Please consult the changelog of each individual functions. The changes |
| 164 |
are mainly about simplifying and clarifying the interface. The Req is no |
| 165 |
longer returned when not necessary, maps are used wherever possible, |
| 166 |
and some functions have been renamed. |
| 167 |
|
| 168 |
* The position of the `Opts` argument for `cowboy_req:set_resp_cookie/4` |
| 169 |
has changed to improve consistency. It is now the last argument. |
| 170 |
|
| 171 |
=== Removed functions |
| 172 |
|
| 173 |
* The functions `cowboy_req:url/1` and `cowboy_req:host_url/1` have been |
| 174 |
removed in favor of the new function `cowboy_req:uri/1,2`. |
| 175 |
|
| 176 |
* The functions `cowboy_req:meta/2,3` and `cowboy_req:set_meta/3` have |
| 177 |
been removed. The Req object is now a public map, therefore they became |
| 178 |
unnecessary. |
| 179 |
|
| 180 |
* The functions `cowboy_req:set_resp_body_fun/2,3` have been removed. |
| 181 |
For sending files, the function `cowboy_req:set_resp_body/2` can now |
| 182 |
take a sendfile tuple. |
| 183 |
|
| 184 |
* Remove many undocumented functions from `cowboy_req`, including the |
| 185 |
functions `cowboy_req:get/2` and `cowboy_req:set/3`. |
| 186 |
|
| 187 |
=== Other changes |
| 188 |
|
| 189 |
* The correct percent-decoding algorithm is now used for path elements |
| 190 |
during routing. It will no longer decode `+` characters. |
| 191 |
|
| 192 |
* The router will now properly handle path segments `.` and `..`. |
| 193 |
|
| 194 |
* Routing behavior has changed for URIs containing latin1 characters. |
| 195 |
They are no longer allowed. URIs are expected to be in UTF-8 once |
| 196 |
they are percent-decoded. |
| 197 |
|
| 198 |
* Clients that send multiple headers of the same name |
| 199 |
will have the values of those headers concatenated into a |
| 200 |
comma-separated list. This is of special importance in the |
| 201 |
case of the content-type header, as previously only the |
| 202 |
first value was used in the `content_types_accepted/2` step |
| 203 |
in REST handlers. |
| 204 |
|
| 205 |
* Etag comparison in REST handlers has been fixed. Some requests may |
| 206 |
now fail when they succeeded in the past. |
| 207 |
|
| 208 |
* The `If-*-Since` headers are now ignored in REST handlers if |
| 209 |
the corresponding `If*-Match` header exist. The former is |
| 210 |
largely a backward compatible header and this shouldn't create |
| 211 |
any issue. The new behavior follows the current RFCs more closely. |
| 212 |
|
| 213 |
* The static file handler has been improved to handle more special |
| 214 |
characters on systems that accept them. |