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

1125 commits

Author SHA1 Message Date
Loïc Hoguin
bc82679330
Fix a few rfc7540 tests
Cowboy takes a few shortcuts to avoid wasting resources when
there is a protocol error. The RFC wants us to send a different
error depending on the state of the stream at the time of the
error, and for us to maintain the connection in cases where we
would have to spend valuable resources to decode headers. In
all these cases Cowboy will simply close the connection with
an appropriate error.
2017-11-27 13:42:04 +01:00
Loïc Hoguin
c4e43ec26a
Add more rfc7230 tests and better handle bad chunk sizes
Bad chunk sizes used to be accepted and could result in
a badly parsed body or a timeout. They are now properly
rejected.

Chunk extensions now have a hard limit of 129 characters.
I haven't heard of anyone using them and Cowboy does not
provide an interface for them, but we can always increase
or make configurable if it ever becomes necessary (but
I honestly doubt it).

Also a test from the old http suite could be removed. Yay!
2017-11-22 23:10:00 +01:00
Loïc Hoguin
1af508c4cd
Add more rfc7230 tests and improve transfer-encoding
It's worth noting that transfer-encoding now takes precedence
over content-length as recommended by the RFC, so that when
both headers are sent we only care about transfer-encoding
and explicitly remove content-length from the headers.
2017-11-20 21:12:05 +01:00
Loïc Hoguin
3da9a6eef9
Add a test for early errors that occur on the request-line 2017-11-20 16:26:37 +01:00
Loïc Hoguin
92672b49af
Queue HTTP/2 trailers when there's still data in the buffer 2017-11-20 15:46:23 +01:00
Loïc Hoguin
6c765101b1
Fix compression when trailers are sent 2017-11-20 12:13:57 +01:00
Loïc Hoguin
ecb2a735ef
Remove a useless todo 2017-11-20 11:28:22 +01:00
Loïc Hoguin
62bf505d33
Add more rfc7230 tests
Also fixes the handling of the max_headers option for HTTP/1.1.
It is now a strict limit and not dependent on whether data is
already in the buffer.
2017-11-20 00:23:27 +01:00
Loïc Hoguin
ce5ab4b49a
Initialize trace patterns only once
They are global for the node for all future call trace flags,
so it's not necessary to set them repeatedly with every request.

Doing it once at startup also ensures we can't have race
conditions when the user wants to change which trace patterns
should be used (because requests are concurrent and patterns
end up overwriting themselves repeatedly), and makes this
changing of trace patterns much more straightforward: the
user can just define the ones they want. The default function
traces everything.

In addition I have also added the tracer_flags option to make
the trace flags configurable, excluding the tracer pid.
2017-11-17 13:23:38 +01:00
Loïc Hoguin
39baed6c80
Add preliminary support for trailers in responses
This depends on changes in Cowlib that are only available on
master.
2017-11-15 14:58:49 +01:00
Loïc Hoguin
a1ad482eb4
Don't supervise the tracer process
If we do then we end up killing the tracer after the stream
terminates and this is not what we want. This prevents us from
getting useful information from requests that are still ongoing
(when they run concurrently) and completely prevents us from
tracing Websocket handlers.

I'm not the biggest fan of having unsupervised modules but if
this is properly documented there should be no problem.
2017-11-13 20:45:08 +01:00
Loïc Hoguin
6a8f9ebbb2
Fix packet being dropped when using switch_protocol
This only happens if the switch takes too long, and should not
happen unless a spawned process refuses to shut down immediately.
2017-11-13 18:03:25 +01:00
Loïc Hoguin
6cc162583d
Keep the state returned by the tracer callback
It was mistakenly discarded.
2017-11-10 14:59:11 +00:00
Loïc Hoguin
da304799fe
Add a terminate event to the tracer and more tests 2017-11-03 17:52:18 +00:00
Loïc Hoguin
5bb6438e10
Don't crash when cowboy_clock is not running
This can happen normally when Cowboy is restarted, for example.
Instead of failing requests when that happens, we degrade
gracefully and do a little more work to provide the current
date header.
2017-11-01 17:06:37 +00:00
Loïc Hoguin
836342abb8
Add {switch_handler, Module} return value to cowboy_rest
Also {switch_handler, Module, Opts}.

