Add URL parsing.
[cubemap] / input.cpp
index 401aa6a..8307819 100644 (file)
--- a/input.cpp
+++ b/input.cpp
 using namespace std;
 
 extern ServerPool *servers;
+         
+// 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;
+}
 
 Input::Input(const string &stream_id, const string &url)
        : state(NOT_CONNECTED),
          stream_id(stream_id),
          url(url),
-         // TODO
-         host("gruessi.zrh.sesse.net"),
-         port("4013"),
-         path("/test.flv"),
          has_metacube_header(false),
          sock(-1)
 {
@@ -132,6 +171,11 @@ void Input::do_work()
                        request.clear();
                        request_bytes_sent = 0;
                        response.clear();
+       
+                       if (!parse_url(url, &host, &port, &path)) {
+                               fprintf(stderr, "Failed to parse URL '%s'\n", url.c_str());
+                               break;
+                       }
 
                        sock = lookup_and_connect(host, port);
                        if (sock != -1) {