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_HTTPS_UPSTREAM_H
26 #define SHRPX_HTTPS_UPSTREAM_H
27 
28 #include "shrpx.h"
29 
30 #include <cinttypes>
31 #include <memory>
32 
33 #include "llhttp.h"
34 
35 #include "shrpx_upstream.h"
36 #include "memchunk.h"
37 
38 using namespace nghttp2;
39 
40 namespace shrpx {
41 
42 class ClientHandler;
43 
44 class HttpsUpstream : public Upstream {
45 public:
46   HttpsUpstream(ClientHandler *handler);
47   virtual ~HttpsUpstream();
48   virtual int on_read();
49   virtual int on_write();
50   virtual int on_event();
51   virtual int on_downstream_abort_request(Downstream *downstream,
52                                           unsigned int status_code);
53   virtual int
54   on_downstream_abort_request_with_https_redirect(Downstream *downstream);
55   virtual ClientHandler *get_client_handler() const;
56 
57   virtual int downstream_read(DownstreamConnection *dconn);
58   virtual int downstream_write(DownstreamConnection *dconn);
59   virtual int downstream_eof(DownstreamConnection *dconn);
60   virtual int downstream_error(DownstreamConnection *dconn, int events);
61 
62   void attach_downstream(std::unique_ptr<Downstream> downstream);
63   void delete_downstream();
64   Downstream *get_downstream() const;
65   std::unique_ptr<Downstream> pop_downstream();
66   void error_reply(unsigned int status_code);
67 
68   virtual void pause_read(IOCtrlReason reason);
69   virtual int resume_read(IOCtrlReason reason, Downstream *downstream,
70                           size_t consumed);
71 
72   virtual int on_downstream_header_complete(Downstream *downstream);
73   virtual int on_downstream_body(Downstream *downstream, const uint8_t *data,
74                                  size_t len, bool flush);
75   virtual int on_downstream_body_complete(Downstream *downstream);
76 
77   virtual void on_handler_delete();
78   virtual int on_downstream_reset(Downstream *downstream, bool no_retry);
79   virtual int send_reply(Downstream *downstream, const uint8_t *body,
80                          size_t bodylen);
81   virtual int initiate_push(Downstream *downstream, const StringRef &uri);
82   virtual int response_riovec(struct iovec *iov, int iovcnt) const;
83   virtual void response_drain(size_t n);
84   virtual bool response_empty() const;
85 
86   virtual Downstream *on_downstream_push_promise(Downstream *downstream,
87                                                  int32_t promised_stream_id);
88   virtual int
89   on_downstream_push_promise_complete(Downstream *downstream,
90                                       Downstream *promised_downstream);
91   virtual bool push_enabled() const;
92   virtual void cancel_premature_downstream(Downstream *promised_downstream);
93 
94   void reset_current_header_length();
95   void log_response_headers(DefaultMemchunks *buf) const;
96   int redirect_to_https(Downstream *downstream);
97 
98   // Called when new request has started.
99   void on_start_request();
100 
101 private:
102   ClientHandler *handler_;
103   llhttp_t htp_;
104   size_t current_header_length_;
105   std::unique_ptr<Downstream> downstream_;
106   IOControl ioctrl_;
107   // The number of requests seen so far.
108   size_t num_requests_;
109 };
110 
111 } // namespace shrpx
112 
113 #endif // SHRPX_HTTPS_UPSTREAM_H
114