6 #include <boost/bind.hpp>
7 #include <boost/algorithm/string.hpp>
18 :
TcpSession(client, socket) , delete_called_(0) {
36 event_cb_(static_cast<HttpClientSession *>(session.get()), event);
66 boost::asio::ip::address String2Addr(
const std::string& host) {
67 boost::system::error_code ec;
74 host_(ep.address().to_string()), endpoint_(ep),
75 id_(id), cb_(NULL), offset_(0), curl_handle_(NULL),
76 session_(NULL), client_(client), use_ssl_(false), client_cert_(
""),
77 client_cert_type_(
"PEM"), client_key_(
""), ca_cert_(
""), state_(STATUS) {
82 host_(host), endpoint_(String2Addr(host), port),
83 id_(id), cb_(NULL), offset_(0), curl_handle_(NULL),
84 session_(NULL), client_(client), use_ssl_(false), client_cert_(
""),
85 client_cert_type_(
"PEM"), client_key_(
""), ca_cert_(
""), state_(STATUS) {
93 std::ostringstream ret;
96 ret <<
"https://" <<
host_;
98 ret <<
"http://" <<
host_;
136 std::vector<std::string> hdr_options;
137 return HttpGet(path,
false,
true,
false, hdr_options, cb);
141 bool short_timeout,
bool reuse,
142 std::vector<std::string> &hdr_options,
144 const std::string body;
147 this, body, path,
bool2bf(header, short_timeout,
148 reuse), hdr_options, cb, HTTP_GET));
153 bool reuse, std::vector<std::string> &hdr_options,
155 const std::string body;
157 this, body, path,
bool2bf(header, short_timeout,
158 reuse), hdr_options, cb, HTTP_HEAD));
163 const std::string &path,
HttpCb cb) {
164 std::vector<std::string> hdr_options;
165 return HttpPut(put_string, path,
false,
true,
false, hdr_options, cb);
169 const std::string &path,
bool header,
171 bool reuse, std::vector<std::string> &hdr_options,
174 this, put_string, path,
175 bool2bf(header, short_timeout, reuse),
176 hdr_options, cb, HTTP_PUT));
181 const std::string &path,
HttpCb cb) {
182 std::vector<std::string> hdr_options;
183 return HttpPost(post_string, path,
false,
true,
false, hdr_options, cb);
187 const std::string &path,
bool header,
188 bool short_timeout,
bool reuse,
189 std::vector<std::string> &hdr_options,
HttpCb cb) {
191 this, post_string, path,
bool2bf(header,
192 short_timeout, reuse), hdr_options, cb, HTTP_POST));
197 std::vector<std::string> hdr_options;
198 return HttpDelete(path,
false,
true,
false, hdr_options, cb);
202 bool reuse, std::vector<std::string> &hdr_options,
204 const std::string body;
206 this, body, path,
bool2bf(header, short_timeout,
207 reuse), hdr_options, cb, HTTP_DELETE));
217 unsigned short hdr_shortTimeout_reuse,
218 std::vector<std::string> hdr_options,
219 HttpCb cb, http_method method) {
220 bool short_timeout, reuse;
226 if (
client()->AddConnection(
this) ==
false) {
236 LOG(DEBUG,
"Http : unable to create new connection");
254 std::map<CURLoption, int>::iterator iter = curl_options->begin();
255 while (iter != curl_options->end()) {
267 for (uint32_t i = 0; i < hdr_options.size(); ++i)
280 if (!hdr_options.size()) {
289 if (!hdr_options.size()) {
308 buf_.assign(ptr, size);
311 boost::system::error_code
error;
318 buf_.assign(ptr, size);
323 int i =
buf_.find(
' ', 0);
324 version_ = boost::algorithm::trim_copy(
buf_.substr(0, i));
325 int j =
buf_.find(
' ', i+1);
327 reason_ = boost::algorithm::trim_copy(
buf_.substr(j+1));
333 std::cout <<
"Status Line: " << std::dec <<
status_ <<
":"
334 << reason_ <<
"(" <<
version_ <<
")" << std::endl;
340 if (
buf_ !=
"\r\n") {
341 std::istringstream iss(
buf_);
343 while (std::getline(iss, tok,
'\r') && tok !=
"\n") {
344 unsigned int i = tok.find(
':', 0);
345 if (i != std::string::npos) {
347 boost::algorithm::trim_copy(tok.substr(0, i)),
348 boost::algorithm::trim_copy(tok.substr(i + 1))));
354 boost::system::error_code
error;
375 curl_timer_(
TimerManager::CreateTimer(*evm->io_service(), task_name,
377 id_(0), work_queue_(
TaskScheduler::GetInstance()->GetTaskId(task_name), 0,
378 boost::bind(&
HttpClient::DequeueEvent, this, _1)) {
385 for (HttpConnectionMap::iterator iter =
map_.begin(), next = iter;
386 iter !=
map_.end(); iter = next) {
397 assert(!
map_.size());
442 boost::system::error_code err;
443 socket->open(boost::asio::ip::tcp::v4(), err);
446 LOG(ERROR,
"http socket open failed: " << err);
467 map_.insert(key, conn);
507 size_t id = connection->
id();
509 map_.erase(std::make_pair(endpoint,
id));
void UpdateOffset(size_t bytes)
boost::asio::ip::tcp::endpoint endpoint_
enum HttpConnection::HTTPHeaderDataState state_
boost::asio::const_buffer Buffer
virtual void DeleteSession(TcpSession *session)
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
boost::asio::ip::tcp::socket Socket
const std::string & GetData()
int HttpGet(const std::string &path, HttpCb)
#define set_curl_option(handle, option, parameter)
int http_head(ConnInfo *conn, GlobalInfo *g)
HttpConnection * CreateConnection(boost::asio::ip::tcp::endpoint)
boost::function< void()> EnqueuedCb
struct _GlobalInfo * GlobalInfo()
HttpClient(EventManager *evm, std::string task_name=std::string("http client"))
void ProcessEvent(EnqueuedCb cb)
virtual TcpSession * CreateSession()
boost::asio::io_context * io_service()
std::pair< endpoint, size_t > Key
HttpConnection(boost::asio::ip::tcp::endpoint, size_t id, HttpClient *client)
int http_delete(ConnInfo *conn, GlobalInfo *g)
EventManager * event_manager()
HttpClientSession * session()
void RemoveConnectionInternal(HttpConnection *)
struct _ConnInfo * curl_handle_
std::string make_url(std::string &path)
int HttpPost(const std::string &post_string, const std::string &path, HttpCb)
HttpClientSession * CreateSession()
void ScheduleShutdown(bool delete_entries=true)
virtual TcpSession * AllocSession(Socket *socket)
boost::function< void(std::string &, boost::system::error_code &)> HttpCb
virtual TcpSession * CreateSession()
bool DequeueEvent(EnqueuedCb)
boost::asio::ip::tcp::endpoint endpoint()
boost::asio::ip::tcp::socket Socket
void set_observer(EventObserver observer)
int HttpDelete(const std::string &path, HttpCb)
std::map< std::string, std::string > headers_
int curl_init(HttpClient *client)
unsigned short bool2bf(bool header, bool short_timeout, bool reuse)
void RegisterEventCb(SessionEventCb cb)
HttpConnection * connection
HttpConnection * connection_
void set_put_string(ConnInfo *conn, const char *put, uint32_t len)
std::map< CURLoption, int > * curl_options()
std::string client_cert_type_
int http_put(ConnInfo *conn, GlobalInfo *g)
HttpClientSession * session_
HttpClientSession(HttpClient *client, Socket *socket)
static bool IsSocketErrorHard(const boost::system::error_code &ec)
void TimerErrorHandler(std::string name, std::string error)
void set_post_string(ConnInfo *conn, const char *post, uint32_t len)
boost::asio::ip::tcp::endpoint endpoint
void RemoveConnection(HttpConnection *)
void OnEventInternal(TcpSessionPtr session, Event event)
virtual void OnRead(Buffer buffer)
char error[CURL_ERROR_SIZE+1]
void AssignData(const char *ptr, size_t size)
int http_get(ConnInfo *conn, GlobalInfo *g)
WorkQueue< EnqueuedCb > work_queue_
void del_curl_handle(ConnInfo *curl_handle, GlobalInfo *g)
boost::intrusive_ptr< TcpSession > TcpSessionPtr
void del_conn(HttpConnection *connection, GlobalInfo *g)
struct _ConnInfo * curl_handle()
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
HttpClientSession::SessionEventCb event_cb_
int http_post(ConnInfo *conn, GlobalInfo *g)
int HttpHead(const std::string &path, bool header, bool short_timeout, bool reuse, std::vector< std::string > &hdr_options, HttpCb cb)
bool AddConnection(HttpConnection *)
bool IsErrorHard(const boost::system::error_code &ec)
void HttpProcessInternal(const std::string body, std::string path, unsigned short header_shortTimeout_reuse, std::vector< std::string > hdr_options, HttpCb cb, http_method m)
void OnEvent(TcpSession *session, Event event)
void set_header_options(ConnInfo *conn, const char *options)
bool Start(int time, Handler handler, ErrorHandler error_handler=NULL)
boost::asio::io_context * io_service()
void SetConnection(HttpConnection *conn)
void set_url(ConnInfo *conn, const char *url)
int HttpPut(const std::string &put_string, const std::string &path, HttpCb)
#define LOG(_Level, _Msg)
void set_curl_handle(struct _ConnInfo *handle)
boost::function< void(HttpClientSession *session, TcpSession::Event event)> SessionEventCb
void set_ssl_options(ConnInfo *conn, const char *client_cert, const char *client_cert_type, const char *client_key, const char *ca_cert)
bool timer_cb(GlobalInfo *g)
ConnInfo * new_conn(HttpConnection *connection, GlobalInfo *g, bool header, bool short_timeout, bool reuse)
void set_session(HttpClientSession *session)
virtual Socket * socket() const
bool Enqueue(QueueEntryT entry)
void bf2bool(unsigned short bf, bool &header, bool &short_timeout, bool &reuse)
void AssignHeader(const char *ptr, size_t size)
virtual boost::system::error_code SetSocketOptions()
static bool DeleteTimer(Timer *Timer)