-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpAgent.cpp
More file actions
157 lines (134 loc) · 3.88 KB
/
HttpAgent.cpp
File metadata and controls
157 lines (134 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "HttpAgent.h"
#include <curl/curl.h>
#include <mutex>
#include <cstring>
#include <algorithm>
namespace Utility
{
namespace http
{
namespace
{
std::once_flag curlInit;
}
Initializer::Initializer()
{
std::call_once( curlInit, [](){ ::curl_global_init( CURL_GLOBAL_ALL ); } );
}
Agent::~Agent() noexcept
{
free_slist_();
::curl_easy_cleanup( curl_ );
}
Agent::Agent(std::ostream& os, bool keepeol)
: osi_(os)
, curl_(::curl_easy_init())
, respCode_(200)
, lp_(nullptr)
{
set_options_( keepeol );
}
bool
Agent::get( char const* url ) const
{
errbuf_[0] = '\0';
type_ = nullptr;
::curl_easy_setopt( curl_, CURLOPT_HTTPGET, 1L );
::curl_easy_setopt( curl_, CURLOPT_URL, url );
return perform_();
}
bool
Agent::post( char const* url, char const* data, long size, char const* type ) const
{
errbuf_[0] = '\0';
type_ = nullptr;
::curl_easy_setopt( curl_, CURLOPT_POSTFIELDS, const_cast<char*>(data) );
::curl_easy_setopt( curl_, CURLOPT_POSTFIELDSIZE, size );
if ( type )
{
lp_ = ::curl_slist_append( static_cast<curl_slist*>(lp_), type );
}
::curl_easy_setopt( curl_, CURLOPT_URL, url );
return perform_();
}
Agent&
Agent::set_headers( Headers const& headers )
{
set_headers_( headers );
return *this;
}
Agent&
Agent::set_timeout( long timeout)
{
::curl_easy_setopt( curl_, CURLOPT_TIMEOUT, timeout );
return *this;
}
//=========================================================================
std::size_t
Agent::on_data0_( char* buf, std::size_t len )
{
std::copy( buf, buf + len, osi_ );
return len;
}
std::size_t
Agent::on_data1_( char* buf, std::size_t len )
{
std::copy_if( buf, buf + len, osi_, []( char const c ) -> bool
{
return ::strchr( "\r\n", c ) == nullptr;
} );
return len;
}
std::size_t /* static */
Agent::wr_cb0( char* buf, std::size_t size, std::size_t nmemb, void* optr )
{
return static_cast<Agent*>(optr)->on_data0_( buf, size * nmemb );
}
std::size_t /* static */
Agent::wr_cb1( char* buf, std::size_t size, std::size_t nmemb, void* optr )
{
return static_cast<Agent*>(optr)->on_data1_( buf, size * nmemb );
}
void
Agent::set_options_( bool keepeol ) const
{
::curl_easy_setopt( curl_, CURLOPT_ERRORBUFFER, errbuf_ );
::curl_easy_setopt( curl_, CURLOPT_VERBOSE, 0L );
::curl_easy_setopt( curl_, CURLOPT_WRITEFUNCTION, keepeol ? &wr_cb0 : &wr_cb1 );
::curl_easy_setopt( curl_, CURLOPT_FILE, this );
}
void
Agent::set_headers_( Headers const& headers ) const
{
if ( headers.empty() ) { return; }
for ( auto& hdr : headers )
{
lp_ = ::curl_slist_append( static_cast<curl_slist*>(lp_), hdr.c_str() );
}
}
bool
Agent::perform_() const
{
if ( lp_ ) { ::curl_easy_setopt( curl_, CURLOPT_HTTPHEADER, lp_ ); }
auto _ok(::curl_easy_perform( curl_ ));
::curl_easy_getinfo( curl_, CURLINFO_RESPONSE_CODE, &respCode_ );
//::curl_easy_getinfo( curl_, CURLINFO_SIZE_DOWNLOAD_T, &size_ );
{
// double _tmp;
curl_off_t _tmp;
::curl_easy_getinfo( curl_, CURLINFO_SIZE_DOWNLOAD_T, &_tmp );
size_ = _tmp;
}
::curl_easy_getinfo( curl_, CURLINFO_CONTENT_TYPE, &type_ );
return CURLE_OK == _ok;
}
void
Agent::free_slist_() const
{
if ( lp_ )
{
::curl_slist_free_all( static_cast<curl_slist*>(lp_) );
lp_ = nullptr;
}
}
}} // namespace http, Utility