00001 /* -*- C++ -*- */ 00002 00003 /* 00004 * The Apache Software License, Version 1.1 00005 * 00006 * 00007 * Copyright (c) 2002 The Apache Software Foundation. All rights 00008 * reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without 00011 * modification, are permitted provided that the following conditions 00012 * are met: 00013 * 00014 * 1. Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * 00017 * 2. Redistributions in binary form must reproduce the above copyright 00018 * notice, this list of conditions and the following disclaimer in 00019 * the documentation and/or other materials provided with the 00020 * distribution. 00021 * 00022 * 3. The end-user documentation included with the redistribution, 00023 * if any, must include the following acknowledgment: 00024 * "This product includes software developed by the 00025 * Apache Software Foundation (http://www.apache.org/)." 00026 * Alternately, this acknowledgment may appear in the software itself, 00027 * if and wherever such third-party acknowledgments normally appear. 00028 * 00029 * 4. The names "SOAP" and "Apache Software Foundation" must 00030 * not be used to endorse or promote products derived from this 00031 * software without prior written permission. For written 00032 * permission, please contact apache@apache.org. 00033 * 00034 * 5. Products derived from this software may not be called "Apache", 00035 * nor may "Apache" appear in their name, without prior written 00036 * permission of the Apache Software Foundation. 00037 * 00038 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 00039 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00040 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00041 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 00042 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00043 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00044 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 00045 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00046 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00047 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00048 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00049 * SUCH DAMAGE. 00050 * ==================================================================== 00051 * 00052 * This software consists of voluntary contributions made by many 00053 * individuals on behalf of the Apache Software Foundation. For more 00054 * information on the Apache Software Foundation, please see 00055 * <http://www.apache.org/>. 00056 */ 00057 00058 /* 00059 * 00060 * 00061 * @author Lilantha Darshana (lilantha@erunway.com) 00062 * 00063 */ 00064 00065 00066 #include "Platform.hpp" 00067 #include "Url.hpp" 00068 00069 00070 Url::Url() 00071 : m_Protocol(unknown), m_Port(0) 00072 { 00073 } 00074 00075 Url::Url(std::string url) 00076 { 00077 m_URL = url; 00078 if(isascii((int)url.at(0))) 00079 { 00080 // check this a valid URL 00081 if((url.at(1) == (int)':') && 00082 ((url.at(2) == (int)'/') || (url.at(2) == (int)'\\'))) 00083 return; 00084 else // assume valid URL hence, proceed with finding entries 00085 ParseURL(url); 00086 } 00087 } 00088 00089 Url::~Url() 00090 { 00091 00092 } 00093 00094 void Url::SetProtocol(std::string prot) 00095 { 00096 m_Port = 0; 00097 if(prot == "http") 00098 { 00099 m_Protocol = http; 00100 m_Port = HTTP_PORT; 00101 } 00102 else if( prot == "file") 00103 { 00104 m_Protocol = file; 00105 } 00106 else if( prot == "ftp") 00107 { 00108 m_Protocol = ftp; 00109 m_Port = FTP_PORT; 00110 } 00111 else if( prot == "https") 00112 { 00113 00114 m_Protocol = https; 00115 m_Port = HTTPS_PORT; 00116 } 00117 else 00118 m_Protocol = unknown; 00119 00120 } 00121 00122 void Url::ParseURL(std::string& url) 00123 { 00124 std::string::size_type begpos, pos; 00125 00126 // try to find out the protocol 00127 if((pos = url.find("://")) != std::string::npos) 00128 { 00129 SetProtocol(url.substr(0, pos)); 00130 // find m_Host name 00131 if(m_Protocol != unknown) 00132 { 00133 url = url.substr(pos + 3); // rest of the URL string 00134 begpos = pos = 0; 00135 std::string key(":/?"); 00136 00137 while(pos = url.find_first_of(key, begpos)) 00138 { 00139 if(pos == std::string::npos) // only host name found 00140 { 00141 if(m_Host.empty()) 00142 m_Host = url; 00143 if (key == "?") // found path 00144 m_Path = url.substr(begpos - 1); 00145 break; 00146 } 00147 else 00148 if(pos == 0) break; 00149 00150 switch(url.at(pos)) 00151 { 00152 case ':': 00153 if(m_Host.empty()) 00154 m_Host = url.substr(begpos, pos - begpos); 00155 pos++; 00156 begpos = pos; 00157 key = "/?"; // scan for the rest to get the path & query 00158 continue; 00159 00160 case '/': 00161 if (key == "/?") // found port number 00162 { 00163 m_Port = atoi(url.substr(begpos, pos - begpos + 1).c_str()); 00164 if(m_Host.empty()) 00165 m_Host = url.substr(0, begpos - 1); 00166 } 00167 else 00168 m_Host = url.substr(0, pos); 00169 pos++; 00170 begpos = pos; 00171 key = "?"; 00172 continue; 00173 00174 case '?': 00175 // not correctly supported 00176 m_Query = url.substr(pos); 00177 break; 00178 } 00179 break; 00180 } 00181 } 00182 } 00183 } 00184 00185 00186 00187 00188 00189 00190 00191 00192
Copyright © 2001 SL_OpenSource XML Project. All Rights Reserved