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

122 commits

Author SHA1 Message Date
geeksilva97
308045fd67
Reject responses with explicit set-cookie header
LH: The tests received a lot of fixes and tweaking.
    I also reworded the error message to be more concise.
2024-01-15 17:43:14 +01:00
Loïc Hoguin
906a7ffc3c
Better error message when trying to reply twice
Also crash if trying to push after a reply was sent.
2024-01-09 13:06:11 +01:00
Loïc Hoguin
a40bab8fb3
Improve the error when trying to send a 204/304 with a body 2024-01-09 10:59:40 +01:00
Loïc Hoguin
e4a78aaeb1
Document body reading in auto mode
It is now tested both via cowboy_req:read_body and
via cowboy_req:cast.

Removes a bad example from the guide of body reading
with period of infinity, which does not work.
2024-01-08 15:13:18 +01:00
Loïc Hoguin
c1490d7d55
Ensure HTTP/1.1 Websocket resets the trap_exit flag
While we are identified as a supervisor in the tree,
we no longer manage children processes at that point,
so do not need to trap exit signals. Users can still
enable trap_exit if they prefer to.
2024-01-08 11:47:59 +01:00
Loïc Hoguin
5b2f600036
Don't automatically compress when response has etag
In the cowboy_compress_h stream handler.

Otherwise this could cause issues with caching, with the
etag being the same for compressed/uncompressed content.

Users that wish to send etags AND compress will have to
do it manually for the time being.
2024-01-05 15:53:42 +01:00
Loïc Hoguin
fd9711d949
Rework and improve the decompress stream handler
The read buffer was changed into an iovec to avoid doing
too many binary concatenations and allocations.

Decompression happens transparently: when decoding gzip,
the content-encoding header is removed (we only decode
when "gzip" is the only encoding so nothing remains).

We always add a content_decoded key to the Req object.
This key contains a list of codings that were decoded,
in the reverse order in which they were. Currently it
can only be empty or contain <<"gzip">> but future
improvements or user handlers may see it contain more
values.

The option to disable decompression was renamed to
decompress_enabled and defaults to true.

It is no longer possible to enable/disable decompression
in the middle of reading the body: this ensures that the
data we pass forward is always valid.

Various smaller improvements were made to the code,
tests and manual pages.
2024-01-04 15:50:12 +01:00
jdamanalo
3ed1b24dd6
Add cowboy_decompress_h stream handler 2023-12-21 15:39:08 +01:00
Robert J. Macomber
f74b69c3ed
Optionally reset the idle timeout when sending data
A new option reset_idle_timeout_on_send has been added.
When set to 'true', the idle timeout is reset not only
when data is received, but also when data is sent.

This allows sending large responses without having to
worry about timeouts triggering.

The default is currently unchanged but might change in
a future release.

LH: Greatly reworked the implementation so that the
    timeout gets reset on almost all socket writes.
	This essentially completely supersets the original
	work. Tests are mostly the same although I
	refactored a bit to avoid test code duplication.

This commit also changes HTTP/2 behavior a little when
data is received: Cowboy will not attempt to update the
window before running stream handler commands to avoid
sending WINDOW_UPDATE frames twice. Now it has some
small heuristic to ensure they can only be sent once
at most.
2023-12-21 14:03:07 +01:00
Loïc Hoguin
1547e9b93e
Increase loop_handler_timeout timeouts
It seems that macOS GH runners don't do timeouts well.
2023-12-15 16:22:06 +01:00
jdamanalo
a81dc8af9d
Add timeout to cowboy_loop
LH: I have added a test that does both hibernate and timeout
    and fixed a related issue. I also tweaked the docs and tests.
2023-12-15 15:37:34 +01:00
Loïc Hoguin
67bd791dcc
Change send_timeout_close test to accomodate macOS 2023-12-14 15:25:25 +01:00
Loïc Hoguin
efb681d749
Handle socket errors in HTTP/1.1 and HTTP/2
Doing so will let us notice when the connection is gone instead
of waiting for timeouts, at least in the cases where the remote
socket was closed properly. Timeouts are still needed in case
of TCP half-open problems.

This change means that the order of stream handler commands is
more important than before because socket errors may occur
during the processing of commands.
2023-12-12 15:05:33 +01:00
Sergei Shuvatov
3f5f326b73
Add test for send_timeout_close
LH: I reworked the test a little and added the same test
for HTTP/2 so that both HTTP/1.1 and HTTP/2 get the issue
fixed.
2023-12-12 15:05:01 +01:00
Martin Björklund
8795233c57
AcceptCallback may now return created/see_other tuples for POST
They replace and deprecate the {true,URI} return value.
2020-11-27 16:17:43 +01:00
Viktor Söderqvist
059d58d39f
Graceful shutdown
Note: This commit makes cowboy depend on cowlib master.

