| 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 |
#include <boost/regex.hpp> |
| 29 |
#include <boost/lexical_cast.hpp> |
| 30 |
#include "http_protocol_handler.h" |
| 31 |
#include "reg_exp_definer.h" |
| 32 |
#include "string_tools.h" |
| 33 |
#include "file_io_utils.h" |
| 34 |
#include "net_parse_helpers.h" |
| 35 |
#include "time_helper.h" |
| 36 |
|
| 37 |
#undef MONERO_DEFAULT_LOG_CATEGORY |
| 38 |
#define MONERO_DEFAULT_LOG_CATEGORY "net.http" |
| 39 |
|
| 40 |
#define HTTP_MAX_URI_LEN 9000 |
| 41 |
#define HTTP_MAX_HEADER_LEN 100000 |
| 42 |
#define HTTP_MAX_STARTING_NEWLINES 8 |
| 43 |
|
| 44 |
namespace epee |
| 45 |
{ |
| 46 |
namespace net_utils |
| 47 |
{ |
| 48 |
namespace http |
| 49 |
{ |
| 50 |
|
| 51 |
struct multipart_entry |
| 52 |
{ |
| 53 |
std::list<std::pair<std::string, std::string> > m_etc_header_fields; |
| 54 |
std::string m_content_disposition; |
| 55 |
std::string m_content_type; |
| 56 |
std::string m_body; |
| 57 |
}; |
| 58 |
|
| 59 |
inline |
| 60 |
bool match_boundary(const std::string& content_type, std::string& boundary) |
| 61 |
{ |
| 62 |
STATIC_REGEXP_EXPR_1(rexp_match_boundary, "boundary=(.*?)(($)|([;\\s,]))", boost::regex::icase | boost::regex::normal); |
| 63 |
// 1 |
| 64 |
boost::smatch result; |
| 65 |
if(boost::regex_search(content_type, result, rexp_match_boundary, boost::match_default) && result[0].matched) |
| 66 |
{ |
| 67 |
boundary = result[1]; |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
inline |
| 75 |
bool parse_header(std::string::const_iterator it_begin, std::string::const_iterator it_end, multipart_entry& entry) |
| 76 |
{ |
| 77 |
STATIC_REGEXP_EXPR_1(rexp_mach_field, |
| 78 |
"\n?((Content-Disposition)|(Content-Type)" |
| 79 |
// 12 3 |
| 80 |
"|([\\w-]+?)) ?: ?((.*?)(\r?\n))[^\t ]", |
| 81 |
//4 56 7 |
| 82 |
boost::regex::icase | boost::regex::normal); |
| 83 |
|
| 84 |
boost::smatch result; |
| 85 |
std::string::const_iterator it_current_bound = it_begin; |
| 86 |
std::string::const_iterator it_end_bound = it_end; |
| 87 |
|
| 88 |
//lookup all fields and fill well-known fields |
| 89 |
while( boost::regex_search( it_current_bound, it_end_bound, result, rexp_mach_field, boost::match_default) && result[0].matched) |
| 90 |
{ |
| 91 |
const size_t field_val = 6; |
| 92 |
const size_t field_etc_name = 4; |
| 93 |
|
| 94 |
int i = 2; //start position = 2 |
| 95 |
if(result[i++].matched)//"Content-Disposition" |
| 96 |
entry.m_content_disposition = result[field_val]; |
| 97 |
else if(result[i++].matched)//"Content-Type" |
| 98 |
entry.m_content_type = result[field_val]; |
| 99 |
else if(result[i++].matched)//e.t.c (HAVE TO BE MATCHED!) |
| 100 |
entry.m_etc_header_fields.push_back(std::pair<std::string, std::string>(result[field_etc_name], result[field_val])); |
| 101 |
else |
| 102 |
{ |
| 103 |
LOG_ERROR("simple_http_connection_handler::parse_header() not matched last entry in:"<<std::string(it_current_bound, it_end)); |
| 104 |
} |
| 105 |
|
| 106 |
it_current_bound = result[(int)result.size()-1].first; |
| 107 |
} |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
inline |
| 112 |
bool handle_part_of_multipart(std::string::const_iterator it_begin, std::string::const_iterator it_end, multipart_entry& entry) |
| 113 |
{ |
| 114 |
std::string end_str = "\r\n\r\n"; |
| 115 |
std::string::const_iterator end_header_it = std::search(it_begin, it_end, end_str.begin(), end_str.end()); |
| 116 |
if(end_header_it == it_end) |
| 117 |
{ |
| 118 |
//header not matched |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
if(!parse_header(it_begin, end_header_it+4, entry)) |
| 123 |
{ |
| 124 |
LOG_ERROR("Failed to parse header:" << std::string(it_begin, end_header_it+2)); |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
entry.m_body.assign(end_header_it+4, it_end); |
| 129 |
|
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
inline |
| 134 |
bool parse_multipart_body(const std::string& content_type, const std::string& body, std::list<multipart_entry>& out_values) |
| 135 |
{ |
| 136 |
//bool res = file_io_utils::load_file_to_string("C:\\public\\multupart_data", body); |
| 137 |
|
| 138 |
std::string boundary; |
| 139 |
if(!match_boundary(content_type, boundary)) |
| 140 |
{ |
| 141 |
MERROR("Failed to match boundary in content type: " << content_type); |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
boundary+="\r\n"; |
| 146 |
bool is_stop = false; |
| 147 |
bool first_step = true; |
| 148 |
|
| 149 |
std::string::const_iterator it_begin = body.begin(); |
| 150 |
std::string::const_iterator it_end; |
| 151 |
while(!is_stop) |
| 152 |
{ |
| 153 |
std::string::size_type pos = body.find(boundary, std::distance(body.begin(), it_begin)); |
| 154 |
|
| 155 |
if(std::string::npos == pos) |
| 156 |
{ |
| 157 |
is_stop = true; |
| 158 |
boundary.erase(boundary.size()-2, 2); |
| 159 |
boundary+= "--"; |
| 160 |
pos = body.find(boundary, std::distance(body.begin(), it_begin)); |
| 161 |
if(std::string::npos == pos) |
| 162 |
{ |
| 163 |
MERROR("Error: Filed to match closing multipart tag"); |
| 164 |
it_end = body.end(); |
| 165 |
}else |
| 166 |
{ |
| 167 |
it_end = body.begin() + pos; |
| 168 |
} |
| 169 |
}else |
| 170 |
it_end = body.begin() + pos; |
| 171 |
|
| 172 |
|
| 173 |
if(first_step && !is_stop) |
| 174 |
{ |
| 175 |
first_step = false; |
| 176 |
it_begin = it_end + boundary.size(); |
| 177 |
std::string temp = "\r\n--"; |
| 178 |
boundary = temp + boundary; |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
out_values.push_back(multipart_entry()); |
| 183 |
if(!handle_part_of_multipart(it_begin, it_end, out_values.back())) |
| 184 |
{ |
| 185 |
MERROR("Failed to handle_part_of_multipart"); |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
it_begin = it_end + boundary.size(); |
| 190 |
} |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
//-------------------------------------------------------------------------------------------- |
| 199 |
template<class t_connection_context> |
| 200 |
simple_http_connection_handler<t_connection_context>::simple_http_connection_handler(i_service_endpoint* psnd_hndlr, config_type& config, t_connection_context& conn_context): |
| 201 |
m_state(http_state_retriving_comand_line), |
| 202 |
m_body_transfer_type(http_body_transfer_undefined), |
| 203 |
m_is_stop_handling(false), |
| 204 |
m_len_summary(0), |
| 205 |
m_len_remain(0), |
| 206 |
m_config(config), |
| 207 |
m_want_close(false), |
| 208 |
m_newlines(0), |
| 209 |
m_bytes_read(0), |
| 210 |
m_psnd_hndlr(psnd_hndlr), |
| 211 |
m_conn_context(conn_context), |
| 212 |
m_initialized(false) |
| 213 |
{ |
| 214 |
|
| 215 |
} |
| 216 |
//-------------------------------------------------------------------------------------------- |
| 217 |
template<class t_connection_context> |
| 218 |
simple_http_connection_handler<t_connection_context>::~simple_http_connection_handler() |
| 219 |
{ |
| 220 |
try |
| 221 |
{ |
| 222 |
if (m_initialized) |
| 223 |
{ |
| 224 |
CRITICAL_REGION_LOCAL(m_config.m_lock); |
| 225 |
if (m_config.m_connection_count) |
| 226 |
--m_config.m_connection_count; |
| 227 |
auto elem = m_config.m_connections.find(m_conn_context.m_remote_address.host_str()); |
| 228 |
if (elem != m_config.m_connections.end()) |
| 229 |
{ |
| 230 |
if (elem->second == 1 || elem->second == 0) |
| 231 |
m_config.m_connections.erase(elem); |
| 232 |
else |
| 233 |
--(elem->second); |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
catch (...) |
| 238 |
{} |
| 239 |
} |
| 240 |
//-------------------------------------------------------------------------------------------- |
| 241 |
template<class t_connection_context> |
| 242 |
bool simple_http_connection_handler<t_connection_context>::after_init_connection() |
| 243 |
{ |
| 244 |
CRITICAL_REGION_LOCAL(m_config.m_lock); |
| 245 |
++m_config.m_connections[m_conn_context.m_remote_address.host_str()]; |
| 246 |
++m_config.m_connection_count; |
| 247 |
m_initialized = true; |
| 248 |
return true; |
| 249 |
} |
| 250 |
//-------------------------------------------------------------------------------------------- |
| 251 |
template<class t_connection_context> |
| 252 |
bool simple_http_connection_handler<t_connection_context>::set_ready_state() |
| 253 |
{ |
| 254 |
m_is_stop_handling = false; |
| 255 |
m_state = http_state_retriving_comand_line; |
| 256 |
m_body_transfer_type = http_body_transfer_undefined; |
| 257 |
m_query_info.clear(); |
| 258 |
m_len_summary = 0; |
| 259 |
m_newlines = 0; |
| 260 |
m_bytes_read = 0; |
| 261 |
return true; |
| 262 |
} |
| 263 |
//-------------------------------------------------------------------------------------------- |
| 264 |
template<class t_connection_context> |
| 265 |
bool simple_http_connection_handler<t_connection_context>::handle_recv(const void* ptr, size_t cb) |
| 266 |
{ |
| 267 |
std::string buf((const char*)ptr, cb); |
| 268 |
//LOG_PRINT_L0("HTTP_RECV: " << ptr << "\r\n" << buf); |
| 269 |
//file_io_utils::save_string_to_file(string_tools::get_current_module_folder() + "/" + boost::lexical_cast<std::string>(ptr), std::string((const char*)ptr, cb)); |
| 270 |
|
| 271 |
bool res = handle_buff_in(buf); |
| 272 |
if(m_want_close/*m_state == http_state_connection_close || m_state == http_state_error*/) |
| 273 |
return false; |
| 274 |
return res; |
| 275 |
} |
| 276 |
//-------------------------------------------------------------------------------------------- |
| 277 |
template<class t_connection_context> |
| 278 |
bool simple_http_connection_handler<t_connection_context>::handle_buff_in(std::string& buf) |
| 279 |
{ |
| 280 |
|
| 281 |
size_t ndel; |
| 282 |
|
| 283 |
m_bytes_read += buf.size(); |
| 284 |
if (m_bytes_read > m_config.m_max_content_length) |
| 285 |
{ |
| 286 |
LOG_ERROR("simple_http_connection_handler::handle_buff_in: Too much data: got " << m_bytes_read); |
| 287 |
m_state = http_state_error; |
| 288 |
return false; |
| 289 |
} |
| 290 |
|
| 291 |
if(m_cache.size()) |
| 292 |
m_cache += buf; |
| 293 |
else |
| 294 |
m_cache.swap(buf); |
| 295 |
|
| 296 |
m_is_stop_handling = false; |
| 297 |
while(!m_is_stop_handling) |
| 298 |
{ |
| 299 |
switch(m_state) |
| 300 |
{ |
| 301 |
case http_state_retriving_comand_line: |
| 302 |
//The HTTP protocol does not place any a priori limit on the length of a URI. (c)RFC2616 |
| 303 |
//but we forebly restirct it len to HTTP_MAX_URI_LEN to make it more safely |
| 304 |
if(!m_cache.size()) |
| 305 |
break; |
| 306 |
|
| 307 |
//check_and_handle_fake_response(); |
| 308 |
ndel = m_cache.find_first_not_of("\r\n"); |
| 309 |
if (ndel != 0) |
| 310 |
{ |
| 311 |
//some times it could be that before query line cold be few line breaks |
| 312 |
//so we have to be calm without panic with assers |
| 313 |
m_newlines += std::string::npos == ndel ? m_cache.size() : ndel; |
| 314 |
if (m_newlines > HTTP_MAX_STARTING_NEWLINES) |
| 315 |
{ |
| 316 |
LOG_ERROR("simple_http_connection_handler::handle_buff_out: Too many starting newlines"); |
| 317 |
m_state = http_state_error; |
| 318 |
return false; |
| 319 |
} |
| 320 |
m_cache.erase(0, ndel); |
| 321 |
break; |
| 322 |
} |
| 323 |
|
| 324 |
if(std::string::npos != m_cache.find('\n', 0)) |
| 325 |
handle_invoke_query_line(); |
| 326 |
else |
| 327 |
{ |
| 328 |
m_is_stop_handling = true; |
| 329 |
if(m_cache.size() > HTTP_MAX_URI_LEN) |
| 330 |
{ |
| 331 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler::handle_buff_out: Too long URI line"); |
| 332 |
m_state = http_state_error; |
| 333 |
return false; |
| 334 |
} |
| 335 |
} |
| 336 |
break; |
| 337 |
case http_state_retriving_header: |
| 338 |
{ |
| 339 |
std::string::size_type pos = match_end_of_header(m_cache); |
| 340 |
if(std::string::npos == pos) |
| 341 |
{ |
| 342 |
m_is_stop_handling = true; |
| 343 |
if(m_cache.size() > HTTP_MAX_HEADER_LEN) |
| 344 |
{ |
| 345 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler::handle_buff_in: Too long header area"); |
| 346 |
m_state = http_state_error; |
| 347 |
return false; |
| 348 |
} |
| 349 |
break; |
| 350 |
} |
| 351 |
if (!analize_cached_request_header_and_invoke_state(pos)) |
| 352 |
return false; |
| 353 |
break; |
| 354 |
} |
| 355 |
case http_state_retriving_body: |
| 356 |
return handle_retriving_query_body(); |
| 357 |
case http_state_connection_close: |
| 358 |
return false; |
| 359 |
default: |
| 360 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler::handle_char_out: Wrong state: " << m_state); |
| 361 |
return false; |
| 362 |
case http_state_error: |
| 363 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler::handle_char_out: Error state!!!"); |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
if(!m_cache.size()) |
| 368 |
m_is_stop_handling = true; |
| 369 |
} |
| 370 |
|
| 371 |
return true; |
| 372 |
} |
| 373 |
//-------------------------------------------------------------------------------------------- |
| 374 |
inline bool analize_http_method(const boost::smatch& result, http::http_method& method, int& http_ver_major, int& http_ver_minor) |
| 375 |
{ |
| 376 |
CHECK_AND_ASSERT_MES(result[0].matched, false, "simple_http_connection_handler::analize_http_method() assert failed..."); |
| 377 |
if (!boost::conversion::try_lexical_convert<int>(result[11], http_ver_major)) |
| 378 |
return false; |
| 379 |
if (!boost::conversion::try_lexical_convert<int>(result[12], http_ver_minor)) |
| 380 |
return false; |
| 381 |
|
| 382 |
if(result[3].matched) |
| 383 |
method = http::http_method_options; |
| 384 |
else if(result[4].matched) |
| 385 |
method = http::http_method_get; |
| 386 |
else if(result[5].matched) |
| 387 |
method = http::http_method_head; |
| 388 |
else if(result[6].matched) |
| 389 |
method = http::http_method_post; |
| 390 |
else if(result[7].matched) |
| 391 |
method = http::http_method_put; |
| 392 |
else |
| 393 |
method = http::http_method_etc; |
| 394 |
|
| 395 |
return true; |
| 396 |
} |
| 397 |
|
| 398 |
//-------------------------------------------------------------------------------------------- |
| 399 |
template<class t_connection_context> |
| 400 |
bool simple_http_connection_handler<t_connection_context>::handle_invoke_query_line() |
| 401 |
{ |
| 402 |
STATIC_REGEXP_EXPR_1(rexp_match_command_line, "^(((OPTIONS)|(GET)|(HEAD)|(POST)|(PUT)|(DELETE)|(TRACE)) (\\S+) HTTP/(\\d+)\\.(\\d+))\r?\n", boost::regex::icase | boost::regex::normal); |
| 403 |
// 123 4 5 6 7 8 9 10 11 12 |
| 404 |
//size_t match_len = 0; |
| 405 |
boost::smatch result; |
| 406 |
if(boost::regex_search(m_cache, result, rexp_match_command_line, boost::match_default) && result[0].matched) |
| 407 |
{ |
| 408 |
if (!analize_http_method(result, m_query_info.m_http_method, m_query_info.m_http_ver_hi, m_query_info.m_http_ver_hi)) |
| 409 |
{ |
| 410 |
m_state = http_state_error; |
| 411 |
MERROR("Failed to analyze method"); |
| 412 |
return false; |
| 413 |
} |
| 414 |
m_query_info.m_URI = result[10]; |
| 415 |
if (!parse_uri(m_query_info.m_URI, m_query_info.m_uri_content)) |
| 416 |
{ |
| 417 |
m_state = http_state_error; |
| 418 |
MERROR("Failed to parse URI: m_query_info.m_URI"); |
| 419 |
return false; |
| 420 |
} |
| 421 |
m_query_info.m_http_method_str = result[2]; |
| 422 |
m_query_info.m_full_request_str = result[0]; |
| 423 |
|
| 424 |
m_cache.erase(m_cache.begin(), result[0].second); |
| 425 |
|
| 426 |
m_state = http_state_retriving_header; |
| 427 |
|
| 428 |
return true; |
| 429 |
}else |
| 430 |
{ |
| 431 |
m_state = http_state_error; |
| 432 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler<t_connection_context>::handle_invoke_query_line(): Failed to match first line: " << m_cache); |
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
return false; |
| 437 |
} |
| 438 |
//-------------------------------------------------------------------------------------------- |
| 439 |
template<class t_connection_context> |
| 440 |
std::string::size_type simple_http_connection_handler<t_connection_context>::match_end_of_header(const std::string& buf) |
| 441 |
{ |
| 442 |
|
| 443 |
//Here we returning head size, including terminating sequence (\r\n\r\n or \n\n) |
| 444 |
std::string::size_type res = buf.find("\r\n\r\n"); |
| 445 |
if(std::string::npos != res) |
| 446 |
return res+4; |
| 447 |
res = buf.find("\n\n"); |
| 448 |
if(std::string::npos != res) |
| 449 |
return res+2; |
| 450 |
return res; |
| 451 |
} |
| 452 |
//-------------------------------------------------------------------------------------------- |
| 453 |
template<class t_connection_context> |
| 454 |
bool simple_http_connection_handler<t_connection_context>::analize_cached_request_header_and_invoke_state(size_t pos) |
| 455 |
{ |
| 456 |
LOG_PRINT_L3("HTTP HEAD:\r\n" << m_cache.substr(0, pos)); |
| 457 |
|
| 458 |
m_query_info.m_full_request_buf_size = pos; |
| 459 |
m_query_info.m_request_head.assign(m_cache.begin(), m_cache.begin()+pos); |
| 460 |
|
| 461 |
if(!parse_cached_header(m_query_info.m_header_info, m_cache, pos)) |
| 462 |
{ |
| 463 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler<t_connection_context>::analize_cached_request_header_and_invoke_state(): failed to anilize request header: " << m_cache); |
| 464 |
m_state = http_state_error; |
| 465 |
return false; |
| 466 |
} |
| 467 |
|
| 468 |
m_cache.erase(0, pos); |
| 469 |
|
| 470 |
std::string req_command_str = m_query_info.m_full_request_str; |
| 471 |
//if we have POST or PUT command, it is very possible tha we will get body |
| 472 |
//but now, we suppose than we have body only in case of we have "ContentLength" |
| 473 |
if(m_query_info.m_header_info.m_content_length.size()) |
| 474 |
{ |
| 475 |
m_state = http_state_retriving_body; |
| 476 |
m_body_transfer_type = http_body_transfer_measure; |
| 477 |
if(!get_len_from_content_lenght(m_query_info.m_header_info.m_content_length, m_len_summary)) |
| 478 |
{ |
| 479 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler<t_connection_context>::analize_cached_request_header_and_invoke_state(): Failed to get_len_from_content_lenght();, m_query_info.m_content_length="<<m_query_info.m_header_info.m_content_length); |
| 480 |
m_state = http_state_error; |
| 481 |
return false; |
| 482 |
} |
| 483 |
if(0 == m_len_summary) |
| 484 |
{ //current query finished, next will be next query |
| 485 |
if(handle_request_and_send_response(m_query_info)) |
| 486 |
set_ready_state(); |
| 487 |
else |
| 488 |
m_state = http_state_error; |
| 489 |
} |
| 490 |
m_len_remain = m_len_summary; |
| 491 |
}else |
| 492 |
{//current query finished, next will be next query |
| 493 |
handle_request_and_send_response(m_query_info); |
| 494 |
set_ready_state(); |
| 495 |
} |
| 496 |
|
| 497 |
return true; |
| 498 |
} |
| 499 |
//----------------------------------------------------------------------------------- |
| 500 |
template<class t_connection_context> |
| 501 |
bool simple_http_connection_handler<t_connection_context>::handle_retriving_query_body() |
| 502 |
{ |
| 503 |
switch(m_body_transfer_type) |
| 504 |
{ |
| 505 |
case http_body_transfer_measure: |
| 506 |
return handle_query_measure(); |
| 507 |
case http_body_transfer_chunked: |
| 508 |
case http_body_transfer_connection_close: |
| 509 |
case http_body_transfer_multipart: |
| 510 |
case http_body_transfer_undefined: |
| 511 |
default: |
| 512 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler<t_connection_context>::handle_retriving_query_body(): Unexpected m_body_query_type state:" << m_body_transfer_type); |
| 513 |
m_state = http_state_error; |
| 514 |
return false; |
| 515 |
} |
| 516 |
|
| 517 |
return true; |
| 518 |
} |
| 519 |
//----------------------------------------------------------------------------------- |
| 520 |
template<class t_connection_context> |
| 521 |
bool simple_http_connection_handler<t_connection_context>::handle_query_measure() |
| 522 |
{ |
| 523 |
|
| 524 |
if(m_len_remain >= m_cache.size()) |
| 525 |
{ |
| 526 |
m_len_remain -= m_cache.size(); |
| 527 |
m_query_info.m_body += m_cache; |
| 528 |
m_cache.clear(); |
| 529 |
}else |
| 530 |
{ |
| 531 |
m_query_info.m_body.append(m_cache.begin(), m_cache.begin() + m_len_remain); |
| 532 |
m_cache.erase(0, m_len_remain); |
| 533 |
m_len_remain = 0; |
| 534 |
} |
| 535 |
|
| 536 |
if(!m_len_remain) |
| 537 |
{ |
| 538 |
if(handle_request_and_send_response(m_query_info)) |
| 539 |
set_ready_state(); |
| 540 |
else |
| 541 |
m_state = http_state_error; |
| 542 |
} |
| 543 |
return true; |
| 544 |
} |
| 545 |
//-------------------------------------------------------------------------------------------- |
| 546 |
template<class t_connection_context> |
| 547 |
bool simple_http_connection_handler<t_connection_context>::parse_cached_header(http_header_info& body_info, const std::string& m_cache_to_process, size_t pos) |
| 548 |
{ |
| 549 |
STATIC_REGEXP_EXPR_1(rexp_mach_field, |
| 550 |
"\n?((Connection)|(Referer)|(Content-Length)|(Content-Type)|(Transfer-Encoding)|(Content-Encoding)|(Host)|(Cookie)|(User-Agent)|(Origin)" |
| 551 |
// 12 3 4 5 6 7 8 9 10 11 |
| 552 |
"|([\\w-]+?)) ?: ?((.*?)(\r?\n))[^\t ]", |
| 553 |
//11 1213 14 |
| 554 |
boost::regex::icase | boost::regex::normal); |
| 555 |
|
| 556 |
boost::smatch result; |
| 557 |
std::string::const_iterator it_current_bound = m_cache_to_process.begin(); |
| 558 |
std::string::const_iterator it_end_bound = m_cache_to_process.begin()+pos; |
| 559 |
|
| 560 |
body_info.clear(); |
| 561 |
|
| 562 |
//lookup all fields and fill well-known fields |
| 563 |
while( boost::regex_search( it_current_bound, it_end_bound, result, rexp_mach_field, boost::match_default) && result[0].matched) |
| 564 |
{ |
| 565 |
const size_t field_val = 14; |
| 566 |
const size_t field_etc_name = 12; |
| 567 |
|
| 568 |
int i = 2; //start position = 2 |
| 569 |
if(result[i++].matched)//"Connection" |
| 570 |
body_info.m_connection = result[field_val]; |
| 571 |
else if(result[i++].matched)//"Referer" |
| 572 |
body_info.m_referer = result[field_val]; |
| 573 |
else if(result[i++].matched)//"Content-Length" |
| 574 |
body_info.m_content_length = result[field_val]; |
| 575 |
else if(result[i++].matched)//"Content-Type" |
| 576 |
body_info.m_content_type = result[field_val]; |
| 577 |
else if(result[i++].matched)//"Transfer-Encoding" |
| 578 |
body_info.m_transfer_encoding = result[field_val]; |
| 579 |
else if(result[i++].matched)//"Content-Encoding" |
| 580 |
body_info.m_content_encoding = result[field_val]; |
| 581 |
else if(result[i++].matched)//"Host" |
| 582 |
body_info.m_host = result[field_val]; |
| 583 |
else if(result[i++].matched)//"Cookie" |
| 584 |
body_info.m_cookie = result[field_val]; |
| 585 |
else if(result[i++].matched)//"User-Agent" |
| 586 |
body_info.m_user_agent = result[field_val]; |
| 587 |
else if(result[i++].matched)//"Origin" |
| 588 |
body_info.m_origin = result[field_val]; |
| 589 |
else if(result[i++].matched)//e.t.c (HAVE TO BE MATCHED!) |
| 590 |
body_info.m_etc_fields.push_back(std::pair<std::string, std::string>(result[field_etc_name], result[field_val])); |
| 591 |
else |
| 592 |
{ |
| 593 |
LOG_ERROR_CC(m_conn_context, "simple_http_connection_handler<t_connection_context>::parse_cached_header() not matched last entry in:" << m_cache_to_process); |
| 594 |
} |
| 595 |
|
| 596 |
it_current_bound = result[(int)result.size()-1]. first; |
| 597 |
} |
| 598 |
return true; |
| 599 |
} |
| 600 |
//----------------------------------------------------------------------------------- |
| 601 |
template<class t_connection_context> |
| 602 |
bool simple_http_connection_handler<t_connection_context>::get_len_from_content_lenght(const std::string& str, size_t& OUT len) |
| 603 |
{ |
| 604 |
STATIC_REGEXP_EXPR_1(rexp_mach_field, "\\d+", boost::regex::normal); |
| 605 |
std::string res; |
| 606 |
boost::smatch result; |
| 607 |
if(!(boost::regex_search( str, result, rexp_mach_field, boost::match_default) && result[0].matched)) |
| 608 |
return false; |
| 609 |
|
| 610 |
try { len = boost::lexical_cast<size_t>(result[0]); } |
| 611 |
catch(...) { return false; } |
| 612 |
return true; |
| 613 |
} |
| 614 |
//----------------------------------------------------------------------------------- |
| 615 |
template<class t_connection_context> |
| 616 |
bool simple_http_connection_handler<t_connection_context>::handle_request_and_send_response(const http::http_request_info& query_info) |
| 617 |
{ |
| 618 |
http_response_info response{}; |
| 619 |
//CHECK_AND_ASSERT_MES(res, res, "handle_request(query_info, response) returned false" ); |
| 620 |
bool res = true; |
| 621 |
|
| 622 |
if (query_info.m_http_method != http::http_method_options) |
| 623 |
{ |
| 624 |
res = handle_request(query_info, response); |
| 625 |
if (response.m_response_code == 500) |
| 626 |
{ |
| 627 |
m_want_close = true; // close on all "Internal server error"s |
| 628 |
} |
| 629 |
} |
| 630 |
else |
| 631 |
{ |
| 632 |
response.m_response_code = 200; |
| 633 |
response.m_response_comment = "OK"; |
| 634 |
} |
| 635 |
|
| 636 |
std::string response_data = get_response_header(response); |
| 637 |
//LOG_PRINT_L0("HTTP_SEND: << \r\n" << response_data + response.m_body); |
| 638 |
|
| 639 |
LOG_PRINT_L3("HTTP_RESPONSE_HEAD: << \r\n" << response_data); |
| 640 |
|
| 641 |
if ((response.m_body.size() && (query_info.m_http_method != http::http_method_head)) || (query_info.m_http_method == http::http_method_options)) |
| 642 |
response_data += response.m_body; |
| 643 |
|
| 644 |
m_psnd_hndlr->do_send(byte_slice{std::move(response_data)}); |
| 645 |
m_psnd_hndlr->send_done(); |
| 646 |
return res; |
| 647 |
} |
| 648 |
//----------------------------------------------------------------------------------- |
| 649 |
template<class t_connection_context> |
| 650 |
bool simple_http_connection_handler<t_connection_context>::handle_request(const http::http_request_info& query_info, http_response_info& response) |
| 651 |
{ |
| 652 |
|
| 653 |
std::string uri_to_path = query_info.m_uri_content.m_path; |
| 654 |
if("/" == uri_to_path) |
| 655 |
uri_to_path = "/index.html"; |
| 656 |
|
| 657 |
//slash_to_back_slash(uri_to_path); |
| 658 |
m_config.m_lock.lock(); |
| 659 |
std::string destination_file_path = m_config.m_folder + uri_to_path; |
| 660 |
m_config.m_lock.unlock(); |
| 661 |
if(!file_io_utils::load_file_to_string(destination_file_path.c_str(), response.m_body)) |
| 662 |
{ |
| 663 |
MWARNING("URI \""<< query_info.m_full_request_str.substr(0, query_info.m_full_request_str.size()-2) << "\" [" << destination_file_path << "] Not Found (404 )"); |
| 664 |
response.m_body = get_not_found_response_body(query_info.m_URI); |
| 665 |
response.m_response_code = 404; |
| 666 |
response.m_response_comment = "Not found"; |
| 667 |
response.m_mime_tipe = "text/html"; |
| 668 |
return true; |
| 669 |
} |
| 670 |
|
| 671 |
MDEBUG(" -->> " << query_info.m_full_request_str << "\r\n<<--OK"); |
| 672 |
response.m_response_code = 200; |
| 673 |
response.m_response_comment = "OK"; |
| 674 |
response.m_mime_tipe = get_file_mime_tipe(uri_to_path); |
| 675 |
|
| 676 |
return true; |
| 677 |
} |
| 678 |
//----------------------------------------------------------------------------------- |
| 679 |
template<class t_connection_context> |
| 680 |
std::string simple_http_connection_handler<t_connection_context>::get_response_header(const http_response_info& response) |
| 681 |
{ |
| 682 |
std::string buf = "HTTP/1.1 "; |
| 683 |
buf += boost::lexical_cast<std::string>(response.m_response_code) + " " + response.m_response_comment + "\r\n" + |
| 684 |
"Server: Epee-based\r\n" |
| 685 |
"Content-Length: "; |
| 686 |
buf += boost::lexical_cast<std::string>(response.m_body.size()) + "\r\n"; |
| 687 |
|
| 688 |
if(!response.m_mime_tipe.empty()) |
| 689 |
{ |
| 690 |
buf += "Content-Type: "; |
| 691 |
buf += response.m_mime_tipe + "\r\n"; |
| 692 |
} |
| 693 |
|
| 694 |
buf += "Last-Modified: "; |
| 695 |
time_t tm; |
| 696 |
time(&tm); |
| 697 |
buf += misc_utils::get_internet_time_str(tm) + "\r\n"; |
| 698 |
buf += "Accept-Ranges: bytes\r\n"; |
| 699 |
//Wed, 01 Dec 2010 03:27:41 GMT" |
| 700 |
|
| 701 |
string_tools::trim(m_query_info.m_header_info.m_connection); |
| 702 |
if(m_query_info.m_header_info.m_connection.size()) |
| 703 |
{ |
| 704 |
if(!string_tools::compare_no_case("close", m_query_info.m_header_info.m_connection)) |
| 705 |
{ |
| 706 |
//closing connection after sending |
| 707 |
buf += "Connection: close\r\n"; |
| 708 |
m_state = http_state_connection_close; |
| 709 |
m_want_close = true; |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
// Cross-origin resource sharing |
| 714 |
if(m_query_info.m_header_info.m_origin.size()) |
| 715 |
{ |
| 716 |
if (std::binary_search(m_config.m_access_control_origins.begin(), m_config.m_access_control_origins.end(), "*") || std::binary_search(m_config.m_access_control_origins.begin(), m_config.m_access_control_origins.end(), m_query_info.m_header_info.m_origin)) |
| 717 |
{ |
| 718 |
buf += "Access-Control-Allow-Origin: "; |
| 719 |
buf += m_query_info.m_header_info.m_origin; |
| 720 |
buf += "\r\n"; |
| 721 |
buf += "Access-Control-Expose-Headers: www-authenticate\r\n"; |
| 722 |
if (m_query_info.m_http_method == http::http_method_options) |
| 723 |
buf += "Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With\r\n"; |
| 724 |
buf += "Access-Control-Allow-Methods: POST, PUT, GET, OPTIONS\r\n"; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
//add additional fields, if it is |
| 729 |
for(fields_list::const_iterator it = response.m_additional_fields.begin(); it!=response.m_additional_fields.end(); it++) |
| 730 |
buf += it->first + ": " + it->second + "\r\n"; |
| 731 |
|
| 732 |
buf+="\r\n"; |
| 733 |
|
| 734 |
return buf; |
| 735 |
} |
| 736 |
//----------------------------------------------------------------------------------- |
| 737 |
template<class t_connection_context> |
| 738 |
std::string simple_http_connection_handler<t_connection_context>::get_file_mime_tipe(const std::string& path) |
| 739 |
{ |
| 740 |
std::string result; |
| 741 |
std::string ext = string_tools::get_extension(path); |
| 742 |
if(!string_tools::compare_no_case(ext, "gif")) |
| 743 |
result = "image/gif"; |
| 744 |
else if(!string_tools::compare_no_case(ext, "jpg")) |
| 745 |
result = "image/jpeg"; |
| 746 |
else if(!string_tools::compare_no_case(ext, "html")) |
| 747 |
result = "text/html"; |
| 748 |
else if(!string_tools::compare_no_case(ext, "htm")) |
| 749 |
result = "text/html"; |
| 750 |
else if(!string_tools::compare_no_case(ext, "js")) |
| 751 |
result = "application/x-javascript"; |
| 752 |
else if(!string_tools::compare_no_case(ext, "css")) |
| 753 |
result = "text/css"; |
| 754 |
else if(!string_tools::compare_no_case(ext, "xml")) |
| 755 |
result = "application/xml"; |
| 756 |
else if(!string_tools::compare_no_case(ext, "svg")) |
| 757 |
result = "image/svg+xml"; |
| 758 |
|
| 759 |
|
| 760 |
return result; |
| 761 |
} |
| 762 |
//----------------------------------------------------------------------------------- |
| 763 |
template<class t_connection_context> |
| 764 |
std::string simple_http_connection_handler<t_connection_context>::get_not_found_response_body(const std::string& URI) |
| 765 |
{ |
| 766 |
std::string body = |
| 767 |
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n" |
| 768 |
"<html><head>\r\n" |
| 769 |
"<title>404 Not Found</title>\r\n" |
| 770 |
"</head><body>\r\n" |
| 771 |
"<h1>Not Found</h1>\r\n" |
| 772 |
"<p>The requested URL \r\n"; |
| 773 |
body += URI; |
| 774 |
body += "was not found on this server.</p>\r\n" |
| 775 |
"</body></html>\r\n"; |
| 776 |
|
| 777 |
return body; |
| 778 |
} |
| 779 |
//-------------------------------------------------------------------------------------------- |
| 780 |
template<class t_connection_context> |
| 781 |
bool simple_http_connection_handler<t_connection_context>::slash_to_back_slash(std::string& str) |
| 782 |
{ |
| 783 |
for(std::string::iterator it = str.begin(); it!=str.end(); it++) |
| 784 |
if('/' == *it) |
| 785 |
*it = '\\'; |
| 786 |
return true; |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
//-------------------------------------------------------------------------------------------- |
| 793 |
//-------------------------------------------------------------------------------------------- |
| 794 |
//-------------------------------------------------------------------------------------------- |