Go to the documentation of this file.00001 #include "base/Url.h"
00002
00003 #include <stdlib.h>
00004 #include <stdio.h>
00005
00006 base::Url::Url()
00007 {
00008 SetUrl(std::string());
00009 }
00010
00011 base::Url::Url(const char* url)
00012 {
00013 SetUrl(std::string(url ? url : ""));
00014 }
00015
00016 base::Url::Url(const std::string& url)
00017 {
00018 SetUrl(url);
00019 }
00020
00021 base::Url::~Url()
00022 {
00023 }
00024
00025 bool base::Url::SetUrl(const std::string& url, bool showerr)
00026 {
00027 fValid = false;
00028 fUrl.clear();
00029 fProtocol.clear();
00030 fHostName.clear();
00031 fPort = 0;
00032 fFileName.clear();
00033 fOptions.clear();
00034
00035 if (url.length()==0) return false;
00036
00037 fUrl = url;
00038 fValid = true;
00039
00040 std::string s = fUrl;
00041 std::size_t pos = s.find("://");
00042
00043 if (pos != std::string::npos) {
00044 fProtocol = s.substr(0, pos);
00045 s.erase(0, pos + 3);
00046 }
00047
00048 if (s.length() == 0) return fValid;
00049
00050
00051 pos = s.rfind("?");
00052 if (pos != std::string::npos) {
00053 fOptions = s.substr(pos+1);
00054 s.erase(pos);
00055 }
00056
00057 pos = s.find("/");
00058
00059 if (pos==0) {
00060 fFileName = s;
00061 } else
00062 if (pos != std::string::npos) {
00063 fHostName = s.substr(0, pos);
00064 fFileName = s.substr(pos+1);
00065 } else {
00066 fHostName = s;
00067 }
00068
00069 pos = fHostName.find(":");
00070 if (pos != std::string::npos) {
00071 char* errpos = 0;
00072 fPort = strtol(fHostName.c_str()+pos+1, &errpos, 10);
00073 if (errpos!=0) {
00074 if (showerr) fprintf(stderr, "Invalid URL format:%s - wrong port number\n", fHostName.c_str());
00075 fValid = false;
00076 } else {
00077 fHostName.erase(pos);
00078 }
00079 }
00080
00081 return fValid;
00082 }
00083
00084 std::string base::Url::GetFullName() const
00085 {
00086 if (fFileName.length()==0) return fHostName;
00087 if (fHostName.length()==0) return fFileName;
00088
00089 std::string res = fHostName;
00090 res+="/";
00091 res+=fFileName;
00092 return res;
00093 }
00094
00095 std::string base::Url::GetPortStr() const
00096 {
00097 if (fPort<=0) return std::string();
00098
00099 char sbuf[80];
00100 sprintf(sbuf, "%d", fPort);
00101
00102 return std::string(sbuf);
00103 }
00104
00105 bool base::Url::GetOption(const std::string& optname, std::string* value) const
00106 {
00107 if (value) value->clear();
00108
00109 if (optname.empty() || fOptions.empty()) return false;
00110
00111 int p = 0;
00112
00113 while (p < fOptions.length()) {
00114
00115 int separ = fOptions.find("&", p);
00116
00117 if (separ==std::string::npos) separ = fOptions.length();
00118
00119
00120
00121 if (separ-p >= optname.length()) {
00122 bool find = fOptions.compare(p, optname.length(), optname)==0;
00123 if (find) {
00124
00125 p+=optname.length();
00126
00127
00128 if (p==separ) return true;
00129
00130 if (fOptions[p]=='=') {
00131
00132 p++;
00133 if ((p<separ) && value) *value = fOptions.substr(p, separ-p);
00134 return true;
00135 }
00136 }
00137 }
00138
00139 p = separ;
00140 }
00141
00142 return false;
00143 }
00144
00145
00146 std::string base::Url::GetOptionValue(const std::string& optname) const
00147 {
00148 std::string res;
00149
00150 if (GetOption(optname, &res)) return res;
00151
00152 return std::string("");
00153
00154 }
00155
00156
00157