31 #include <boost/shared_ptr.hpp> 32 #include <boost/regex.hpp> 33 #include <boost/optional/optional.hpp> 34 #include <boost/utility/string_ref.hpp> 55 #undef MONERO_DEFAULT_LOG_CATEGORY 56 #define MONERO_DEFAULT_LOG_CATEGORY "net.http" 106 template<
typename net_client_type>
192 bool connect(std::chrono::milliseconds timeout)
override 214 piece_of_transfer.clear();
226 return invoke(
uri,
"GET", body, timeout, ppresponse_info, additional_params);
230 inline bool invoke(
const boost::string_ref
uri,
const boost::string_ref method,
const boost::string_ref body, std::chrono::milliseconds timeout,
const http_response_info** ppresponse_info = NULL,
const fields_list& additional_params =
fields_list())
override 237 MWARNING(
"Auto connect attempt to " <<
m_host_buff <<
":" <<
m_port <<
" disabled");
240 MDEBUG(
"Reconnecting...");
249 req_buff.reserve(2048);
250 req_buff.append(method.data(), method.size()).append(
" ").append(
uri.data(),
uri.size()).append(
" HTTP/1.1\r\n");
255 for(
const auto& field : additional_params)
258 for (
unsigned sends = 0; sends < 2; ++sends)
260 const std::size_t initial_size = req_buff.size();
269 CHECK_AND_ASSERT_MES(
res,
false,
"HTTP_CLIENT: Failed to SEND");
272 CHECK_AND_ASSERT_MES(
res,
false,
"HTTP_CLIENT: Failed to SEND");
294 LOG_ERROR(
"Bad server response for authentication");
297 req_buff.resize(initial_size);
299 LOG_ERROR(
"Client has incorrect username/password for server requiring authentication");
306 return invoke(
uri,
"POST", body, timeout, ppresponse_info, additional_params);
337 bool keep_handling =
true;
338 bool need_more_data =
true;
346 MERROR(
"Unexpected recv fail");
349 if(!recv_buffer.size())
357 need_more_data =
false;
374 keep_handling =
false;
377 keep_handling =
false;
392 LOG_PRINT_L3(
"Returning false because of wrong state machine. state: " <<
m_state);
402 if(!recv_buff.size())
404 LOG_ERROR(
"Connection closed at handle_header");
412 if(pos != std::string::npos)
420 MDEBUG(
"Connection cancelled by on_header");
426 need_more_data =
true;
430 need_more_data =
true;
438 if(!recv_buff.size())
440 MERROR(
"Warning: Content-Len mode, but connection unexpectedly closed");
444 CHECK_AND_ASSERT_MES(
m_len_in_remain >= recv_buff.size(),
false,
"m_len_in_remain >= recv_buff.size()");
455 need_more_data =
true;
464 if(!recv_buff.size())
469 need_more_data =
true;
479 if( (ch >=
'0' && ch <=
'9')||(ch >=
'A' && ch <=
'F')||(ch >=
'a' && ch <=
'f'))
488 std::stringstream str_stream;
490 if(!(str_stream << chunk_head && str_stream >> result_size))
501 for(std::string::iterator it = buff.begin(); it!= buff.end(); it++, offset++)
505 if(*it ==
'\r' || *it ==
' ' )
522 for(it++;it != buff.end(); it++)
530 LOG_ERROR(
"http_stream_filter: Wrong last chunk terminator");
539 buff.erase(buff.begin(), ++it);
556 if(!recv_buff.size())
558 MERROR(
"Warning: CHUNKED mode, but connection unexpectedly closed");
564 bool is_matched =
false;
570 need_more_data =
true;
588 LOG_ERROR(
"http_stream_filter::handle_chunked(*) Failed to get length from chunked head:" <<
m_chunked_cache);
595 need_more_data =
true;
638 LOG_ERROR(
"http_stream_filter::handle_chunked(): Wrong state" <<
m_chunked_state);
648 MTRACE(
"http_stream_filter::parse_cached_header(*)");
650 const char *ptr = m_cache_to_process.c_str();
651 while (ptr[0] !=
'\r' || ptr[1] !=
'\n')
657 const char *key_pos = ptr;
658 while (isalnum(*ptr) || *ptr ==
'_' || *ptr ==
'-')
660 const char *key_end = ptr;
664 CHECK_AND_ASSERT_MES(*ptr ==
':',
true,
"http_stream_filter::parse_cached_header() invalid header in: " << m_cache_to_process);
667 while (isblank(*ptr))
669 const char *value_pos = ptr;
670 while (*ptr !=
'\r' && *ptr !=
'\n')
672 const char *value_end = ptr;
674 while (value_end > value_pos && isblank(*(value_end-1)))
678 CHECK_AND_ASSERT_MES(*ptr ==
'\n',
true,
"http_stream_filter::parse_cached_header() invalid header in: " << m_cache_to_process);
716 CHECK_AND_ASSERT_MES(!memcmp(ptr,
"HTTP/", 5),
false,
"Invalid first response line: " +
m_header_cache);
721 ul = strtoul(ptr, &end, 10);
722 CHECK_AND_ASSERT_MES(ul <= INT_MAX && *end ==
'.',
false,
"Invalid first response line: " +
m_header_cache);
726 ul = strtoul(ptr, &end, 10);
727 CHECK_AND_ASSERT_MES(ul <= INT_MAX && isblank(*end),
false,
"Invalid first response line: " +
m_header_cache +
", ptr: " << ptr);
730 while (isblank(*ptr))
733 ul = strtoul(ptr, &end, 10);
734 CHECK_AND_ASSERT_MES(ul >= 100 && ul <= 999 &&
isspace(*end),
false,
"Invalid first response line: " +
m_header_cache);
738 while (*ptr !=
'\r' && *ptr !=
'\n')
742 CHECK_AND_ASSERT_MES(*ptr ==
'\n',
false,
"Invalid first response line: " <<
m_header_cache);
751 STATIC_REGEXP_EXPR_1(rexp_match_gzip,
"^.*?((gzip)|(deflate))", boost::regex::icase | boost::regex::normal);
756 LOG_ERROR(
"GZIP encoding not supported");
774 CHECK_AND_ASSERT_MES(
res,
false,
"http_stream_filter::analize_cached_reply_header_and_invoke_state(): failed to anilize reply header: " <<
m_header_cache);
779 bool content_len_valid =
false;
807 if(!content_len_valid)
830 LOG_ERROR(
"Unsupported MULTIPART BODY.");
835 MERROR(
"Undefined transfer type, consider http_body_transfer_connection_close method. header: " <<
m_header_cache);
843 STATIC_REGEXP_EXPR_1(rexp_match_close,
"^\\s*close", boost::regex::icase | boost::regex::normal);
845 if(boost::regex_search(
str,
result, rexp_match_close, boost::match_default) &&
result[0].matched)
854 STATIC_REGEXP_EXPR_1(rexp_match_multipart_type,
"^\\s*multipart/([\\w\\-]+); boundary=((\"(.*?)\")|(\\\\\"(.*?)\\\\\")|([^\\s;]*))", boost::regex::icase | boost::regex::normal);
856 if(boost::regex_search(head_info.
m_content_type,
result, rexp_match_multipart_type, boost::match_default) &&
result[0].matched)
860 else if(
result[6].matched)
862 else if(
result[7].matched)
866 LOG_ERROR(
"Failed to match boundary in content-type=" << head_info.
m_content_type);
const char * res
Definition: hmac_keccak.cpp:42
#define STATIC_REGEXP_EXPR_1(var_name, xpr_text, reg_exp_flags)
Definition: reg_exp_definer.h:48
std::list< std::pair< std::string, std::string > > fields_list
Definition: http_base.h:66
static void add_field(std::string &out, const boost::string_ref name, const boost::string_ref value)
Definition: http_base.h:72
bool isdigit(char c)
Definition: parserse_base_utils.h:91
http_simple_client_template()
Definition: http_client.h:147
reciev_machine_state m_state
Definition: http_client.h:140
Definition: http_client_base.h:53
http_header_info m_header_info
Definition: http_base.h:169
::std::string string
Definition: gtest-port.h:1097
Implements RFC 2617 digest auth. Digests from RFC 7616 can be added.
Definition: http_auth.h:94
#define F(w, k)
Definition: sha512-blocks.c:61
virtual bool on_header(const http_response_info &headers)
Definition: http_client.h:218
bool is_multipart_body(const http_header_info &head_info, OUT std::string &boundary)
Definition: http_client.h:851
void set_server(std::string host, std::string port, boost::optional< login > user, ssl_options_t ssl_options=ssl_support_t::e_ssl_support_autodetect) override
Definition: http_client.h:170
net_client_type m_net_client
Definition: http_client.h:130
host
Definition: console.py:27
const std::string & get_port() const
Definition: http_client.h:166
bool isspace(char c)
Definition: parserse_base_utils.h:86
const char * key
Definition: hmac_keccak.cpp:40
Definition: http_client.h:113
const char * s
Definition: minissdp.c:596
int m_http_ver_hi
Definition: http_base.h:170
bool set_server(const std::string &address, boost::optional< login > user, ssl_options_t ssl_options=ssl_support_t::e_ssl_support_autodetect)
Definition: abstract_http_client.cpp:134
bool invoke_get(const boost::string_ref uri, std::chrono::milliseconds timeout, const std::string &body=std::string(), const http_response_info **ppresponse_info=NULL, const fields_list &additional_params=fields_list()) override
Definition: http_client.h:223
Definition: http_client.h:107
tools::wallet2::message_signature_result_t result
Definition: signature.cpp:62
Definition: abstract_http_client.h:59
void wipe_response()
Definition: http_client.h:327
Definition: http_client.h:124
bool get_len_from_chunk_head(const std::string &chunk_head, size_t &result_size)
Definition: http_client.h:486
bool analize_cached_header_and_invoke_state()
Definition: http_client.h:767
http_client_auth m_auth
Definition: http_client.h:133
boost::shared_ptr< i_sub_handler > m_pcontent_encoding_handler
Definition: http_client.h:139
bool get_chunk_head(std::string &buff, size_t &chunk_size, bool &is_matched)
Definition: http_client.h:497
uint64_t get_bytes_sent() const override
Definition: http_client.h:317
size_t m_len_in_summary
Definition: http_client.h:136
std::string m_host_buff
Definition: http_client.h:131
bool test(const std::string &s, std::chrono::milliseconds timeout)
Definition: http_client.h:309
Definition: http_client.h:112
http_simple_client_template< blocked_mode_client > http_simple_client
Definition: http_client.h:877
bool handle_header(std::string &recv_buff, bool &need_more_data)
Definition: http_client.h:398
void clear()
Definition: http_base.h:173
Definition: http_client_base.h:60
bool invoke_post(const boost::string_ref uri, const std::string &body, std::chrono::milliseconds timeout, const http_response_info **ppresponse_info=NULL, const fields_list &additional_params=fields_list()) override
Definition: http_client.h:303
status handle_401(const http_response_info &response)
Definition: http_auth.h:143
bool is_connected(bool *ssl=NULL) override
Definition: http_client.h:204
Definition: http_base.h:162
http_response_info m_response_info
Definition: http_client.h:135
Definition: http_client.h:117
bool m_auto_connect
Definition: http_client.h:143
chunked_state m_chunked_state
Definition: http_client.h:141
Definition: http_client.h:116
unsigned __int64 uint64_t
Definition: stdint.h:136
#define CRITICAL_REGION_LOCAL(x)
Definition: syncobj.h:153
const std::string & get_host() const
Definition: http_client.h:165
Definition: http_client.h:125
critical_section m_lock
Definition: http_client.h:144
int m_response_code
Definition: http_base.h:164
uint64_t get_bytes_received() const override
Definition: http_client.h:322
reciev_machine_state
Definition: http_client.h:110
bool analize_first_response_line()
Definition: http_client.h:712
int m_http_ver_lo
Definition: http_base.h:171
Definition: http_client.h:126
virtual bool handle_target_data(std::string &piece_of_transfer) override
Definition: http_client.h:210
boost::endian::big_uint16_t port
Definition: socks.cpp:61
Definition: http_auth.h:97
bool parse_header(http_header_info &body_info, const std::string &m_cache_to_process)
Definition: http_client.h:646
const char *const str
Definition: portlistingparse.c:23
bool is_connection_close_field(const std::string &str)
Definition: http_client.h:841
Definition: http_client.h:123
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:39
bool handle_body_body_chunked(std::string &recv_buff, bool &need_more_data)
Definition: http_client.h:553
bool invoke(const boost::string_ref uri, const boost::string_ref method, const boost::string_ref body, std::chrono::milliseconds timeout, const http_response_info **ppresponse_info=NULL, const fields_list &additional_params=fields_list()) override
Definition: http_client.h:230
bool disconnect() override
Definition: http_client.h:198
const T & move(const T &t)
Definition: gtest-port.h:1317
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
epee::critical_section gregexp_lock
void set_auto_connect(bool auto_connect) override
Definition: http_client.h:180
bool set_reply_content_encoder()
Definition: http_client.h:749
std::string m_header_cache
Definition: http_client.h:134
std::string m_chunked_cache
Definition: http_client.h:142
Definition: http_auth.h:97
size_t m_len_in_remain
Definition: http_client.h:137
bool handle_body_content_len(std::string &recv_buff, bool &need_more_data)
Definition: http_client.h:435
bool handle_body_connection_close(std::string &recv_buff, bool &need_more_data)
Definition: http_client.h:461
bool is_hex_symbol(char ch)
Definition: http_client.h:476
Definition: abstract_http_client.h:61
std::string m_body
Definition: http_base.h:167
Definition: http_auth.h:97
Definition: http_client.h:115
std::string to_string(t_connection_type type)
Definition: connection_basic.cpp:70
boost::optional< std::pair< std::string, std::string > > get_auth_field(const boost::string_ref method, const boost::string_ref uri)
Definition: http_auth.h:157
void set_connector(F connector)
Definition: http_client.h:186
bool connect(std::chrono::milliseconds timeout) override
Definition: http_client.h:192
static constexpr const char hex[]
Definition: wipeable_string.cpp:36
#define true
Definition: stdbool.h:36
chunked_state
Definition: http_client.h:122
Definition: http_client.h:114
void wipe()
Definition: http_base.h:179
std::string m_port
Definition: http_client.h:132
bool handle_reciev(std::chrono::milliseconds timeout)
Definition: http_client.h:334