X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=input.cpp;h=0ee7d1ce918af18a9999a414cc9014a97ae07f7d;hp=00b654734f2aac406dd0bc34826b13fc76d4d61a;hb=70c47a998c5aa2eb536c3c8f71f3178cd217a14d;hpb=7c4707af4ea7af05e34bc0414d18d9fd54e4f23e diff --git a/input.cpp b/input.cpp index 00b6547..0ee7d1c 100644 --- a/input.cpp +++ b/input.cpp @@ -1,32 +1,52 @@ -#include +#include #include #include "httpinput.h" #include "input.h" #include "state.pb.h" +#include "udpinput.h" 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) { - if (url.find("http://") != 0) { + size_t split = url.find("://"); + if (split == string::npos) { return false; } + *protocol = string(url.begin(), url.begin() + split); - *protocol = "http"; - - string rest = url.substr(strlen("http://")); - size_t split = rest.find_first_of(":/"); + string rest = string(url.begin() + split + 3, url.end()); + split = rest.find_first_of(":/"); if (split == string::npos) { // http://foo - *host = rest; - *port = "http"; + 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()); @@ -47,32 +67,38 @@ bool parse_url(const string &url, string *protocol, string *host, string *port, } // http://foo/bar - *port = "http"; - *path = rest; + *port = *protocol; + *path = "/" + rest; return true; } -Input *create_input(const std::string &stream_id, const std::string &url) +Input *create_input(const std::string &url) { - 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(stream_id, url); + return new HTTPInput(url); + } + if (protocol == "udp") { + return new UDPInput(url); } return NULL; } 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") { return new HTTPInput(serialized); } + if (protocol == "udp") { + return new UDPInput(serialized); + } return NULL; }