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_UPSTREAM_H
26 #define SHRPX_UPSTREAM_H
27 
28 #include "shrpx.h"
29 #include "shrpx_io_control.h"
30 #include "memchunk.h"
31 
32 using namespace nghttp2;
33 
34 namespace shrpx {
35 
36 class ClientHandler;
37 class Downstream;
38 class DownstreamConnection;
39 
40 class Upstream {
41 public:
~Upstream()42   virtual ~Upstream() {}
43   virtual int on_read() = 0;
44   virtual int on_write() = 0;
on_timeout(Downstream * downstream)45   virtual int on_timeout(Downstream *downstream) { return 0; };
46   virtual int on_downstream_abort_request(Downstream *downstream,
47                                           unsigned int status_code) = 0;
48   // Called when the current request is aborted without forwarding it
49   // to backend, and it should be redirected to https URI.
50   virtual int
51   on_downstream_abort_request_with_https_redirect(Downstream *downstream) = 0;
52   virtual int downstream_read(DownstreamConnection *dconn) = 0;
53   virtual int downstream_write(DownstreamConnection *dconn) = 0;
54   virtual int downstream_eof(DownstreamConnection *dconn) = 0;
55   virtual int downstream_error(DownstreamConnection *dconn, int events) = 0;
56   virtual ClientHandler *get_client_handler() const = 0;
57 
58   virtual int on_downstream_header_complete(Downstream *downstream) = 0;
59   virtual int on_downstream_body(Downstream *downstream, const uint8_t *data,
60                                  size_t len, bool flush) = 0;
61   virtual int on_downstream_body_complete(Downstream *downstream) = 0;
62 
63   virtual void on_handler_delete() = 0;
64   // Called when downstream connection for |downstream| is reset.
65   // Currently this is only used by Http2Session.  If |no_retry| is
66   // true, another connection attempt using new DownstreamConnection
67   // is not allowed.
68   virtual int on_downstream_reset(Downstream *downstream, bool no_retry) = 0;
69 
70   virtual void pause_read(IOCtrlReason reason) = 0;
71   virtual int resume_read(IOCtrlReason reason, Downstream *downstream,
72                           size_t consumed) = 0;
73   virtual int send_reply(Downstream *downstream, const uint8_t *body,
74                          size_t bodylen) = 0;
75 
76   // Starts server push.  The |downstream| is an associated stream for
77   // the pushed resource.  This function returns 0 if it succeeds,
78   // otherwise -1.
79   virtual int initiate_push(Downstream *downstream, const StringRef &uri) = 0;
80 
81   // Fills response data in |iov| whose capacity is |iovcnt|.  Returns
82   // the number of iovs filled.
83   virtual int response_riovec(struct iovec *iov, int iovcnt) const = 0;
84   virtual void response_drain(size_t n) = 0;
85   virtual bool response_empty() const = 0;
86 
87   // Called when PUSH_PROMISE was started in downstream.  The
88   // associated downstream is given as |downstream|.  The promised
89   // stream ID is given as |promised_stream_id|.  If upstream supports
90   // server push for the corresponding upstream connection, it should
91   // return Downstream object for pushed stream.  Otherwise, returns
92   // nullptr.
93   virtual Downstream *
94   on_downstream_push_promise(Downstream *downstream,
95                              int32_t promised_stream_id) = 0;
96   // Called when PUSH_PROMISE frame was completely received in
97   // downstream.  The associated downstream is given as |downstream|.
98   // This function returns 0 if it succeeds, or -1.
99   virtual int
100   on_downstream_push_promise_complete(Downstream *downstream,
101                                       Downstream *promised_downstream) = 0;
102   // Returns true if server push is enabled in upstream connection.
103   virtual bool push_enabled() const = 0;
104   // Cancels promised downstream.  This function is called when
105   // PUSH_PROMISE for |promised_downstream| is not submitted to
106   // upstream session.
107   virtual void cancel_premature_downstream(Downstream *promised_downstream) = 0;
108 };
109 
110 } // namespace shrpx
111 
112 #endif // SHRPX_UPSTREAM_H
113