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!).

Files at this revision

API Documentation at this revision

Comitter:
hlipka
Date:
Tue Jan 11 23:00:09 2011 +0000
Child:
1:62e112d14c8f
Commit message:
initial version

Changed in this revision

url.cpp Show annotated file Show diff for this revision Revisions of this file
url.h Show annotated file Show diff for this revision Revisions of this file
urlencode.cpp Show annotated file Show diff for this revision Revisions of this file
webservice.cpp Show annotated file Show diff for this revision Revisions of this file
webservice.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/url.cpp	Tue Jan 11 23:00:09 2011 +0000
@@ -0,0 +1,151 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "url.h"
+
+Url::Url() : m_port(0)
+{
+  
+}
+
+string Url::getProtocol()
+{
+  return m_protocol;
+}
+
+string Url::getHost()
+{
+  return m_host;
+}
+
+bool Url::getHostIp(IpAddr* ip)
+{
+  unsigned int ipArr[4] = {0};
+  if ( sscanf(m_host.c_str(), "%u.%u.%u.%u", &ipArr[0], &ipArr[1], &ipArr[2], &ipArr[3]) != 4 )
+  {
+    return false;
+  }
+  *ip = IpAddr(ipArr[0], ipArr[1], ipArr[2], ipArr[3]);
+  return true;
+}
+
+uint16_t Url::getPort()
+{
+  return m_port;
+}
+
+string Url::getPath()
+{
+  return m_path;
+}
+
+void Url::setProtocol(string protocol)
+{
+  m_protocol = protocol;
+}
+
+void Url::setHost(string host)
+{
+  m_host = host;
+}
+
+void Url::setPort(uint16_t port)
+{
+  m_port = port;
+}
+
+void Url::setPath(string path)
+{
+  m_path = path;
+}
+  
+void Url::fromString(string str)
+{
+  //URI form [protocol://]host[:port]/path
+  int pos = 0;
+  int len = str.find("://");
+  if( len > 0)
+  {
+    m_protocol = str.substr(0, len);
+    pos += len + 3;
+  }
+  else
+  {
+    m_protocol = "";
+  }
+  
+  bool isPort = false;
+  int cln_pos = str.find(":", pos);
+  int slash_pos = str.find("/", pos);
+  if( slash_pos == -1 )
+  {
+    slash_pos = str.length();
+  }
+  if( (cln_pos != -1) && (cln_pos < slash_pos) )
+  {
+    isPort = true;
+    len = cln_pos - pos;
+  }
+  else
+  {
+    len = slash_pos - pos;
+  }
+  
+  m_host = str.substr(pos, len);
+  
+  pos += len;
+  if( isPort )
+  {
+    pos+=1; //Do not keep :
+    unsigned int port = 0;
+    sscanf(str.substr(pos, cln_pos-pos).c_str(),"%u", &port);
+    m_port = (uint16_t)port;
+    pos = slash_pos;
+  }
+  
+  m_path = str.substr(pos);  
+}
+
+string Url::toString()
+{
+  string url;
+  if( !m_protocol.empty() )
+  {
+    url.append(m_protocol);
+    url.append("://");
+  }
+  url.append(m_host);
+  if( m_port )
+  {
+    char c_port[6] = {0};
+    sprintf(c_port, "%d", m_port);
+    url.append(":");
+    url.append(c_port);
+  }
+  if(m_path[0]!='/')
+  {
+    url.append("/");
+  }
+  url.append(m_path);
+  return url;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/url.h	Tue Jan 11 23:00:09 2011 +0000
@@ -0,0 +1,88 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef URL_H
+#define URL_H
+
+#include "core/ipaddr.h"
+
+#include <string>
+using std::string;
+
+#include "mbed.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+char *url_encode(char *str);
+char *url_decode(char *str);
+
+#ifdef __cplusplus
+}
+#endif
+
+class Url
+{
+public:
+  static string encode(const string& url)
+  {
+    char* c_res = url_encode( (char*) url.c_str() );
+    string res(c_res);
+    free(c_res); //Alloc'ed in url_encode()
+    return res;
+  }
+  
+  static string decode(const string& url)
+  {
+    char* c_res = url_decode( (char*) url.c_str() );
+    string res(c_res);
+    free(c_res); //Alloc'ed in url_decode()
+    return res;
+  }
+  
+  Url();
+
+  string getProtocol();
+  string getHost();
+  bool getHostIp(IpAddr* ip); //If host is in IP form, return true & proper object by ptr
+  uint16_t getPort();
+  string getPath();
+  
+  void setProtocol(string protocol);
+  void setHost(string host);
+  void setPort(uint16_t port);
+  void setPath(string path);
+  
+  void fromString(string str);
+  string toString();
+
+private:
+  string m_protocol;
+  string m_host;
+  uint16_t m_port;
+  string m_path;
+  
+};
+
+#endif /* LWIP_UTILS_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/urlencode.cpp	Tue Jan 11 23:00:09 2011 +0000
@@ -0,0 +1,80 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "url.h"
+#include <stdlib.h>
+#include <ctype.h>
+
+//From http://www.geekhideout.com/urlcode.shtml
+
+char from_hex(char ch);
+char to_hex(char code);
+
+/* Converts a hex character to its integer value */
+char from_hex(char ch) {
+  return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
+}
+
+/* Converts an integer value to its hex character*/
+char to_hex(char code) {
+  static char hex[] = "0123456789abcdef";
+  return hex[code & 15];
+}
+
+/* Returns a url-encoded version of str */
+/* IMPORTANT: be sure to free() the returned string after use */
+char *url_encode(char *str) {
+  char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
+  while (*pstr) {
+    if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') 
+      *pbuf++ = *pstr;
+    else if (*pstr == ' ') 
+      *pbuf++ = '+';
+    else 
+      *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
+    pstr++;
+  }
+  *pbuf = '\0';
+  return buf;
+}
+
+/* Returns a url-decoded version of str */
+/* IMPORTANT: be sure to free() the returned string after use */
+char *url_decode(char *str) {
+  char *pstr = str, *buf = (char*)malloc(strlen(str) + 1), *pbuf = buf;
+  while (*pstr) {
+    if (*pstr == '%') {
+      if (pstr[1] && pstr[2]) {
+        *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
+        pstr += 2;
+      }
+    } else if (*pstr == '+') { 
+      *pbuf++ = ' ';
+    } else {
+      *pbuf++ = *pstr;
+    }
+    pstr++;
+  }
+  *pbuf = '\0';
+  return buf;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/webservice.cpp	Tue Jan 11 23:00:09 2011 +0000
@@ -0,0 +1,200 @@
+#include "Timer.h"
+
+#include "webservice.h"
+
+#include "spxmlnode.hpp"
+#include "spxmlhandle.hpp"
+
+#include "dnsresolve.h"
+
+void WebService::onTCPSocketEvent(TCPSocketEvent e) {
+//    printf("New TCPSocketEvent: %d\n",e);
+    switch (e) {
+        case TCPSOCKET_CONNECTED:
+            _connected = true;
+            _connecting=false;
+            break;
+
+        case TCPSOCKET_READABLE:
+            break;
+        case TCPSOCKET_WRITEABLE:
+            break;
+
+        case TCPSOCKET_CONTIMEOUT:
+        case TCPSOCKET_CONRST:
+        case TCPSOCKET_CONABRT:
+        case TCPSOCKET_ERROR:
+        case TCPSOCKET_DISCONNECTED: {
+            // don't close the real socket here, as this may skip the already received data
+            _connected = false;
+            _connecting=false;
+        }
+        break;
+    }
+}
+
+WebService::WebService(const char* url):_url(url) {
+    _closed=false;
+    _readpos=0;
+    _readlen=0;
+    _readpos=0;
+    _readlen=0;
+
+    _socket.setOnEvent(this,&WebService::onTCPSocketEvent);
+}
+
+SP_XmlDomParser* WebService::callService() {
+    Url url;
+    url.fromString(_url);
+
+    string request=string("GET ").append(_url).append(" HTTP/1.0\r\n\r\n");
+
+    IpAddr addr;
+    if (!url.getHostIp(&addr)) {
+        DNSResolver dr;
+        addr=dr.resolveName(url.getHost().c_str());
+    }
+//    printf("host ip=%i.%i.%i.%i\n",addr[0],addr[1],addr[2],addr[3]);
+
+    int port=0==url.getPort()?80:url.getPort();
+
+    Host host(addr, port);
+
+    TCPSocketErr bindErr = _socket.connect(host);
+
+    if (bindErr != 0) {
+        printf("connection error %i\n", bindErr);
+        return false;
+    }
+
+    // wait for connection established
+    _connecting=true;
+    mbed::Timer tmr;
+    tmr.start();
+
+    while (_connecting) {
+        Net::poll();
+        wait(0.1);
+        if (tmr.read()>10) {
+            printf("reached timeout\n");
+            break;
+        }
+    }
+
+    if (!_connected) {
+        printf("error - could not connect (timeout)\n");
+        return NULL;
+    }
+//    printf("send request[%s]\n",request.c_str());
+    _socket.send(request.c_str(),request.length());
+    Net::poll();
+
+    if (!_connected)
+        return NULL;
+
+    string firstLine=readResponseLine();
+    // todo: parse for HTTP response
+    // response must be for HTTP/1.0, and be 200
+    if (0!=firstLine.compare("HTTP/1.0 200 OK"))
+    {
+        printf("call not sucessfull, response=%s\n",firstLine.c_str());
+        return NULL;
+    }    
+    // skip headers    
+    while (true) {
+        string line=readResponseLine();
+//        printf("header=[%s]\n",line.c_str());
+        if (0==line.length())
+            break;
+    }
+    SP_XmlDomParser *parser=new SP_XmlDomParser();
+    while (true) {
+        string line=readResponseLine();
+//        printf("content=[%s]\n",line.c_str());
+        parser->append(line.c_str(),line.length());
+        if (0==line.length())
+            break;
+    }
+
+    return parser;
+}
+
+void WebService::close() {
+    if (_closed)
+        return;
+
+    while (_connected) {
+        Net::poll();
+        // read remaining data
+        int err=_socket.recv(_readbuf,BUFSIZE-1);
+        if (err<=0)
+            break;
+    }
+
+    while (true) {
+        Net::poll();
+        // read remaining data
+        int err=_socket.recv(_readbuf,BUFSIZE-1);
+        if (err<=0)
+            break;
+    }
+
+    _socket.resetOnEvent();
+    _socket.close();
+
+    _closed=true;
+}
+
+
+
+string WebService::readResponseLine() {
+    string r;
+    bool got_r=false;
+    
+    int emptyCount=0;
+    while (true) {
+        Net::poll();
+        if (_readlen>_readpos) {
+            while (_readbuf[_readpos]!=0 && _readpos<_readlen) {
+                char c=_readbuf[_readpos++];
+                if (!got_r) {
+                    if (c=='\r') {
+                        got_r=true;
+                    } else {
+                        r.push_back(c);
+                    }
+                } else {
+                    if (c=='\n') {
+                        return r;
+                    } else {
+                        r.push_back('\r'); // push missed \r also, so push it to string
+                        r.push_back(c);
+                        got_r=false;
+                    }
+                }
+            }
+        } else {
+            int err=_socket.recv(_readbuf,BUFSIZE-1);
+            if (err < 0) {
+                printf("error while receiving data: %i!\n",err);
+                break;
+            } else if (err>0) {
+                emptyCount=0;
+                _readbuf[err]=0;
+                _readlen=err;
+                _readpos=0;
+//                printf("r=%s\n",_readbuf);
+            }
+            else
+            {
+                // when we don't receive any data, and are not connected
+                // we stop because there isn't anything left
+                if (emptyCount++>2)
+                    if (!_connected)
+                        break;
+            }
+        }
+        wait(0.1);
+    }
+    return r;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/webservice.h	Tue Jan 11 23:00:09 2011 +0000
@@ -0,0 +1,58 @@
+#ifndef __WEBSERVICE_H__
+#define __WEBSERVICE_H__
+
+#include "spdomparser.hpp"
+
+#include "TCPSocket.h"
+
+#include "url.h"
+
+#define BUFSIZE 256
+
+/**
+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 and DNSResolver library
+The code for URL handling has been copied directly from the original NetServices library (Thanks to Donatien!).
+
+*/
+class WebService
+{
+    public:
+        /**
+            create the web service instance.
+            @params url the URL to call via GET
+        */
+        WebService(const char* url);
+        /**
+            Execute the web service call. Note that the caller is responsible for freeing the return parser instance.
+            @return the XML parser, or NULL if an error occured
+        */
+        SP_XmlDomParser *callService();
+        /**
+            close all resources
+        */
+        ~WebService(){close();};
+        /**
+            close all resources
+        */
+        void close();
+        
+    private:
+        void onTCPSocketEvent(TCPSocketEvent e);
+        string readResponseLine();
+        
+        const char* _url;
+        bool _connecting;
+        bool _connected;
+        bool _closed;
+        TCPSocket _socket;
+
+        char _readbuf[BUFSIZE];
+        int _readpos;
+        int _readlen;
+        
+};
+
+
+
+#endif
\ No newline at end of file