#include <fcntl.h>
#include <boost/foreach.hpp>
#include <sstream>
+#include "rapidjson/stringbuffer.h"
+#include "rapidjson/writer.h"
#ifdef REMOTEBACKEND_HTTP
#include <curl/curl.h>
this->d_url_suffix = "";
}
this->timeout = 2;
+ this->d_post = false;
+ this->d_post_json = false;
+
if (options.find("timeout") != options.end()) {
this->timeout = boost::lexical_cast<int>(options.find("timeout")->second)/1000;
}
+ if (options.find("post") != options.end()) {
+ std::string val = options.find("post")->second;
+ if (val == "yes" || val == "true" || val == "on" || val == "1") {
+ this->d_post = true;
+ }
+ }
+ if (options.find("post_json") != options.end()) {
+ std::string val = options.find("post_json")->second;
+ if (val == "yes" || val == "true" || val == "on" || val == "1") {
+ this->d_post_json = true;
+ }
+ }
}
HTTPConnector::~HTTPConnector() {
return stream.str();
}
-// builds our request
-void HTTPConnector::requestbuilder(const std::string &method, const rapidjson::Value ¶meters, struct curl_slist **slist)
+// builds our request (near-restful)
+void HTTPConnector::restful_requestbuilder(const std::string &method, const rapidjson::Value ¶meters, struct curl_slist **slist)
{
std::stringstream ss;
std::string sparam;
curl_easy_setopt(d_c, CURLOPT_HTTPHEADER, *slist);
}
+void HTTPConnector::post_requestbuilder(const rapidjson::Document &input, struct curl_slist **slist) {
+ if (this->d_post_json) {
+ // simple case, POST JSON into url. nothing fancy.
+ std::string out = makeStringFromDocument(input);
+ (*slist) = curl_slist_append((*slist), "Content-Type: text/javascript; charset=utf-8");
+ curl_easy_setopt(d_c, CURLOPT_POSTFIELDSIZE, out.size());
+ curl_easy_setopt(d_c, CURLOPT_COPYPOSTFIELDS, out.c_str());
+ curl_easy_setopt(d_c, CURLOPT_URL, d_url.c_str());
+ curl_easy_setopt(d_c, CURLOPT_HTTPHEADER, *slist);
+ } else {
+ std::stringstream url,content;
+ char *tmpstr;
+ // call url/method.suffix
+ rapidjson::StringBuffer output;
+ rapidjson::Writer<rapidjson::StringBuffer> w(output);
+ input["parameters"].Accept(w);
+ url << d_url << "/" << input["method"].GetString() << d_url_suffix;
+ // then build content
+ tmpstr = curl_easy_escape(d_c, output.GetString(), 0);
+ content << "parameters=" << tmpstr;
+ // convert into parameters=urlencoded
+ curl_easy_setopt(d_c, CURLOPT_POSTFIELDSIZE, content.str().size());
+ curl_easy_setopt(d_c, CURLOPT_COPYPOSTFIELDS, content.str().c_str());
+ free(tmpstr);
+ curl_easy_setopt(d_c, CURLOPT_URL, d_url.c_str());
+ curl_easy_setopt(d_c, CURLOPT_URL, url.str().c_str());
+ }
+}
+
int HTTPConnector::send_message(const rapidjson::Document &input) {
int rv;
long rcode;
slist = NULL;
- // build request
- requestbuilder(input["method"].GetString(), input["parameters"], &slist);
+ // build request based on mode
+
+ if (d_post)
+ post_requestbuilder(input, &slist);
+ else
+ restful_requestbuilder(input["method"].GetString(), input["parameters"], &slist);
// setup write function helper
curl_easy_setopt(d_c, CURLOPT_WRITEFUNCTION, &(httpconnector_write_data));