00001 /* 00002 * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By 00003 * downloading, copying, installing or using the software you agree to 00004 * this license. If you do not agree to this license, do not download, 00005 * install, copy or use the software. 00006 * 00007 * Intel Open Source License 00008 * 00009 * Copyright (c) 2004 Intel Corporation. All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions are 00013 * met: 00014 * 00015 * Redistributions of source code must retain the above copyright 00016 * notice, this list of conditions and the following disclaimer. 00017 * 00018 * Redistributions in binary form must reproduce the above copyright 00019 * notice, this list of conditions and the following disclaimer in the 00020 * documentation and/or other materials provided with the distribution. 00021 * 00022 * Neither the name of the Intel Corporation nor the names of its 00023 * contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00027 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00028 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00029 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR 00030 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00031 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00032 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00033 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00034 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00035 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00036 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00037 */ 00038 00039 #ifndef _OASYS_URL_H_ 00040 #define _OASYS_URL_H_ 00041 00042 00043 #include <list> 00044 #include <string> 00045 00046 #include "../compat/inttypes.h" 00047 00048 namespace oasys { 00049 00050 typedef enum urlerr_t { 00051 URLPARSE_OK, /* parsed ok */ 00052 URLPARSE_UNPARSED, /* not parsed yet */ 00053 URLPARSE_NOURL, /* no url in object */ 00054 URLPARSE_BADSEP, /* bad or missing separator char */ 00055 URLPARSE_BADPROTO, /* bad protocol */ 00056 URLPARSE_BADPORT, /* bad port */ 00057 URLPARSE_NOHOST, /* no host */ 00058 }; 00059 00063 class URL { 00064 public: 00068 URL() 00069 { 00070 clear(); 00071 } 00072 00076 URL(const std::string& url) 00077 { 00078 url_.assign(url.data(), url.length()); 00079 parse(); 00080 } 00081 00085 URL(const char* url) 00086 { 00087 url_.assign(url); 00088 parse(); 00089 } 00090 00094 URL(const URL& copy) 00095 : url_(copy.url_), 00096 proto_(copy.proto_), 00097 host_(copy.host_), 00098 port_(copy.port_), 00099 path_(copy.path_), 00100 err_(copy.err_) 00101 { 00102 } 00103 00107 void clear() 00108 { 00109 url_.erase(); 00110 err_ = URLPARSE_UNPARSED; 00111 } 00112 00116 urlerr_t parse(); 00117 00121 urlerr_t parse(const std::string& url) 00122 { 00123 url_.assign(url); 00124 return parse(); 00125 } 00126 00130 void format(const std::string& proto, 00131 const std::string& host, u_int16_t port, 00132 const std::string& path); 00133 00137 urlerr_t status() const 00138 { 00139 return err_; 00140 } 00141 00145 bool valid() const 00146 { 00147 return (err_ == URLPARSE_OK); 00148 } 00149 00154 00155 const char* c_str() const { return url_.c_str(); } 00156 const char* data() const { return url_.data(); } 00157 size_t length() const { return url_.length(); } 00158 00159 /* 00160 * The constituent fields are public to avoid the need for a bunch 00161 * of accessors. Assume that users of the class don't actually 00162 * muck with the objects or things may not work well. 00163 */ 00164 std::string url_; /* the url std::string */ 00165 std::string proto_; /* the protocol */ 00166 std::string host_; /* the hostname part */ 00167 u_int16_t port_; /* the port (0 if doesn't exists) */ 00168 std::string path_; /* the path part */ 00169 00170 urlerr_t err_; /* parse status */ 00171 00172 protected: 00173 urlerr_t parse_internal(); 00174 }; 00175 00176 } // namespace oasys 00177 00178 #endif /* _OASYS_URL_H_ */ 00179