URL.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #ifndef _OASYS_URL_H_
00019 #define _OASYS_URL_H_
00020 
00021 
00022 #include <list>
00023 #include <string>
00024 
00025 #include "../compat/inttypes.h"
00026 
00027 namespace oasys {
00028 
00029 typedef enum urlerr_t {
00030     URLPARSE_OK,        /* parsed ok */
00031     URLPARSE_UNPARSED,  /* not parsed yet */
00032     URLPARSE_NOURL,     /* no url in object */
00033     URLPARSE_BADSEP,    /* bad or missing separator char */
00034     URLPARSE_BADPROTO,  /* bad protocol */
00035     URLPARSE_BADPORT,   /* bad port */
00036     URLPARSE_NOHOST,    /* no host */
00037 };
00038 
00042 class URL {
00043 public:
00047     URL()
00048     {
00049         clear();
00050     }
00051 
00055     URL(const std::string& url)
00056     {
00057         url_.assign(url.data(), url.length());
00058         parse();
00059     }
00060 
00064     URL(const char* url)
00065     {
00066         url_.assign(url);
00067         parse();
00068     }
00069 
00073     URL(const URL& copy)
00074         : url_(copy.url_),
00075           proto_(copy.proto_),
00076           host_(copy.host_),
00077           port_(copy.port_),
00078           path_(copy.path_),
00079           err_(copy.err_)
00080     {
00081     }
00082 
00086     void clear()
00087     {
00088         url_.erase();
00089         err_ = URLPARSE_UNPARSED;
00090     }
00091 
00095     urlerr_t parse();
00096 
00100     urlerr_t parse(const std::string& url)
00101     {
00102         url_.assign(url);
00103         return parse();
00104     }
00105 
00109     void format(const std::string& proto,
00110                 const std::string& host, u_int16_t port,
00111                 const std::string& path);
00112     
00116     urlerr_t status() const
00117     {
00118         return err_;
00119     }
00120 
00124     bool valid() const
00125     {
00126         return (err_ == URLPARSE_OK);
00127     }
00128 
00133 
00134     const char* c_str()  const { return url_.c_str(); } 
00135     const char* data()   const { return url_.data(); }
00136     size_t      length() const { return url_.length(); }
00137 
00138     /*
00139      * The constituent fields are public to avoid the need for a bunch
00140      * of accessors. Assume that users of the class don't actually
00141      * muck with the objects or things may not work well.
00142      */
00143     std::string url_;   /* the url std::string */
00144     std::string proto_; /* the protocol */
00145     std::string host_;  /* the hostname part */
00146     u_int16_t   port_;  /* the port (0 if doesn't exists) */
00147     std::string path_;  /* the path part */
00148     
00149     urlerr_t err_;      /* parse status */
00150 
00151 protected:
00152     urlerr_t parse_internal();
00153 };
00154 
00155 } // namespace oasys
00156 
00157 #endif /* _OASYS_URL_H_ */
00158 

Generated on Sat Sep 8 08:43:36 2007 for DTN Reference Implementation by  doxygen 1.5.3