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 size_t split = url.find("://");
34 if (split == string::npos) {
37 *protocol = string(url.begin(), url.begin() + split);
39 string rest = string(url.begin() + split + 3, url.end());
41 // Split at the first slash, or the first colon that's not within [].
42 bool within_brackets = false;
43 for (split = 0; split < rest.size(); ++split) {
44 if (rest[split] == '[') {
45 if (within_brackets) {
46 // Can't nest brackets.
49 within_brackets = true;
50 } else if (rest[split] == ']') {
51 if (!within_brackets) {
52 // ] without matching [.
55 within_brackets = false;
56 } else if (rest[split] == '/') {
58 } else if (rest[split] == ':' && !within_brackets) {
63 if (split == rest.size()) {
65 split_user_host(rest, user, host);
71 split_user_host(string(rest.begin(), rest.begin() + split), user, host);
72 char ch = rest[split]; // Colon or slash.
73 rest = string(rest.begin() + split + 1, rest.end());
77 split = rest.find_first_of('/');
78 if (split == string::npos) {
84 // http://foo:1234/bar
85 *port = string(rest.begin(), rest.begin() + split);
86 *path = string(rest.begin() + split, rest.end());
97 Input *create_input(const string &url, Input::Encoding encoding)
99 string protocol, user, host, port, path;
100 if (!parse_url(url, &protocol, &user, &host, &port, &path)) {
103 if (protocol == "http") {
104 return new HTTPInput(url, encoding);
106 if (protocol == "udp") {
107 assert(encoding == Input::INPUT_ENCODING_RAW);
108 return new UDPInput(url);
113 Input *create_input(const InputProto &serialized)
115 string protocol, user, host, port, path;
116 if (!parse_url(serialized.url(), &protocol, &user, &host, &port, &path)) {
119 if (protocol == "http") {
120 return new HTTPInput(serialized);
122 if (protocol == "udp") {
123 return new UDPInput(serialized);