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