Monero
Loading...
Searching...
No Matches
http_protocol_handler.h
Go to the documentation of this file.
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#include "syncobj.h"
40
41#undef MONERO_DEFAULT_LOG_CATEGORY
42#define MONERO_DEFAULT_LOG_CATEGORY "net.http"
43
44namespace epee
45{
46namespace net_utils
47{
48 namespace http
49 {
50
51
52 /************************************************************************/
53 /* */
54 /************************************************************************/
56 {
57 std::string m_folder;
58 std::vector<std::string> m_access_control_origins;
59 std::unordered_map<std::string, std::size_t> m_connections;
60 boost::optional<login> m_user;
61 size_t m_max_content_length{std::numeric_limits<size_t>::max()};
62 std::size_t m_connection_count{0};
65 std::size_t m_max_connections{100};
67 };
68
69 // RPC limits groupable IPv6 clients by /64 to avoid per-address limit bypasses.
71
72 /************************************************************************/
73 /* */
74 /************************************************************************/
75 template<class t_connection_context = net_utils::connection_context_base>
77 {
78 public:
79 typedef t_connection_context connection_context;//t_connection_context net_utils::connection_context_base connection_context;
81
82 simple_http_connection_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context);
84
86 {
87 return true;
88 }
89
90 virtual bool thread_init()
91 {
92 return true;
93 }
94
95 virtual bool thread_deinit()
96 {
97 return true;
98 }
100 virtual bool handle_recv(const void* ptr, size_t cb);
101 virtual bool handle_request(const http::http_request_info& query_info, http_response_info& response);
102
103 private:
111
120
121 bool handle_buff_in(std::string& buf);
122
124
126 bool parse_cached_header(http_header_info& body_info, const std::string& m_cache_to_process, size_t pos);
127 std::string::size_type match_end_of_header(const std::string& buf);
128 bool get_len_from_content_lenght(const std::string& str, size_t& len);
131 bool set_ready_state();
132 bool slash_to_back_slash(std::string& str);
133 std::string get_file_mime_tipe(const std::string& path);
134 std::string get_response_header(const http_response_info& response);
135
136 //major function
137 inline bool handle_request_and_send_response(const http::http_request_info& query_info);
138
139
140 std::string get_not_found_response_body(const std::string& URI);
141
142 std::string m_root_path;
143 std::string m_cache;
153 protected:
155 t_connection_context& m_conn_context;
157 };
158
159 template<class t_connection_context>
161 {
163 virtual bool handle_http_request(const http_request_info& query_info,
164 http_response_info& response,
165 t_connection_context& m_conn_context) = 0;
166 virtual bool init_server_thread(){return true;}
167 virtual bool deinit_server_thread(){return true;}
168 };
169
170 template<class t_connection_context>
176
177 /************************************************************************/
178 /* */
179 /************************************************************************/
180
181 template<class t_connection_context = net_utils::connection_context_base>
182 class http_custom_handler: public simple_http_connection_handler<t_connection_context>
183 {
184 public:
186
187 http_custom_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context)
188 : simple_http_connection_handler<t_connection_context>(psnd_hndlr, config, conn_context),
191 {}
192 inline bool handle_request(const http_request_info& query_info, http_response_info& response)
193 {
194 CHECK_AND_ASSERT_MES(m_config.m_phandler, false, "m_config.m_phandler is NULL!!!!");
195
196 const auto auth_response = m_auth.get_response(query_info);
197 if (auth_response)
198 {
199 response = std::move(*auth_response);
200 return true;
201 }
202
203 //fill with default values
204 response.m_mime_tipe = "text/plain";
205 response.m_response_code = 200;
206 response.m_response_comment = "OK";
207 response.m_body.clear();
208
209 return m_config.m_phandler->handle_http_request(query_info, response, this->m_conn_context);
210 }
211
212 virtual bool thread_init()
213 {
214 return m_config.m_phandler->init_server_thread();
215 }
216
217 virtual bool thread_deinit()
218 {
219 return m_config.m_phandler->deinit_server_thread();
220 }
222 {}
223
224 private:
225 //simple_http_connection_handler::config_type m_stub_config;
228 };
229 }
230}
231}
232
234
235#endif //_HTTP_SERVER_H_
Definition syncobj.h:82
void handle_qued_callback()
Definition http_protocol_handler.h:221
virtual bool thread_deinit()
Definition http_protocol_handler.h:217
http_custom_handler(i_service_endpoint *psnd_hndlr, config_type &config, t_connection_context &conn_context)
Definition http_protocol_handler.h:187
bool handle_request(const http_request_info &query_info, http_response_info &response)
Definition http_protocol_handler.h:192
custum_handler_config< t_connection_context > config_type
Definition http_protocol_handler.h:185
config_type & m_config
Definition http_protocol_handler.h:226
http_server_auth m_auth
Definition http_protocol_handler.h:227
virtual bool thread_init()
Definition http_protocol_handler.h:212
Implements RFC 2617 digest auth. Digests from RFC 7616 can be added.
Definition http_auth.h:61
boost::optional< http_response_info > get_response(const http_request_info &request)
Definition http_auth.h:78
size_t m_bytes_read
Definition http_protocol_handler.h:152
bool release_protocol()
Definition http_protocol_handler.h:85
config_type & m_config
Definition http_protocol_handler.h:149
virtual bool thread_deinit()
Definition http_protocol_handler.h:95
bool get_len_from_content_lenght(const std::string &str, size_t &len)
Definition http_protocol_handler.inl:601
bool parse_cached_header(http_header_info &body_info, const std::string &m_cache_to_process, size_t pos)
Definition http_protocol_handler.inl:548
body_transfer_type
Definition http_protocol_handler.h:112
@ http_body_transfer_measure
Definition http_protocol_handler.h:114
@ http_body_transfer_undefined
Definition http_protocol_handler.h:118
@ http_body_transfer_connection_close
Definition http_protocol_handler.h:116
@ http_body_transfer_multipart
Definition http_protocol_handler.h:117
@ http_body_transfer_chunked_instead_measure
Definition http_protocol_handler.h:115
@ http_body_transfer_chunked
Definition http_protocol_handler.h:113
i_service_endpoint * m_psnd_hndlr
Definition http_protocol_handler.h:154
bool after_init_connection()
Definition http_protocol_handler.inl:243
std::string m_cache
Definition http_protocol_handler.h:143
bool handle_retriving_query_body()
Definition http_protocol_handler.inl:502
bool slash_to_back_slash(std::string &str)
Definition http_protocol_handler.inl:780
machine_state
Definition http_protocol_handler.h:104
@ http_state_retriving_body
Definition http_protocol_handler.h:107
@ http_state_connection_close
Definition http_protocol_handler.h:108
@ http_state_error
Definition http_protocol_handler.h:109
@ http_state_retriving_header
Definition http_protocol_handler.h:106
@ http_state_retriving_comand_line
Definition http_protocol_handler.h:105
size_t m_newlines
Definition http_protocol_handler.h:151
http::http_request_info m_query_info
Definition http_protocol_handler.h:147
machine_state m_state
Definition http_protocol_handler.h:144
bool m_initialized
Definition http_protocol_handler.h:156
std::string m_root_path
Definition http_protocol_handler.h:142
std::string get_file_mime_tipe(const std::string &path)
Definition http_protocol_handler.inl:737
bool handle_invoke_query_line()
Definition http_protocol_handler.inl:401
bool handle_buff_in(std::string &buf)
Definition http_protocol_handler.inl:279
bool handle_query_measure()
Definition http_protocol_handler.inl:522
body_transfer_type m_body_transfer_type
Definition http_protocol_handler.h:145
bool set_ready_state()
Definition http_protocol_handler.inl:253
bool handle_request_and_send_response(const http::http_request_info &query_info)
Definition http_protocol_handler.inl:615
size_t m_len_remain
Definition http_protocol_handler.h:148
t_connection_context & m_conn_context
Definition http_protocol_handler.h:155
bool analize_cached_request_header_and_invoke_state(size_t pos)
Definition http_protocol_handler.inl:455
std::string get_response_header(const http_response_info &response)
Definition http_protocol_handler.inl:679
std::string::size_type match_end_of_header(const std::string &buf)
Definition http_protocol_handler.inl:441
virtual bool handle_request(const http::http_request_info &query_info, http_response_info &response)
Definition http_protocol_handler.inl:649
bool m_is_stop_handling
Definition http_protocol_handler.h:146
virtual bool thread_init()
Definition http_protocol_handler.h:90
http_server_config config_type
Definition http_protocol_handler.h:80
bool m_want_close
Definition http_protocol_handler.h:150
std::string get_not_found_response_body(const std::string &URI)
Definition http_protocol_handler.inl:763
simple_http_connection_handler(i_service_endpoint *psnd_hndlr, config_type &config, t_connection_context &conn_context)
Definition http_protocol_handler.inl:201
size_t m_len_summary
Definition http_protocol_handler.h:148
t_connection_context connection_context
Definition http_protocol_handler.h:79
Definition net_utils_base.h:229
conn m_protocol_handler handle_recv(buf, len)
Definition cryptonote_config.h:221
std::string get_rpc_connection_limit_key(const net_utils::network_address &address)
Definition http_protocol_handler.cpp:33
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
unsigned char uint8_t
Definition stdint.h:124
Definition http_protocol_handler.h:172
i_http_server_handler< t_connection_context > * m_phandler
Definition http_protocol_handler.h:173
std::function< void(size_t, uint8_t *)> rng
Definition http_protocol_handler.h:174
Definition http_base.h:121
std::string m_response_comment
Definition http_base.h:202
std::string m_mime_tipe
Definition http_base.h:205
int m_response_code
Definition http_base.h:201
std::string m_body
Definition http_base.h:204
Definition http_protocol_handler.h:56
std::vector< std::string > m_access_control_origins
Definition http_protocol_handler.h:58
std::size_t m_max_public_ip_connections
Definition http_protocol_handler.h:63
critical_section m_lock
Definition http_protocol_handler.h:66
std::size_t m_max_private_ip_connections
Definition http_protocol_handler.h:64
std::size_t m_connection_count
Definition http_protocol_handler.h:62
size_t m_max_content_length
Definition http_protocol_handler.h:61
std::size_t m_max_connections
Definition http_protocol_handler.h:65
std::unordered_map< std::string, std::size_t > m_connections
Definition http_protocol_handler.h:59
boost::optional< login > m_user
Definition http_protocol_handler.h:60
std::string m_folder
Definition http_protocol_handler.h:57
Definition http_protocol_handler.h:161
virtual bool init_server_thread()
Definition http_protocol_handler.h:166
virtual bool deinit_server_thread()
Definition http_protocol_handler.h:167
virtual ~i_http_server_handler()
Definition http_protocol_handler.h:162
virtual bool handle_http_request(const http_request_info &query_info, http_response_info &response, t_connection_context &m_conn_context)=0
Definition net_utils_base.h:446