Apply PIE to nghttpx
[platform/upstream/nghttp2.git] / src / shrpx_client_handler.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_CLIENT_HANDLER_H
26 #define SHRPX_CLIENT_HANDLER_H
27
28 #include "shrpx.h"
29
30 #include <memory>
31
32 #include <ev.h>
33
34 #include <openssl/ssl.h>
35
36 #include "shrpx_rate_limit.h"
37 #include "shrpx_connection.h"
38 #include "buffer.h"
39 #include "memchunk.h"
40 #include "allocator.h"
41
42 using namespace nghttp2;
43
44 namespace shrpx {
45
46 class Upstream;
47 class DownstreamConnection;
48 class HttpsUpstream;
49 class ConnectBlocker;
50 class DownstreamConnectionPool;
51 class Worker;
52 class Downstream;
53 struct WorkerStat;
54 struct DownstreamAddrGroup;
55 struct DownstreamAddr;
56
57 class ClientHandler {
58 public:
59   ClientHandler(Worker *worker, int fd, SSL *ssl, const StringRef &ipaddr,
60                 const StringRef &port, int family, const UpstreamAddr *faddr);
61   ~ClientHandler();
62
63   int noop();
64   // Performs clear text I/O
65   int read_clear();
66   int write_clear();
67   // Performs TLS handshake
68   int tls_handshake();
69   // Performs TLS I/O
70   int read_tls();
71   int write_tls();
72
73   int upstream_noop();
74   int upstream_read();
75   int upstream_http2_connhd_read();
76   int upstream_http1_connhd_read();
77   int upstream_write();
78
79   int proxy_protocol_read();
80   int proxy_protocol_v2_read();
81   int on_proxy_protocol_finish();
82
83   // Performs I/O operation.  Internally calls on_read()/on_write().
84   int do_read();
85   int do_write();
86
87   // Processes buffers.  No underlying I/O operation will be done.
88   int on_read();
89   int on_write();
90
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);
94
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();
100
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();
124
125   // Writes upstream accesslog using |downstream|.  The |downstream|
126   // must not be nullptr.
127   void write_accesslog(Downstream *downstream);
128
129   Worker *get_worker() const;
130
131   // Initializes forwarded_for_.
132   void init_forwarded_for(int family, const StringRef &ipaddr);
133
134   using ReadBuf = DefaultMemchunkBuffer;
135
136   ReadBuf *get_rb();
137
138   RateLimit *get_rlimit();
139   RateLimit *get_wlimit();
140
141   void signal_write();
142   ev_io *get_wev();
143
144   void setup_upstream_io_callback();
145
146   // Returns string suitable for use in "by" parameter of Forwarded
147   // header field.
148   StringRef get_forwarded_by() const;
149   // Returns string suitable for use in "for" parameter of Forwarded
150   // header field.
151   StringRef get_forwarded_for() const;
152
153   Http2Session *
154   get_http2_session(const std::shared_ptr<DownstreamAddrGroup> &group,
155                     DownstreamAddr *addr);
156
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);
161
162   const UpstreamAddr *get_upstream_addr() const;
163
164   void repeat_read_timer();
165   void stop_read_timer();
166
167   Connection *get_connection();
168
169   // Stores |sni| which is TLS SNI extension value client sent in this
170   // connection.
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;
174
175   // Returns ALPN negotiated in this connection.
176   StringRef get_alpn() const;
177
178   BlockAllocator &get_block_allocator();
179
180 private:
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_;
186   Connection conn_;
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
190   // "localhost".
191   StringRef ipaddr_;
192   StringRef port_;
193   // The ALPN identifier negotiated for this connection.
194   StringRef alpn_;
195   // The client address used in "for" parameter of Forwarded header
196   // field.
197   StringRef forwarded_for_;
198   // lowercased TLS SNI which client sent.
199   StringRef sni_;
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_;
204   Worker *worker_;
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_;
212 };
213
214 } // namespace shrpx
215
216 #endif // SHRPX_CLIENT_HANDLER_H