Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/corelibs/vos/url.cc

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001-2003 Peter Amstutz
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020 
00021     Peter Amstutz <tetron@interreality.org>
00022 */
00023 
00024 #include <vos/corelibs/vos/url.hh>
00025 
00026 using namespace VOS;
00027 
00028 #include <sys/types.h>
00029 
00030 /*
00031 #ifdef HAVE_BOOST_REGEX_H
00032 # include <boost/regex.h>
00033 #else
00034 extern "C" {
00035 # include <regex.h>
00036 }
00037 #endif
00038 */
00039 
00040 /** @file
00041     Implements URL.
00042 */
00043 
00044 URL URL::defaults("vop", "", "4231", "");
00045 
00046 void URL::setDefaults(const URL& u)
00047 {
00048     defaults=u;
00049 }
00050 
00051 URL::URL() : protocol(defaults.protocol),
00052              host(defaults.host),
00053              port(defaults.port),
00054              path(defaults.path)
00055 {
00056     makeURLstr();
00057 }
00058 
00059 URL::URL(const string& pr,
00060          const string& ho,
00061          const string& po,
00062          const string& pa)
00063     : protocol(pr), host(ho),
00064       port(po), path(pa)
00065 {
00066     makeURLstr();
00067 }
00068 
00069 URL::URL(const string& p) throw (BadURLError)
00070 {
00071     /*static regex_t url_regexp;
00072     static regmatch_t pmatch[7];
00073     bool did_regcomp = false;
00074     int e;
00075 
00076     if(! did_regcomp) {
00077         regcomp(&url_regexp, "^(.+)://(.+@)?([^:/]+)(:[[:digit:]]+)?(/.*)?", REG_EXTENDED);
00078         did_regcomp = true;
00079     }
00080     e=regexec(&url_regexp, p.c_str(), 7, pmatch, 0);
00081     if(e == 0) {
00082         LOG("url", 4, p);
00083         if(pmatch[1].rm_so > -1)
00084             protocol=p.substr(pmatch[1].rm_so, pmatch[1].rm_eo - pmatch[1].rm_so);
00085         else setProtocol(defaults.getProtocol());
00086         if(pmatch[2].rm_so > -1)
00087             object=p.substr(pmatch[2].rm_so, pmatch[2].rm_eo - pmatch[2].rm_so - 1);
00088         else setObject(defaults.getObject());
00089         if(pmatch[3].rm_so > -1)
00090             host=p.substr(pmatch[3].rm_so, pmatch[3].rm_eo - pmatch[3].rm_so);
00091         else setHost(defaults.getHost());
00092         if(pmatch[4].rm_so > -1)
00093             port=p.substr(pmatch[4].rm_so + 1, pmatch[4].rm_eo - (pmatch[4].rm_so + 1));
00094         else setPort(defaults.getPort());
00095         if(pmatch[5].rm_so > -1)
00096             path=p.substr(pmatch[5].rm_so, pmatch[5].rm_eo - pmatch[5].rm_so);
00097         else setPath(defaults.getPath());
00098     } else {
00099         throw BadURLError("Maelformed URL");
00100         }*/
00101 
00102     // as elegant (relatively) as using a regular expression
00103     // above is, there is a huge performance
00104     // improvement using the following code (!!!)
00105 
00106     port = defaults.getPort();
00107     path = defaults.getPath();
00108     host = defaults.getHost();
00109     protocol = defaults.getProtocol();
00110 
00111     unsigned int i;
00112     for(i = 0; i < p.size() && p[i] != ':'; i++);
00113     protocol = p.substr(0,i);
00114 
00115     unsigned int c = i+3;
00116     if(p.size() > i+3 && p[i+1] == '/' && p[i+2] == '/') {
00117         for(i+=3; i < p.size() && p[i] != ':' && p[i] != '/'; i++);
00118         host = p.substr(c, i - c);
00119         if(i < p.size()) {
00120             switch(p[i]) {
00121             case ':':
00122                 c = ++i;
00123                 for(; i < p.size() && p[i] != '/'; i++);
00124                 port = p.substr(c, i - c);
00125             /* fall through */
00126             case '/':
00127                 path = p.substr(i);
00128             }
00129         }
00130     } else throw BadURLError("Malformed URL");
00131 
00132     makeURLstr();
00133 }
00134 
00135 void URL::makeURLstr()
00136 {
00137     urlstr.erase(urlstr.begin(), urlstr.end());
00138     urlstr += protocol + "://";
00139     urlstr += host;
00140     if(port != "") urlstr += ":" + port;
00141     urlstr += path;
00142     if(port != "") hostandport = host + ":" + port;
00143     else hostandport = host;
00144 }
00145 
00146 const string& URL::getProtocol() const { return protocol; }
00147 const string& URL::getHost() const { return host; }
00148 const string& URL::getPort() const { return port; }
00149 const string& URL::getPath() const { return path; }
00150 const string& URL::getHostAndPort() const { return hostandport; }
00151 const string& URL::getString() const { return urlstr; }
00152 
00153 void URL::setProtocol(const string& p) { protocol=p; makeURLstr(); }
00154 void URL::setHost(const string& h) { host=h; makeURLstr(); }
00155 void URL::setPort(const string& p) { port=p; makeURLstr(); }
00156 void URL::setPath(const string& p) { path=p; makeURLstr(); }
00157 
00158 void URL::setHostAndPort(const string& hp)
00159 {
00160     unsigned int c = (unsigned int)hp.find(":");
00161     if(c != string::npos) {
00162         host=hp.substr(0, c);
00163         port=hp.substr(c+1);
00164     } else {
00165         host=hp;
00166         port=defaults.port;
00167     }
00168     makeURLstr();
00169 }
00170 

Generated on Tue Aug 12 03:55:42 2003 for Interreality Project - VOS by doxygen 1.3.2