]> git.sesse.net Git - cubemap/commitdiff
Add URL parsing.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 8 Apr 2013 22:24:31 +0000 (00:24 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 8 Apr 2013 22:24:31 +0000 (00:24 +0200)
input.cpp

index 401aa6a366f8e555a8adeb3bf2e7761551e50762..8307819437d687307d107b370f52e017d87be2fc 100644 (file)
--- a/input.cpp
+++ b/input.cpp
 using namespace std;
 
 extern ServerPool *servers;
 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),
 
 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)
 {
          has_metacube_header(false),
          sock(-1)
 {
@@ -132,6 +171,11 @@ void Input::do_work()
                        request.clear();
                        request_bytes_sent = 0;
                        response.clear();
                        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) {
 
                        sock = lookup_and_connect(host, port);
                        if (sock != -1) {