RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/types/url.cc
00001 
00002 #include "url.h"
00003 
00004 #include "googleurl/src/gurl.h"
00005 #include "googleurl/src/url_util.h"
00006 #include "jml/arch/exception.h"
00007 #include "jml/db/persistent.h"
00008 
00009 #include <iostream>
00010 
00011 using namespace std;
00012 
00013 namespace Datacratic {
00014 
00015 
00016 namespace {
00017 
00018 struct Init {
00019     Init()
00020     {
00021         url_util::Initialize();
00022     }
00023 } init;
00024 
00025 }
00026 
00027 Url::
00028 Url()
00029     : url(new GURL())
00030 {
00031 }
00032 
00033 Url::
00034 Url(const std::string & s_)
00035     : original(s_)
00036 {
00037     std::string s = s_;
00038     if (s == "") {
00039         url.reset(new GURL(s));
00040         return;
00041     }
00042 
00043     if (s.find("://") == string::npos) {
00044         s = "http://" + s;
00045     }
00046     url.reset(new GURL(s));
00047 
00048     if (url->possibly_invalid_spec().empty()) {
00049         //cerr << "bad parse 1" << endl;
00050         url.reset(new GURL("http://" + s));
00051         if (url->possibly_invalid_spec().empty()) {
00052             //cerr << "bad parse 2" << endl;
00053             url.reset(new GURL("http://" + s + "/"));
00054         }
00055     }
00056     
00057     //cerr << "parsing " << s << " returned " << toString()
00058     //     << " host " << host()
00059     //     << " valid " << valid()
00060     //     << endl;
00061     //cerr << "url has scheme " + gurl.scheme() << endl;
00062     //cerr << "url has spec " + gurl.spec() << endl;
00063 }
00064 
00065 Url::
00066 Url(const Utf8String & s_)
00067     : original(s_.rawString())
00068 {
00069     std::string s = original;
00070     if (s == "") {
00071         url.reset(new GURL(s));
00072         return;
00073     }
00074 
00075     if (s.find("://") == string::npos) {
00076         s = "http://" + s;
00077     }
00078     url.reset(new GURL(s));
00079 
00080     if (url->possibly_invalid_spec().empty()) {
00081         //cerr << "bad parse 1" << endl;
00082         url.reset(new GURL("http://" + s));
00083         if (url->possibly_invalid_spec().empty()) {
00084             //cerr << "bad parse 2" << endl;
00085             url.reset(new GURL("http://" + s + "/"));
00086         }
00087     }
00088 }
00089 
00090 Url::
00091 ~Url()
00092 {
00093 }
00094 
00095 std::string
00096 Url::
00097 toString() const
00098 {
00099     if (valid())
00100         return canonical();
00101     return original;
00102 }
00103 
00104 Utf8String
00105 Url::
00106 toUtf8String() const
00107 {
00108     if (valid())
00109         return Utf8String(canonical());
00110     return Utf8String(original);
00111 }
00112 
00113 const char *
00114 Url::
00115 c_str() const
00116 {
00117     if (valid())
00118         return url->spec().c_str();
00119     return original.c_str();
00120 }
00121 
00122 bool
00123 Url::
00124 valid() const
00125 {
00126     return url->is_valid();
00127 }
00128 
00129 bool
00130 Url::
00131 empty() const
00132 {
00133     return url->is_empty();
00134 }
00135 
00136 std::string
00137 Url::
00138 canonical() const
00139 {
00140     if (!valid()) return "";
00141     return url->spec();
00142 }
00143 
00144 std::string
00145 Url::
00146 scheme() const
00147 {
00148     return url->scheme();
00149 }
00150 
00151 std::string
00152 Url::
00153 host() const
00154 {
00155     return url->host();
00156 }
00157 
00158 bool
00159 Url::
00160 hostIsIpAddress() const
00161 {
00162     return url->HostIsIPAddress();
00163 }
00164 
00165 bool
00166 Url::
00167 domainMatches(const std::string & str) const
00168 {
00169     return url->DomainIs(str.c_str(), str.length());
00170 }
00171 
00172 int
00173 Url::
00174 port() const
00175 {
00176     return url->IntPort();
00177 }
00178 
00179 std::string
00180 Url::
00181 path() const
00182 {
00183     return url->path();
00184 }
00185 
00186 std::string
00187 Url::
00188 query() const
00189 {
00190     return url->query();
00191 }
00192 
00193 uint64_t
00194 Url::
00195 urlHash()
00196 {
00197     throw ML::Exception("urlHash");
00198 }
00199 
00200 uint64_t
00201 Url::
00202 hostHash()
00203 {
00204     throw ML::Exception("hostHash");
00205 }
00206 
00207 void
00208 Url::
00209 serialize(ML::DB::Store_Writer & store) const
00210 {
00211     unsigned char version = 0;
00212     store << version << original;
00213 }
00214 
00215 void
00216 Url::
00217 reconstitute(ML::DB::Store_Reader & store)
00218 {
00219     unsigned char version;
00220     store >> version;
00221     if (version != 0)
00222         store >> original;
00223     *this = Url(original);
00224 }
00225 
00226 } // namespace Datacratic
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator