• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2016 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_API_DOWNSTREAM_CONNECTION_H
26 #define SHRPX_API_DOWNSTREAM_CONNECTION_H
27 
28 #include "shrpx_downstream_connection.h"
29 #include "template.h"
30 
31 using namespace nghttp2;
32 
33 namespace shrpx {
34 
35 class Worker;
36 
37 // If new method is added, don't forget to update API_METHOD_STRING as
38 // well.
39 enum APIMethod {
40   API_METHOD_GET,
41   API_METHOD_POST,
42   API_METHOD_PUT,
43   API_METHOD_MAX,
44 };
45 
46 // API status code, which is independent from HTTP status code.  But
47 // generally, 2xx code for SUCCESS, and otherwise FAILURE.
48 enum class APIStatusCode {
49   SUCCESS,
50   FAILURE,
51 };
52 
53 class APIDownstreamConnection;
54 
55 struct APIEndpoint {
56   // Endpoint path.  It must start with "/api/".
57   StringRef path;
58   // true if we evaluate request body.
59   bool require_body;
60   // Allowed methods.  This is bitwise OR of one or more of (1 <<
61   // APIMethod value).
62   uint8_t allowed_methods;
63   std::function<int(APIDownstreamConnection &)> handler;
64 };
65 
66 class APIDownstreamConnection : public DownstreamConnection {
67 public:
68   APIDownstreamConnection(Worker *worker);
69   virtual ~APIDownstreamConnection();
70   virtual int attach_downstream(Downstream *downstream);
71   virtual void detach_downstream(Downstream *downstream);
72 
73   virtual int push_request_headers();
74   virtual int push_upload_data_chunk(const uint8_t *data, size_t datalen);
75   virtual int end_upload_data();
76 
77   virtual void pause_read(IOCtrlReason reason);
78   virtual int resume_read(IOCtrlReason reason, size_t consumed);
79   virtual void force_resume_read();
80 
81   virtual int on_read();
82   virtual int on_write();
83 
84   virtual void on_upstream_change(Upstream *uptream);
85 
86   // true if this object is poolable.
87   virtual bool poolable() const;
88 
89   virtual const std::shared_ptr<DownstreamAddrGroup> &
90   get_downstream_addr_group() const;
91   virtual DownstreamAddr *get_addr() const;
92 
93   int send_reply(unsigned int http_status, APIStatusCode api_status,
94                  const StringRef &data = StringRef{});
95   int error_method_not_allowed();
96 
97   // Handles backendconfig API request.
98   int handle_backendconfig();
99   // Handles configrevision API request.
100   int handle_configrevision();
101 
102 private:
103   Worker *worker_;
104   // This points to the requested APIEndpoint struct.
105   const APIEndpoint *api_;
106   // The file descriptor for temporary file to store request body.
107   int fd_;
108   // true if we stop reading request body.
109   bool shutdown_read_;
110 };
111 
112 } // namespace shrpx
113 
114 #endif // SHRPX_API_DOWNSTREAM_CONNECTION_H
115