From ec5f59dac8d5eca3504ec5fe205dcb7d8fd382a6 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 16 Nov 2009 06:00:12 +0000 Subject: Fixed support for nested elements and lists. Improved failure/error reporting monad. Minor code cleanup. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@880598 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/sca/modules/http/curl.hpp | 58 ++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 20 deletions(-) (limited to 'cpp/sca/modules/http/curl.hpp') diff --git a/cpp/sca/modules/http/curl.hpp b/cpp/sca/modules/http/curl.hpp index c0d79cef13..5ee3a090b0 100644 --- a/cpp/sca/modules/http/curl.hpp +++ b/cpp/sca/modules/http/curl.hpp @@ -137,13 +137,13 @@ template size_t headerCallback(void *ptr, size_t size, size_t nmemb, * Apply an HTTP verb to a list containing a list of headers and a list of content, and * a reduce function used to process the response. */ -curl_slist* headers(curl_slist* cl, const list h) { +curl_slist* headers(curl_slist* cl, const list& h) { if (isNil(h)) return cl; return headers(curl_slist_append(cl, std::string(car(h)).c_str()), cdr(h)); } -template const failable, std::string> apply(const list > req, const lambda& reduce, const R& initial, const std::string& url, const std::string& verb, CURLHandle& ch) { +template const failable, std::string> apply(const list >& req, const lambda& reduce, const R& initial, const std::string& url, const std::string& verb, const CURLHandle& ch) { // Init the curl session curl_easy_reset(ch); @@ -190,13 +190,13 @@ template const failable, std::string> apply(const list, std::string>(curl_easy_strerror(rc)); long httprc; curl_easy_getinfo (ch, CURLINFO_RESPONSE_CODE, &httprc); if (httprc != 200 && httprc != 201) { std::ostringstream es; es << "HTTP code " << httprc; - return es.str(); + return mkfailure, std::string>(es.str()); } return mklist(hcx.accum, wcx.accum); } @@ -204,13 +204,13 @@ template const failable, std::string> apply(const list evalExpr(const value expr, const std::string& url, CURLHandle& ch) { +const failable evalExpr(const value& expr, const std::string& url, const CURLHandle& ch) { // Convert expression to a JSON-RPC request json::JSONContext cx; const failable, std::string> jsreq = jsonRequest(1, car(expr), cdr(expr), cx); if (!hasValue(jsreq)) - return std::string(jsreq); + return mkfailure(reason(jsreq)); if (logContent) { std::cout<< "content: " << std::endl; @@ -223,7 +223,7 @@ const failable evalExpr(const value expr, const std::string const list h = mklist("Content-Type: application/json-rpc"); const failable >, std::string> res = apply >(mklist >(h, jsreq), rcons, list(), url, "POST", ch); if (!hasValue(res)) - return std::string(res); + return mkfailure(reason(res)); // Return result if (logContent) { @@ -238,7 +238,7 @@ const failable evalExpr(const value expr, const std::string /** * HTTP GET, return the resource at the given URL. */ -template const failable, std::string> get(const lambda& reduce, const R& initial, const std::string& url, CURLHandle& ch) { +template const failable, std::string> get(const lambda& reduce, const R& initial, const std::string& url, const CURLHandle& ch) { const list > req = mklist(list(), list()); return apply(req, reduce, initial, url, "GET", ch); } @@ -246,17 +246,17 @@ template const failable, std::string> get(const lambda get(const std::string& url, CURLHandle& ch) { +const failable get(const std::string& url, const CURLHandle& ch) { // Get the contents of the resource at the given URL const failable >, std::string> res = get >(rcons, list(), url, ch); if (!hasValue(res)) - return std::string(res); + return mkfailure(reason(res)); const list > ls = res; - // TODO Return an ATOM feed const std::string ct; if (ct.find("application/atom+xml") != std::string::npos) { + // TODO Return an ATOM feed } // Return the content as a string value @@ -268,12 +268,12 @@ const failable get(const std::string& url, CURLHandle& ch) { /** * HTTP POST. */ -const failable post(const value& val, const std::string& url, CURLHandle& ch) { +const failable post(const value& val, const std::string& url, const CURLHandle& ch) { // Convert value to an ATOM entry const failable, std::string> entry = atom::writeATOMEntry(atom::entryValuesToElements(val)); if (!hasValue(entry)) - return std::string(entry); + return mkfailure(reason(entry)); if (logContent) { std::cout << "content:" << std::endl; write(list(entry), std::cout); @@ -285,45 +285,63 @@ const failable post(const value& val, const std::string& url const list > req = mklist >(h, entry); const failable >, std::string> res = apply >(req, rcons, list(), url, "POST", ch); if (!hasValue(res)) - return std::string(res); + return mkfailure(reason(res)); return value(true); } /** * HTTP PUT. */ -const failable put(const value& val, const std::string& url, CURLHandle& ch) { +const failable put(const value& val, const std::string& url, const CURLHandle& ch) { // Convert value to an ATOM entry const failable, std::string> entry = atom::writeATOMEntry(atom::entryValuesToElements(val)); if (!hasValue(entry)) - return std::string(entry); + return mkfailure(reason(entry)); if (logContent) { std::cout << "content:" << std::endl; write(list(entry), std::cout); std::cout << std::endl; } - // POST it to the URL + // PUT it to the URL const list h = mklist("Content-Type: application/atom+xml"); const list > req = mklist >(h, entry); const failable >, std::string> res = apply >(req, rcons, list(), url, "PUT", ch); if (!hasValue(res)) - return std::string(res); + return mkfailure(reason(res)); return value(true); } /** * HTTP DELETE. */ -const failable del(const std::string& url, CURLHandle& ch) { +const failable del(const std::string& url, const CURLHandle& ch) { const list > req = mklist(list(), list()); const failable >, std::string> res = apply >(req, rcons, list(), url, "DELETE", ch); if (!hasValue(res)) - return std::string(res); + return mkfailure(reason(res)); return value(true); } +/** + * HTTP client proxy function. + */ +struct proxy { + proxy(const std::string& url, const CURLHandle& ch) : url(url), ch(ch) { + } + + const value operator()(const list& args) const { + failable val = evalExpr(args, url, ch); + if (!hasValue(val)) + return value(); + return val; + } + + const std::string url; + const CURLHandle& ch; +}; + } } -- cgit v1.2.3