Graceful shutdown for HTTP/2:

1. A GOAWAY frame with the last stream id set to 2^31-1 is sent and a
   timer is started (goaway_initial_timeout, default 1000ms), to wait
   for any in-flight requests sent by the client, and the status is set
   to 'closing_initiated'. If the client responds with GOAWAY and closes
   the connection, we're done.
2. A second GOAWAY frame is sent with the actual last stream id and the
   status is set to 'closing'. If no streams exist, the connection
   terminates. Otherwise a second timer (goaway_complete_timeout,
   default 3000ms) is started, to wait for the streams to complete. New
   streams are not accepted when status is 'closing'.
3. If all streams haven't completed after the second timeout, the
   connection is forcefully terminated.

Graceful shutdown for HTTP/1.x:

1. If a request is currently being handled, it is waited for and the
   response is sent back to the client with the header "Connection:
   close". Then, the connection is closed.
2. If the current request handler is not finished within the time
   configured in transport option 'shutdown' (default 5000ms), the
   connection process is killed by its supervisor (ranch).

Implemented for HTTP/1.x and HTTP/2 in the following scenarios:

* When receiving exit signal 'shutdown' from the supervisor (e.g. when
  cowboy:stop_listener/3 is called).
* When a connection process is requested to terminate using
  sys:terminate/2,3.

