migrating_from_1.0.asciidoc 8.0 KB

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