+
+// Extremely rudimentary URL parsing.
+bool parse_url(const string &url, string *host, string *port, string *path)
+{
+ if (url.find("http://") != 0) {
+ return false;
+ }
+
+ string rest = url.substr(strlen("http://"));
+ size_t split = rest.find_first_of(":/");
+ if (split == string::npos) {
+ // http://foo
+ *host = rest;
+ *port = "http";
+ *path = "/";
+ return true;
+ }
+
+ *host = string(rest.begin(), rest.begin() + split);
+ char ch = rest[split]; // Colon or slash.
+ rest = string(rest.begin() + split + 1, rest.end());
+
+ if (ch == ':') {
+ // Parse the port.
+ split = rest.find_first_of('/');
+ if (split == string::npos) {
+ // http://foo:1234
+ *port = rest;
+ *path = "/";
+ return true;
+ } else {
+ // http://foo:1234/bar
+ *port = string(rest.begin(), rest.begin() + split);
+ *path = string(rest.begin() + split, rest.end());
+ return true;
+ }
+ }
+
+ // http://foo/bar
+ *port = "http";
+ *path = rest;
+ return true;
+}