Electroneum
Loading...
Searching...
No Matches
epee::net_utils::http::http_simple_client_template< net_client_type > Class Template Reference

#include <http_client.h>

Inheritance diagram for epee::net_utils::http::http_simple_client_template< net_client_type >:
Collaboration diagram for epee::net_utils::http::http_simple_client_template< net_client_type >:

Public Member Functions

 http_simple_client_template ()
const std::string & get_host () const
const std::string & get_port () const
bool set_server (const std::string &address, boost::optional< login > user, ssl_options_t ssl_options=ssl_support_t::e_ssl_support_autodetect)
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)
void set_auto_connect (bool auto_connect)
template<typename F>
void set_connector (F connector)
bool connect (std::chrono::milliseconds timeout)
bool disconnect ()
bool is_connected (bool *ssl=NULL)
virtual bool handle_target_data (std::string &piece_of_transfer)
virtual bool on_header (const http_response_info &headers)
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())
bool invoke (const boost::string_ref uri, const boost::string_ref method, const std::string &body, std::chrono::milliseconds timeout, const http_response_info **ppresponse_info=NULL, const fields_list &additional_params=fields_list())
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())
bool test (const std::string &s, std::chrono::milliseconds timeout)
uint64_t get_bytes_sent () const
uint64_t get_bytes_received () const
Public Member Functions inherited from epee::net_utils::i_target_handler
virtual ~i_target_handler ()

Detailed Description

template<typename net_client_type>
class epee::net_utils::http::http_simple_client_template< net_client_type >

Definition at line 241 of file http_client.h.

Constructor & Destructor Documentation

◆ http_simple_client_template()

template<typename net_client_type>
epee::net_utils::http::http_simple_client_template< net_client_type >::http_simple_client_template ( )
inlineexplicit

Definition at line 281 of file http_client.h.

283 , m_net_client()
284 , m_host_buff()
285 , m_port()
286 , m_auth()
287 , m_header_cache()
288 , m_response_info()
289 , m_len_in_summary(0)
290 , m_len_in_remain(0)
291 , m_pcontent_encoding_handler(nullptr)
292 , m_state()
293 , m_chunked_state()
294 , m_chunked_cache()
295 , m_auto_connect(true)
296 , m_lock()
297 {}

Member Function Documentation

◆ connect()

template<typename net_client_type>
bool epee::net_utils::http::http_simple_client_template< net_client_type >::connect ( std::chrono::milliseconds timeout)
inline

Definition at line 333 of file http_client.h.

334 {
335 CRITICAL_REGION_LOCAL(m_lock);
336 return m_net_client.connect(m_host_buff, m_port, timeout);
337 }
#define CRITICAL_REGION_LOCAL(x)
Definition syncobj.h:228
Here is the caller graph for this function:

◆ disconnect()

template<typename net_client_type>
bool epee::net_utils::http::http_simple_client_template< net_client_type >::disconnect ( )
inline

Definition at line 339 of file http_client.h.

340 {
341 CRITICAL_REGION_LOCAL(m_lock);
342 return m_net_client.disconnect();
343 }
Here is the caller graph for this function:

◆ get_bytes_received()

template<typename net_client_type>
uint64_t epee::net_utils::http::http_simple_client_template< net_client_type >::get_bytes_received ( ) const
inline

Definition at line 464 of file http_client.h.

465 {
466 return m_net_client.get_bytes_received();
467 }

◆ get_bytes_sent()

template<typename net_client_type>
uint64_t epee::net_utils::http::http_simple_client_template< net_client_type >::get_bytes_sent ( ) const
inline

Definition at line 459 of file http_client.h.

460 {
461 return m_net_client.get_bytes_sent();
462 }

◆ get_host()

template<typename net_client_type>
const std::string & epee::net_utils::http::http_simple_client_template< net_client_type >::get_host ( ) const
inline

Definition at line 299 of file http_client.h.

299{ return m_host_buff; };

◆ get_port()

template<typename net_client_type>
const std::string & epee::net_utils::http::http_simple_client_template< net_client_type >::get_port ( ) const
inline

Definition at line 300 of file http_client.h.

300{ return m_port; };

◆ handle_target_data()

template<typename net_client_type>
virtual bool epee::net_utils::http::http_simple_client_template< net_client_type >::handle_target_data ( std::string & piece_of_transfer)
inlinevirtual

Implements epee::net_utils::i_target_handler.

Definition at line 351 of file http_client.h.

352 {
353 CRITICAL_REGION_LOCAL(m_lock);
354 m_response_info.m_body += piece_of_transfer;
355 piece_of_transfer.clear();
356 return true;
357 }

◆ invoke()

template<typename net_client_type>
bool epee::net_utils::http::http_simple_client_template< net_client_type >::invoke ( const boost::string_ref uri,
const boost::string_ref method,
const std::string & body,
std::chrono::milliseconds timeout,
const http_response_info ** ppresponse_info = NULL,
const fields_list & additional_params = fields_list() )
inline

Definition at line 372 of file http_client.h.

