Debian Codegraph @

contrib/epee/include/net/http_protocol_handler.h

raw symbols SCIP
1 // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of the Andrey N. Sabelnikov nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26
27
28
29
30 #ifndef _HTTP_SERVER_H_
31 #define _HTTP_SERVER_H_
32
33 #include <boost/optional/optional.hpp>
34 #include <string>
35 #include <unordered_map>
36 #include "net_utils_base.h"
37 #include "http_auth.h"
38 #include "http_base.h"
39
40 #undef MONERO_DEFAULT_LOG_CATEGORY
41 #define MONERO_DEFAULT_LOG_CATEGORY "net.http"
42
43 namespace epee
44 {
45 namespace net_utils
46 {
47 namespace http
48 {
49
50
51 /************************************************************************/
52 /* */
53 /************************************************************************/
54 struct http_server_config
55 {
56 std::string m_folder;
57 std::vector<std::string> m_access_control_origins;
58 std::unordered_map<std::string, std::size_t> m_connections;
59 boost::optional<login> m_user;
60 size_t m_max_content_length{std::numeric_limits<size_t>::max()};
61 std::size_t m_connection_count{0};
62 std::size_t m_max_public_ip_connections{3};
63 std::size_t m_max_private_ip_connections{25};
64 std::size_t m_max_connections{100};
65 critical_section m_lock;
66 };
67
68 /************************************************************************/
69 /* */
70 /************************************************************************/
71 template<class t_connection_context = net_utils::connection_context_base>
72 class simple_http_connection_handler
73 {
74 public:
75 typedef t_connection_context connection_context;//t_connection_context net_utils::connection_context_base connection_context;
76 typedef http_server_config config_type;
77
78 simple_http_connection_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context);
79 virtual ~simple_http_connection_handler();
80
81 bool release_protocol()
82 {
83 return true;
84 }
85
86 virtual bool thread_init()
87 {
88 return true;
89 }
90
91 virtual bool thread_deinit()
92 {
93 return true;
94 }
95 bool after_init_connection();
96 virtual bool handle_recv(const void* ptr, size_t cb);
97 virtual bool handle_request(const http::http_request_info& query_info, http_response_info& response);
98
99 private:
100 enum machine_state{
101 http_state_retriving_comand_line,
102 http_state_retriving_header,
103 http_state_retriving_body,
104 http_state_connection_close,
105 http_state_error
106 };
107
108 enum body_transfer_type{
109 http_body_transfer_chunked,
110 http_body_transfer_measure,//mean "Content-Length" valid
111 http_body_transfer_chunked_instead_measure,
112 http_body_transfer_connection_close,
113 http_body_transfer_multipart,
114 http_body_transfer_undefined
115 };
116
117 bool handle_buff_in(std::string& buf);
118
119 bool analize_cached_request_header_and_invoke_state(size_t pos);
120
121 bool handle_invoke_query_line();
122 bool parse_cached_header(http_header_info& body_info, const std::string& m_cache_to_process, size_t pos);
123 std::string::size_type match_end_of_header(const std::string& buf);
124 bool get_len_from_content_lenght(const std::string& str, size_t& len);
125 bool handle_retriving_query_body();
126 bool handle_query_measure();
127 bool set_ready_state();
128 bool slash_to_back_slash(std::string& str);
129 std::string get_file_mime_tipe(const std::string& path);
130 std::string get_response_header(const http_response_info& response);
131
132 //major function
133 inline bool handle_request_and_send_response(const http::http_request_info& query_info);
134
135
136 std::string get_not_found_response_body(const std::string& URI);
137
138 std::string m_root_path;
139 std::string m_cache;
140 machine_state m_state;
141 body_transfer_type m_body_transfer_type;
142 bool m_is_stop_handling;
143 http::http_request_info m_query_info;
144 size_t m_len_summary, m_len_remain;
145 config_type& m_config;
146 bool m_want_close;
147 size_t m_newlines;
148 size_t m_bytes_read;
149 protected:
150 i_service_endpoint* m_psnd_hndlr;
151 t_connection_context& m_conn_context;
152 bool m_initialized;
153 };
154
155 template<class t_connection_context>
156 struct i_http_server_handler
157 {
158 virtual ~i_http_server_handler(){}
159 virtual bool handle_http_request(const http_request_info& query_info,
160 http_response_info& response,
161 t_connection_context& m_conn_context) = 0;
162 virtual bool init_server_thread(){return true;}
163 virtual bool deinit_server_thread(){return true;}
164 };
165
166 template<class t_connection_context>
167 struct custum_handler_config: public http_server_config
168 {
169 i_http_server_handler<t_connection_context>* m_phandler;
170 std::function<void(size_t, uint8_t*)> rng;
171 };
172
173 /************************************************************************/
174 /* */
175 /************************************************************************/
176
177 template<class t_connection_context = net_utils::connection_context_base>
178 class http_custom_handler: public simple_http_connection_handler<t_connection_context>
179 {
180 public:
181 typedef custum_handler_config<t_connection_context> config_type;
182
183 http_custom_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context)
184 : simple_http_connection_handler<t_connection_context>(psnd_hndlr, config, conn_context),
185 m_config(config),
186 m_auth(m_config.m_user ? http_server_auth{*m_config.m_user, config.rng} : http_server_auth{})
187 {}
188 inline bool handle_request(const http_request_info& query_info, http_response_info& response)
189 {
190 CHECK_AND_ASSERT_MES(m_config.m_phandler, false, "m_config.m_phandler is NULL!!!!");
191
192 const auto auth_response = m_auth.get_response(query_info);
193 if (auth_response)
194 {
195 response = std::move(*auth_response);
196 return true;
197 }
198
199 //fill with default values
200 response.m_mime_tipe = "text/plain";
201 response.m_response_code = 200;
202 response.m_response_comment = "OK";
203 response.m_body.clear();
204
205 return m_config.m_phandler->handle_http_request(query_info, response, this->m_conn_context);
206 }
207
208 virtual bool thread_init()
209 {
210 return m_config.m_phandler->init_server_thread();
211 }
212
213 virtual bool thread_deinit()
214 {
215 return m_config.m_phandler->deinit_server_thread();
216 }
217 void handle_qued_callback()
218 {}
219
220 private:
221 //simple_http_connection_handler::config_type m_stub_config;
222 config_type& m_config;
223 http_server_auth m_auth;
224 };
225 }
226 }
227 }
228
229 #include "http_protocol_handler.inl"
230
231 #endif //_HTTP_SERVER_H_