00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 const char *XrdClientUrlInfoCVSID = "$Id: XrdClientUrlInfo.cc 30949 2009-11-02 16:37:58Z ganis $";
00017
00018 #include "XrdClient/XrdClientDebug.hh"
00019 #include "XrdClient/XrdClientUrlInfo.hh"
00020 #include "XrdNet/XrdNetDNS.hh"
00021 #ifndef WIN32
00022 #include <netinet/in.h>
00023 #include <arpa/inet.h>
00024 #else
00025 #include "XrdSys/XrdWin32.hh"
00026 #endif
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029
00030
00031 XrdClientUrlInfo::XrdClientUrlInfo()
00032 {
00033
00034
00035 Clear();
00036 }
00037
00038
00039 XrdClientUrlInfo::XrdClientUrlInfo(const char *url)
00040 {
00041
00042
00043
00044
00045
00046 Clear();
00047 TakeUrl(XrdOucString(url));
00048 }
00049
00050
00051 XrdClientUrlInfo::XrdClientUrlInfo(const XrdOucString &url)
00052 {
00053
00054
00055
00056
00057
00058 Clear();
00059 TakeUrl(url);
00060 }
00061
00062
00063 XrdClientUrlInfo::XrdClientUrlInfo(const XrdClientUrlInfo &url)
00064 {
00065
00066
00067 Proto = url.Proto;
00068 User = url.User;
00069 Passwd = url.Passwd;
00070 Host = url.Host;
00071 HostWPort= url.HostWPort;
00072 HostAddr = url.HostAddr;
00073 Port = url.Port;
00074 File = url.File;
00075
00076 }
00077
00078
00079 void XrdClientUrlInfo::Clear() {
00080
00081
00082 Proto = "";
00083 User = "";
00084 Passwd = "";
00085 Host = "";
00086 HostWPort= "";
00087 HostAddr = "";
00088 Port = -1;
00089 File = "/";
00090
00091 }
00092
00093
00094 XrdClientUrlInfo& XrdClientUrlInfo::operator=(const XrdOucString &url)
00095 {
00096
00097
00098 this->TakeUrl(url);
00099
00100 return (*this);
00101 }
00102
00103
00104 XrdClientUrlInfo& XrdClientUrlInfo::operator=(const XrdClientUrlInfo &url)
00105 {
00106
00107
00108 Proto = url.Proto;
00109 User = url.User;
00110 Passwd = url.Passwd;
00111 Host = url.Host;
00112 HostWPort= url.HostWPort;
00113 HostAddr = url.HostAddr;
00114 Port = url.Port;
00115 File = url.File;
00116
00117 return (*this);
00118 }
00119
00120
00121 void XrdClientUrlInfo::TakeUrl(XrdOucString u)
00122 {
00123
00124
00125
00126
00127
00128 int p1 = 0, p2 = STR_NPOS, p3 = STR_NPOS, left = u.length();
00129
00130 Clear();
00131
00132 Info(XrdClientDebug::kHIDEBUG,"TakeUrl", "parsing url: " << u);
00133
00134 if (u.length() <= 0) return;
00135
00136
00137 if ((p2 = u.find("://")) != STR_NPOS) {
00138 Proto.assign(u, p1, p2-1);
00139 Info(XrdClientDebug::kHIDEBUG,"TakeUrl", " Proto: " << Proto);
00140
00141 p1 = p2 + 3;
00142 left = u.length() - p1;
00143 }
00144 if (left <= 0) {
00145 Clear();
00146 return;
00147 }
00148
00149
00150 if ((p2 = u.find('/', p1)) != STR_NPOS) {
00151 if (p2 > p1) {
00152 HostWPort.assign(u, p1, p2-1);
00153
00154 p1 = p2+1;
00155 left = u.length() - p1;
00156 }
00157 } else {
00158 HostWPort.assign(u, p1);
00159 left = 0;
00160 }
00161 Info(XrdClientDebug::kHIDEBUG,"TakeUrl", " HostWPort: " << HostWPort);
00162
00163
00164 if (left > 0)
00165 File.assign(u, p1);
00166 Info(XrdClientDebug::kHIDEBUG,"TakeUrl", " File: " << File);
00167
00168
00169
00170 p1 = 0;
00171 left = HostWPort.length();
00172
00173 if ((p2 = HostWPort.find('@', p1)) != STR_NPOS) {
00174 p3 = HostWPort.find(':', p1);
00175 if (p3 != STR_NPOS && p3 < p2) {
00176 User.assign(HostWPort, p1, p3-1);
00177 Passwd.assign(HostWPort, p3+1, p2-1);
00178 Info(XrdClientDebug::kHIDEBUG,"TakeUrl", " Passwd: " << Passwd);
00179 } else {
00180 User.assign(HostWPort, p1, p2-1);
00181 }
00182 Info(XrdClientDebug::kHIDEBUG,"TakeUrl", " User: " << User);
00183
00184 p1 = p2 + 1;
00185 left -= p1;
00186 }
00187
00188
00189 if ((p2 = HostWPort.find(':', p1)) != STR_NPOS ) {
00190 Host.assign(HostWPort, p1, p2-1);
00191 Port = strtol((HostWPort.c_str())+p2+1, (char **)0, 10);
00192 } else {
00193 Host.assign(HostWPort, p1);
00194 Port = 0;
00195 }
00196 Info(XrdClientDebug::kHIDEBUG,"TakeUrl", " Host: " << Host);
00197 Info(XrdClientDebug::kHIDEBUG,"TakeUrl", " Port: " << Port);
00198 }
00199
00200
00201 XrdOucString XrdClientUrlInfo::GetUrl()
00202 {
00203
00204
00205
00206
00207 XrdOucString s;
00208
00209 if (Proto != "") {
00210 s = Proto;
00211 s += "://";
00212 }
00213
00214 if (User != "") {
00215 s += User;
00216
00217 if (Passwd != "") {
00218 s += ":";
00219 s += Passwd;
00220 }
00221
00222 s += "@";
00223 }
00224
00225 s += Host;
00226
00227 if ( (Host != "") && (Port > 0) ) {
00228 char buf[256];
00229 sprintf(buf, "%d", Port);
00230 s += ":";
00231 s += buf;
00232 }
00233
00234 if (File != "") {
00235 s += "/";
00236 s += File;
00237 }
00238
00239 return s;
00240
00241 }
00242
00243
00244 void XrdClientUrlInfo::SetAddrFromHost()
00245 {
00246 struct sockaddr_in ip[2];
00247 char buf[255], **errmsg = 0;
00248
00249 int numaddr = XrdNetDNS::getHostAddr((char *)Host.c_str(),
00250 (struct sockaddr *)ip, 1, errmsg);
00251 if (numaddr > 0)
00252 HostAddr = inet_ntop(ip[0].sin_family, &ip[0].sin_addr, buf, sizeof(buf));
00253 }