373 {
374 CRITICAL_REGION_LOCAL(m_lock);
375 if(!is_connected())
376 {
377 if (!m_auto_connect)
378 {
379 MWARNING("Auto connect attempt to " << m_host_buff << ":" << m_port << " disabled");
380 return false;
381 }
382 MDEBUG("Reconnecting...");
383 if(!connect(timeout))
384 {
385 MDEBUG("Failed to connect to " << m_host_buff << ":" << m_port);
386 return false;
387 }
388 }
389
391 req_buff.reserve(2048);
392 req_buff.append(method.data(), method.size()).append(" ").append(uri.data(), uri.size()).append(" HTTP/1.1\r\n");
393 add_field(req_buff, "Host", m_host_buff);
394 add_field(req_buff, "Content-Length", std::to_string(body.size()));
395
396 //handle "additional_params"
397 for(const auto& field : additional_params)
399
400 for (unsigned sends = 0; sends < 2; ++sends)
401 {
402 const std::size_t initial_size = req_buff.size();
403 const auto auth = m_auth.get_auth_field(method, uri);
404 if (auth)
406
407 req_buff += "\r\n";
408 //--
409
410 bool res = m_net_client.send(req_buff, timeout);
411 CHECK_AND_NO_ASSERT_MES_L(res, false, 1, "HTTP_CLIENT: Failed to SEND");
412 if(body.size())
413 res = m_net_client.send(body, timeout);
414 CHECK_AND_ASSERT_MES(res, false, "HTTP_CLIENT: Failed to SEND");
415
416 m_response_info.clear();
417 m_state = reciev_machine_state_header;
418 if (!handle_reciev(timeout))
419 return false;
420 if (m_response_info.m_response_code != 401)
421 {
423 *ppresponse_info = std::addressof(m_response_info);
424 return true;
425 }
426
427 switch (m_auth.handle_401(m_response_info))
428 {
430 break;
432 sends = 2;
433 break;
434 default:
436 LOG_ERROR("Bad server response for authentication");
437 return false;
438 }
439 req_buff.resize(initial_size); // rollback for new auth generation
440 }
441 LOG_ERROR("Client has incorrect username/password for server requiring authentication");
442 return false;
443 }
bool connect(std::chrono::milliseconds timeout)
#define MWARNING(x)
Definition misc_log_ex.h:74
#define MDEBUG(x)
Definition misc_log_ex.h:76
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
#define LOG_ERROR(x)
Definition misc_log_ex.h:98
#define CHECK_AND_NO_ASSERT_MES_L(expr, fail_ret_val, l, message)
Here is the caller graph for this function:

◆ invoke_get()

template<typename net_client_type>
bool epee::net_utils::http::http_simple_client_template< net_client_type >::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() )
inline

Definition at line 365 of file http_client.h.

366 {
367 CRITICAL_REGION_LOCAL(m_lock);
369 }
bool invoke(const boost::string_ref uri, const boost::string_ref method, const std::string &body, std::chrono::milliseconds timeout, const http_response_info **ppresponse_info=NULL, const fields_list &additional_params=fields_list())

◆ invoke_post()

template<typename net_client_type>
bool epee::net_utils::http::http_simple_client_template< net_client_type >::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() )
inline

Definition at line 445 of file http_client.h.

446 {
447 CRITICAL_REGION_LOCAL(m_lock);
449 }

◆ is_connected()

template<typename net_client_type>
bool epee::net_utils::http::http_simple_client_template< net_client_type >::is_connected ( bool * ssl = NULL)
inline

Definition at line 345 of file http_client.h.

346 {
347 CRITICAL_REGION_LOCAL(m_lock);
348 return m_net_client.is_connected(ssl);
349 }
Here is the caller graph for this function:

◆ on_header()

template<typename net_client_type>
virtual bool epee::net_utils::http::http_simple_client_template< net_client_type >::on_header ( const http_response_info & headers)
inlinevirtual

Definition at line 359 of file http_client.h.

360 {
361 return true;
362 }

◆ set_auto_connect()

template<typename net_client_type>
void epee::net_utils::http::http_simple_client_template< net_client_type >::set_auto_connect ( bool auto_connect)
inline

Definition at line 321 of file http_client.h.

322 {
323 m_auto_connect = auto_connect;
324 }

◆ set_connector()

template<typename net_client_type>
template<typename F>
void epee::net_utils::http::http_simple_client_template< net_client_type >::set_connector ( F connector)
inline

Definition at line 327 of file http_client.h.

328 {
329 CRITICAL_REGION_LOCAL(m_lock);
330 m_net_client.set_connector(std::move(connector));
331 }

◆ set_server() [1/2]

template<typename net_client_type>
bool epee::net_utils::http::http_simple_client_template< net_client_type >::set_server ( const std::string & address,
boost::optional< login > user,
ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect )
inline

Definition at line 302 of file http_client.h.

303 {
305 const bool r = parse_url(address, parsed);
306 CHECK_AND_ASSERT_MES(r, false, "failed to parse url: " << address);
308 return true;
309 }
bool set_server(const std::string &address, boost::optional< login > user, ssl_options_t ssl_options=ssl_support_t::e_ssl_support_autodetect)
bool parse_url(const std::string url_str, http::url_content &content)
Here is the caller graph for this function:

◆ set_server() [2/2]

template<typename net_client_type>
void epee::net_utils::http::http_simple_client_template< net_client_type >::set_server ( std::string host,
std::string port,
boost::optional< login > user,
ssl_options_t ssl_options = ssl_support_t::e_ssl_support_autodetect )
inline

Definition at line 311 of file http_client.h.

312 {
313 CRITICAL_REGION_LOCAL(m_lock);
314 disconnect();
315 m_host_buff = std::move(host);
316 m_port = std::move(port);
318 m_net_client.set_ssl(std::move(ssl_options));
319 }

◆ test()

template<typename net_client_type>
bool epee::net_utils::http::http_simple_client_template< net_client_type >::test ( const std::string & s,
std::chrono::milliseconds timeout )
inline

Definition at line 451 of file http_client.h.

452 {
453 CRITICAL_REGION_LOCAL(m_lock);
454 m_net_client.set_test_data(s);
455 m_state = reciev_machine_state_header;
456 return handle_reciev(timeout);
457 }

The documentation for this class was generated from the following file:
  • /home/abuild/rpmbuild/BUILD/electroneum-5.1.3.1-build/electroneum-5.1.3.1/contrib/epee/include/net/http_client.h