X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=input.cpp;h=25012a6bbad930f63a53914ed53fd0e9d8cc4fcf;hp=d37b7d997e16f41f5afbe6d4421e5d669ad4d516;hb=6544fa0ec3f3a501bcb89ea977756911bd7f3ebd;hpb=f8e6be4ca4d53ca6dbb9a56063c7dfca9b6760c9 diff --git a/input.cpp b/input.cpp index d37b7d9..25012a6 100644 --- a/input.cpp +++ b/input.cpp @@ -8,8 +8,27 @@ using namespace std; +namespace { + +// Does not support passwords, only user:host, since this is really only used +// to parse VLC's udp://source@multicastgroup:1234/ syntax (we do not support +// even basic auth). +void split_user_host(const string &user_host, string *user, string *host) +{ + size_t split = user_host.find("@"); + if (split == string::npos) { + user->clear(); + *host = user_host; + } else { + *user = string(user_host.begin(), user_host.begin() + split); + *host = string(user_host.begin() + split + 1, user_host.end()); + } +} + +} // namespace + // Extremely rudimentary URL parsing. -bool parse_url(const string &url, string *protocol, string *host, string *port, string *path) +bool parse_url(const string &url, string *protocol, string *user, string *host, string *port, string *path) { size_t split = url.find("://"); if (split == string::npos) { @@ -18,16 +37,38 @@ bool parse_url(const string &url, string *protocol, string *host, string *port, *protocol = string(url.begin(), url.begin() + split); string rest = string(url.begin() + split + 3, url.end()); - split = rest.find_first_of(":/"); - if (split == string::npos) { + + // Split at the first slash, or the first colon that's not within []. + bool within_brackets = false; + for (split = 0; split < rest.size(); ++split) { + if (rest[split] == '[') { + if (within_brackets) { + // Can't nest brackets. + return false; + } + within_brackets = true; + } else if (rest[split] == ']') { + if (!within_brackets) { + // ] without matching [. + return false; + } + within_brackets = false; + } else if (rest[split] == '/') { + break; + } else if (rest[split] == ':' && !within_brackets) { + break; + } + } + + if (split == rest.size()) { // http://foo - *host = rest; + split_user_host(rest, user, host); *port = *protocol; *path = "/"; return true; } - *host = string(rest.begin(), rest.begin() + split); + split_user_host(string(rest.begin(), rest.begin() + split), user, host); char ch = rest[split]; // Colon or slash. rest = string(rest.begin() + split + 1, rest.end()); @@ -53,16 +94,19 @@ bool parse_url(const string &url, string *protocol, string *host, string *port, return true; } -Input *create_input(const std::string &url) +Input *create_input(const string &url, Input::Encoding encoding) { - string protocol, host, port, path; - if (!parse_url(url, &protocol, &host, &port, &path)) { + string protocol, user, host, port, path; + if (!parse_url(url, &protocol, &user, &host, &port, &path)) { return NULL; } if (protocol == "http") { - return new HTTPInput(url); + return new HTTPInput(url, encoding); } if (protocol == "udp") { + if (encoding == Input::INPUT_ENCODING_METACUBE) { + return NULL; + } return new UDPInput(url); } return NULL; @@ -70,8 +114,8 @@ Input *create_input(const std::string &url) Input *create_input(const InputProto &serialized) { - string protocol, host, port, path; - if (!parse_url(serialized.url(), &protocol, &host, &port, &path)) { + string protocol, user, host, port, path; + if (!parse_url(serialized.url(), &protocol, &user, &host, &port, &path)) { return NULL; } if (protocol == "http") {