Skip to content

Commit

Permalink
Always set curl_forbid_reuse flag.
Browse files Browse the repository at this point in the history
Also, reset easy private data when curl handle is deleted and
remove use of global data.

Change-Id: Id880550646dcc66a004624b226cb010965ed695d
Closes-bug: 1457432
  • Loading branch information
haripk committed May 23, 2015
1 parent 141be2a commit e89699c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 49 deletions.
21 changes: 13 additions & 8 deletions src/http/client/http_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ HttpConnection::HttpConnection(boost::asio::ip::tcp::endpoint ep, size_t id,
}

HttpConnection::~HttpConnection() {
if (session_) {
{
tbb::mutex::scoped_lock lock(session_->mutex());
session_->SetConnection(NULL);
}
client_->DeleteSession(session_);
set_session(NULL);
}
delete_session();
}

std::string HttpConnection::make_url(std::string &path) {
Expand All @@ -98,6 +91,18 @@ HttpClientSession *HttpConnection::CreateSession() {
return session;
}

void HttpConnection::delete_session() {
HttpClientSession *session = session_;
if (session_) {
{
tbb::mutex::scoped_lock lock(session_->mutex());
session_->SetConnection(NULL);
session_ = NULL;
}
client_->DeleteSession(session);
}
}

void HttpConnection::set_session(HttpClientSession *session) {
session_ = session;
if (session && event_cb_ && !event_cb_.empty())
Expand Down
1 change: 1 addition & 0 deletions src/http/client/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class HttpConnection {
void set_curl_handle(struct _ConnInfo *handle) { curl_handle_ = handle; }
HttpClientSession *CreateSession();
void set_session(HttpClientSession *session);
void delete_session();
void AssignData(const char *ptr, size_t size);
void UpdateOffset(size_t bytes);
size_t GetOffset();
Expand Down
68 changes: 27 additions & 41 deletions src/http/client/http_curl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ using tbb::mutex;

const CurlErrorCategory curl_error_category;

/* boost::asio related objects
* using global variables for simplicity
*/
std::map<curl_socket_t, HttpClientSession *> socket_map;


/* Update the event timer after curl_multi library calls */
static int multi_timer_cb(CURLM *multi, long timeout_ms, HttpClient *client)
{
Expand Down Expand Up @@ -98,12 +92,12 @@ static void check_multi_info(GlobalInfo *g)
curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn);
curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);

boost::system::error_code error(res, curl_error_category);
std::string empty_str("");
if (conn->connection->HttpClientCb() != NULL)
conn->connection->HttpClientCb()(empty_str, error);

if (conn) {
boost::system::error_code error(res, curl_error_category);
std::string empty_str("");
if (conn->connection->HttpClientCb() != NULL)
conn->connection->HttpClientCb()(empty_str, error);

conn->connection->set_curl_handle(NULL);
del_curl_handle(conn, g);
}
Expand Down Expand Up @@ -165,30 +159,28 @@ bool timer_cb(GlobalInfo *g)
}

/* Clean up any data */
static void remsock(int *f, GlobalInfo *g)
static void remsock(SockInfo *sock_info, GlobalInfo *g)
{
if ( f )
if ( sock_info )
{
free(f);
free(sock_info);
}
}

static bool setsock(int *fdp, curl_socket_t s, CURL*e, int act, GlobalInfo *g)
static bool setsock(SockInfo *sock_info, curl_socket_t s, CURL*e, int act, GlobalInfo *g)
{
std::map<curl_socket_t, HttpClientSession *>::iterator it = socket_map.find(s);

if ( it == socket_map.end() )
if (!sock_info || !sock_info->conn_info || !sock_info->conn_info->connection)
{
return false;
}

HttpClientSession *session = it->second;
HttpClientSession *session = sock_info->conn_info->connection->session();
if (session->IsClosed())
return false;

boost::asio::ip::tcp::socket * tcp_socket = session->socket();

*fdp = act;
sock_info->action = act;

if ( act == CURL_POLL_IN )
{
Expand Down Expand Up @@ -217,35 +209,37 @@ static bool setsock(int *fdp, curl_socket_t s, CURL*e, int act, GlobalInfo *g)

static void addsock(curl_socket_t s, CURL *easy, int action, GlobalInfo *g)
{
int *fdp = (int *)calloc(sizeof(int), 1); /* fdp is used to store current action */
// store socket details in SockInfo
SockInfo *sock_info = (SockInfo *)calloc(sizeof(SockInfo), 1);
curl_easy_getinfo(easy, CURLINFO_PRIVATE, &sock_info->conn_info);

if (!setsock(fdp, s, easy, action, g)) {
free(fdp);
if (!setsock(sock_info, s, easy, action, g)) {
free(sock_info);
return;
}

curl_multi_assign(g->multi, s, fdp);
curl_multi_assign(g->multi, s, sock_info);
}

/* CURLMOPT_SOCKETFUNCTION */
static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
{
GlobalInfo *g = (GlobalInfo*) cbp;
int *actionp = (int*) sockp;
SockInfo *sock_info = (SockInfo *)sockp;

if ( what == CURL_POLL_REMOVE )
{
remsock(actionp, g);
remsock(sock_info, g);
}
else
{
if ( !actionp )
if ( !sockp )
{
addsock(s, e, what, g);
}
else
{
setsock(actionp, s, e, what, g);
setsock(sock_info, s, e, what, g);
}
}
return 0;
Expand Down Expand Up @@ -313,24 +307,18 @@ static curl_socket_t opensocket(void *data,
HttpClientSession *session = conn->CreateSession();
if (session) {
sockfd = session->socket()->native_handle();
socket_map.insert(std::pair<curl_socket_t, HttpClientSession *>(
sockfd, static_cast<HttpClientSession *>(session)));
conn->set_session(session);
}
}


return sockfd;
}

/* CURLOPT_CLOSESOCKETFUNCTION */
static int closesocket(void *clientp, curl_socket_t item)
{
std::map<curl_socket_t, HttpClientSession *>::iterator it = socket_map.find(item);
if ( it != socket_map.end() ) {
socket_map.erase(it);
}

HttpConnection *conn = static_cast<HttpConnection *>(clientp);
conn->delete_session();
return 0;
}

Expand Down Expand Up @@ -361,7 +349,6 @@ void del_conn(HttpConnection *connection, GlobalInfo *g) {

if (connection->session()) {
tbb::mutex::scoped_lock lock(connection->session()->mutex());
closesocket(NULL, connection->session()->socket()->native_handle());
connection->session()->SetConnection(NULL);
}

Expand All @@ -374,6 +361,7 @@ void del_conn(HttpConnection *connection, GlobalInfo *g) {

void del_curl_handle(ConnInfo *curl_handle, GlobalInfo *g) {
if (curl_handle) {
curl_easy_setopt(curl_handle->easy, CURLOPT_PRIVATE, NULL);
curl_multi_remove_handle(g->multi, curl_handle->easy);
curl_slist_free_all(curl_handle->headers);
free(curl_handle->post);
Expand Down Expand Up @@ -417,9 +405,7 @@ ConnInfo *new_conn(HttpConnection *connection, GlobalInfo *g,
curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_TIME, 30L);
curl_easy_setopt(conn->easy, CURLOPT_LOW_SPEED_LIMIT, 10L);
}
if (!reuse) {
curl_easy_setopt(conn->easy, CURLOPT_FORBID_REUSE, 1L);
}
curl_easy_setopt(conn->easy, CURLOPT_FORBID_REUSE, 1L);

/* to include the header in the body */
if (header)
Expand All @@ -431,6 +417,7 @@ ConnInfo *new_conn(HttpConnection *connection, GlobalInfo *g,

/* call this function to close a socket */
curl_easy_setopt(conn->easy, CURLOPT_CLOSESOCKETFUNCTION, closesocket);
curl_easy_setopt(conn->easy, CURLOPT_CLOSESOCKETDATA, connection);

return conn;
}
Expand Down Expand Up @@ -490,7 +477,6 @@ int http_delete(ConnInfo *conn, GlobalInfo *g) {

int curl_init(HttpClient *client)
{

struct _GlobalInfo *g = client->GlobalInfo();

memset(g, 0, sizeof(GlobalInfo));
Expand Down
6 changes: 6 additions & 0 deletions src/http/client/http_curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ typedef struct _ConnInfo
HttpConnection *connection;
} ConnInfo;

typedef struct _SockInfo
{
int action;
ConnInfo *conn_info;
} SockInfo;

class CurlErrorCategory : public boost::system::error_category
{
public:
Expand Down

0 comments on commit e89699c

Please sign in to comment.