Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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