コード例 #1
0
ファイル: ext_apache.cpp プロジェクト: Orvid/hhvm
static Array get_headers(const HeaderMap& headers, bool allHeaders = false) {
  ArrayInit ret(headers.size(), ArrayInit::Mixed{});
  for (auto& iter : headers) {
    const auto& values = iter.second;
    if (auto size = values.size()) {
      if (!allHeaders) {
        ret.set(String(iter.first), String(values.back()));
      } else {
        PackedArrayInit dups(size);
        for (auto& dup : values) {
          dups.append(String(dup));
        }
        ret.set(String(toLower(iter.first)), dups.toArray());
      }
    }
  }
  return ret.toArray();
}
コード例 #2
0
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
}