LH: Edited tests a bit and added todos for useful tests to add.
2020-11-27 15:38:21 +01:00
Loïc Hoguin
39b2816255
204 and 304 responses must not include a body
When calling cowboy_req:reply/4 with a body a crash will occur
resulting in a 500 response. When calling cowboy_req:stream_reply/2,3
and then attempting to send a body a crash will occur.
2020-05-20 13:41:05 +02:00
Loïc Hoguin
8fc3da2fc3
Fix HTTP/1.1 bug when a flow command is returned after fin
This resulted in a badarith error due to the current flow being
set to infinity when the body has been fully read. A test case
has been added reproducing the issue.
2020-02-07 11:32:15 +01:00
Loïc Hoguin
47ecfd7318
Add a test confirming push requests have no body 2020-02-05 18:06:00 +01:00
Tony Han
43f1754f33
Test trailers bug when there are data frames in queue 2019-12-31 11:06:31 +01:00
Loïc Hoguin
d52e84bdd9
Add shutdown_reason Websocket command
This allows changing the normal exit reason of Websocket
processes, providing a way to signal other processes of
why the exit occurred.
2019-10-10 11:33:35 +02:00
Loïc Hoguin
2e8fcb9a9e
Add cowboy_req:cast/2
Better than sending messages manually.
2019-10-07 13:25:49 +02:00
Loïc Hoguin
3977f2b96f
Document the commands based Websocket interface
The old interface with ok|reply|stop tuples is deprecated.
2019-10-06 16:51:27 +02:00
Loïc Hoguin
3e23aff1d1
Add Websocket option validate_utf8
This allows disabling the UTF-8 validation check
for text and close frames.
2019-10-05 17:32:50 +02:00
Loïc Hoguin
03dac1486d
Add cowboy_req:filter_cookies/2 2019-10-05 11:23:57 +02:00
Loïc Hoguin
5ffb4f98e0
Make cowboy_compress_h add vary: accept-encoding 2019-10-04 13:32:35 +02:00
Loïc Hoguin
28aee1f272
Document media type wildcard in content_types_accepted 2019-10-03 16:20:29 +02:00
Loïc Hoguin
57badc9082
Add HTTP/2 tests with responses with HTTP/1.1 specific headers 2019-10-03 11:56:57 +02:00
Loïc Hoguin
eaa052616f
Ensure we can stream the response body from any process 2019-10-02 20:30:32 +02:00
Loïc Hoguin
20660d7566
Ensure we can read the request body from any process 2019-10-02 19:12:05 +02:00
Loïc Hoguin
f673e191b3
Add {set_options, #{metrics_user_data := Map}}
This allows giving custom metadata to the metrics stream handler.
This can be useful to for example provide the name of the
module handling the request which is only known after routing.
But any user data is allowed.

When called multiple times the user data maps are merged.
2019-10-02 15:23:23 +02:00
Loïc Hoguin
49af57d546
Implement backpressure on cowboy_req:stream_body
This should limit the amount of memory that Cowboy is using
when a handler is sending data much faster than the network.

The new max_stream_buffer_size is a soft limit and only has
an effect when the cowboy_stream_h handler is used.
2019-09-14 18:21:05 +02:00
Loïc Hoguin
42eb6bae69
Fix using custom fields in Req 2019-09-06 12:25:02 +02:00
Tony Han
7708fc77cd
Data received after RST_STREAM counts toward window 2019-07-16 15:32:58 +02:00
Loïc Hoguin
0223f69fcd
Move the final old HTTP suite tests and remove it 2018-11-22 00:12:18 +01:00
Loïc Hoguin
037b286aa8
Move many old HTTP test cases to the rest_handler test suite
A bug was fixed in cowboy_rest where when content_types_provided
returned a media type with a wildcard as first in the list, and
a request comes in without an accept header, then the media_type
value in the Req object would contain '*' instead of [] for the
parameters.
2018-11-21 18:39:59 +01:00
Loïc Hoguin
6058800abb
Move another test from the old HTTP test suite 2018-11-21 10:47:31 +01:00
Loïc Hoguin
8d6d78575f
Add the chunked option for HTTP/1.1
It allows disabling the chunked transfer-encoding. It
can also be disabled on a per-request basis, although
it will be ignored for responses that are not streamed.
2018-11-18 13:25:12 +01:00
Loïc Hoguin
f5015cb14b
Add the set_options Websocket command
It allows overriding the idle_timeout option only for now.
2018-11-16 13:49:00 +01:00
Loïc Hoguin
75045637fc
Ensure unknown options are ignored in set_options command 2018-11-16 13:09:01 +01:00
Loïc Hoguin
1949357f0c
Allow overriding cowboy_http's idle_timeout per request
This allows requests that expect to run longer to do so
without impacting the configuration of other requests.
2018-11-16 12:54:34 +01:00
Loïc Hoguin
240da3f2d9
Add the set_options stream handler command
The first two options to benefit from this are the
cowboy_compress_h options.
2018-11-15 18:53:42 +01:00
Loïc Hoguin
fbfec873f6
Add a compress_buffering option to cowboy_compress_h
Also changes the behavior to disable buffering by default, so
that the default works in all cases, including server-sent events.
2018-11-15 10:11:36 +01:00
Loïc Hoguin
292039362a
Don't send the content-length header in empty 304 responses
It's OK to send it when set explicitly, as it can be set
to what the representation's size would have been.
2018-11-14 19:24:39 +01:00
Loïc Hoguin
4fedb33631
Ignore the boundary parameter when accepting multipart 2018-11-14 18:04:32 +01:00
Loïc Hoguin
8d920f3db9
Add the {deflate, boolean()} Websocket command
It allows to temporarily disable Websocket compression
when it was negotiated. It's ignored otherwise. This
can be used as fine-grained control when some frames
do not compress well.
2018-11-13 15:55:09 +01:00
Loïc Hoguin
8164b50453
Add deflate options for Websocket compression
They allow the server to configure what it is willing to accept
for both the negotiated configuration (takeover and window bits)
and the other zlib options (level, mem_level and strategy).

This can be used to reduce the memory and/or CPU footprint of
the compressed data, which comes with a cost in compression ratio.
2018-11-12 18:12:44 +01:00
Loïc Hoguin
7840f6db7b
Add ProvideRangeCallback tests using sendfile
And fix this case when multiple ranges are requested.
2018-11-11 16:09:28 +01:00
Loïc Hoguin
dd0fbab6b7
Add automatic ranged request handling for bytes units
Returning the atom auto instead of a callback informs Cowboy
that it needs to handle range requests automatically. This
changes the behavior so that the ProvideCallback function
is called and then Cowboy splits the data on its own and
sends the response without any other user involvement other
than defining the ranges_provided/2 callback.

This is a quick and dirty way to add range request support
to resources, and will be good enough for many cases including
for cowboy_static as it also works when the normal response
body is a sendfile tuple.
2018-11-11 13:57:26 +01:00
Loïc Hoguin
d7b7580b39
Add sendfile support to cowboy_req:stream_body
It is now possible to stream one or more sendfile tuples.
A simple example of what can now be done would be for
example to build a tar file on the fly using the sendfile
syscall for sending the files, or to support Range requests
with more than one range with the sendfile syscall.

When using cowboy_compress_h unfortunately we have to read
the file in order to send it. More options will be added
at a later time to make sure users don't read too much
into memory. This is a new feature however so existing
code is not affected.

Also rework cowboy_http's data sending to be flatter.
2018-11-09 17:42:37 +01:00