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_HTTP_DOWNSTREAM_CONNECTION_H
26 #define SHRPX_HTTP_DOWNSTREAM_CONNECTION_H
27 
28 #include "shrpx.h"
29 
30 #include "llhttp.h"
31 
32 #include "shrpx_downstream_connection.h"
33 #include "shrpx_io_control.h"
34 #include "shrpx_connection.h"
35 
36 namespace shrpx {
37 
38 class DownstreamConnectionPool;
39 class Worker;
40 struct DownstreamAddrGroup;
41 struct DownstreamAddr;
42 struct DNSQuery;
43 
44 class HttpDownstreamConnection : public DownstreamConnection {
45 public:
46   HttpDownstreamConnection(const std::shared_ptr<DownstreamAddrGroup> &group,
47                            DownstreamAddr *addr, struct ev_loop *loop,
48                            Worker *worker);
49   virtual ~HttpDownstreamConnection();
50   virtual int attach_downstream(Downstream *downstream);
51   virtual void detach_downstream(Downstream *downstream);
52 
53   virtual int push_request_headers();
54   virtual int push_upload_data_chunk(const uint8_t *data, size_t datalen);
55   virtual int end_upload_data();
56   void end_upload_data_chunk();
57 
58   virtual void pause_read(IOCtrlReason reason);
59   virtual int resume_read(IOCtrlReason reason, size_t consumed);
60   virtual void force_resume_read();
61 
62   virtual int on_read();
63   virtual int on_write();
64 
65   virtual void on_upstream_change(Upstream *upstream);
66 
67   virtual bool poolable() const;
68 
69   virtual const std::shared_ptr<DownstreamAddrGroup> &
70   get_downstream_addr_group() const;
71   virtual DownstreamAddr *get_addr() const;
72 
73   int initiate_connection();
74 
75   int write_first();
76   int read_clear();
77   int write_clear();
78   int read_tls();
79   int write_tls();
80 
81   int process_input(const uint8_t *data, size_t datalen);
82   int tls_handshake();
83 
84   int connected();
85   void signal_write();
86   int actual_signal_write();
87 
88   // Returns address used to connect to backend.  Could be nullptr.
89   const Address *get_raddr() const;
90 
91   int noop();
92 
93   int process_blocked_request_buf();
94 
95 private:
96   Connection conn_;
97   std::function<int(HttpDownstreamConnection &)> on_read_, on_write_,
98       signal_write_;
99   Worker *worker_;
100   // nullptr if TLS is not used.
101   SSL_CTX *ssl_ctx_;
102   std::shared_ptr<DownstreamAddrGroup> group_;
103   // Address of remote endpoint
104   DownstreamAddr *addr_;
105   // Actual remote address used to contact backend.  This is initially
106   // nullptr, and may point to either &addr_->addr, or
107   // resolved_addr_.get().
108   const Address *raddr_;
109   // Resolved IP address if dns parameter is used
110   std::unique_ptr<Address> resolved_addr_;
111   std::unique_ptr<DNSQuery> dns_query_;
112   IOControl ioctrl_;
113   llhttp_t response_htp_;
114   // true if first write succeeded.
115   bool first_write_done_;
116   // true if this object can be reused
117   bool reusable_;
118   // true if request header is written to request buffer.
119   bool request_header_written_;
120 };
121 
122 } // namespace shrpx
123 
124 #endif // SHRPX_HTTP_DOWNSTREAM_CONNECTION_H
125