Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_http2_upstream.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_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
40 using namespace nghttp2;
41
42 namespace shrpx {
43
44 class ClientHandler;
45 class HttpsUpstream;
46
47 class Http2Upstream : public Upstream {
48 public:
49   Http2Upstream(ClientHandler *handler);
50   virtual ~Http2Upstream();
51   virtual int on_read();
52   virtual int on_write();
53   virtual int on_timeout(Downstream *downstream);
54   virtual int on_downstream_abort_request(Downstream *downstream,
55                                           unsigned int status_code);
56   virtual ClientHandler *get_client_handler() const;
57
58   virtual int downstream_read(DownstreamConnection *dconn);
59   virtual int downstream_write(DownstreamConnection *dconn);
60   virtual int downstream_eof(DownstreamConnection *dconn);
61   virtual int downstream_error(DownstreamConnection *dconn, int events);
62
63   void add_pending_downstream(std::unique_ptr<Downstream> downstream);
64   void remove_downstream(Downstream *downstream);
65
66   int rst_stream(Downstream *downstream, uint32_t error_code);
67   int terminate_session(uint32_t error_code);
68   int error_reply(Downstream *downstream, unsigned int status_code);
69
70   virtual void pause_read(IOCtrlReason reason);
71   virtual int resume_read(IOCtrlReason reason, Downstream *downstream,
72                           size_t consumed);
73
74   virtual int on_downstream_header_complete(Downstream *downstream);
75   virtual int on_downstream_body(Downstream *downstream, const uint8_t *data,
76                                  size_t len, bool flush);
77   virtual int on_downstream_body_complete(Downstream *downstream);
78
79   virtual void on_handler_delete();
80   virtual int on_downstream_reset(bool no_retry);
81
82   bool get_flow_control() const;
83   // Perform HTTP/2 upgrade from |upstream|. On success, this object
84   // takes ownership of the |upstream|. This function returns 0 if it
85   // succeeds, or -1.
86   int upgrade_upstream(HttpsUpstream *upstream);
87   void start_settings_timer();
88   void stop_settings_timer();
89   int consume(int32_t stream_id, size_t len);
90   void log_response_headers(Downstream *downstream,
91                             const std::vector<nghttp2_nv> &nva) const;
92   void start_downstream(Downstream *downstream);
93   void initiate_downstream(Downstream *downstream);
94
95   void submit_goaway();
96   void check_shutdown();
97
98   int prepare_push_promise(Downstream *downstream);
99   int submit_push_promise(const std::string &path, Downstream *downstream);
100
101   int on_request_headers(Downstream *downstream, const nghttp2_frame *frame);
102
103 private:
104   std::unique_ptr<HttpsUpstream> pre_upstream_;
105   DownstreamQueue downstream_queue_;
106   ev_timer settings_timer_;
107   ev_timer shutdown_timer_;
108   ev_prepare prep_;
109   ClientHandler *handler_;
110   nghttp2_session *session_;
111   const uint8_t *data_pending_;
112   size_t data_pendinglen_;
113   bool flow_control_;
114   bool shutdown_handled_;
115 };
116
117 nghttp2_session_callbacks *create_http2_upstream_callbacks();
118
119 } // namespace shrpx
120
121 #endif // SHRPX_HTTP2_UPSTREAM_H