Allows switching to a different handler type. This is
particularly useful for processing most of the request
with cowboy_rest and then streaming the response body
using cowboy_loop.
2017-11-01 16:27:26 +00:00
Loïc Hoguin
5e88a9b394
Update Cowlib to 2.0.1 and fix OTP 20.1+ Websocket compression
Unfortunately compression will be disabled for 20.1, 20.1.1
and 20.1.2. In additiona I do not recommend 20.1.3 due to
issues inflating some specific sizes.
2017-11-01 15:41:52 +00:00
Loïc Hoguin
83bd8bc935
Fix two edge cases for cowboy_req:stream_body
Sending data of size 0 with the fin flag set resulted in nothing
being sent to the client and still considering the response to
be finished for HTTP/1.1.

For both HTTP/1.1 and HTTP/2, the final chunk of body that is
sent automatically by Cowboy at the end of a response that the
user did not properly terminate was not passing through stream
handlers. This resulted in issues like compression being incorrect.

Some tests still fail under 20.1.3. They are due to recent zlib
changes and should be fixed in a future patch release. Unfortunately
it does not seem to be any 20.1 version that is safe to use for
Cowboy, although some will work better than others.
2017-11-01 15:33:10 +00:00
Loïc Hoguin
774824cd0f
Fix another warning 2017-10-31 22:36:37 +00:00
Loïc Hoguin
ad9ab51648
Add some todos 2017-10-31 17:15:11 +00:00
Loïc Hoguin
a97640d56d
Add informational responses to metrics 2017-10-31 15:04:00 +00:00
Loïc Hoguin
217fac7f44
Handle expect: 100-continue request headers
The 100 continue response will only be sent if the client
has not sent the body yet (at all), if the connection is
HTTP/1.1 or above and if the user has not sent it yet.

The 100 continue response is sent when the user calls
read_body and it is cowboy_stream_h's responsibility
to send it. This means projects that don't use the
cowboy_stream_h stream handler will need to handle the
expect header themselves (but that's okay because they
might have different considerations than normal Cowboy).
2017-10-30 16:21:25 +00:00
Loïc Hoguin
f3d6b05b86
Add cowboy_req:inform/2,3
User code can now send as many 1xx responses as necessary.
2017-10-29 21:03:04 +00:00
Loïc Hoguin
f4331f7c16
Add cowboy_tracer_h stream handler
Another experimental stream handler. It enables tracing for
the connection process and any children processes based on
the matching of the request. It can be used to do ad-hoc
tracing by sending a specific header, path, method or other.

It is meant to be used both for tests and production. Some
configuration scenarios are NOT safe for production, beware.

It's important to understand that, at this time, tracing
is enabled on the scale of the entire connection including
any future request processes. Keep this in mind when trying
to use it in production. The only way to stop tracing is
by having the callback function exit (by calling exit/1
explicitly). This can be done after a certain number of
events for example. Tracing can generate a lot of events,
so it's a good idea to stop after a small number of events
(between 1000 and 10000 should be good) and to avoid tracing
the whole world.

Documentation will follow at a later time.
2017-10-27 17:18:28 +01:00
Loïc Hoguin
ef58e15547
Introduce cowboy_req:sock/1 and cowboy_req:cert/1
To obtain the local socket ip/port and the client TLS
certificate, respectively.
2017-10-25 21:03:26 +01:00
Loïc Hoguin
e7114577bb
Fix a bug in HTTP/2 where connection gets closed prematurely
When the user code was sending a response fully without reading
the request body, the connection could get closed when receiving
DATA frames for that body. We now ask the client to stop sending
data via a NO_ERROR RST_STREAM, and linger any stream that has
been reset so that we can skip any pending frames from that
stream.

This fixes a number of intermittent failures in req_SUITE, which
now passes reliably.

In addition a small number of rfc7540_SUITE test cases have been
corrected as they were incorrect.
2017-10-23 14:49:33 +01:00
Loïc Hoguin
4bebe39975
Ensure stream terminate is called when switching protocols 2017-10-22 14:53:04 +01:00
Loïc Hoguin
76f4db863e
Fix sending of final chunk in HTTP/1.1
I broke this when fixing stream handlers earlier.
2017-10-21 21:01:40 +01:00
Loïc Hoguin
c62ce1c63e
Fix stream handler state being discarded on terminate
When we have to send a response before terminating a stream,
we call info. The state returned by this info call was
discarded when we called terminate after that. This commit
fixes it.

