bool HTTPSocket::OnConnected() { std::string request = "GET " + url.request + " HTTP/1.1\r\n"; // Dump headers into the request HeaderMap headers = req.GetHeaders(); for (HeaderMap::iterator i = headers.begin(); i != headers.end(); i++) request += i->first + ": " + i->second + "\r\n"; // The Host header is required for HTTP 1.1 and isn't known when the request is created; if they didn't overload it // manually, add it here if (headers.find("Host") == headers.end()) request += "Host: " + url.domain + "\r\n"; request += "\r\n"; this->status = HTTP_REQSENT; return this->Write(request); }
void apply(BenchmarkHeaders &headers, const string &name, const string &string_value) { HeaderEntry entry = map.find(name); if (entry != map.end()) { (headers.*(entry->second)).set(string_value); } }
void Communication::getRawData(const string &_url, const string &method, string data, const HeaderMap &headers, bool ignoreTimeout){ string url = baseURL + _url; #ifdef COUCH_DB_ANNOUNCE_URLS printf("%s %s\n", method.c_str(), url.c_str()); #endif buffer.clear(); if(curl_easy_setopt(curl, CURLOPT_URL, url.c_str()) != CURLE_OK) throw Exception("Unable to set URL: " + url); if(data.size() > 0){ #ifdef COUCH_DB_DEBUG printf("Sending data: %s\n", data.c_str()); #endif if(curl_easy_setopt(curl, CURLOPT_READFUNCTION, reader) != CURLE_OK) throw Exception("Unable to set read function"); if(curl_easy_setopt(curl, CURLOPT_READDATA, &data) != CURLE_OK) throw Exception("Unable to set data: " + data); if(curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L) != CURLE_OK) throw Exception("Unable to set upload request"); if(curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)data.size()) != CURLE_OK) throw Exception("Unable to set content size: " + data.size()); } if(headers.size() > 0 || data.size() > 0){ struct curl_slist *chunk = NULL; HeaderMap::const_iterator header = headers.begin(); const HeaderMap::const_iterator &headerEnd = headers.end(); for(; header != headerEnd; ++header){ string headerStr = header->first + ": " + header->second; chunk = curl_slist_append(chunk, headerStr.c_str()); } if(data.size() > 0 && headers.find("Content-Type") == headers.end()) chunk = curl_slist_append(chunk, "Content-Type: application/json"); if(curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk) != CURLE_OK) throw Exception("Unable to set custom headers"); } if(curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str()) != CURLE_OK) throw Exception("Unable to set HTTP method: " + method); CURLcode rc = curl_easy_perform(curl); if ( !(ignoreTimeout && rc == CURLE_OPERATION_TIMEDOUT) ){ if( rc != CURLE_OK) throw Exception("Unable to load URL: " + url); } if(data.size() > 0 || headers.size() > 0){ if(curl_easy_setopt(curl, CURLOPT_UPLOAD, 0L) != CURLE_OK) throw Exception("Unable to reset upload request"); if(curl_easy_setopt(curl, CURLOPT_HTTPHEADER, NULL) != CURLE_OK) throw Exception("Unable to reset custom headers"); } #ifdef COUCH_DB_DEBUG long responseCode; if(curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode) != CURLE_OK) throw Exception("Unable to get response code"); printf("Response code: %ld\n", responseCode); printf("Raw buffer: %s\n", buffer.c_str()); #endif }