URL.cc

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 #include "URL.h"
00019 #include "StringBuffer.h"
00020 #include "debug/DebugUtils.h"
00021 
00022 namespace oasys {
00023 
00024 urlerr_t
00025 URL::parse_internal()
00026 {
00027     size_t beg, end;
00028 
00029     // reset all fields
00030     proto_.erase();
00031     host_.erase();
00032     port_ = 0;
00033     path_.erase();
00034 
00035     // extract protocol
00036     if ((end = url_.find(':')) == std::string::npos)
00037         return URLPARSE_BADSEP;
00038     
00039     // skip the separator
00040     if (url_[end+1] != '/' || url_[end+2] != '/')
00041         return URLPARSE_BADPROTO;
00042 
00043     // valid protocol -- assign it and skip
00044     proto_.assign(url_, 0, end);
00045     beg = end + 3;
00046 
00047     // check for no host
00048     if (beg == url_.length())
00049         return URLPARSE_NOHOST;
00050 
00051     // search for the hostname/path separator
00052     end = url_.find_first_of(":/", beg, 2);
00053 
00054     if (end == std::string::npos) {
00055         // there's no port or pathname, it's just a host name, but
00056         // append a / for the canonical form
00057         host_.assign(url_, beg, url_.length());
00058         url_.push_back('/');
00059         return URLPARSE_OK;
00060     }
00061 
00062     // grab the hostname
00063     host_.assign(url_, beg, end - beg);
00064 
00065     // parse the port if it exists
00066     if (url_[end] == ':') {
00067         char* endstr;
00068         beg = end + 1;
00069         end = url_.find('/', beg);
00070         if (end == std::string::npos) {
00071             end = url_.length();
00072 
00073             // again, adjust for no trailing slash
00074             url_.push_back('/');
00075         }
00076         
00077         std::string portstr(url_, beg, end - beg);
00078         u_int32_t portval = strtoul(portstr.c_str(), &endstr, 10);
00079         
00080         if (portval > 65535)
00081             return URLPARSE_BADPORT;
00082 
00083         if (endstr != (portstr.c_str() + portstr.length()))
00084             return URLPARSE_BADPORT;
00085 
00086         port_ = portval;
00087 
00088         beg = end + 1;
00089     } else {
00090         // otherwise just skip past the slash separator
00091         ASSERT(url_[end] == '/');
00092         beg = end + 1;
00093     }
00094 
00095     // check for an empty path
00096     if (beg >= url_.length())
00097         return URLPARSE_OK;
00098 
00099     // otherwise store the path and we're done
00100     path_.assign(url_, beg, url_.length() - beg);
00101     return URLPARSE_OK;
00102 }
00103 
00104 urlerr_t
00105 URL::parse()
00106 {
00107     err_ = parse_internal();
00108     return err_;
00109 }
00110 
00111 void
00112 URL::format(const std::string& proto, const std::string& host, u_int16_t port,
00113             const std::string& path)
00114 {
00115     StringBuffer buf;
00116     
00117     proto_ = proto;
00118     host_ = host;
00119     port_ = port;
00120     path_ = path;
00121 
00122     buf.append(proto);
00123     buf.append("://");
00124     buf.append(host);
00125 
00126     if (port != 0) {
00127         buf.appendf(":%d", port);
00128     }
00129 
00130     if (path[0] != '/')
00131         buf.append('/');
00132 
00133     buf.append(path);
00134     
00135     url_.assign(buf.data(), buf.length());
00136     err_ = URLPARSE_OK;
00137 }
00138 
00139 } // namespace oasys

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