13 // Does not support passwords, only user:host, since this is really only used
14 // to parse VLC's udp://source@multicastgroup:1234/ syntax (we do not support
16 void split_user_host(const string &user_host, string *user, string *host)
18 size_t split = user_host.find("@");
19 if (split == string::npos) {
23 *user = string(user_host.begin(), user_host.begin() + split);
24 *host = string(user_host.begin() + split + 1, user_host.end());
30 // Extremely rudimentary URL parsing.
31 bool parse_url(const string &url, string *protocol, string *user, string *host, string *port, string *path)
33 // pipe:foo (or pipe:"foo").
34 if (url.find("pipe:") == 0) {
36 *path = string(url.begin() + 5, url.end());
40 size_t split = url.find("://");
41 if (split == string::npos) {
44 *protocol = string(url.begin(), url.begin() + split);
46 string rest = string(url.begin() + split + 3, url.end());
48 // Split at the first slash, or the first colon that's not within [].
49 bool within_brackets = false;
50 for (split = 0; split < rest.size(); ++split) {
51 if (rest[split] == '[') {
52 if (within_brackets) {
53 // Can't nest brackets.
56 within_brackets = true;
57 } else if (rest[split] == ']') {
58 if (!within_brackets) {
59 // ] without matching [.
62 within_brackets = false;
63 } else if (rest[split] == '/') {
65 } else if (rest[split] == ':' && !within_brackets) {
70 if (split == rest.size()) {
72 split_user_host(rest, user, host);
78 split_user_host(string(rest.begin(), rest.begin() + split), user, host);
79 char ch = rest[split]; // Colon or slash.
80 rest = string(rest.begin() + split + 1, rest.end());
84 split = rest.find_first_of('/');
85 if (split == string::npos) {
91 // http://foo:1234/bar
92 *port = string(rest.begin(), rest.begin() + split);
93 *path = string(rest.begin() + split, rest.end());
104 Input *create_input(const string &url, Input::Encoding encoding)
106 string protocol, user, host, port, path;
107 if (!parse_url(url, &protocol, &user, &host, &port, &path)) {
110 if (protocol == "http" || protocol == "pipe") {
111 return new HTTPInput(url, encoding);
113 if (protocol == "udp") {
114 assert(encoding == Input::INPUT_ENCODING_RAW);
115 return new UDPInput(url);
120 Input *create_input(const InputProto &serialized)
122 string protocol, user, host, port, path;
123 if (!parse_url(serialized.url(), &protocol, &user, &host, &port, &path)) {
126 if (protocol == "http" || protocol == "pipe") {
127 return new HTTPInput(serialized);
129 if (protocol == "udp") {
130 return new UDPInput(serialized);