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