There are no tests for this, however the new metrics test
in the next commit requires the correct behavior so this
is ultimately covered.
2017-10-21 13:09:20 +01:00
Loïc Hoguin
1ef5a1c45b
Add a test for metrics with a request body 2017-10-20 18:00:40 +01:00
Loïc Hoguin
4211ea41bd
Add experimental metrics stream handler
It collects metrics and passes them to a configurable callback
once the stream terminates. It will be documented in a future
release. More tests incoming.
2017-10-20 14:46:10 +01:00
Loïc Hoguin
c602871f86
Fix HTTP/1.1 stopping streams too early
It is possible in some cases to move on to the next request
without waiting, but that can be done as an optimization
later on if necessary.
2017-10-20 13:16:04 +01:00
Loïc Hoguin
6fc05a6d64
Make sure a path parsing error in the router gives a 400 2017-10-03 00:30:15 +02:00
Loïc Hoguin
9317751cb3
Fix cowboy:set_env when the env value is missing 2017-10-02 16:19:13 +02:00
Jeffrey Griffin
6460e9d2d2
Fix infinite loop on incomplete multipart body
I have amended a lot of changes from the original commit
to make it behave as expected, including returning a 400
error. LH
2017-10-02 13:28:43 +02:00
Loïc Hoguin
292e732abf
Fix the documentation for the command flow 2017-10-02 10:46:45 +02:00
Loïc Hoguin
fda2d150db
Fix error response when constraint validation fails 2017-09-27 16:20:38 +02:00
Loïc Hoguin
11ae407eed
Ensure the behavior on stream handler crash is consistent
Also corrects the lack of error response when HTTP/1.1 is used.
2017-09-27 14:17:27 +02:00
Loïc Hoguin
d47e22edaa
Cleanup various comments 2017-09-25 13:52:58 +02:00
Loïc Hoguin
ea80e7a309
Avoid some crashes when HTTP/2 streams flush their response 2017-09-25 12:55:30 +02:00
Loïc Hoguin
c09b10190b
Move body length count to cowboy_stream_h instead of protocols
The documentation was correct, the code was not.

This should make it easier to implement new protocols. Note that
for HTTP/2 we will need to add some form of counting later on to
check for malformed requests, but we can do simpler and just
reduce from the expected length and then check if that's 0 when
IsFin=fin.
2017-09-25 12:48:47 +02:00
Loïc Hoguin
a6126306a2
Centralize stream handler error reporting in cowboy_stream 2017-09-21 12:53:21 +02:00
Loïc Hoguin
1cc877b649
Fix propagating the stacktrace on errors for OTP 19 2017-09-14 18:23:55 +02:00
Loïc Hoguin
5027d1335d
Rework the proc_lib_hack
It is completely removed for connection processes, because
assuming Cowboy is written properly this should bring us
nothing anymore in 2.0.

It is reworked for request processes, there we want to
always propagate the stacktrace (including for exits)
because we will print a report to help with debugging
and proc_lib doesn't propagate it for exits.

At the same time the initial callback for connection
and request processes has been changed to connection_process
and request_process, which should help with identifying
processes when inspecting.
2017-09-14 15:34:37 +02:00
Loïc Hoguin
3cbf885961
Improve how we detect request errors
When the request process exits with a {request_error, Reason, Human}
exit reason, Cowboy will return a 400 status code instead of 500.
Cowboy may also return a more specific status code depending on
the error. Currently it may also return 408 or 413.

This should prove to be more solid that looking inside the stack
trace.
2017-09-14 13:42:17 +02:00
Loïc Hoguin
2376983295
Remove transfer-encoding parsing from cowboy_req
The header never reaches this point.
2017-09-05 13:25:06 +02:00
Loïc Hoguin
d1c210b105
Remove an unused variable 2017-09-05 13:24:46 +02:00
Loïc Hoguin
4fd6e2f7cd
Accept sendfile tuple with 0 length in cowboy_req
This will result in no data being sent. It's simply easier to
do this than to have to handle 0 size cases in user code.
2017-09-04 20:48:07 +02:00
Loïc Hoguin
58e9e76814
Fix more documentation todos
I have decided not to include a manual page for
cowboy_stream_h at this point because it clashes
with the cowboy_stream manual page. This decision
will be revisited in the future.
2017-09-04 14:33:44 +02:00
Loïc Hoguin
9a1810bf13
Tentative fix for out of order queued data 2017-08-21 16:55:30 +02:00