12c593315Sopenharmony_ci/* 22c593315Sopenharmony_ci * nghttp2 - HTTP/2 C Library 32c593315Sopenharmony_ci * 42c593315Sopenharmony_ci * Copyright (c) 2012 Tatsuhiro Tsujikawa 52c593315Sopenharmony_ci * 62c593315Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 72c593315Sopenharmony_ci * a copy of this software and associated documentation files (the 82c593315Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 92c593315Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 102c593315Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 112c593315Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 122c593315Sopenharmony_ci * the following conditions: 132c593315Sopenharmony_ci * 142c593315Sopenharmony_ci * The above copyright notice and this permission notice shall be 152c593315Sopenharmony_ci * included in all copies or substantial portions of the Software. 162c593315Sopenharmony_ci * 172c593315Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 182c593315Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 192c593315Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 202c593315Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 212c593315Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 222c593315Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 232c593315Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 242c593315Sopenharmony_ci */ 252c593315Sopenharmony_ci#ifndef SHRPX_CLIENT_HANDLER_H 262c593315Sopenharmony_ci#define SHRPX_CLIENT_HANDLER_H 272c593315Sopenharmony_ci 282c593315Sopenharmony_ci#include "shrpx.h" 292c593315Sopenharmony_ci 302c593315Sopenharmony_ci#include <memory> 312c593315Sopenharmony_ci 322c593315Sopenharmony_ci#include <ev.h> 332c593315Sopenharmony_ci 342c593315Sopenharmony_ci#include <openssl/ssl.h> 352c593315Sopenharmony_ci 362c593315Sopenharmony_ci#include "shrpx_rate_limit.h" 372c593315Sopenharmony_ci#include "shrpx_connection.h" 382c593315Sopenharmony_ci#include "buffer.h" 392c593315Sopenharmony_ci#include "memchunk.h" 402c593315Sopenharmony_ci#include "allocator.h" 412c593315Sopenharmony_ci 422c593315Sopenharmony_ciusing namespace nghttp2; 432c593315Sopenharmony_ci 442c593315Sopenharmony_cinamespace shrpx { 452c593315Sopenharmony_ci 462c593315Sopenharmony_ciclass Upstream; 472c593315Sopenharmony_ciclass DownstreamConnection; 482c593315Sopenharmony_ciclass HttpsUpstream; 492c593315Sopenharmony_ciclass ConnectBlocker; 502c593315Sopenharmony_ciclass DownstreamConnectionPool; 512c593315Sopenharmony_ciclass Worker; 522c593315Sopenharmony_ciclass Downstream; 532c593315Sopenharmony_cistruct WorkerStat; 542c593315Sopenharmony_cistruct DownstreamAddrGroup; 552c593315Sopenharmony_cistruct SharedDownstreamAddr; 562c593315Sopenharmony_cistruct DownstreamAddr; 572c593315Sopenharmony_ci#ifdef ENABLE_HTTP3 582c593315Sopenharmony_ciclass Http3Upstream; 592c593315Sopenharmony_ci#endif // ENABLE_HTTP3 602c593315Sopenharmony_ci 612c593315Sopenharmony_ciclass ClientHandler { 622c593315Sopenharmony_cipublic: 632c593315Sopenharmony_ci ClientHandler(Worker *worker, int fd, SSL *ssl, const StringRef &ipaddr, 642c593315Sopenharmony_ci const StringRef &port, int family, const UpstreamAddr *faddr); 652c593315Sopenharmony_ci ~ClientHandler(); 662c593315Sopenharmony_ci 672c593315Sopenharmony_ci int noop(); 682c593315Sopenharmony_ci // Performs clear text I/O 692c593315Sopenharmony_ci int read_clear(); 702c593315Sopenharmony_ci int write_clear(); 712c593315Sopenharmony_ci // Specialized for PROXY-protocol use; peek data from socket. 722c593315Sopenharmony_ci int proxy_protocol_peek_clear(); 732c593315Sopenharmony_ci // Performs TLS handshake 742c593315Sopenharmony_ci int tls_handshake(); 752c593315Sopenharmony_ci // Performs TLS I/O 762c593315Sopenharmony_ci int read_tls(); 772c593315Sopenharmony_ci int write_tls(); 782c593315Sopenharmony_ci 792c593315Sopenharmony_ci int upstream_noop(); 802c593315Sopenharmony_ci int upstream_read(); 812c593315Sopenharmony_ci int upstream_http2_connhd_read(); 822c593315Sopenharmony_ci int upstream_http1_connhd_read(); 832c593315Sopenharmony_ci int upstream_write(); 842c593315Sopenharmony_ci 852c593315Sopenharmony_ci int proxy_protocol_read(); 862c593315Sopenharmony_ci int proxy_protocol_v2_read(); 872c593315Sopenharmony_ci int on_proxy_protocol_finish(); 882c593315Sopenharmony_ci 892c593315Sopenharmony_ci // Performs I/O operation. Internally calls on_read()/on_write(). 902c593315Sopenharmony_ci int do_read(); 912c593315Sopenharmony_ci int do_write(); 922c593315Sopenharmony_ci 932c593315Sopenharmony_ci // Processes buffers. No underlying I/O operation will be done. 942c593315Sopenharmony_ci int on_read(); 952c593315Sopenharmony_ci int on_write(); 962c593315Sopenharmony_ci 972c593315Sopenharmony_ci struct ev_loop *get_loop() const; 982c593315Sopenharmony_ci void reset_upstream_read_timeout(ev_tstamp t); 992c593315Sopenharmony_ci void reset_upstream_write_timeout(ev_tstamp t); 1002c593315Sopenharmony_ci 1012c593315Sopenharmony_ci int validate_next_proto(); 1022c593315Sopenharmony_ci const StringRef &get_ipaddr() const; 1032c593315Sopenharmony_ci bool get_should_close_after_write() const; 1042c593315Sopenharmony_ci void set_should_close_after_write(bool f); 1052c593315Sopenharmony_ci Upstream *get_upstream(); 1062c593315Sopenharmony_ci 1072c593315Sopenharmony_ci void pool_downstream_connection(std::unique_ptr<DownstreamConnection> dconn); 1082c593315Sopenharmony_ci void remove_downstream_connection(DownstreamConnection *dconn); 1092c593315Sopenharmony_ci DownstreamAddr *get_downstream_addr(int &err, DownstreamAddrGroup *group, 1102c593315Sopenharmony_ci Downstream *downstream); 1112c593315Sopenharmony_ci // Returns DownstreamConnection object based on request path. This 1122c593315Sopenharmony_ci // function returns non-null DownstreamConnection, and assigns 0 to 1132c593315Sopenharmony_ci // |err| if it succeeds, or returns nullptr, and assigns negative 1142c593315Sopenharmony_ci // error code to |err|. 1152c593315Sopenharmony_ci std::unique_ptr<DownstreamConnection> 1162c593315Sopenharmony_ci get_downstream_connection(int &err, Downstream *downstream); 1172c593315Sopenharmony_ci MemchunkPool *get_mcpool(); 1182c593315Sopenharmony_ci SSL *get_ssl() const; 1192c593315Sopenharmony_ci // Call this function when HTTP/2 connection header is received at 1202c593315Sopenharmony_ci // the start of the connection. 1212c593315Sopenharmony_ci void direct_http2_upgrade(); 1222c593315Sopenharmony_ci // Performs HTTP/2 Upgrade from the connection managed by 1232c593315Sopenharmony_ci // |http|. If this function fails, the connection must be 1242c593315Sopenharmony_ci // terminated. This function returns 0 if it succeeds, or -1. 1252c593315Sopenharmony_ci int perform_http2_upgrade(HttpsUpstream *http); 1262c593315Sopenharmony_ci bool get_http2_upgrade_allowed() const; 1272c593315Sopenharmony_ci // Returns upstream scheme, either "http" or "https" 1282c593315Sopenharmony_ci StringRef get_upstream_scheme() const; 1292c593315Sopenharmony_ci void start_immediate_shutdown(); 1302c593315Sopenharmony_ci 1312c593315Sopenharmony_ci // Writes upstream accesslog using |downstream|. The |downstream| 1322c593315Sopenharmony_ci // must not be nullptr. 1332c593315Sopenharmony_ci void write_accesslog(Downstream *downstream); 1342c593315Sopenharmony_ci 1352c593315Sopenharmony_ci Worker *get_worker() const; 1362c593315Sopenharmony_ci 1372c593315Sopenharmony_ci // Initializes forwarded_for_. 1382c593315Sopenharmony_ci void init_forwarded_for(int family, const StringRef &ipaddr); 1392c593315Sopenharmony_ci 1402c593315Sopenharmony_ci using ReadBuf = DefaultMemchunkBuffer; 1412c593315Sopenharmony_ci 1422c593315Sopenharmony_ci ReadBuf *get_rb(); 1432c593315Sopenharmony_ci 1442c593315Sopenharmony_ci RateLimit *get_rlimit(); 1452c593315Sopenharmony_ci RateLimit *get_wlimit(); 1462c593315Sopenharmony_ci 1472c593315Sopenharmony_ci void signal_write(); 1482c593315Sopenharmony_ci ev_io *get_wev(); 1492c593315Sopenharmony_ci 1502c593315Sopenharmony_ci void setup_upstream_io_callback(); 1512c593315Sopenharmony_ci 1522c593315Sopenharmony_ci#ifdef ENABLE_HTTP3 1532c593315Sopenharmony_ci void setup_http3_upstream(std::unique_ptr<Http3Upstream> &&upstream); 1542c593315Sopenharmony_ci int read_quic(const UpstreamAddr *faddr, const Address &remote_addr, 1552c593315Sopenharmony_ci const Address &local_addr, const ngtcp2_pkt_info &pi, 1562c593315Sopenharmony_ci const uint8_t *data, size_t datalen); 1572c593315Sopenharmony_ci int write_quic(); 1582c593315Sopenharmony_ci#endif // ENABLE_HTTP3 1592c593315Sopenharmony_ci 1602c593315Sopenharmony_ci // Returns string suitable for use in "by" parameter of Forwarded 1612c593315Sopenharmony_ci // header field. 1622c593315Sopenharmony_ci StringRef get_forwarded_by() const; 1632c593315Sopenharmony_ci // Returns string suitable for use in "for" parameter of Forwarded 1642c593315Sopenharmony_ci // header field. 1652c593315Sopenharmony_ci StringRef get_forwarded_for() const; 1662c593315Sopenharmony_ci 1672c593315Sopenharmony_ci Http2Session * 1682c593315Sopenharmony_ci get_http2_session(const std::shared_ptr<DownstreamAddrGroup> &group, 1692c593315Sopenharmony_ci DownstreamAddr *addr); 1702c593315Sopenharmony_ci 1712c593315Sopenharmony_ci // Returns an affinity cookie value for |downstream|. |cookie_name| 1722c593315Sopenharmony_ci // is used to inspect cookie header field in request header fields. 1732c593315Sopenharmony_ci uint32_t get_affinity_cookie(Downstream *downstream, 1742c593315Sopenharmony_ci const StringRef &cookie_name); 1752c593315Sopenharmony_ci 1762c593315Sopenharmony_ci DownstreamAddr *get_downstream_addr_strict_affinity( 1772c593315Sopenharmony_ci int &err, const std::shared_ptr<SharedDownstreamAddr> &shared_addr, 1782c593315Sopenharmony_ci Downstream *downstream); 1792c593315Sopenharmony_ci 1802c593315Sopenharmony_ci const UpstreamAddr *get_upstream_addr() const; 1812c593315Sopenharmony_ci 1822c593315Sopenharmony_ci void repeat_read_timer(); 1832c593315Sopenharmony_ci void stop_read_timer(); 1842c593315Sopenharmony_ci 1852c593315Sopenharmony_ci Connection *get_connection(); 1862c593315Sopenharmony_ci 1872c593315Sopenharmony_ci // Stores |sni| which is TLS SNI extension value client sent in this 1882c593315Sopenharmony_ci // connection. 1892c593315Sopenharmony_ci void set_tls_sni(const StringRef &sni); 1902c593315Sopenharmony_ci // Returns TLS SNI extension value client sent in this connection. 1912c593315Sopenharmony_ci StringRef get_tls_sni() const; 1922c593315Sopenharmony_ci 1932c593315Sopenharmony_ci // Returns ALPN negotiated in this connection. 1942c593315Sopenharmony_ci StringRef get_alpn() const; 1952c593315Sopenharmony_ci 1962c593315Sopenharmony_ci BlockAllocator &get_block_allocator(); 1972c593315Sopenharmony_ci 1982c593315Sopenharmony_ci void set_alpn_from_conn(); 1992c593315Sopenharmony_ci 2002c593315Sopenharmony_ciprivate: 2012c593315Sopenharmony_ci // Allocator to allocate memory for connection-wide objects. Make 2022c593315Sopenharmony_ci // sure that the allocations must be bounded, and not proportional 2032c593315Sopenharmony_ci // to the number of requests. 2042c593315Sopenharmony_ci BlockAllocator balloc_; 2052c593315Sopenharmony_ci DefaultMemchunkBuffer rb_; 2062c593315Sopenharmony_ci Connection conn_; 2072c593315Sopenharmony_ci ev_timer reneg_shutdown_timer_; 2082c593315Sopenharmony_ci std::unique_ptr<Upstream> upstream_; 2092c593315Sopenharmony_ci // IP address of client. If UNIX domain socket is used, this is 2102c593315Sopenharmony_ci // "localhost". 2112c593315Sopenharmony_ci StringRef ipaddr_; 2122c593315Sopenharmony_ci StringRef port_; 2132c593315Sopenharmony_ci // The ALPN identifier negotiated for this connection. 2142c593315Sopenharmony_ci StringRef alpn_; 2152c593315Sopenharmony_ci // The client address used in "for" parameter of Forwarded header 2162c593315Sopenharmony_ci // field. 2172c593315Sopenharmony_ci StringRef forwarded_for_; 2182c593315Sopenharmony_ci // lowercased TLS SNI which client sent. 2192c593315Sopenharmony_ci StringRef sni_; 2202c593315Sopenharmony_ci std::function<int(ClientHandler &)> read_, write_; 2212c593315Sopenharmony_ci std::function<int(ClientHandler &)> on_read_, on_write_; 2222c593315Sopenharmony_ci // Address of frontend listening socket 2232c593315Sopenharmony_ci const UpstreamAddr *faddr_; 2242c593315Sopenharmony_ci Worker *worker_; 2252c593315Sopenharmony_ci // The number of bytes of HTTP/2 client connection header to read 2262c593315Sopenharmony_ci size_t left_connhd_len_; 2272c593315Sopenharmony_ci // hash for session affinity using client IP 2282c593315Sopenharmony_ci uint32_t affinity_hash_; 2292c593315Sopenharmony_ci bool should_close_after_write_; 2302c593315Sopenharmony_ci // true if affinity_hash_ is computed 2312c593315Sopenharmony_ci bool affinity_hash_computed_; 2322c593315Sopenharmony_ci}; 2332c593315Sopenharmony_ci 2342c593315Sopenharmony_ci} // namespace shrpx 2352c593315Sopenharmony_ci 2362c593315Sopenharmony_ci#endif // SHRPX_CLIENT_HANDLER_H 237