Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / src / shrpx_downstream.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef SHRPX_DOWNSTREAM_H
26 #define SHRPX_DOWNSTREAM_H
27
28 #include "shrpx.h"
29
30 #include <cinttypes>
31 #include <vector>
32 #include <string>
33 #include <memory>
34 #include <chrono>
35 #include <algorithm>
36
37 #include <ev.h>
38
39 #include <nghttp2/nghttp2.h>
40
41 #ifdef ENABLE_HTTP3
42 #  include <nghttp3/nghttp3.h>
43 #endif // ENABLE_HTTP3
44
45 #include "llhttp.h"
46
47 #include "shrpx_io_control.h"
48 #include "shrpx_log_config.h"
49 #include "http2.h"
50 #include "memchunk.h"
51 #include "allocator.h"
52
53 using namespace nghttp2;
54
55 namespace shrpx {
56
57 class Upstream;
58 class DownstreamConnection;
59 struct BlockedLink;
60 struct DownstreamAddrGroup;
61 struct DownstreamAddr;
62
63 class FieldStore {
64 public:
65   FieldStore(BlockAllocator &balloc, size_t headers_initial_capacity)
66       : content_length(-1),
67         balloc_(balloc),
68         buffer_size_(0),
69         header_key_prev_(false),
70         trailer_key_prev_(false) {
71     headers_.reserve(headers_initial_capacity);
72   }
73
74   const HeaderRefs &headers() const { return headers_; }
75   const HeaderRefs &trailers() const { return trailers_; }
76
77   HeaderRefs &headers() { return headers_; }
78
79   const void add_extra_buffer_size(size_t n) { buffer_size_ += n; }
80   size_t buffer_size() const { return buffer_size_; }
81
82   size_t num_fields() const { return headers_.size() + trailers_.size(); }
83
84   // Returns pointer to the header field with the name |name|.  If
85   // multiple header have |name| as name, return last occurrence from
86   // the beginning.  If no such header is found, returns nullptr.
87   const HeaderRefs::value_type *header(int32_t token) const;
88   HeaderRefs::value_type *header(int32_t token);
89   // Returns pointer to the header field with the name |name|.  If no
90   // such header is found, returns nullptr.
91   const HeaderRefs::value_type *header(const StringRef &name) const;
92
93   void add_header_token(const StringRef &name, const StringRef &value,
94                         bool no_index, int32_t token);
95
96   // Adds header field name |name|.  First, the copy of header field
97   // name pointed by name.c_str() of length name.size() is made, and
98   // stored.
99   void alloc_add_header_name(const StringRef &name);
100
101   void append_last_header_key(const char *data, size_t len);
102   void append_last_header_value(const char *data, size_t len);
103
104   bool header_key_prev() const { return header_key_prev_; }
105
106   // Parses content-length, and records it in the field.  If there are
107   // multiple Content-Length, returns -1.
108   int parse_content_length();
109
110   // Empties headers.
111   void clear_headers();
112
113   void add_trailer_token(const StringRef &name, const StringRef &value,
114                          bool no_index, int32_t token);
115
116   // Adds trailer field name |name|.  First, the copy of trailer field
117   // name pointed by name.c_str() of length name.size() is made, and
118   // stored.
119   void alloc_add_trailer_name(const StringRef &name);
120
121   void append_last_trailer_key(const char *data, size_t len);
122   void append_last_trailer_value(const char *data, size_t len);
123
124   bool trailer_key_prev() const { return trailer_key_prev_; }
125
126   // erase_content_length_and_transfer_encoding erases content-length
127   // and transfer-encoding header fields.
128   void erase_content_length_and_transfer_encoding();
129
130   // content-length, -1 if it is unknown.
131   int64_t content_length;
132
133 private:
134   BlockAllocator &balloc_;
135   HeaderRefs headers_;
136   // trailer fields.  For HTTP/1.1, trailer fields are only included
137   // with chunked encoding.  For HTTP/2, there is no such limit.
138   HeaderRefs trailers_;
139   // Sum of the length of name and value in headers_ and trailers_.
140   // This could also be increased by add_extra_buffer_size() to take
141   // into account for request URI in case of HTTP/1.x request.
142   size_t buffer_size_;
143   bool header_key_prev_;
144   bool trailer_key_prev_;
145 };
146
147 // Protocols allowed in HTTP/2 :protocol header field.
148 enum class ConnectProto {
149   NONE,
150   WEBSOCKET,
151 };
152
153 struct Request {
154   Request(BlockAllocator &balloc)
155       : fs(balloc, 16),
156         recv_body_length(0),
157         unconsumed_body_length(0),
158         method(-1),
159         http_major(1),
160         http_minor(1),
161         connect_proto(ConnectProto::NONE),
162         upgrade_request(false),
163         http2_upgrade_seen(false),
164         connection_close(false),
165         http2_expect_body(false),
166         no_authority(false),
167         forwarded_once(false) {}
168
169   void consume(size_t len) {
170     assert(unconsumed_body_length >= len);
171     unconsumed_body_length -= len;
172   }
173
174   bool regular_connect_method() const {
175     return method == HTTP_CONNECT && connect_proto == ConnectProto::NONE;
176   }
177
178   bool extended_connect_method() const {
179     return connect_proto != ConnectProto::NONE;
180   }
181
182   FieldStore fs;
183   // Timestamp when all request header fields are received.
184   std::shared_ptr<Timestamp> tstamp;
185   // Request scheme.  For HTTP/2, this is :scheme header field value.
186   // For HTTP/1.1, this is deduced from URI or connection.
187   StringRef scheme;
188   // Request authority.  This is HTTP/2 :authority header field value
189   // or host header field value.  We may deduce it from absolute-form
190   // HTTP/1 request.  We also store authority-form HTTP/1 request.
191   // This could be empty if request comes from HTTP/1.0 without Host
192   // header field and origin-form.
193   StringRef authority;
194   // Request path, including query component.  For HTTP/1.1, this is
195   // request-target.  For HTTP/2, this is :path header field value.
196   // For CONNECT request, this is empty.
197   StringRef path;
198   // This is original authority which cannot be changed by per-pattern
199   // mruby script.
200   StringRef orig_authority;
201   // This is original path which cannot be changed by per-pattern
202   // mruby script.
203   StringRef orig_path;
204   // the length of request body received so far
205   int64_t recv_body_length;
206   // The number of bytes not consumed by the application yet.
207   size_t unconsumed_body_length;
208   int method;
209   // HTTP major and minor version
210   int http_major, http_minor;
211   // connect_proto specified in HTTP/2 :protocol pseudo header field
212   // which enables extended CONNECT method.  This field is also set if
213   // WebSocket upgrade is requested in h1 frontend for convenience.
214   ConnectProto connect_proto;
215   // Returns true if the request is HTTP upgrade (HTTP Upgrade or
216   // CONNECT method).  Upgrade to HTTP/2 is excluded.  For HTTP/2
217   // Upgrade, check get_http2_upgrade_request().
218   bool upgrade_request;
219   // true if h2c is seen in Upgrade header field.
220   bool http2_upgrade_seen;
221   bool connection_close;
222   // true if this is HTTP/2, and request body is expected.  Note that
223   // we don't take into account HTTP method here.
224   bool http2_expect_body;
225   // true if request does not have any information about authority.
226   // This happens when: For HTTP/2 request, :authority is missing.
227   // For HTTP/1 request, origin or asterisk form is used.
228   bool no_authority;
229   // true if backend selection is done for request once.
230   // orig_authority and orig_path have the authority and path which
231   // are used for the first backend selection.
232   bool forwarded_once;
233 };
234
235 struct Response {
236   Response(BlockAllocator &balloc)
237       : fs(balloc, 32),
238         recv_body_length(0),
239         unconsumed_body_length(0),
240         http_status(0),
241         http_major(1),
242         http_minor(1),
243         connection_close(false),
244         headers_only(false) {}
245
246   void consume(size_t len) {
247     assert(unconsumed_body_length >= len);
248     unconsumed_body_length -= len;
249   }
250
251   // returns true if a resource denoted by scheme, authority, and path
252   // has already been pushed.
253   bool is_resource_pushed(const StringRef &scheme, const StringRef &authority,
254                           const StringRef &path) const {
255     if (!pushed_resources) {
256       return false;
257     }
258     return std::find(std::begin(*pushed_resources), std::end(*pushed_resources),
259                      std::make_tuple(scheme, authority, path)) !=
260            std::end(*pushed_resources);
261   }
262
263   // remember that a resource denoted by scheme, authority, and path
264   // is pushed.
265   void resource_pushed(const StringRef &scheme, const StringRef &authority,
266                        const StringRef &path) {
267     if (!pushed_resources) {
268       pushed_resources = std::make_unique<
269           std::vector<std::tuple<StringRef, StringRef, StringRef>>>();
270     }
271     pushed_resources->emplace_back(scheme, authority, path);
272   }
273
274   FieldStore fs;
275   // array of the tuple of scheme, authority, and path of pushed
276   // resource.  This is required because RFC 8297 says that server
277   // typically includes header fields appeared in non-final response
278   // header fields in final response header fields.  Without checking
279   // that a particular resource has already been pushed, or not, we
280   // end up pushing the same resource at least twice.  It is unknown
281   // that we should use more complex data structure (e.g., std::set)
282   // to find the resources faster.
283   std::unique_ptr<std::vector<std::tuple<StringRef, StringRef, StringRef>>>
284       pushed_resources;
285   // the length of response body received so far
286   int64_t recv_body_length;
287   // The number of bytes not consumed by the application yet.  This is
288   // mainly for HTTP/2 backend.
289   size_t unconsumed_body_length;
290   // HTTP status code
291   unsigned int http_status;
292   int http_major, http_minor;
293   bool connection_close;
294   // true if response only consists of HEADERS, and it bears
295   // END_STREAM.  This is used to tell Http2Upstream that it can send
296   // response with single HEADERS with END_STREAM flag only.
297   bool headers_only;
298 };
299
300 enum class DownstreamState {
301   INITIAL,
302   HEADER_COMPLETE,
303   MSG_COMPLETE,
304   STREAM_CLOSED,
305   CONNECT_FAIL,
306   MSG_RESET,
307   // header contains invalid header field.  We can safely send error
308   // response (502) to a client.
309   MSG_BAD_HEADER,
310   // header fields in HTTP/1 request exceed the configuration limit.
311   // This state is only transitioned from INITIAL state, and solely
312   // used to signal 431 status code to the client.
313   HTTP1_REQUEST_HEADER_TOO_LARGE,
314 };
315
316 enum class DispatchState {
317   NONE,
318   PENDING,
319   BLOCKED,
320   ACTIVE,
321   FAILURE,
322 };
323
324 class Downstream {
325 public:
326   Downstream(Upstream *upstream, MemchunkPool *mcpool, int64_t stream_id);
327   ~Downstream();
328   void reset_upstream(Upstream *upstream);
329   Upstream *get_upstream() const;
330   void set_stream_id(int64_t stream_id);
331   int64_t get_stream_id() const;
332   void set_assoc_stream_id(int64_t stream_id);
333   int64_t get_assoc_stream_id() const;
334   void pause_read(IOCtrlReason reason);
335   int resume_read(IOCtrlReason reason, size_t consumed);
336   void force_resume_read();
337   // Set stream ID for downstream HTTP2 connection.
338   void set_downstream_stream_id(int64_t stream_id);
339   int64_t get_downstream_stream_id() const;
340
341   int attach_downstream_connection(std::unique_ptr<DownstreamConnection> dconn);
342   void detach_downstream_connection();
343   DownstreamConnection *get_downstream_connection();
344   // Returns dconn_ and nullifies dconn_.
345   std::unique_ptr<DownstreamConnection> pop_downstream_connection();
346
347   // Returns true if output buffer is full. If underlying dconn_ is
348   // NULL, this function always returns false.
349   bool request_buf_full();
350   // Returns true if upgrade (HTTP Upgrade or CONNECT) is succeeded in
351   // h1 backend.  This should not depend on inspect_http1_response().
352   void check_upgrade_fulfilled_http1();
353   // Returns true if upgrade (HTTP Upgrade or CONNECT) is succeeded in
354   // h2 backend.
355   void check_upgrade_fulfilled_http2();
356   // Returns true if the upgrade is succeeded as a result of the call
357   // check_upgrade_fulfilled_http*().  HTTP/2 Upgrade is excluded.
358   bool get_upgraded() const;
359   // Inspects HTTP/2 request.
360   void inspect_http2_request();
361   // Inspects HTTP/1 request.  This checks whether the request is
362   // upgrade request and tranfer-encoding etc.
363   void inspect_http1_request();
364   // Returns true if the request is HTTP Upgrade for HTTP/2
365   bool get_http2_upgrade_request() const;
366   // Returns the value of HTTP2-Settings request header field.
367   StringRef get_http2_settings() const;
368
369   // downstream request API
370   const Request &request() const { return req_; }
371   Request &request() { return req_; }
372
373   // Count number of crumbled cookies
374   size_t count_crumble_request_cookie();
375   // Crumbles (split cookie by ";") in request_headers_ and adds them
376   // in |nva|.  Headers::no_index is inherited.
377   void crumble_request_cookie(std::vector<nghttp2_nv> &nva);
378   // Assembles request cookies.  The opposite operation against
379   // crumble_request_cookie().
380   StringRef assemble_request_cookie();
381
382   void
383   set_request_start_time(std::chrono::high_resolution_clock::time_point time);
384   const std::chrono::high_resolution_clock::time_point &
385   get_request_start_time() const;
386   int push_request_headers();
387   bool get_chunked_request() const;
388   void set_chunked_request(bool f);
389   int push_upload_data_chunk(const uint8_t *data, size_t datalen);
390   int end_upload_data();
391   // Validates that received request body length and content-length
392   // matches.
393   bool validate_request_recv_body_length() const;
394   void set_request_downstream_host(const StringRef &host);
395   bool expect_response_body() const;
396   bool expect_response_trailer() const;
397   void set_request_state(DownstreamState state);
398   DownstreamState get_request_state() const;
399   DefaultMemchunks *get_request_buf();
400   void set_request_pending(bool f);
401   bool get_request_pending() const;
402   void set_request_header_sent(bool f);
403   bool get_request_header_sent() const;
404   // Returns true if request is ready to be submitted to downstream.
405   // When sending pending request, get_request_pending() should be
406   // checked too because this function may return true when
407   // get_request_pending() returns false.
408   bool request_submission_ready() const;
409
410   DefaultMemchunks *get_blocked_request_buf();
411   bool get_blocked_request_data_eof() const;
412   void set_blocked_request_data_eof(bool f);
413
414   // downstream response API
415   const Response &response() const { return resp_; }
416   Response &response() { return resp_; }
417
418   // Rewrites the location response header field.
419   void rewrite_location_response_header(const StringRef &upstream_scheme);
420
421   bool get_chunked_response() const;
422   void set_chunked_response(bool f);
423
424   void set_response_state(DownstreamState state);
425   DownstreamState get_response_state() const;
426   DefaultMemchunks *get_response_buf();
427   bool response_buf_full();
428   // Validates that received response body length and content-length
429   // matches.
430   bool validate_response_recv_body_length() const;
431   uint32_t get_response_rst_stream_error_code() const;
432   void set_response_rst_stream_error_code(uint32_t error_code);
433   // Inspects HTTP/1 response.  This checks tranfer-encoding etc.
434   void inspect_http1_response();
435   // Clears some of member variables for response.
436   void reset_response();
437   // True if the response is non-final (1xx status code).  Note that
438   // if connection was upgraded, 101 status code is treated as final.
439   bool get_non_final_response() const;
440   // True if protocol version used by client supports non final
441   // response.  Only HTTP/1.1 and HTTP/2 clients support it.
442   bool supports_non_final_response() const;
443   void set_expect_final_response(bool f);
444   bool get_expect_final_response() const;
445
446   // Call this method when there is incoming data in downstream
447   // connection.
448   int on_read();
449
450   // Resets upstream read timer.  If it is active, timeout value is
451   // reset.  If it is not active, timer will be started.
452   void reset_upstream_rtimer();
453   // Resets upstream write timer. If it is active, timeout value is
454   // reset.  If it is not active, timer will be started.  This
455   // function also resets read timer if it has been started.
456   void reset_upstream_wtimer();
457   // Makes sure that upstream write timer is started.  If it has been
458   // started, do nothing.  Otherwise, write timer will be started.
459   void ensure_upstream_wtimer();
460   // Disables upstream read timer.
461   void disable_upstream_rtimer();
462   // Disables upstream write timer.
463   void disable_upstream_wtimer();
464
465   // Downstream timer functions.  They works in a similar way just
466   // like the upstream timer function.
467   void reset_downstream_rtimer();
468   void reset_downstream_wtimer();
469   void ensure_downstream_wtimer();
470   void disable_downstream_rtimer();
471   void disable_downstream_wtimer();
472
473   // Returns true if accesslog can be written for this downstream.
474   bool accesslog_ready() const;
475
476   // Increment retry count
477   void add_retry();
478   // true if retry attempt should not be done.
479   bool no_more_retry() const;
480
481   DispatchState get_dispatch_state() const;
482   void set_dispatch_state(DispatchState s);
483
484   void attach_blocked_link(BlockedLink *l);
485   BlockedLink *detach_blocked_link();
486
487   // Returns true if downstream_connection can be detached and reused.
488   bool can_detach_downstream_connection() const;
489
490   DefaultMemchunks pop_response_buf();
491
492   BlockAllocator &get_block_allocator();
493
494   void add_rcbuf(nghttp2_rcbuf *rcbuf);
495 #ifdef ENABLE_HTTP3
496   void add_rcbuf(nghttp3_rcbuf *rcbuf);
497 #endif // ENABLE_HTTP3
498
499   void
500   set_downstream_addr_group(const std::shared_ptr<DownstreamAddrGroup> &group);
501   void set_addr(const DownstreamAddr *addr);
502
503   const DownstreamAddr *get_addr() const;
504
505   void set_accesslog_written(bool f);
506
507   // Finds affinity cookie from request header fields.  The name of
508   // cookie is given in |name|.  If an affinity cookie is found, it is
509   // assigned to a member function, and is returned.  If it is not
510   // found, or is malformed, returns 0.
511   uint32_t find_affinity_cookie(const StringRef &name);
512   // Set |h| as affinity cookie.
513   void renew_affinity_cookie(uint32_t h);
514   // Returns affinity cookie to send.  If it does not need to be sent,
515   // for example, because the value is retrieved from a request header
516   // field, returns 0.
517   uint32_t get_affinity_cookie_to_send() const;
518
519   void set_ws_key(const StringRef &key);
520
521   bool get_expect_100_continue() const;
522
523   bool get_stop_reading() const;
524   void set_stop_reading(bool f);
525
526   enum {
527     EVENT_ERROR = 0x1,
528     EVENT_TIMEOUT = 0x2,
529   };
530
531   Downstream *dlnext, *dlprev;
532
533   // the length of response body sent to upstream client
534   int64_t response_sent_body_length;
535
536 private:
537   BlockAllocator balloc_;
538
539   std::vector<nghttp2_rcbuf *> rcbufs_;
540 #ifdef ENABLE_HTTP3
541   std::vector<nghttp3_rcbuf *> rcbufs3_;
542 #endif // ENABLE_HTTP3
543
544   Request req_;
545   Response resp_;
546
547   std::chrono::high_resolution_clock::time_point request_start_time_;
548
549   // host we requested to downstream.  This is used to rewrite
550   // location header field to decide the location should be rewritten
551   // or not.
552   StringRef request_downstream_host_;
553
554   // Data arrived in frontend before sending header fields to backend
555   // are stored in this buffer.
556   DefaultMemchunks blocked_request_buf_;
557   DefaultMemchunks request_buf_;
558   DefaultMemchunks response_buf_;
559
560   // The Sec-WebSocket-Key field sent to the peer.  This field is used
561   // if frontend uses RFC 8441 WebSocket bootstrapping via HTTP/2.
562   StringRef ws_key_;
563
564   ev_timer upstream_rtimer_;
565   ev_timer upstream_wtimer_;
566
567   ev_timer downstream_rtimer_;
568   ev_timer downstream_wtimer_;
569
570   Upstream *upstream_;
571   std::unique_ptr<DownstreamConnection> dconn_;
572
573   // only used by HTTP/2 upstream
574   BlockedLink *blocked_link_;
575   // The backend address used to fulfill this request.  These are for
576   // logging purpose.
577   std::shared_ptr<DownstreamAddrGroup> group_;
578   const DownstreamAddr *addr_;
579   // How many times we tried in backend connection
580   size_t num_retry_;
581   // The stream ID in frontend connection
582   int64_t stream_id_;
583   // The associated stream ID in frontend connection if this is pushed
584   // stream.
585   int64_t assoc_stream_id_;
586   // stream ID in backend connection
587   int64_t downstream_stream_id_;
588   // RST_STREAM error_code from downstream HTTP2 connection
589   uint32_t response_rst_stream_error_code_;
590   // An affinity cookie value.
591   uint32_t affinity_cookie_;
592   // request state
593   DownstreamState request_state_;
594   // response state
595   DownstreamState response_state_;
596   // only used by HTTP/2 upstream
597   DispatchState dispatch_state_;
598   // true if the connection is upgraded (HTTP Upgrade or CONNECT),
599   // excluding upgrade to HTTP/2.
600   bool upgraded_;
601   // true if backend request uses chunked transfer-encoding
602   bool chunked_request_;
603   // true if response to client uses chunked transfer-encoding
604   bool chunked_response_;
605   // true if we have not got final response code
606   bool expect_final_response_;
607   // true if downstream request is pending because backend connection
608   // has not been established or should be checked before use;
609   // currently used only with HTTP/2 connection.
610   bool request_pending_;
611   // true if downstream request header is considered to be sent.
612   bool request_header_sent_;
613   // true if access.log has been written.
614   bool accesslog_written_;
615   // true if affinity cookie is generated for this request.
616   bool new_affinity_cookie_;
617   // true if eof is received from client before sending header fields
618   // to backend.
619   bool blocked_request_data_eof_;
620   // true if request contains "expect: 100-continue" header field.
621   bool expect_100_continue_;
622   bool stop_reading_;
623 };
624
625 } // namespace shrpx
626
627 #endif // SHRPX_DOWNSTREAM_H