X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=input.cpp;h=47c3c0193472d8cfa56747fd2461cdf5595632cd;hp=8307819437d687307d107b370f52e017d87be2fc;hb=9b565a9e6e66f076abb7266b2c2f015f585fa9cb;hpb=c1ad30c9e885b411b11a10e08ff2415260b1c02d diff --git a/input.cpp b/input.cpp index 8307819..47c3c01 100644 --- a/input.cpp +++ b/input.cpp @@ -22,6 +22,7 @@ #include "server.h" #include "serverpool.h" #include "parse.h" +#include "state.pb.h" using namespace std; @@ -79,6 +80,39 @@ Input::Input(const string &stream_id, const string &url) { } +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()), + http_header(serialized.http_header()), + 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_http_header(http_header); + 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; @@ -144,6 +178,84 @@ int Input::lookup_and_connect(const string &host, const string &port) freeaddrinfo(ai); return -1; } + +bool Input::parse_response(const std::string &request) +{ + vector lines = split_lines(response); + if (lines.empty()) { + fprintf(stderr, "WARNING: Empty HTTP response from input.\n"); + return false; + } + + vector first_line_tokens = split_tokens(lines[0]); + if (first_line_tokens.size() < 2) { + fprintf(stderr, "WARNING: Malformed response line '%s' from input.\n", + lines[0].c_str()); + return false; + } + + int response = atoi(first_line_tokens[1].c_str()); + if (response != 200) { + fprintf(stderr, "WARNING: Non-200 response '%s' from input.\n", + lines[0].c_str()); + return false; + } + + multimap parameters; + for (size_t i = 1; i < lines.size(); ++i) { + size_t split = lines[i].find(":"); + if (split == string::npos) { + fprintf(stderr, "WARNING: Ignoring malformed HTTP response line '%s'\n", + lines[i].c_str()); + continue; + } + + string key(lines[i].begin(), lines[i].begin() + split); + + // Skip any spaces after the colon. + do { + ++split; + } while (split < lines[i].size() && lines[i][split] == ' '); + + string value(lines[i].begin() + split, lines[i].end()); + + // Remove “Content-encoding: metacube”. + // TODO: Make case-insensitive. + if (key == "Content-encoding" && value == "metacube") { + continue; + } + + parameters.insert(make_pair(key, value)); + } + + // Change “Server: foo” to “Server: metacube/0.1 (reflecting: foo)” + // TODO: Make case-insensitive. + // XXX: Use a Via: instead? + if (parameters.count("Server") == 0) { + parameters.insert(make_pair("Server", "metacube/0.1")); + } else { + for (multimap::iterator it = parameters.begin(); + it != parameters.end(); + ++it) { + if (it->first != "Server") { + continue; + } + it->second = "metacube/0.1 (reflecting: " + it->second + ")"; + } + } + + // Construct the new HTTP header. + http_header = "HTTP/1.0 200 OK\r\n"; + for (multimap::iterator it = parameters.begin(); + it != parameters.end(); + ++it) { + http_header.append(it->first + ": " + it->second + "\r\n"); + } + http_header.append("\r\n"); + servers->set_header(stream_id, http_header); + + return true; +} void Input::do_work() { @@ -255,7 +367,10 @@ void Input::do_work() response.resize(ptr - response.data()); } - // TODO: Check that the response is 200, save the headers, etc. + if (!parse_response(response)) { + state = CLOSING_SOCKET; + continue; + } if (!extra_data.empty()) { process_data(&extra_data[0], extra_data.size()); @@ -365,7 +480,7 @@ void Input::process_data(char *ptr, size_t bytes) char *inner_data = pending_data.data() + sizeof(metacube_block_header); if (flags & METACUBE_FLAGS_HEADER) { string header(inner_data, inner_data + size); - servers->set_header(stream_id, header); + servers->set_header(stream_id, http_header + header); } else { servers->add_data(stream_id, inner_data, size); }