XrdClientUrlInfo.cc

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////
00002 //                                                                      //
00003 // XrdClientUrlInfo                                                     // 
00004 //                                                                      //
00005 // Author: Fabrizio Furano (INFN Padova, 2004)                          //
00006 // Adapted from TXNetFile (root.cern.ch) originally done by             //
00007 // Alvise Dorigo, Fabrizio Furano, INFN Padova, 2003                    //
00008 // Revised by G. Ganis, CERN, June 2005                                 //
00009 //                                                                      //
00010 // Class handling information about an url                              //
00011 //                                                                      //
00012 //////////////////////////////////////////////////////////////////////////
00013 
00014 //       $Id: XrdClientUrlInfo.cc 30949 2009-11-02 16:37:58Z ganis $
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    // Default constructor
00034 
00035    Clear();
00036 }
00037 
00038 //_____________________________________________________________________________
00039 XrdClientUrlInfo::XrdClientUrlInfo(const char *url)
00040 {
00041    // Constructor from a string specifying an url or multiple urls in the
00042    // form:
00043    //         [proto://][user1@]host1:port1[,[user2@]host2:port2, ... ,
00044    //                   [userN@]hostN:portN]]/pathfile
00045 
00046    Clear();
00047    TakeUrl(XrdOucString(url));
00048 }
00049 
00050 //_____________________________________________________________________________
00051 XrdClientUrlInfo::XrdClientUrlInfo(const XrdOucString &url)
00052 {
00053    // Constructor from a string specifying an url or multiple urls in the
00054    // form:
00055    //         [proto://][user1@]host1:port1[,[user2@]host2:port2, ... ,
00056    //                   [userN@]hostN:portN]]/pathfile
00057 
00058    Clear();
00059    TakeUrl(url);
00060 }
00061 
00062 //______________________________________________________________________________
00063 XrdClientUrlInfo::XrdClientUrlInfo(const XrdClientUrlInfo &url)
00064 {
00065    // Copy constructor
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    // Set defaults
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    // Assign url to local url.
00097 
00098    this->TakeUrl(url);
00099 
00100    return (*this);
00101 }
00102 
00103 //______________________________________________________________________________
00104 XrdClientUrlInfo& XrdClientUrlInfo::operator=(const XrdClientUrlInfo &url)
00105 {
00106    // Assign url to local url.
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    // Parse url character string and split in its different subcomponents.
00124    // Use IsValid() to check if URL is legal.
00125    // Url format:
00126    //             [proto://][user[:passwd]@]host:port/pathfile
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    // Get protocol
00137    if ((p2 = u.find("://")) != STR_NPOS) {
00138       Proto.assign(u, p1, p2-1);
00139       Info(XrdClientDebug::kHIDEBUG,"TakeUrl", "   Proto:   " << Proto);
00140       // Update start of search range and remaining length
00141       p1 = p2 + 3;
00142       left = u.length() - p1;
00143    }
00144    if (left <= 0) {
00145       Clear();
00146       return;
00147    }
00148 
00149    // Store the whole "[user[:passwd]@]host:port" thing in HostWPort
00150    if ((p2 = u.find('/', p1)) != STR_NPOS) {
00151       if (p2 > p1) {
00152          HostWPort.assign(u, p1, p2-1);
00153          // Update start of search range and remaining length
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    // Get pathfile
00164    if (left > 0)
00165      File.assign(u, p1);
00166    Info(XrdClientDebug::kHIDEBUG,"TakeUrl", "   File:   " << File);
00167 
00168    //
00169    // Resolve username, passwd, host and port.
00170    p1 = 0;
00171    left = HostWPort.length();
00172    // Get user/pwd
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       // Update start of search range and remaining length
00184       p1 = p2 + 1;
00185       left -= p1;
00186    }
00187 
00188    // Get host:port
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    // Get full url
00204    // The fields might have been modified, so the full url
00205    // must be reconstructed
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 }

Generated on Tue Jul 5 14:46:20 2011 for ROOT_528-00b_version by  doxygen 1.5.1