NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers url.cpp Source File

url.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "url.h"
00025 
00026 Url::Url() : m_port(0)
00027 {
00028   
00029 }
00030 
00031 string Url::getProtocol()
00032 {
00033   return m_protocol;
00034 }
00035 
00036 string Url::getHost()
00037 {
00038   return m_host;
00039 }
00040 
00041 bool Url::getHostIp(IpAddr* ip)
00042 {
00043   unsigned int ipArr[4] = {0};
00044   if ( sscanf(m_host.c_str(), "%u.%u.%u.%u", &ipArr[0], &ipArr[1], &ipArr[2], &ipArr[3]) != 4 )
00045   {
00046     return false;
00047   }
00048   *ip = IpAddr(ipArr[0], ipArr[1], ipArr[2], ipArr[3]);
00049   return true;
00050 }
00051 
00052 uint16_t Url::getPort()
00053 {
00054   return m_port;
00055 }
00056 
00057 string Url::getPath()
00058 {
00059   return m_path;
00060 }
00061 
00062 void Url::setProtocol(string protocol)
00063 {
00064   m_protocol = protocol;
00065 }
00066 
00067 void Url::setHost(string host)
00068 {
00069   m_host = host;
00070 }
00071 
00072 void Url::setPort(uint16_t port)
00073 {
00074   m_port = port;
00075 }
00076 
00077 void Url::setPath(string path)
00078 {
00079   m_path = path;
00080 }
00081   
00082 void Url::fromString(string str)
00083 {
00084   //URI form [protocol://]host[:port]/path
00085   int pos = 0;
00086   int len = str.find("://");
00087   if( len > 0)
00088   {
00089     m_protocol = str.substr(0, len);
00090     pos += len + 3;
00091   }
00092   else
00093   {
00094     m_protocol = "";
00095   }
00096   
00097   bool isPort = false;
00098   int cln_pos = str.find(":", pos);
00099   int slash_pos = str.find("/", pos);
00100   if( slash_pos == -1 )
00101   {
00102     slash_pos = str.length();
00103   }
00104   if( (cln_pos != -1) && (cln_pos < slash_pos) )
00105   {
00106     isPort = true;
00107     len = cln_pos - pos;
00108   }
00109   else
00110   {
00111     len = slash_pos - pos;
00112   }
00113   
00114   m_host = str.substr(pos, len);
00115   
00116   pos += len;
00117   if( isPort )
00118   {
00119     pos+=1; //Do not keep :
00120     unsigned int port = 0;
00121     sscanf(str.substr(pos, cln_pos-pos).c_str(),"%u", &port);
00122     m_port = (uint16_t)port;
00123     pos = slash_pos;
00124   }
00125   
00126   m_path = str.substr(pos);  
00127 }
00128 
00129 string Url::toString()
00130 {
00131   string url;
00132   if( !m_protocol.empty() )
00133   {
00134     url.append(m_protocol);
00135     url.append("://");
00136   }
00137   url.append(m_host);
00138   if( m_port )
00139   {
00140     char c_port[6] = {0};
00141     sprintf(c_port, "%d", m_port);
00142     url.append(":");
00143     url.append(c_port);
00144   }
00145   if(m_path[0]!='/')
00146   {
00147     url.append("/");
00148   }
00149   url.append(m_path);
00150   return url;
00151 }