From c1ad30c9e885b411b11a10e08ff2415260b1c02d Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 9 Apr 2013 00:24:31 +0200 Subject: [PATCH 1/1] Add URL parsing. --- input.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/input.cpp b/input.cpp index 401aa6a..8307819 100644 --- a/input.cpp +++ b/input.cpp @@ -26,15 +26,54 @@ 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) { -- 2.39.2