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