]> git.kaiwu.me - nginx.git/log
nginx.git
5 years agoHTTP/2: fixed segfault on DATA frames after 400 errors.
Maxim Dounin [Wed, 23 Sep 2020 16:50:49 +0000 (19:50 +0300)]
HTTP/2: fixed segfault on DATA frames after 400 errors.

If 400 errors were redirected to an upstream server using the error_page
directive, DATA frames from the client might cause segmentation fault
due to null pointer dereference.  The bug had appeared in 6989:2c4dbcd6f2e4
(1.13.0).

Fix is to skip such frames in ngx_http_v2_state_read_data() (similarly
to 7561:9f1f9d6e056a).  With the fix, behaviour of 400 errors in HTTP/2
is now similar to one in HTTP/1.x, that is, nginx doesn't try to read the
request body.

Note that proxying 400 errors, as well as other early stage errors, to
upstream servers might not be a good idea anyway.  These errors imply
that reading and processing of the request (and the request headers)
wasn't complete, and proxying of such incomplete request might lead to
various errors.

Reported by Chenglong Zhang.

5 years agoSSL: disabled shutdown when there are buffered data.
Maxim Dounin [Wed, 16 Sep 2020 15:26:25 +0000 (18:26 +0300)]
SSL: disabled shutdown when there are buffered data.

This fixes "SSL_shutdown() failed (SSL: ... bad write retry)" errors
as observed on the second SSL_shutdown() call after SSL shutdown fixes in
09fb2135a589 (1.19.2), notably when HTTP/2 connections are closed due
to read timeouts while there are incomplete writes.

5 years agoSSL: disabled shutdown after connection errors.
Maxim Dounin [Wed, 16 Sep 2020 15:26:24 +0000 (18:26 +0300)]
SSL: disabled shutdown after connection errors.

This fixes "SSL_shutdown() failed (SSL: ... bad write retry)" errors
as observed on the second SSL_shutdown() call after SSL shutdown fixes in
09fb2135a589 (1.19.2), notably when sending fails in ngx_http_test_expect(),
similarly to ticket #1194.

Note that there are some places where c->error is misused to prevent
further output, such as ngx_http_v2_finalize_connection() if there
are pending streams, or in filter finalization.  These places seem
to be extreme enough to don't care about missing shutdown though.
For example, filter finalization currently prevents keepalive from
being used.

5 years agoSSL: fixed event handling during shutdown.
Maxim Dounin [Wed, 16 Sep 2020 15:26:23 +0000 (18:26 +0300)]
SSL: fixed event handling during shutdown.

The c->read->ready and c->write->ready flags need to be cleared to ensure
that appropriate read or write events will be reported by kernel.  Without
this, SSL shutdown might wait till the timeout after blocking on writing
or reading even if there is a socket activity.

5 years agoSSL: workaround for incorrect SSL_write() errors in OpenSSL 1.1.1.
Maxim Dounin [Wed, 16 Sep 2020 15:26:22 +0000 (18:26 +0300)]
SSL: workaround for incorrect SSL_write() errors in OpenSSL 1.1.1.

OpenSSL 1.1.1 fails to return SSL_ERROR_SYSCALL if an error happens
during SSL_write() after close_notify alert from the peer, and returns
SSL_ERROR_ZERO_RETURN instead.  Broken by this commit, which removes
the "i == 0" check around the SSL_RECEIVED_SHUTDOWN one:

https://git.openssl.org/?p=openssl.git;a=commitdiff;h=8051ab2

In particular, if a client closed the connection without reading
the response but with properly sent close_notify alert, this resulted in
unexpected "SSL_write() failed while ..." critical log message instead
of correct "SSL_write() failed (32: Broken pipe)" at the info level.

Since SSL_ERROR_ZERO_RETURN cannot be legitimately returned after
SSL_write(), the fix is to convert all SSL_ERROR_ZERO_RETURN errors
after SSL_write() to SSL_ERROR_SYSCALL.

