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_HTTP2_UPSTREAM_H
26 #define SHRPX_HTTP2_UPSTREAM_H
27 
28 #include "shrpx.h"
29 
30 #include <memory>
31 
32 #include <ev.h>
33 
34 #include <nghttp2/nghttp2.h>
35 
36 #include "shrpx_upstream.h"
37 #include "shrpx_downstream_queue.h"
38 #include "memchunk.h"
39 #include "buffer.h"
40 
41 using namespace nghttp2;
42 
43 namespace shrpx {
44 
45 class ClientHandler;
46 class HttpsUpstream;
47 
48 class Http2Upstream : public Upstream {
49 public:
50   Http2Upstream(ClientHandler *handler);
51   virtual ~Http2Upstream();
52   virtual int on_read();
53   virtual int on_write();
54   virtual int on_timeout(Downstream *downstream);
55   virtual int on_downstream_abort_request(Downstream *downstream,
56                                           unsigned int status_code);
57   virtual int
58   on_downstream_abort_request_with_https_redirect(Downstream *downstream);
59   virtual ClientHandler *get_client_handler() const;
60 
61   virtual int downstream_read(DownstreamConnection *dconn);
62   virtual int downstream_write(DownstreamConnection *dconn);
63   virtual int downstream_eof(DownstreamConnection *dconn);
64   virtual int downstream_error(DownstreamConnection *dconn, int events);
65 
66   void add_pending_downstream(std::unique_ptr<Downstream> downstream);
67   void remove_downstream(Downstream *downstream);
68 
69   int rst_stream(Downstream *downstream, uint32_t error_code);
70   int terminate_session(uint32_t error_code);
71   int error_reply(Downstream *downstream, unsigned int status_code);
72 
73   virtual void pause_read(IOCtrlReason reason);
74   virtual int resume_read(IOCtrlReason reason, Downstream *downstream,
75                           size_t consumed);
76 
77   virtual int on_downstream_header_complete(Downstream *downstream);
78   virtual int on_downstream_body(Downstream *downstream, const uint8_t *data,
79                                  size_t len, bool flush);
80   virtual int on_downstream_body_complete(Downstream *downstream);
81 
82   virtual void on_handler_delete();
83   virtual int on_downstream_reset(Downstream *downstream, bool no_retry);
84   virtual int send_reply(Downstream *downstream, const uint8_t *body,
85                          size_t bodylen);
86   virtual int initiate_push(Downstream *downstream, const StringRef &uri);
87   virtual int response_riovec(struct iovec *iov, int iovcnt) const;
88   virtual void response_drain(size_t n);
89   virtual bool response_empty() const;
90 
91   virtual Downstream *on_downstream_push_promise(Downstream *downstream,
92                                                  int32_t promised_stream_id);
93   virtual int
94   on_downstream_push_promise_complete(Downstream *downstream,
95                                       Downstream *promised_downstream);
96   virtual bool push_enabled() const;
97   virtual void cancel_premature_downstream(Downstream *promised_downstream);
98 
99   bool get_flow_control() const;
100   // Perform HTTP/2 upgrade from |upstream|. On success, this object
101   // takes ownership of the |upstream|. This function returns 0 if it
102   // succeeds, or -1.
103   int upgrade_upstream(HttpsUpstream *upstream);
104   void start_settings_timer();
105   void stop_settings_timer();
106   int consume(int32_t stream_id, size_t len);
107   void log_response_headers(Downstream *downstream,
108                             const std::vector<nghttp2_nv> &nva) const;
109   void start_downstream(Downstream *downstream);
110   void initiate_downstream(Downstream *downstream);
111 
112   void submit_goaway();
113   void check_shutdown();
114   // Starts graceful shutdown period.
115   void start_graceful_shutdown();
116 
117   int prepare_push_promise(Downstream *downstream);
118   int submit_push_promise(const StringRef &scheme, const StringRef &authority,
119                           const StringRef &path, Downstream *downstream);
120 
121   // Called when new request has started.
122   void on_start_request(const nghttp2_frame *frame);
123   int on_request_headers(Downstream *downstream, const nghttp2_frame *frame);
124 
125   DefaultMemchunks *get_response_buf();
126 
127   size_t get_max_buffer_size() const;
128 
129   int redirect_to_https(Downstream *downstream);
130 
131 private:
132   DefaultMemchunks wb_;
133   std::unique_ptr<HttpsUpstream> pre_upstream_;
134   DownstreamQueue downstream_queue_;
135   ev_timer settings_timer_;
136   ev_timer shutdown_timer_;
137   ev_prepare prep_;
138   ClientHandler *handler_;
139   nghttp2_session *session_;
140   size_t max_buffer_size_;
141   // The number of requests seen so far.
142   size_t num_requests_;
143   bool flow_control_;
144 };
145 
146 nghttp2_session_callbacks *create_http2_upstream_callbacks();
147 
148 } // namespace shrpx
149 
150 #endif // SHRPX_HTTP2_UPSTREAM_H
151