- // Set cURL options based on HTTPFetchRequest
- curl_easy_setopt(curl, CURLOPT_URL,
- request.url.c_str());
- curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,
- request.timeout);
- curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
- request.connect_timeout);
-
- if (request.useragent != "")
- curl_easy_setopt(curl, CURLOPT_USERAGENT, request.useragent.c_str());
-
- // Set up a write callback that writes to the
- // ostringstream ongoing->oss, unless the data
- // is to be discarded
- if (request.caller == HTTPFETCH_DISCARD) {
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
- httpfetch_discardfunction);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
- }
- else {
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
- httpfetch_writefunction);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
- }
- // Set POST (or GET) data
- if (request.post_fields.empty()) {
- curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
- }
- else {
- curl_easy_setopt(curl, CURLOPT_POST, 1);
+ // Set cURL options based on HTTPFetchRequest
+ curl_easy_setopt(curl, CURLOPT_URL,
+ request.url.c_str());
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS,
+ request.timeout);
+ curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
+ request.connect_timeout);
+
+ if (!request.useragent.empty())
+ curl_easy_setopt(curl, CURLOPT_USERAGENT, request.useragent.c_str());
+
+ // Set up a write callback that writes to the
+ // ostringstream ongoing->oss, unless the data
+ // is to be discarded
+ if (request.caller == HTTPFETCH_DISCARD) {
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
+ httpfetch_discardfunction);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
+ } else {
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
+ httpfetch_writefunction);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &oss);
+ }
+
+ // Set data from fields or raw_data
+ if (request.multipart) {
+ curl_httppost *last = NULL;
+ for (StringMap::iterator it = request.fields.begin();
+ it != request.fields.end(); ++it) {
+ curl_formadd(&post, &last,
+ CURLFORM_NAMELENGTH, it->first.size(),
+ CURLFORM_PTRNAME, it->first.c_str(),
+ CURLFORM_CONTENTSLENGTH, it->second.size(),
+ CURLFORM_PTRCONTENTS, it->second.c_str(),
+ CURLFORM_END);
+ }
+ curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
+ // request.post_fields must now *never* be
+ // modified until CURLOPT_HTTPPOST is cleared
+ } else {
+ switch (request.method) {
+ case HTTP_GET:
+ curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
+ break;
+ case HTTP_POST:
+ curl_easy_setopt(curl, CURLOPT_POST, 1);
+ break;
+ case HTTP_PUT:
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
+ break;
+ case HTTP_DELETE:
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
+ break;
+ }
+ if (request.method != HTTP_GET) {
+ if (!request.raw_data.empty()) {