void LOGAdapter::Send()

in adapter.cpp [302:397]


void LOGAdapter::Send(const string& httpMethod, const string& host, const int32_t port, const string& url, const string& queryString, const map<string, string>& header, const string& body, const int32_t timeout, HttpMessage& httpMessage, const int64_t maxspeed) 
{
    /*
    for(map<string, string>::const_iterator iter = header.begin();iter != header.end();iter++)
    {
    }
    cout << endl;
    cout << "HOST:" << host << "\n" << endl;
    //cout << "URI:" << url << "\n" << endl;
    //cout << "BODY:" << body << "\n" << endl;
    //cout << "QUERYSTRING:" << queryString << "\n" << endl;
*/
    CURLcode res;
    std::string response;
    int64_t statusCode;
    map<std::string,std::string> responseHeader;
    struct curl_slist *headers = NULL;
    for(map<std::string, std::string>::const_iterator iter=header.begin(); iter!=header.end(); iter++)
    {
        headers = curl_slist_append(headers, (iter->first + ":" + iter->second).c_str());
    }
    string queryUrl = host + url;
    if(queryString.empty() == false)
        queryUrl += "?"+queryString;

    CURL* curl = curl_easy_init();
    
    if (curl)
    {
        if(headers)
        {
            curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        }
        curl_easy_setopt(curl, CURLOPT_URL, queryUrl.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, data_write_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &responseHeader);
        curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_write_callback);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CONNECT_TIMEOUT);
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
        curl_easy_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t)maxspeed);
        if (httpMethod == HTTP_POST)
        {
            curl_easy_setopt(curl, CURLOPT_POST, 1L);
            BodyTransfer transfer(body);
            curl_easy_setopt(curl, CURLOPT_READDATA, &transfer);
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, sendBody);
        }
        else if (httpMethod == HTTP_DELETE)
        {
            curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, HTTP_DELETE);
        }
        else if (httpMethod == HTTP_PUT)
        {
            curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
            BodyTransfer transfer(body);
            curl_easy_setopt(curl, CURLOPT_READDATA, &transfer);
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, sendBody);
        }

        res = curl_easy_perform(curl);
#define CURL_CLEAN_UP         curl_easy_cleanup(curl);\
        if(headers)\
        {\
            curl_slist_free_all(headers);\
        }

        switch(res)
        {
            case CURLE_OK:
            curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &statusCode);
            CURL_CLEAN_UP
                break;
            case CURLE_OPERATION_TIMEDOUT:
            CURL_CLEAN_UP
                throw LOGException(LOGE_REQUEST_TIMEOUT, "Request operation timeout.");
                break;
            case CURLE_COULDNT_CONNECT:
            CURL_CLEAN_UP
                throw LOGException(LOGE_CLIENT_NETWORK_ERROR, "Can not connect to server.");
                break;
            default:
            CURL_CLEAN_UP
                throw LOGException(LOGE_CLIENT_NETWORK_ERROR, std::string("Request operation failed.")+"CURL_ERROR_CODE:"+ToString(res));
                break;
        }
    }
    else
    {
        throw LOGException(LOGE_UNKNOWN_ERROR, "Initailizing request failed.");
    }

    httpMessage = HttpMessage(statusCode,responseHeader, response);
}