00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00030 proto_.erase();
00031 host_.erase();
00032 port_ = 0;
00033 path_.erase();
00034
00035
00036 if ((end = url_.find(':')) == std::string::npos)
00037 return URLPARSE_BADSEP;
00038
00039
00040 if (url_[end+1] != '/' || url_[end+2] != '/')
00041 return URLPARSE_BADPROTO;
00042
00043
00044 proto_.assign(url_, 0, end);
00045 beg = end + 3;
00046
00047
00048 if (beg == url_.length())
00049 return URLPARSE_NOHOST;
00050
00051
00052 end = url_.find_first_of(":/", beg, 2);
00053
00054 if (end == std::string::npos) {
00055
00056
00057 host_.assign(url_, beg, url_.length());
00058 url_.push_back('/');
00059 return URLPARSE_OK;
00060 }
00061
00062
00063 host_.assign(url_, beg, end - beg);
00064
00065
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
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
00091 ASSERT(url_[end] == '/');
00092 beg = end + 1;
00093 }
00094
00095
00096 if (beg >= url_.length())
00097 return URLPARSE_OK;
00098
00099
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 }