#include "server.h"
#include "serverpool.h"
#include "parse.h"
+#include "state.pb.h"
using namespace std;
extern ServerPool *servers;
+
+// Extremely rudimentary URL parsing.
+bool parse_url(const string &url, string *host, string *port, string *path)
+{
+ if (url.find("http://") != 0) {
+ return false;
+ }
+
+ string rest = url.substr(strlen("http://"));
+ size_t split = rest.find_first_of(":/");
+ if (split == string::npos) {
+ // http://foo
+ *host = rest;
+ *port = "http";
+ *path = "/";
+ return true;
+ }
+
+ *host = string(rest.begin(), rest.begin() + split);
+ char ch = rest[split]; // Colon or slash.
+ rest = string(rest.begin() + split + 1, rest.end());
+
+ if (ch == ':') {
+ // Parse the port.
+ split = rest.find_first_of('/');
+ if (split == string::npos) {
+ // http://foo:1234
+ *port = rest;
+ *path = "/";
+ return true;
+ } else {
+ // http://foo:1234/bar
+ *port = string(rest.begin(), rest.begin() + split);
+ *path = string(rest.begin() + split, rest.end());
+ return true;
+ }
+ }
+
+ // http://foo/bar
+ *port = "http";
+ *path = rest;
+ return true;
+}
Input::Input(const string &stream_id, const string &url)
: state(NOT_CONNECTED),
stream_id(stream_id),
url(url),
- // TODO
- host("gruessi.zrh.sesse.net"),
- port("4013"),
- path("/test.flv"),
has_metacube_header(false),
sock(-1)
{
}
+Input::Input(const InputProto &serialized)
+ : state(State(serialized.state())),
+ stream_id(serialized.stream_id()),
+ url(serialized.url()),
+ request(serialized.request()),
+ request_bytes_sent(serialized.request_bytes_sent()),
+ response(serialized.response()),
+ has_metacube_header(serialized.has_metacube_header()),
+ sock(serialized.sock())
+{
+ pending_data.resize(serialized.pending_data().size());
+ memcpy(&pending_data[0], serialized.pending_data().data(), serialized.pending_data().size());
+
+ parse_url(url, &host, &port, &path); // Don't care if it fails.
+}
+
+InputProto Input::serialize() const
+{
+ InputProto serialized;
+ serialized.set_state(state);
+ serialized.set_stream_id(stream_id);
+ serialized.set_url(url);
+ serialized.set_request(request);
+ serialized.set_request_bytes_sent(request_bytes_sent);
+ serialized.set_response(response);
+ serialized.set_pending_data(string(pending_data.begin(), pending_data.end()));
+ serialized.set_has_metacube_header(has_metacube_header);
+ serialized.set_sock(sock);
+ return serialized;
+}
+
void Input::run()
{
should_stop = false;
request.clear();
request_bytes_sent = 0;
response.clear();
+
+ if (!parse_url(url, &host, &port, &path)) {
+ fprintf(stderr, "Failed to parse URL '%s'\n", url.c_str());
+ break;
+ }
sock = lookup_and_connect(host, port);
if (sock != -1) {