A simple web service over HTTP library. Calls a HTTP server via GET, and returns the response wrapped in a XML parser. All calls are synchronous. Needs the NetServicesMin, DNSResolver, TcpLineStream and spxml libraries. The code for URL handling has been copied directly from the original NetServices library (Thanks to Donatien!).

Committer:
hlipka
Date:
Fri Feb 18 13:15:50 2011 +0000
Revision:
2:687430e7f63a
Parent:
1:62e112d14c8f
added license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 2:687430e7f63a 1 /*
hlipka 2:687430e7f63a 2 * WebService library
hlipka 2:687430e7f63a 3 * Copyright (c) 2010 Hendrik Lipka
hlipka 2:687430e7f63a 4 *
hlipka 2:687430e7f63a 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
hlipka 2:687430e7f63a 6 * of this software and associated documentation files (the "Software"), to deal
hlipka 2:687430e7f63a 7 * in the Software without restriction, including without limitation the rights
hlipka 2:687430e7f63a 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hlipka 2:687430e7f63a 9 * copies of the Software, and to permit persons to whom the Software is
hlipka 2:687430e7f63a 10 * furnished to do so, subject to the following conditions:
hlipka 2:687430e7f63a 11 *
hlipka 2:687430e7f63a 12 * The above copyright notice and this permission notice shall be included in
hlipka 2:687430e7f63a 13 * all copies or substantial portions of the Software.
hlipka 2:687430e7f63a 14 *
hlipka 2:687430e7f63a 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hlipka 2:687430e7f63a 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hlipka 2:687430e7f63a 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hlipka 2:687430e7f63a 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hlipka 2:687430e7f63a 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hlipka 2:687430e7f63a 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hlipka 2:687430e7f63a 21 * THE SOFTWARE.
hlipka 2:687430e7f63a 22 */
hlipka 2:687430e7f63a 23
hlipka 1:62e112d14c8f 24 #include "Timer.h"
hlipka 1:62e112d14c8f 25
hlipka 1:62e112d14c8f 26 #include "webservice.h"
hlipka 1:62e112d14c8f 27
hlipka 1:62e112d14c8f 28 #include "spxmlnode.hpp"
hlipka 1:62e112d14c8f 29 #include "spxmlhandle.hpp"
hlipka 1:62e112d14c8f 30
hlipka 1:62e112d14c8f 31 WebService::WebService(const char* urlStr) {
hlipka 1:62e112d14c8f 32 Url *url=new Url();
hlipka 1:62e112d14c8f 33 url->fromString(urlStr);
hlipka 1:62e112d14c8f 34
hlipka 1:62e112d14c8f 35 // printf("host name from URL=%s\n",url->getHost().c_str());
hlipka 1:62e112d14c8f 36
hlipka 1:62e112d14c8f 37 _stream=new TCPLineStream(url->getHost().c_str(),0==url->getPort()?80:url->getPort(),"\r\n");
hlipka 1:62e112d14c8f 38 delete url;
hlipka 1:62e112d14c8f 39
hlipka 1:62e112d14c8f 40 _request=string("GET ").append(urlStr).append(" HTTP/1.0\r\n");
hlipka 1:62e112d14c8f 41 // printf("request=[%s]\n",_request.c_str());
hlipka 1:62e112d14c8f 42 }
hlipka 1:62e112d14c8f 43
hlipka 1:62e112d14c8f 44 SP_XmlDomParser* WebService::callService() {
hlipka 1:62e112d14c8f 45 if (!_stream->open())
hlipka 1:62e112d14c8f 46 return NULL;
hlipka 1:62e112d14c8f 47
hlipka 1:62e112d14c8f 48 // printf("send request[%s]\n",request.c_str());
hlipka 1:62e112d14c8f 49 _stream->sendLine(_request);
hlipka 1:62e112d14c8f 50
hlipka 1:62e112d14c8f 51 string firstLine=_stream->readLine();
hlipka 1:62e112d14c8f 52 // todo: parse for HTTP response
hlipka 1:62e112d14c8f 53 // response must be for HTTP/1.0, and be 200
hlipka 1:62e112d14c8f 54 if (0!=firstLine.compare("HTTP/1.0 200 OK")) {
hlipka 1:62e112d14c8f 55 printf("call not sucessfull, response=%s\n",firstLine.c_str());
hlipka 1:62e112d14c8f 56 return NULL;
hlipka 1:62e112d14c8f 57 }
hlipka 1:62e112d14c8f 58 // skip headers
hlipka 1:62e112d14c8f 59 while (true) {
hlipka 1:62e112d14c8f 60 string line=_stream->readLine();
hlipka 1:62e112d14c8f 61 // printf("header=[%s]\n",line.c_str());
hlipka 1:62e112d14c8f 62 if (0==line.length())
hlipka 1:62e112d14c8f 63 break;
hlipka 1:62e112d14c8f 64 }
hlipka 1:62e112d14c8f 65 SP_XmlDomParser *parser=new SP_XmlDomParser();
hlipka 1:62e112d14c8f 66 while (true) {
hlipka 1:62e112d14c8f 67 string line=_stream->readLine(1500);
hlipka 1:62e112d14c8f 68 // printf("content=[%s]\n",line.c_str());
hlipka 1:62e112d14c8f 69 parser->append(line.c_str(),line.length());
hlipka 1:62e112d14c8f 70 if (0==line.length())
hlipka 1:62e112d14c8f 71 break;
hlipka 1:62e112d14c8f 72 }
hlipka 1:62e112d14c8f 73 _stream->close();
hlipka 1:62e112d14c8f 74 return parser;
hlipka 1:62e112d14c8f 75 }
hlipka 1:62e112d14c8f 76
hlipka 1:62e112d14c8f 77 void WebService::close() {
hlipka 1:62e112d14c8f 78 if (NULL!=_stream) {
hlipka 1:62e112d14c8f 79 _stream->close();
hlipka 1:62e112d14c8f 80 delete _stream;
hlipka 1:62e112d14c8f 81 _stream=NULL;
hlipka 1:62e112d14c8f 82 }
hlipka 1:62e112d14c8f 83 }
hlipka 1:62e112d14c8f 84