-#include <string.h>
+#include <stddef.h>
#include <string>
+#include "httpinput.h"
#include "input.h"
+#include "state.pb.h"
+#include "udpinput.h"
using namespace std;
// Extremely rudimentary URL parsing.
bool parse_url(const string &url, string *protocol, 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";
+ *port = *protocol;
*path = "/";
return true;
}
}
// http://foo/bar
- *port = "http";
+ *port = *protocol;
*path = rest;
return true;
}
+Input *create_input(const std::string &url)
+{
+ string protocol, host, port, path;
+ if (!parse_url(url, &protocol, &host, &port, &path)) {
+ return NULL;
+ }
+ if (protocol == "http") {
+ 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)) {
+ return NULL;
+ }
+ if (protocol == "http") {
+ return new HTTPInput(serialized);
+ }
+ if (protocol == "udp") {
+ return new UDPInput(serialized);
+ }
+ return NULL;
+}
+
Input::~Input() {}