Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:850eacf3e945 1
RodColeman 0:850eacf3e945 2 /*
RodColeman 0:850eacf3e945 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
RodColeman 0:850eacf3e945 4
RodColeman 0:850eacf3e945 5 Permission is hereby granted, free of charge, to any person obtaining a copy
RodColeman 0:850eacf3e945 6 of this software and associated documentation files (the "Software"), to deal
RodColeman 0:850eacf3e945 7 in the Software without restriction, including without limitation the rights
RodColeman 0:850eacf3e945 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
RodColeman 0:850eacf3e945 9 copies of the Software, and to permit persons to whom the Software is
RodColeman 0:850eacf3e945 10 furnished to do so, subject to the following conditions:
RodColeman 0:850eacf3e945 11
RodColeman 0:850eacf3e945 12 The above copyright notice and this permission notice shall be included in
RodColeman 0:850eacf3e945 13 all copies or substantial portions of the Software.
RodColeman 0:850eacf3e945 14
RodColeman 0:850eacf3e945 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
RodColeman 0:850eacf3e945 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
RodColeman 0:850eacf3e945 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
RodColeman 0:850eacf3e945 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
RodColeman 0:850eacf3e945 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
RodColeman 0:850eacf3e945 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
RodColeman 0:850eacf3e945 21 THE SOFTWARE.
RodColeman 0:850eacf3e945 22 */
RodColeman 0:850eacf3e945 23
RodColeman 0:850eacf3e945 24 #include "url.h"
RodColeman 0:850eacf3e945 25
RodColeman 0:850eacf3e945 26 Url::Url() : m_port(0)
RodColeman 0:850eacf3e945 27 {
RodColeman 0:850eacf3e945 28
RodColeman 0:850eacf3e945 29 }
RodColeman 0:850eacf3e945 30
RodColeman 0:850eacf3e945 31 string Url::getProtocol()
RodColeman 0:850eacf3e945 32 {
RodColeman 0:850eacf3e945 33 return m_protocol;
RodColeman 0:850eacf3e945 34 }
RodColeman 0:850eacf3e945 35
RodColeman 0:850eacf3e945 36 string Url::getHost()
RodColeman 0:850eacf3e945 37 {
RodColeman 0:850eacf3e945 38 return m_host;
RodColeman 0:850eacf3e945 39 }
RodColeman 0:850eacf3e945 40
RodColeman 0:850eacf3e945 41 bool Url::getHostIp(IpAddr* ip)
RodColeman 0:850eacf3e945 42 {
RodColeman 0:850eacf3e945 43 unsigned int ipArr[4] = {0};
RodColeman 0:850eacf3e945 44 if ( sscanf(m_host.c_str(), "%u.%u.%u.%u", &ipArr[0], &ipArr[1], &ipArr[2], &ipArr[3]) != 4 )
RodColeman 0:850eacf3e945 45 {
RodColeman 0:850eacf3e945 46 return false;
RodColeman 0:850eacf3e945 47 }
RodColeman 0:850eacf3e945 48 *ip = IpAddr(ipArr[0], ipArr[1], ipArr[2], ipArr[3]);
RodColeman 0:850eacf3e945 49 return true;
RodColeman 0:850eacf3e945 50 }
RodColeman 0:850eacf3e945 51
RodColeman 0:850eacf3e945 52 uint16_t Url::getPort()
RodColeman 0:850eacf3e945 53 {
RodColeman 0:850eacf3e945 54 return m_port;
RodColeman 0:850eacf3e945 55 }
RodColeman 0:850eacf3e945 56
RodColeman 0:850eacf3e945 57 string Url::getPath()
RodColeman 0:850eacf3e945 58 {
RodColeman 0:850eacf3e945 59 return m_path;
RodColeman 0:850eacf3e945 60 }
RodColeman 0:850eacf3e945 61
RodColeman 0:850eacf3e945 62 void Url::setProtocol(string protocol)
RodColeman 0:850eacf3e945 63 {
RodColeman 0:850eacf3e945 64 m_protocol = protocol;
RodColeman 0:850eacf3e945 65 }
RodColeman 0:850eacf3e945 66
RodColeman 0:850eacf3e945 67 void Url::setHost(string host)
RodColeman 0:850eacf3e945 68 {
RodColeman 0:850eacf3e945 69 m_host = host;
RodColeman 0:850eacf3e945 70 }
RodColeman 0:850eacf3e945 71
RodColeman 0:850eacf3e945 72 void Url::setPort(uint16_t port)
RodColeman 0:850eacf3e945 73 {
RodColeman 0:850eacf3e945 74 m_port = port;
RodColeman 0:850eacf3e945 75 }
RodColeman 0:850eacf3e945 76
RodColeman 0:850eacf3e945 77 void Url::setPath(string path)
RodColeman 0:850eacf3e945 78 {
RodColeman 0:850eacf3e945 79 m_path = path;
RodColeman 0:850eacf3e945 80 }
RodColeman 0:850eacf3e945 81
RodColeman 0:850eacf3e945 82 void Url::fromString(string str)
RodColeman 0:850eacf3e945 83 {
RodColeman 0:850eacf3e945 84 //URI form [protocol://]host[:port]/path
RodColeman 0:850eacf3e945 85 int pos = 0;
RodColeman 0:850eacf3e945 86 int len = str.find("://");
RodColeman 0:850eacf3e945 87 if( len > 0)
RodColeman 0:850eacf3e945 88 {
RodColeman 0:850eacf3e945 89 m_protocol = str.substr(0, len);
RodColeman 0:850eacf3e945 90 pos += len + 3;
RodColeman 0:850eacf3e945 91 }
RodColeman 0:850eacf3e945 92 else
RodColeman 0:850eacf3e945 93 {
RodColeman 0:850eacf3e945 94 m_protocol = "";
RodColeman 0:850eacf3e945 95 }
RodColeman 0:850eacf3e945 96
RodColeman 0:850eacf3e945 97 bool isPort = false;
RodColeman 0:850eacf3e945 98 int cln_pos = str.find(":", pos);
RodColeman 0:850eacf3e945 99 int slash_pos = str.find("/", pos);
RodColeman 0:850eacf3e945 100 if( slash_pos == -1 )
RodColeman 0:850eacf3e945 101 {
RodColeman 0:850eacf3e945 102 slash_pos = str.length();
RodColeman 0:850eacf3e945 103 }
RodColeman 0:850eacf3e945 104 if( (cln_pos != -1) && (cln_pos < slash_pos) )
RodColeman 0:850eacf3e945 105 {
RodColeman 0:850eacf3e945 106 isPort = true;
RodColeman 0:850eacf3e945 107 len = cln_pos - pos;
RodColeman 0:850eacf3e945 108 }
RodColeman 0:850eacf3e945 109 else
RodColeman 0:850eacf3e945 110 {
RodColeman 0:850eacf3e945 111 len = slash_pos - pos;
RodColeman 0:850eacf3e945 112 }
RodColeman 0:850eacf3e945 113
RodColeman 0:850eacf3e945 114 m_host = str.substr(pos, len);
RodColeman 0:850eacf3e945 115
RodColeman 0:850eacf3e945 116 pos += len;
RodColeman 0:850eacf3e945 117 if( isPort )
RodColeman 0:850eacf3e945 118 {
RodColeman 0:850eacf3e945 119 pos+=1; //Do not keep :
RodColeman 0:850eacf3e945 120 unsigned int port = 0;
RodColeman 0:850eacf3e945 121 sscanf(str.substr(pos, cln_pos-pos).c_str(),"%u", &port);
RodColeman 0:850eacf3e945 122 m_port = (uint16_t)port;
RodColeman 0:850eacf3e945 123 pos = slash_pos;
RodColeman 0:850eacf3e945 124 }
RodColeman 0:850eacf3e945 125
RodColeman 0:850eacf3e945 126 m_path = str.substr(pos);
RodColeman 0:850eacf3e945 127 }
RodColeman 0:850eacf3e945 128
RodColeman 0:850eacf3e945 129 string Url::toString()
RodColeman 0:850eacf3e945 130 {
RodColeman 0:850eacf3e945 131 string url;
RodColeman 0:850eacf3e945 132 if( !m_protocol.empty() )
RodColeman 0:850eacf3e945 133 {
RodColeman 0:850eacf3e945 134 url.append(m_protocol);
RodColeman 0:850eacf3e945 135 url.append("://");
RodColeman 0:850eacf3e945 136 }
RodColeman 0:850eacf3e945 137 url.append(m_host);
RodColeman 0:850eacf3e945 138 if( m_port )
RodColeman 0:850eacf3e945 139 {
RodColeman 0:850eacf3e945 140 char c_port[6] = {0};
RodColeman 0:850eacf3e945 141 sprintf(c_port, "%d", m_port);
RodColeman 0:850eacf3e945 142 url.append(":");
RodColeman 0:850eacf3e945 143 url.append(c_port);
RodColeman 0:850eacf3e945 144 }
RodColeman 0:850eacf3e945 145 if(m_path[0]!='/')
RodColeman 0:850eacf3e945 146 {
RodColeman 0:850eacf3e945 147 url.append("/");
RodColeman 0:850eacf3e945 148 }
RodColeman 0:850eacf3e945 149 url.append(m_path);
RodColeman 0:850eacf3e945 150 return url;
RodColeman 0:850eacf3e945 151 }