From ba223e693c0f6e652fd70c05d83d69956262ff09 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 13 Jun 2011 07:57:13 +0000 Subject: Improve support for HTTP verbs and enable calls inside a domain without having to repeat the domain name in all the URLs. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1135047 13f79535-47bb-0310-9956-ffa450edef68 --- sca-cpp/trunk/components/http/Makefile.am | 19 ++++- sca-cpp/trunk/components/http/client-test.cpp | 39 +++++++-- sca-cpp/trunk/components/http/content-test.scm | 23 ++++++ sca-cpp/trunk/components/http/http.composite | 49 ++++++++++++ .../trunk/components/http/httppatch.componentType | 29 +++++++ sca-cpp/trunk/components/http/httppatch.cpp | 93 ++++++++++++++++++++++ .../trunk/components/http/httppost.componentType | 29 +++++++ sca-cpp/trunk/components/http/httppost.cpp | 93 ++++++++++++++++++++++ .../trunk/components/http/httpput.componentType | 29 +++++++ sca-cpp/trunk/components/http/httpput.cpp | 93 ++++++++++++++++++++++ sca-cpp/trunk/components/http/server-test.scm | 44 ++++++++++ sca-cpp/trunk/components/http/url-test.scm | 2 +- 12 files changed, 533 insertions(+), 9 deletions(-) create mode 100644 sca-cpp/trunk/components/http/content-test.scm create mode 100644 sca-cpp/trunk/components/http/httppatch.componentType create mode 100644 sca-cpp/trunk/components/http/httppatch.cpp create mode 100644 sca-cpp/trunk/components/http/httppost.componentType create mode 100644 sca-cpp/trunk/components/http/httppost.cpp create mode 100644 sca-cpp/trunk/components/http/httpput.componentType create mode 100644 sca-cpp/trunk/components/http/httpput.cpp create mode 100644 sca-cpp/trunk/components/http/server-test.scm (limited to 'sca-cpp/trunk/components') diff --git a/sca-cpp/trunk/components/http/Makefile.am b/sca-cpp/trunk/components/http/Makefile.am index 4388ec8057..1a081b8da6 100644 --- a/sca-cpp/trunk/components/http/Makefile.am +++ b/sca-cpp/trunk/components/http/Makefile.am @@ -19,8 +19,8 @@ compdir=$(prefix)/components/http EXTRA_DIST = http.composite httpget.componentType httpdelete.componentType *.scm -comp_LTLIBRARIES = libhttpget.la libhttpdelete.la -noinst_DATA = libhttpget.so libhttpdelete.so +comp_LTLIBRARIES = libhttpget.la libhttpdelete.la libhttppost.la libhttpput.la +noinst_DATA = libhttpget.so libhttpdelete.so libhttppost.so libhttpput.so libhttpget_la_SOURCES = httpget.cpp libhttpget_la_LDFLAGS = -lxml2 -lmozjs -curl @@ -32,6 +32,21 @@ libhttpdelete_la_LDFLAGS = -lxml2 -lmozjs -curl libhttpdelete.so: ln -s .libs/libhttpdelete.so +libhttppost_la_SOURCES = httppost.cpp +libhttppost_la_LDFLAGS = -lxml2 -lmozjs -curl +libhttppost.so: + ln -s .libs/libhttppost.so + +libhttpput_la_SOURCES = httpput.cpp +libhttpput_la_LDFLAGS = -lxml2 -lmozjs -curl +libhttpput.so: + ln -s .libs/libhttpput.so + +libhttppatch_la_SOURCES = httppatch.cpp +libhttppatch_la_LDFLAGS = -lxml2 -lmozjs -curl +libhttppatch.so: + ln -s .libs/libhttppatch.so + client_test_SOURCES = client-test.cpp client_test_LDFLAGS = -lxml2 -lcurl -lmozjs diff --git a/sca-cpp/trunk/components/http/client-test.cpp b/sca-cpp/trunk/components/http/client-test.cpp index a83bf55252..3ce9f1ae68 100644 --- a/sca-cpp/trunk/components/http/client-test.cpp +++ b/sca-cpp/trunk/components/http/client-test.cpp @@ -36,15 +36,16 @@ namespace tuscany { namespace http { -const string uri("http://localhost:8090/httpget"); +const string getURI("http://localhost:8090/httpget"); +const string postURI("http://localhost:8090/httppost"); +const string putURI("http://localhost:8090/httpput"); +const string deleteURI("http://localhost:8090/httpdelete"); bool testGet() { http::CURLSession cs("", "", "", ""); - const failable val = http::get(uri, cs); + const failable val = http::get(getURI, cs); assert(hasContent(val)); - assert(contains(string(car(cadr(content(val)))), "It works")); - return true; } @@ -53,9 +54,8 @@ struct getLoop { getLoop(http::CURLSession cs) : cs(cs) { } const bool operator()() const { - const failable val = http::get(uri, cs); + const failable val = http::get(getURI, cs); assert(hasContent(val)); - assert(contains(string(car(cadr(content(val)))), "It works")); return true; } }; @@ -69,6 +69,30 @@ bool testGetPerf() { return true; } +bool testPost() { + http::CURLSession cs("", "", "", ""); + + const failable val = http::get(postURI, cs); + assert(hasContent(val)); + return true; +} + +bool testPut() { + http::CURLSession cs("", "", "", ""); + + const failable val = http::get(putURI, cs); + assert(hasContent(val)); + return true; +} + +bool testDelete() { + http::CURLSession cs("", "", "", ""); + + const failable val = http::get(deleteURI, cs); + assert(hasContent(val)); + return true; +} + } } @@ -77,6 +101,9 @@ int main() { tuscany::http::testGet(); tuscany::http::testGetPerf(); + tuscany::http::testPost(); + tuscany::http::testPut(); + tuscany::http::testDelete(); tuscany::cout << "OK" << tuscany::endl; diff --git a/sca-cpp/trunk/components/http/content-test.scm b/sca-cpp/trunk/components/http/content-test.scm new file mode 100644 index 0000000000..f381546190 --- /dev/null +++ b/sca-cpp/trunk/components/http/content-test.scm @@ -0,0 +1,23 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you under the Apache License, Version 2.0 (the +; "License"); you may not use this file except in compliance +; with the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, +; software distributed under the License is distributed on an +; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +; KIND, either express or implied. See the License for the +; specific language governing permissions and limitations +; under the License. + +; Content test case + +(define (get id) + (list (list 'entry '(title "Item") '(id "111") '(content (item (name "Apple") (currencyCode "USD") (currencySymbol "$") (price 2.99))))) +) + diff --git a/sca-cpp/trunk/components/http/http.composite b/sca-cpp/trunk/components/http/http.composite index 43d9fb6413..48fe1c3b55 100644 --- a/sca-cpp/trunk/components/http/http.composite +++ b/sca-cpp/trunk/components/http/http.composite @@ -29,6 +29,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -36,4 +71,18 @@ + + + + + + + + + + + + + + diff --git a/sca-cpp/trunk/components/http/httppatch.componentType b/sca-cpp/trunk/components/http/httppatch.componentType new file mode 100644 index 0000000000..8f3173be85 --- /dev/null +++ b/sca-cpp/trunk/components/http/httppatch.componentType @@ -0,0 +1,29 @@ + + + + + + + + + diff --git a/sca-cpp/trunk/components/http/httppatch.cpp b/sca-cpp/trunk/components/http/httppatch.cpp new file mode 100644 index 0000000000..4debca045e --- /dev/null +++ b/sca-cpp/trunk/components/http/httppatch.cpp @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * HTTP client component implementation. + */ + +#include "string.hpp" +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "../../modules/http/http.hpp" + +namespace tuscany { +namespace httppatch { + +/** + * Evaluate an HTTP patch. + */ +const failable get(const lambda&)> url, const lambda&)> val, http::CURLSession& ch) { + debug("httppatch::get"); + const value u = url(mklist("get", list())); + const value v = val(mklist("get", list())); + debug(u, "httppatch::get::url"); + debug(v, "httppatch::get::val"); + return http::patch(v, u, ch); +} + +/** + * Component implementation lambda function. + */ +class applyhttp { +public: + applyhttp(const lambda&)> url, const lambda&)> val, http::CURLSession& ch) : url(url), val(val), ch(ch) { + } + + const value operator()(const list& params) const { + debug(params, "httppatch::applyhttp::params"); + const value func(car(params)); + if (func == "get") + return get(url, val, ch); + return tuscany::mkfailure(); + } + +private: + const lambda&)> url; + const lambda&)> val; + http::CURLSession& ch; +}; + +/** + * Start the component. + */ +const failable start(const list& params) { + // Create a CURL session + http::CURLSession& ch = *(new (gc_new()) http::CURLSession("", "", "", "")); + + // Return the component implementation lambda function + return value(lambda&)>(applyhttp(car(params), cadr(params), ch))); +} + +} +} + +extern "C" { + +const tuscany::value apply(const tuscany::list& params) { + const tuscany::value func(car(params)); + if (func == "start") + return tuscany::httppatch::start(cdr(params)); + return tuscany::mkfailure(); +} + +} diff --git a/sca-cpp/trunk/components/http/httppost.componentType b/sca-cpp/trunk/components/http/httppost.componentType new file mode 100644 index 0000000000..8f3173be85 --- /dev/null +++ b/sca-cpp/trunk/components/http/httppost.componentType @@ -0,0 +1,29 @@ + + + + + + + + + diff --git a/sca-cpp/trunk/components/http/httppost.cpp b/sca-cpp/trunk/components/http/httppost.cpp new file mode 100644 index 0000000000..9784c4513c --- /dev/null +++ b/sca-cpp/trunk/components/http/httppost.cpp @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * HTTP client component implementation. + */ + +#include "string.hpp" +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "../../modules/http/http.hpp" + +namespace tuscany { +namespace httppost { + +/** + * Evaluate an HTTP post. + */ +const failable get(const lambda&)> url, const lambda&)> val, http::CURLSession& ch) { + debug("httppost::get"); + const value u = url(mklist("get", list())); + const value v = val(mklist("get", list())); + debug(u, "httppost::get::url"); + debug(v, "httppost::get::val"); + return http::post(v, u, ch); +} + +/** + * Component implementation lambda function. + */ +class applyhttp { +public: + applyhttp(const lambda&)> url, const lambda&)> val, http::CURLSession& ch) : url(url), val(val), ch(ch) { + } + + const value operator()(const list& params) const { + debug(params, "httppost::applyhttp::params"); + const value func(car(params)); + if (func == "get") + return get(url, val, ch); + return tuscany::mkfailure(); + } + +private: + const lambda&)> url; + const lambda&)> val; + http::CURLSession& ch; +}; + +/** + * Start the component. + */ +const failable start(const list& params) { + // Create a CURL session + http::CURLSession& ch = *(new (gc_new()) http::CURLSession("", "", "", "")); + + // Return the component implementation lambda function + return value(lambda&)>(applyhttp(car(params), cadr(params), ch))); +} + +} +} + +extern "C" { + +const tuscany::value apply(const tuscany::list& params) { + const tuscany::value func(car(params)); + if (func == "start") + return tuscany::httppost::start(cdr(params)); + return tuscany::mkfailure(); +} + +} diff --git a/sca-cpp/trunk/components/http/httpput.componentType b/sca-cpp/trunk/components/http/httpput.componentType new file mode 100644 index 0000000000..8f3173be85 --- /dev/null +++ b/sca-cpp/trunk/components/http/httpput.componentType @@ -0,0 +1,29 @@ + + + + + + + + + diff --git a/sca-cpp/trunk/components/http/httpput.cpp b/sca-cpp/trunk/components/http/httpput.cpp new file mode 100644 index 0000000000..4ec63dabce --- /dev/null +++ b/sca-cpp/trunk/components/http/httpput.cpp @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * HTTP client component implementation. + */ + +#include "string.hpp" +#include "function.hpp" +#include "list.hpp" +#include "value.hpp" +#include "monad.hpp" +#include "../../modules/http/http.hpp" + +namespace tuscany { +namespace httpput { + +/** + * Evaluate an HTTP put. + */ +const failable get(const lambda&)> url, const lambda&)> val, http::CURLSession& ch) { + debug("httpput::get"); + const value u = url(mklist("get", list())); + const value v = val(mklist("get", list())); + debug(u, "httpput::get::url"); + debug(v, "httpput::get::val"); + return http::put(v, u, ch); +} + +/** + * Component implementation lambda function. + */ +class applyhttp { +public: + applyhttp(const lambda&)> url, const lambda&)> val, http::CURLSession& ch) : url(url), val(val), ch(ch) { + } + + const value operator()(const list& params) const { + debug(params, "httpput::applyhttp::params"); + const value func(car(params)); + if (func == "get") + return get(url, val, ch); + return tuscany::mkfailure(); + } + +private: + const lambda&)> url; + const lambda&)> val; + http::CURLSession& ch; +}; + +/** + * Start the component. + */ +const failable start(const list& params) { + // Create a CURL session + http::CURLSession& ch = *(new (gc_new()) http::CURLSession("", "", "", "")); + + // Return the component implementation lambda function + return value(lambda&)>(applyhttp(car(params), cadr(params), ch))); +} + +} +} + +extern "C" { + +const tuscany::value apply(const tuscany::list& params) { + const tuscany::value func(car(params)); + if (func == "start") + return tuscany::httpput::start(cdr(params)); + return tuscany::mkfailure(); +} + +} diff --git a/sca-cpp/trunk/components/http/server-test.scm b/sca-cpp/trunk/components/http/server-test.scm new file mode 100644 index 0000000000..4bbff6e5c2 --- /dev/null +++ b/sca-cpp/trunk/components/http/server-test.scm @@ -0,0 +1,44 @@ +; Licensed to the Apache Software Foundation (ASF) under one +; or more contributor license agreements. See the NOTICE file +; distributed with this work for additional information +; regarding copyright ownership. The ASF licenses this file +; to you under the Apache License, Version 2.0 (the +; "License"); you may not use this file except in compliance +; with the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, +; software distributed under the License is distributed on an +; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +; KIND, either express or implied. See the License for the +; specific language governing permissions and limitations +; under the License. + +; JSON-RPC test case + +(define (echo x) x) + +; ATOMPub test case + +(define (get id) + (if (nul id) + '((feed (title "Sample Feed") (id "123456789") (entry + (((title "Item") (id "111") (content (item (name "Apple") (currencyCode "USD") (currencySymbol "$") (price 2.99)))) + ((title "Item") (id "222") (content (item (name "Orange") (currencyCode "USD") (currencySymbol "$") (price 3.55)))) + ((title "Item") (id "333") (content (item (name "Pear") (currencyCode "USD") (currencySymbol "$") (price 1.55)))))))) + + (list (list 'entry '(title "Item") (list 'id (car id)) '(content (item (name "Apple") (currencyCode "USD") (currencySymbol "$") (price 2.99)))))) +) + +(define (post collection item) + '("123456789") +) + +(define (put id item) + true +) + +(define (delete id) + true +) diff --git a/sca-cpp/trunk/components/http/url-test.scm b/sca-cpp/trunk/components/http/url-test.scm index 29e6629aa5..37a5b345b2 100644 --- a/sca-cpp/trunk/components/http/url-test.scm +++ b/sca-cpp/trunk/components/http/url-test.scm @@ -18,6 +18,6 @@ ; URL test case (define (get id) - "http://localhost:8090/index.html" + "http://localhost:8090/test" ) -- cgit v1.2.3