5 years agoCache: keep c->body_start when Vary changes (ticket #2029).
Sergey Kandaurov [Wed, 9 Sep 2020 16:26:27 +0000 (19:26 +0300)]
Cache: keep c->body_start when Vary changes (ticket #2029).

If the variant hash doesn't match one we used as a secondary cache key,
we switch back to the original key.  In this case, c->body_start was kept
updated from an existing cache node overwriting the new response value.
After file cache update, it led to discrepancy between a cache node and
cache file seen as critical errors "file cache .. has too long header".

5 years agoHTTP/3: skip unknown frames on request stream.
Roman Arutyunyan [Mon, 24 Aug 2020 06:56:36 +0000 (09:56 +0300)]
HTTP/3: skip unknown frames on request stream.

As per HTTP/3 draft 29, section 4.1:

   Frames of unknown types (Section 9), including reserved frames
   (Section 7.2.8) MAY be sent on a request or push stream before,
   after, or interleaved with other frames described in this section.

Also, trailers frame is now used as an indication of the request body end.

5 years agoHTTP/3: fixed handling request body eof.
Roman Arutyunyan [Wed, 16 Sep 2020 17:59:25 +0000 (18:59 +0100)]
HTTP/3: fixed handling request body eof.

While for HTTP/1 unexpected eof always means an error, for HTTP/3 an eof right
after a DATA frame end means the end of the request body.  For this reason,
since adding HTTP/3 support, eof no longer produced an error right after recv()
but was passed to filters which would make a decision.  This decision was made
in ngx_http_parse_chunked() and ngx_http_v3_parse_request_body() based on the
b->last_buf flag.

Now that since 0f7f1a509113 (1.19.2) rb->chunked->length is a lower threshold
for the expected number of bytes, it can be set to zero to indicate that more
bytes may or may not follow.  Now it's possible to move the check for eof from
parser functions to ngx_http_request_body_chunked_filter() and clean up the
parsing code.

Also, in the default branch, in case of eof, the following three things
happened, which were replaced with returning NGX_ERROR while implementing
HTTP/3:

- "client prematurely closed connection" message was logged
- c->error flag was set
- NGX_HTTP_BAD_REQUEST was returned

The change brings back this behavior for HTTP/1 as well as HTTP/3.

5 years agoQUIC: switched to draft 29 by default.
Vladimir Homutov [Fri, 11 Sep 2020 07:56:05 +0000 (10:56 +0300)]
QUIC: switched to draft 29 by default.

5 years agoQUIC: allowed old DCID for initial packets until first ACK.
Roman Arutyunyan [Wed, 9 Sep 2020 13:35:29 +0000 (16:35 +0300)]
QUIC: allowed old DCID for initial packets until first ACK.

If a packet sent in response to an initial client packet was lost, then
successive client initial packets were dropped by nginx with the unexpected
dcid message logged.  This was because the new DCID generated by the server was
not available to the client.

5 years agoQUIC: eliminated idle timeout restart for dropped packets.
Roman Arutyunyan [Tue, 8 Sep 2020 12:54:02 +0000 (15:54 +0300)]
QUIC: eliminated idle timeout restart for dropped packets.

5 years agoQUIC: removed check for packet size beyond MAX_UDP_PAYLOAD_SIZE.
Sergey Kandaurov [Tue, 8 Sep 2020 10:35:50 +0000 (13:35 +0300)]
QUIC: removed check for packet size beyond MAX_UDP_PAYLOAD_SIZE.

The check tested the total size of a packet header and unprotected packet
payload, which doesn't include the packet number length and expansion of
the packet protection AEAD.  If the packet was corrupted, it could cause
false triggering of the condition due to unsigned type underflow leading
to a connection error.

Existing checks for the QUIC header and protected packet payload lengths
should be enough.

5 years agoQUIC: check that the packet length is of at least sample size.
Sergey Kandaurov [Tue, 8 Sep 2020 10:28:56 +0000 (13:28 +0300)]
QUIC: check that the packet length is of at least sample size.

From quic-tls draft, section 5.4.2:
   An endpoint MUST discard packets that are not long enough to contain
   a complete sample.

The check includes the Packet Number field assumed to be 4 bytes long.

5 years agoQUIC: update packet length for short packets too.
Sergey Kandaurov [Tue, 8 Sep 2020 10:27:39 +0000 (13:27 +0300)]
QUIC: update packet length for short packets too.

During long packet header parsing, pkt->len is updated with the Length
field value that is used to find next coalesced packets in a datagram.
For short packets it still contained the whole QUIC packet size.

This change uniforms packet length handling to always contain the total
length of the packet number and protected packet payload in pkt->len.

5 years agoQUIC: added logging output stream frame offset.
Roman Arutyunyan [Mon, 7 Sep 2020 17:55:36 +0000 (20:55 +0300)]
QUIC: added logging output stream frame offset.

5 years agoQUIC: refactored ngx_quic_retry_input().
Vladimir Homutov [Fri, 4 Sep 2020 12:48:53 +0000 (15:48 +0300)]
QUIC: refactored ngx_quic_retry_input().

The function now returns NGX_DECLINED for packets that need to be ignored
and integrates nicely into ngx_quic_input().

5 years agoQUIC: do not send STOP_SENDING after STREAM fin.
Roman Arutyunyan [Sun, 6 Sep 2020 11:51:23 +0000 (14:51 +0300)]
QUIC: do not send STOP_SENDING after STREAM fin.

Previously STOP_SENDING was sent to client upon stream closure if rev->eof and
rev->error were not set.  This was an indirect indication that no RESET_STREAM
or STREAM fin has arrived.  But it is indeed possible that rev->eof is not set,
but STREAM fin has already been received, just not read out by the application.
In this case sending STOP_SENDING does not make sense and can be misleading for
some clients.

5 years agoQUIC: added support for multiple connection IDs.
Vladimir Homutov [Thu, 3 Sep 2020 10:11:27 +0000 (13:11 +0300)]
QUIC: added support for multiple connection IDs.

The peer may issue additional connection IDs up to the limit defined by
transport parameter "active_connection_id_limit", using NEW_CONNECTION_ID
frames, and retire such IDs using RETIRE_CONNECTION_ID frame.

5 years agoQUIC: style.
Vladimir Homutov [Thu, 27 Aug 2020 07:15:37 +0000 (10:15 +0300)]
QUIC: style.

Moved processing of RETIRE_CONNECTION_ID right after the NEW_CONNECTION_ID.

5 years agoQUIC: pass return code from ngx_quic_decrypt() to the caller.
Vladimir Homutov [Wed, 2 Sep 2020 19:34:15 +0000 (22:34 +0300)]
QUIC: pass return code from ngx_quic_decrypt() to the caller.

It is required to distinguish internal errors from corrupted packets and
perform actions accordingly: drop the packet or close the connection.

While there, made processing of ngx_quic_decrypt() erorrs similar and
removed couple of protocol violation errors.

5 years agoQUIC: discard unrecognized long packes.
Vladimir Homutov [Wed, 2 Sep 2020 06:54:15 +0000 (09:54 +0300)]
QUIC: discard unrecognized long packes.

While there, updated comment about discarded packets.

5 years agoHTTP/3: do not set the never-indexed literal bit by default.
Roman Arutyunyan [Mon, 31 Aug 2020 15:42:26 +0000 (18:42 +0300)]
HTTP/3: do not set the never-indexed literal bit by default.

The "Literal Header Field Never Indexed" header field representation is not
used in HTTP/2, and it makes little sense to make a distinction in HTTP/3.

5 years agoQUIC: discard incorrect packets instead of closing the connection.
Vladimir Homutov [Tue, 1 Sep 2020 14:20:42 +0000 (17:20 +0300)]
QUIC: discard incorrect packets instead of closing the connection.

quic-transport

5.2:
    Packets that are matched to an existing connection are discarded if
    the packets are inconsistent with the state of that connection.

5.2.2:
   Servers MUST drop incoming packets under all other circumstances.

5 years agoQUIC: do not update largest packet number from a bad packet.
Roman Arutyunyan [Tue, 1 Sep 2020 12:21:49 +0000 (15:21 +0300)]
QUIC: do not update largest packet number from a bad packet.

The removal of QUIC packet protection depends on the largest packet number
received.  When a garbage packet was received, the decoder still updated the
largest packet number from that packet.  This could affect removing protection
from subsequent QUIC packets.

5 years agoQUIC: handle PATH_CHALLENGE frame.
Roman Arutyunyan [Fri, 28 Aug 2020 09:01:35 +0000 (12:01 +0300)]
QUIC: handle PATH_CHALLENGE frame.

A PATH_RESPONSE frame with the same data is sent in response.

5 years agoQUIC: enforce flow control on incoming STREAM and CRYPTO frames.
Roman Arutyunyan [Tue, 25 Aug 2020 14:22:57 +0000 (17:22 +0300)]
QUIC: enforce flow control on incoming STREAM and CRYPTO frames.

5 years agoHTTP/3: drop the unwanted remainder of the request.
Roman Arutyunyan [Tue, 25 Aug 2020 09:45:21 +0000 (12:45 +0300)]
HTTP/3: drop the unwanted remainder of the request.

As per HTTP/3 draft 29, section 4.1:

   When the server does not need to receive the remainder of the request,
   it MAY abort reading the request stream, send a complete response, and
   cleanly close the sending part of the stream.

5 years agoQUIC: send STOP_SENDING on stream closure.
Roman Arutyunyan [Tue, 25 Aug 2020 11:07:26 +0000 (14:07 +0300)]
QUIC: send STOP_SENDING on stream closure.

The frame is sent for a read-enabled stream which has not received a FIN or
RESET_STREAM.

5 years agoQUIC: updated README.
Vladimir Homutov [Fri, 21 Aug 2020 11:55:32 +0000 (14:55 +0300)]
QUIC: updated README.

 - version negotiation is implemented
 - quic recovery implementation is greatly improved

5 years agoQUIC: disabled bidirectional SSL shutdown after 09fb2135a589.
Sergey Kandaurov [Fri, 21 Aug 2020 11:41:42 +0000 (14:41 +0300)]
QUIC: disabled bidirectional SSL shutdown after 09fb2135a589.

On QUIC connections, SSL_shutdown() is used to call the send_alert callback
to send a CONNECTION_CLOSE frame.  The reverse side is handled by other means.
At least BoringSSL doesn't differentiate whether this is a QUIC SSL method,
so waiting for the peer's close_notify alert should be explicitly disabled.

5 years agoQUIC: stripped down debug traces that have served its purpose.
Sergey Kandaurov [Fri, 21 Aug 2020 11:41:41 +0000 (14:41 +0300)]
QUIC: stripped down debug traces that have served its purpose.

The most observable remainers are incoming packet and stream payload
that could still be useful to debug various QUIC and HTTP/3 frames.

5 years agoQUIC: dead code removed.
Vladimir Homutov [Fri, 21 Aug 2020 07:00:25 +0000 (10:00 +0300)]
QUIC: dead code removed.

This case was already handled in c70446e3d771.

5 years agoQUIC: removed outdated TODOs.
Vladimir Homutov [Thu, 20 Aug 2020 13:45:48 +0000 (16:45 +0300)]
QUIC: removed outdated TODOs.

The logical quic connection state is tested by handler functions that
process corresponding types of packets (initial/handshake/application).
The packet is declined if state is incorrect.

No timeout is required for the input queue.

5 years agoQUIC: added version negotiation support.
Vladimir Homutov [Thu, 20 Aug 2020 14:11:04 +0000 (17:11 +0300)]
QUIC: added version negotiation support.

If a client attemtps to start a new connection with unsupported version,
a version negotiation packet is sent that contains a list of supported
versions (currently this is a single version, selected at compile time).

5 years agoHTTP/3: special handling of client errors in the upstream module.
Roman Arutyunyan [Thu, 20 Aug 2020 09:33:00 +0000 (12:33 +0300)]
HTTP/3: special handling of client errors in the upstream module.

The function ngx_http_upstream_check_broken_connection() terminates the HTTP/1
request if client sends eof.  For QUIC (including HTTP/3) the c->write->error
flag is now checked instead.  This flag is set when the entire QUIC connection
is closed or STOP_SENDING was received from client.

5 years agoHTTP/3: request more client body bytes.
Roman Arutyunyan [Tue, 18 Aug 2020 14:23:16 +0000 (17:23 +0300)]
HTTP/3: request more client body bytes.

Previously the request body DATA frame header was read by one byte because
filters were called only when the requested number of bytes were read.  Now,
after 08ff2e10ae92 (1.19.2), filters are called after each read.  More bytes
can be read at once, which simplifies and optimizes the code.

This also reduces diff with the default branch.

5 years agoQUIC: fixed format specifiers.
Sergey Kandaurov [Wed, 19 Aug 2020 13:00:12 +0000 (16:00 +0300)]
QUIC: fixed format specifiers.

5 years agoQUIC: changed c->quic->pto_count type to ngx_uint_t.
Sergey Kandaurov [Wed, 19 Aug 2020 12:58:03 +0000 (15:58 +0300)]
QUIC: changed c->quic->pto_count type to ngx_uint_t.

This field is served as a simple counter for PTO backoff.

5 years agoQUIC: do not artificially delay sending queued frames.
Sergey Kandaurov [Wed, 19 Aug 2020 10:24:54 +0000 (13:24 +0300)]
QUIC: do not artificially delay sending queued frames.

This interacts badly with retransmissions of lost packets
and can provoke spurious client retransmits.

5 years agoQUIC: do not arm loss detection timer on packet threshold.
Sergey Kandaurov [Wed, 19 Aug 2020 10:24:53 +0000 (13:24 +0300)]
QUIC: do not arm loss detection timer on packet threshold.

5 years agoQUIC: do not arm loss detection timer for succeeding packets.
Sergey Kandaurov [Wed, 19 Aug 2020 10:24:47 +0000 (13:24 +0300)]
QUIC: do not arm loss detection timer for succeeding packets.

5 years agoQUIC: handling packets with send time equal to lost send time.
Sergey Kandaurov [Wed, 19 Aug 2020 10:24:30 +0000 (13:24 +0300)]
QUIC: handling packets with send time equal to lost send time.

Previously, such packets weren't handled as the resulting zero remaining time
prevented setting the loss detection timer, which, instead, could be disarmed.
For implementation details, see quic-recovery draft 29, appendix A.10.

5 years agoQUIC: sending probe packets on PTO timer expiration.
Sergey Kandaurov [Wed, 19 Aug 2020 10:24:23 +0000 (13:24 +0300)]
QUIC: sending probe packets on PTO timer expiration.

The PTO handler is split into separate PTO and loss detection handlers
that operate interchangeably depending on which timer should be set.

The present ngx_quic_lost_handler is now only used for packet loss detection.
It replaces ngx_quic_pto_handler if there are packets preceeding largest_ack.
Once there is no more such packets, ngx_quic_pto_handler is installed again.

Probes carry unacknowledged data previously sent in the oldest packet number,
one per each packet number space.  That is, it could be up to two probes.

PTO backoff is now increased before scheduling next probes.

5 years agoQUIC: changed ctx->largest_ack initial value to type maximum.
Sergey Kandaurov [Tue, 18 Aug 2020 20:33:40 +0000 (23:33 +0300)]
QUIC: changed ctx->largest_ack initial value to type maximum.

In particular, this prevents declaring packet number 0 as lost if
there aren't yet any acknowledgements in this packet number space.
For example, only Initial packets were acknowledged in handshake.

5 years agoHTTP/3: fixed context storage in request body parser.
Sergey Kandaurov [Tue, 18 Aug 2020 14:11:32 +0000 (17:11 +0300)]
HTTP/3: fixed context storage in request body parser.

5 years agoMerged with the default branch.
Roman Arutyunyan [Tue, 18 Aug 2020 13:22:00 +0000 (16:22 +0300)]
Merged with the default branch.

5 years agoQUIC: coalesce neighbouring stream send buffers.
Roman Arutyunyan [Tue, 18 Aug 2020 09:28:33 +0000 (12:28 +0300)]
QUIC: coalesce neighbouring stream send buffers.

Previously a single STREAM frame was created for each buffer in stream output
chain which is wasteful with respect to memory.  The following changes were
made in the stream send code:

- ngx_quic_stream_send_chain() no longer calls ngx_quic_stream_send() and got
  a separate implementation that coalesces neighbouring buffers into a single
  frame
- the new ngx_quic_stream_send_chain() respects the limit argument, which fixes
  sendfile_max_chunk and limit_rate
- ngx_quic_stream_send() is reimplemented to call ngx_quic_stream_send_chain()
- stream frame size limit is moved out to a separate function
  ngx_quic_max_stream_frame()
- flow control is moved out to a separate function ngx_quic_max_stream_flow()
- ngx_quic_stream_send_chain() is relocated next to ngx_quic_stream_send()

5 years agoQUIC: packet based bytes_in_flight accounting.
Sergey Kandaurov [Fri, 14 Aug 2020 13:54:13 +0000 (16:54 +0300)]
QUIC: packet based bytes_in_flight accounting.

A packet size is kept in one of the frames belonging to the packet.

5 years agoQUIC: fixed leak of bytes_in_flight on keys discard.
Sergey Kandaurov [Fri, 14 Aug 2020 13:54:06 +0000 (16:54 +0300)]
QUIC: fixed leak of bytes_in_flight on keys discard.

This applies to discarding Initial and Handshake keys.

5 years agoQUIC: fixed leak of bytes_in_flight attributed to lost packets.
Sergey Kandaurov [Fri, 14 Aug 2020 13:53:56 +0000 (16:53 +0300)]
QUIC: fixed leak of bytes_in_flight attributed to lost packets.

5 years agorelease-1.19.2 tag
Maxim Dounin [Tue, 11 Aug 2020 14:52:30 +0000 (17:52 +0300)]
release-1.19.2 tag

5 years agonginx-1.19.2-RELEASE release-1.19.2
Maxim Dounin [Tue, 11 Aug 2020 14:52:30 +0000 (17:52 +0300)]
nginx-1.19.2-RELEASE

5 years agoCore: reusing connections in advance.
Maxim Dounin [Mon, 10 Aug 2020 15:53:07 +0000 (18:53 +0300)]
Core: reusing connections in advance.

Reworked connections reuse, so closing connections is attempted in
advance, as long as number of free connections is less than 1/16 of
worker connections configured.  This ensures that new connections can
be handled even if closing a reusable connection requires some time,
for example, for a lingering close (ticket #2017).

The 1/16 ratio is selected to be smaller than 1/8 used for disabling
accept when working with accept mutex, so nginx will try to balance
new connections to different workers first, and will start reusing
connections only if this won't help.

5 years agoCore: added a warning about reusing connections.
Maxim Dounin [Mon, 10 Aug 2020 15:52:59 +0000 (18:52 +0300)]
Core: added a warning about reusing connections.

Previously, reusing connections happened silently and was only
visible in monitoring systems.  This was shown to be not very user-friendly,
and administrators often didn't realize there were too few connections
available to withstand the load, and configured timeouts (keepalive_timeout
and http2_idle_timeout) were effectively reduced to keep things running.

To provide at least some information about this, a warning is now logged
(at most once per second, to avoid flooding the logs).

5 years agoSSL: disabled sending shutdown after ngx_http_test_reading().
Maxim Dounin [Mon, 10 Aug 2020 15:52:34 +0000 (18:52 +0300)]
SSL: disabled sending shutdown after ngx_http_test_reading().

Sending shutdown when ngx_http_test_reading() detects the connection is
closed can result in "SSL_shutdown() failed (SSL: ... bad write retry)"
critical log messages if there are blocked writes.

Fix is to avoid sending shutdown via the c->ssl->no_send_shutdown flag,
similarly to how it is done in ngx_http_keepalive_handler() for kqueue
when pending EOF is detected.

Reported by Jan PrachaÅ™
(http://mailman.nginx.org/pipermail/nginx-devel/2018-December/011702.html).

5 years agoHTTP/2: fixed c->timedout flag on timed out connections.
Maxim Dounin [Mon, 10 Aug 2020 15:52:20 +0000 (18:52 +0300)]
HTTP/2: fixed c->timedout flag on timed out connections.

Without the flag, SSL shutdown is attempted on such connections,
resulting in useless work and/or bogus "SSL_shutdown() failed
(SSL: ... bad write retry)" critical log messages if there are
blocked writes.

5 years agoSSL: fixed shutdown handling.
Maxim Dounin [Mon, 10 Aug 2020 15:52:09 +0000 (18:52 +0300)]
SSL: fixed shutdown handling.

Previously, bidirectional shutdown never worked, due to two issues
in the code:

1. The code only tested SSL_ERROR_WANT_READ and SSL_ERROR_WANT_WRITE
   when there was an error in the error queue, which cannot happen.
   The bug was introduced in an attempt to fix unexpected error logging
   as reported with OpenSSL 0.9.8g
   (http://mailman.nginx.org/pipermail/nginx/2008-January/003084.html).

2. The code never called SSL_shutdown() for the second time to wait for
   the peer's close_notify alert.

This change fixes both issues.

Note that after this change bidirectional shutdown is expected to work for
the first time, so c->ssl->no_wait_shutdown now makes a difference.  This
is not a problem for HTTP code which always uses c->ssl->no_wait_shutdown,
but might be a problem for stream and mail code, as well as 3rd party
modules.

To minimize the effect of the change, the timeout, which was used to be 30
seconds and not configurable, though never actually used, is now set to
3 seconds.  It is also expanded to apply to both SSL_ERROR_WANT_READ and
SSL_ERROR_WANT_WRITE, so timeout is properly set if writing to the socket
buffer is not possible.

5 years agoRequest body: optimized handling of small chunks.
Maxim Dounin [Thu, 6 Aug 2020 02:02:57 +0000 (05:02 +0300)]
Request body: optimized handling of small chunks.

If there is a previous buffer, copy small chunks into it instead of
allocating additional buffer.

5 years agoRequest body: allowed large reads on chunk boundaries.
Maxim Dounin [Thu, 6 Aug 2020 02:02:55 +0000 (05:02 +0300)]
Request body: allowed large reads on chunk boundaries.

If some additional data from a pipelined request happens to be
read into the body buffer, we copy it to r->header_in or allocate
an additional large client header buffer for it.

5 years agoRequest body: all read data are now sent to filters.
Maxim Dounin [Thu, 6 Aug 2020 02:02:44 +0000 (05:02 +0300)]
Request body: all read data are now sent to filters.

This is a prerequisite for the next change to allow large reads
on chunk boundaries.

5 years agoAdded size check to ngx_http_alloc_large_header_buffer().
Maxim Dounin [Thu, 6 Aug 2020 02:02:22 +0000 (05:02 +0300)]
Added size check to ngx_http_alloc_large_header_buffer().

This ensures that copying won't write more than the buffer size
even if the buffer comes from hc->free and it is smaller than the large
client header buffer size in the virtual host configuration.  This might
happen if size of large client header buffers is different in name-based
virtual hosts, similarly to the problem with number of buffers fixed
in 6926:e662cbf1b932.

5 years agoQUIC: handle client RESET_STREAM and STOP_SENDING.
Roman Arutyunyan [Mon, 3 Aug 2020 10:31:48 +0000 (13:31 +0300)]
QUIC: handle client RESET_STREAM and STOP_SENDING.

For RESET_STREAM the c->read->error flag is set.
For STOP_SENDING the c->write->error flag is set.

5 years agoQUIC: create streams for STREAM_DATA_BLOCKED and MAX_STREAM_DATA.
Roman Arutyunyan [Tue, 11 Aug 2020 16:10:57 +0000 (19:10 +0300)]
QUIC: create streams for STREAM_DATA_BLOCKED and MAX_STREAM_DATA.

Creating client-initiated streams is moved from ngx_quic_handle_stream_frame()
to a separate function ngx_quic_create_client_stream().  This function is
responsible for creating streams with lower ids as well.

Also, simplified and fixed initial data buffering in
ngx_quic_handle_stream_frame().  It is now done before calling the initial
handler as the handler can destroy the stream.

5 years agoQUIC: fixed ngx_http_test_reading() for QUIC streams.
Roman Arutyunyan [Tue, 11 Aug 2020 07:41:39 +0000 (10:41 +0300)]
QUIC: fixed ngx_http_test_reading() for QUIC streams.

Previously this function generated an error trying to figure out if client shut
down the write end of the connection.  The reason for this error was that a
QUIC stream has no socket descriptor.  However checking for eof is not the
right thing to do for an HTTP/3 QUIC stream since HTTP/3 clients are expected
to shut down the write end of the stream after sending the request.

Now the function handles QUIC streams separately.  It checks if c->read->error
is set.  The error flags for c->read and c->write are now set for all streams
when closing the QUIC connection instead of setting the pending_eof flag.

5 years agoQUIC: fixed ACK Ranges processing.
Sergey Kandaurov [Fri, 7 Aug 2020 09:34:15 +0000 (12:34 +0300)]
QUIC: fixed ACK Ranges processing.

According to quic-transport draft 29, section 19.3.1:

   The value of the Gap field establishes the largest packet number
   value for the subsequent ACK Range using the following formula:

      largest = previous_smallest - gap - 2

   Thus, given a largest packet number for the range, the smallest value
   is determined by the formula:

      smallest = largest - ack_range

While here, changed min/max to uint64_t for consistency.

5 years agoQUIC: fixed possible use-after-free on stream cleanup.
Sergey Kandaurov [Fri, 7 Aug 2020 09:34:11 +0000 (12:34 +0300)]
QUIC: fixed possible use-after-free on stream cleanup.

A QUIC stream could be destroyed by handler while in ngx_quic_stream_input().
To detect this, ngx_quic_find_stream() is used to check that it still exists.

Previously, a stream id was passed to this routine off the frame structure.
In case of stream cleanup, it is freed along with other frames belonging to
the stream on cleanup.  Then, a cleanup handler reuses last frames to update
MAX_STREAMS and serve other purpose.  Thus, ngx_quic_find_stream() is passed
a reused frame with zeroed out part pointed by stream_id.  If a stream with
id 0x0 still exists, this leads to use-after-free.

5 years agoQUIC: fixed format specifiers and removed casts.
Sergey Kandaurov [Tue, 28 Jul 2020 15:54:20 +0000 (18:54 +0300)]
QUIC: fixed format specifiers and removed casts.

5 years agoQUIC: consistent Stream ID logging format.
Sergey Kandaurov [Tue, 28 Jul 2020 14:11:25 +0000 (17:11 +0300)]
QUIC: consistent Stream ID logging format.

5 years agoQUIC: added HTTP/3 directives list to README.
Roman Arutyunyan [Tue, 28 Jul 2020 12:53:42 +0000 (15:53 +0300)]
QUIC: added HTTP/3 directives list to README.

Also removed server push from TODO list.

5 years agoFastCGI: fixed zero size buf alerts on extra data (ticket #2018).
Maxim Dounin [Mon, 27 Jul 2020 13:02:15 +0000 (16:02 +0300)]
FastCGI: fixed zero size buf alerts on extra data (ticket #2018).

After 05e42236e95b (1.19.1) responses with extra data might result in
zero size buffers being generated and "zero size buf" alerts in writer
(if f->rest happened to be 0 when processing additional stdout data).

5 years agoCore: enclosed parameters of the ngx_buf.h macros in parentheses.
balus [Mon, 27 Jul 2020 10:21:51 +0000 (13:21 +0300)]
Core: enclosed parameters of the ngx_buf.h macros in parentheses.

5 years agoOCSP: fixed certificate reference leak.
Sergey Kandaurov [Thu, 23 Jul 2020 14:31:09 +0000 (17:31 +0300)]
OCSP: fixed certificate reference leak.

5 years agoHTTP/3: server pushes.
Roman Arutyunyan [Thu, 23 Jul 2020 10:41:24 +0000 (13:41 +0300)]
HTTP/3: server pushes.

New directives are added:
- http3_max_concurrent_pushes
- http3_push
- http3_push_preload

5 years agoQUIC: limited the number of client-initiated streams.
Roman Arutyunyan [Mon, 27 Jul 2020 16:15:17 +0000 (19:15 +0300)]
QUIC: limited the number of client-initiated streams.

The limits on active bidi and uni client streams are maintained at their
initial values initial_max_streams_bidi and initial_max_streams_uni by sending
a MAX_STREAMS frame upon each client stream closure.

Also, the following is changed for data arriving to non-existing streams:

- if a stream was already closed, such data is ignored
- when creating a new stream, all streams of the same type with lower ids are
  created too

5 years agoQUIC: limited the number of server-initiated streams.
Roman Arutyunyan [Mon, 27 Jul 2020 15:51:42 +0000 (18:51 +0300)]
QUIC: limited the number of server-initiated streams.

Also, ngx_quic_create_uni_stream() is replaced with
ngx_quic_open_stream() which is capable of creating a bidi stream.

5 years agoXslt: disabled ranges.
Roman Arutyunyan [Wed, 22 Jul 2020 19:16:19 +0000 (22:16 +0300)]
Xslt: disabled ranges.

Previously, the document generated by the xslt filter was always fully sent
to client even if a range was requested and response status was 206 with
appropriate Content-Range.

The xslt module is unable to serve a range because of suspending the header
filter chain.  By the moment full response xml is buffered by the xslt filter,
range header filter is not called yet, but the range body filter has already
been called and did nothing.

The fix is to disable ranges by resetting the r->allow_ranges flag much like
the image filter that employs a similar technique.

5 years agoCore: close PID file when writing fails.
Ruslan Ermilov [Tue, 21 Jul 2020 17:34:29 +0000 (20:34 +0300)]
Core: close PID file when writing fails.

Reported by Jinhua Tan.

5 years agoHTTP/3: support $server_protocol variable.
Roman Arutyunyan [Tue, 14 Jul 2020 13:52:44 +0000 (16:52 +0300)]
HTTP/3: support $server_protocol variable.

Now it holds "HTTP/3.0".  Previously it was empty.

5 years agoStyle: moved function declarations to match usual code style.
Roman Arutyunyan [Thu, 23 Jul 2020 08:40:10 +0000 (11:40 +0300)]
Style: moved function declarations to match usual code style.

Plus a few other minor style changes.

5 years agoHTTP/3: renamed server configuration variables from v3cf to h3scf.
Roman Arutyunyan [Thu, 23 Jul 2020 10:12:01 +0000 (13:12 +0300)]
HTTP/3: renamed server configuration variables from v3cf to h3scf.

Now they are similar to HTTP/2 where they are called h2scf.

5 years agoHTTP/3: renamed ngx_http_v3.c to ngx_http_v3_encode.c.
Roman Arutyunyan [Mon, 13 Jul 2020 09:38:08 +0000 (12:38 +0300)]
HTTP/3: renamed ngx_http_v3.c to ngx_http_v3_encode.c.

The file contains only encoding functions.

5 years agoHTTP/3: encode frame ids with ngx_http_v3_encode_varlen_int().
Roman Arutyunyan [Mon, 13 Jul 2020 09:33:00 +0000 (12:33 +0300)]
HTTP/3: encode frame ids with ngx_http_v3_encode_varlen_int().

Even though typically frame ids fit into a single byte, calling
ngx_http_v3_encode_varlen_int() adds to the code clarity.

5 years agoHTTP/3: generate Location response header for absolute redirects.
Roman Arutyunyan [Thu, 23 Jul 2020 09:31:40 +0000 (12:31 +0300)]
HTTP/3: generate Location response header for absolute redirects.

5 years agoHTTP/3: header encoding functions.
Roman Arutyunyan [Mon, 13 Jul 2020 13:00:00 +0000 (16:00 +0300)]
HTTP/3: header encoding functions.

5 years agoQUIC: updated README to mention "quic" listen parameter.
Roman Arutyunyan [Wed, 22 Jul 2020 10:45:34 +0000 (13:45 +0300)]
QUIC: updated README to mention "quic" listen parameter.

5 years agoQUIC: fixed bulding perl module by reducing header pollution.
Sergey Kandaurov [Wed, 22 Jul 2020 11:48:49 +0000 (14:48 +0300)]
QUIC: fixed bulding perl module by reducing header pollution.

The ngx_http_perl_module module doesn't have a notion of including additional
search paths through --with-cc-opt, which results in compile error incomplete
type 'enum ssl_encryption_level_t' when building nginx without QUIC support.
The enum is visible from quic event headers and eventually pollutes ngx_core.h.

The fix is to limit including headers to compile units that are real consumers.

5 years agoSSL: fixed compilation without QUIC after 0d2b2664b41c.
Roman Arutyunyan [Wed, 22 Jul 2020 10:34:48 +0000 (13:34 +0300)]
SSL: fixed compilation without QUIC after 0d2b2664b41c.

5 years agoHTTP/3: do not call shutdown() for QUIC streams.
Roman Arutyunyan [Wed, 22 Jul 2020 08:03:42 +0000 (11:03 +0300)]
HTTP/3: do not call shutdown() for QUIC streams.

Previously, this triggered an alert "shutdown() failed" in error log.

5 years agoQUIC: eliminated connection handler argument in ngx_quic_run().
Roman Arutyunyan [Tue, 21 Jul 2020 20:08:23 +0000 (23:08 +0300)]
QUIC: eliminated connection handler argument in ngx_quic_run().

Now c->listening->handler() is called instead.

5 years agoQUIC: added "quic" listen parameter in Stream.
Roman Arutyunyan [Tue, 21 Jul 2020 20:08:39 +0000 (23:08 +0300)]
QUIC: added "quic" listen parameter in Stream.

Also, introduced ngx_stream_quic_module.

5 years agoQUIC: added "quic" listen parameter.
Roman Arutyunyan [Tue, 21 Jul 2020 20:09:22 +0000 (23:09 +0300)]
QUIC: added "quic" listen parameter.

The parameter allows processing HTTP/0.9-2 over QUIC.

Also, introduced ngx_http_quic_module and moved QUIC settings there

5 years agoQUIC: do not verify the selected ALPN protocol.
Roman Arutyunyan [Fri, 17 Jul 2020 21:08:04 +0000 (00:08 +0300)]
QUIC: do not verify the selected ALPN protocol.

The right protocol is selected by the HTTP code.  In the QUIC code only verify
that some protocol was selected and trigger an error otherwise.

5 years agoQUIC: fixed stream read event log.
Roman Arutyunyan [Fri, 17 Jul 2020 21:08:29 +0000 (00:08 +0300)]
QUIC: fixed stream read event log.

Previously, the main connection log was there.  Now it's the stream connection
log.

5 years agoFixed format specifiers.
Sergey Kandaurov [Mon, 20 Jul 2020 12:19:03 +0000 (15:19 +0300)]
Fixed format specifiers.

5 years agoQUIC: added anti-amplification limit.
Vladimir Homutov [Thu, 16 Jul 2020 13:36:02 +0000 (16:36 +0300)]
QUIC: added anti-amplification limit.

According to quic-transport draft 29, section 21.12.1.1:

   Prior to validation, endpoints are limited in what they are able to
   send.  During the handshake, a server cannot send more than three
   times the data it receives; clients that initiate new connections or
   migrate to a new network path are limited.

5 years agoQUIC: added limit of queued data.
Vladimir Homutov [Thu, 16 Jul 2020 12:02:38 +0000 (15:02 +0300)]
QUIC: added limit of queued data.

The ngx_quic_queue_frame() functions puts a frame into send queue and
schedules a push timer to actually send data.

The patch adds tracking for data amount in the queue and sends data
immediately if amount of data exceeds limit.

5 years agoQUIC: implemented probe timeout (PTO) calculation.
Vladimir Homutov [Thu, 16 Jul 2020 13:05:44 +0000 (16:05 +0300)]
QUIC: implemented probe timeout (PTO) calculation.

5 years agoQUIC: reworked retransmission mechanism.
Vladimir Homutov [Mon, 13 Jul 2020 14:31:29 +0000 (17:31 +0300)]
QUIC: reworked retransmission mechanism.

Instead of timer-based retransmissions with constant packet lifetime,
this patch implements ack-based loss detection and probe timeout
for the cases, when no ack is received, according to the quic-recovery
draft 29.

5 years agoQUIC: reworked ngx_quic_send_frames() function.
Vladimir Homutov [Wed, 15 Jul 2020 12:10:17 +0000 (15:10 +0300)]
QUIC: reworked ngx_quic_send_frames() function.

Instead of returning NGX_DONE/NGX_OK, the function now itself moves
passed frames range into sent queue and sets PTO timer if required.

5 years agoQUIC: renaming.
Vladimir Homutov [Mon, 13 Jul 2020 07:07:15 +0000 (10:07 +0300)]
QUIC: renaming.

The c->quic->retransmit timer is now called "pto".
The ngx_quic_retransmit() function is renamed to "ngx_quic_detect_lost()".

This is a preparation for the following patches.