2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
25 #ifndef SHRPX_CLIENT_HANDLER_H
26 #define SHRPX_CLIENT_HANDLER_H
34 #include <openssl/ssl.h>
36 #include "shrpx_rate_limit.h"
37 #include "shrpx_connection.h"
40 #include "allocator.h"
42 using namespace nghttp2;
47 class DownstreamConnection;
50 class DownstreamConnectionPool;
54 struct DownstreamAddrGroup;
55 struct DownstreamAddr;
59 ClientHandler(Worker *worker, int fd, SSL *ssl, const StringRef &ipaddr,
60 const StringRef &port, int family, const UpstreamAddr *faddr);
64 // Performs clear text I/O
67 // Performs TLS handshake
75 int upstream_http2_connhd_read();
76 int upstream_http1_connhd_read();
79 int proxy_protocol_read();
80 int proxy_protocol_v2_read();
81 int on_proxy_protocol_finish();
83 // Performs I/O operation. Internally calls on_read()/on_write().
87 // Processes buffers. No underlying I/O operation will be done.
91 struct ev_loop *get_loop() const;
92 void reset_upstream_read_timeout(ev_tstamp t);
93 void reset_upstream_write_timeout(ev_tstamp t);
95 int validate_next_proto();
96 const StringRef &get_ipaddr() const;
97 bool get_should_close_after_write() const;
98 void set_should_close_after_write(bool f);
99 Upstream *get_upstream();
101 void pool_downstream_connection(std::unique_ptr<DownstreamConnection> dconn);
102 void remove_downstream_connection(DownstreamConnection *dconn);
103 DownstreamAddr *get_downstream_addr(int &err, DownstreamAddrGroup *group,
104 Downstream *downstream);
105 // Returns DownstreamConnection object based on request path. This
106 // function returns non-null DownstreamConnection, and assigns 0 to
107 // |err| if it succeeds, or returns nullptr, and assigns negative
108 // error code to |err|.
109 std::unique_ptr<DownstreamConnection>
110 get_downstream_connection(int &err, Downstream *downstream);
111 MemchunkPool *get_mcpool();
112 SSL *get_ssl() const;
113 // Call this function when HTTP/2 connection header is received at
114 // the start of the connection.
115 void direct_http2_upgrade();
116 // Performs HTTP/2 Upgrade from the connection managed by
117 // |http|. If this function fails, the connection must be
118 // terminated. This function returns 0 if it succeeds, or -1.
119 int perform_http2_upgrade(HttpsUpstream *http);
120 bool get_http2_upgrade_allowed() const;
121 // Returns upstream scheme, either "http" or "https"
122 StringRef get_upstream_scheme() const;
123 void start_immediate_shutdown();
125 // Writes upstream accesslog using |downstream|. The |downstream|
126 // must not be nullptr.
127 void write_accesslog(Downstream *downstream);
129 Worker *get_worker() const;
131 // Initializes forwarded_for_.
132 void init_forwarded_for(int family, const StringRef &ipaddr);
134 using ReadBuf = DefaultMemchunkBuffer;
138 RateLimit *get_rlimit();
139 RateLimit *get_wlimit();
144 void setup_upstream_io_callback();
146 // Returns string suitable for use in "by" parameter of Forwarded
148 StringRef get_forwarded_by() const;
149 // Returns string suitable for use in "for" parameter of Forwarded
151 StringRef get_forwarded_for() const;
154 get_http2_session(const std::shared_ptr<DownstreamAddrGroup> &group,
155 DownstreamAddr *addr);
157 // Returns an affinity cookie value for |downstream|. |cookie_name|
158 // is used to inspect cookie header field in request header fields.
159 uint32_t get_affinity_cookie(Downstream *downstream,
160 const StringRef &cookie_name);
162 const UpstreamAddr *get_upstream_addr() const;
164 void repeat_read_timer();
165 void stop_read_timer();
167 Connection *get_connection();
169 // Stores |sni| which is TLS SNI extension value client sent in this
171 void set_tls_sni(const StringRef &sni);
172 // Returns TLS SNI extension value client sent in this connection.
173 StringRef get_tls_sni() const;
175 // Returns ALPN negotiated in this connection.
176 StringRef get_alpn() const;
178 BlockAllocator &get_block_allocator();
181 // Allocator to allocate memory for connection-wide objects. Make
182 // sure that the allocations must be bounded, and not proportional
183 // to the number of requests.
184 BlockAllocator balloc_;
185 DefaultMemchunkBuffer rb_;
187 ev_timer reneg_shutdown_timer_;
188 std::unique_ptr<Upstream> upstream_;
189 // IP address of client. If UNIX domain socket is used, this is
193 // The ALPN identifier negotiated for this connection.
195 // The client address used in "for" parameter of Forwarded header
197 StringRef forwarded_for_;
198 // lowercased TLS SNI which client sent.
200 std::function<int(ClientHandler &)> read_, write_;
201 std::function<int(ClientHandler &)> on_read_, on_write_;
202 // Address of frontend listening socket
203 const UpstreamAddr *faddr_;
205 // The number of bytes of HTTP/2 client connection header to read
206 size_t left_connhd_len_;
207 // hash for session affinity using client IP
208 uint32_t affinity_hash_;
209 bool should_close_after_write_;
210 // true if affinity_hash_ is computed
211 bool affinity_hash_computed_;
216 #endif // SHRPX_CLIENT_HANDLER_H