X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=httpinput.cpp;h=9dcaeaa0191dd1951fc8fdb15f34148468c563c0;hp=069572753eaef9b04617eb2f1329f8e8e26615f1;hb=bff5371d96506c8571fdeeafc5404c362022685b;hpb=8077fd7663c0e79e4d42c9db55be1de02027a17d diff --git a/httpinput.cpp b/httpinput.cpp index 0695727..9dcaeaa 100644 --- a/httpinput.cpp +++ b/httpinput.cpp @@ -24,34 +24,31 @@ #include "serverpool.h" #include "state.pb.h" #include "stream.h" +#include "timespec.h" #include "util.h" #include "version.h" using namespace std; -extern ServerPool *servers; - namespace { -// Compute b-a. -timespec clock_diff(const timespec &a, const timespec &b) +string host_header(const string &host, const string &port) { - timespec ret; - ret.tv_sec = b.tv_sec - a.tv_sec; - ret.tv_nsec = b.tv_nsec - a.tv_nsec; - if (ret.tv_nsec < 0) { - ret.tv_sec--; - ret.tv_nsec += 1000000000; + if (port == "http" || atoi(port.c_str()) == 80) { + return host; + } else { + return host + ":" + port; } - assert(ret.tv_nsec >= 0); - return ret; } } // namespace -HTTPInput::HTTPInput(const string &url) +extern ServerPool *servers; + +HTTPInput::HTTPInput(const string &url, Input::Encoding encoding) : state(NOT_CONNECTED), url(url), + encoding(encoding), has_metacube_header(false), sock(-1) { @@ -65,6 +62,9 @@ HTTPInput::HTTPInput(const string &url) HTTPInput::HTTPInput(const InputProto &serialized) : state(State(serialized.state())), url(serialized.url()), + encoding(serialized.is_metacube_encoded() ? + Input::INPUT_ENCODING_METACUBE : + Input::INPUT_ENCODING_RAW), request(serialized.request()), request_bytes_sent(serialized.request_bytes_sent()), response(serialized.response()), @@ -117,6 +117,12 @@ InputProto HTTPInput::serialize() const serialized.set_bytes_received(stats.bytes_received); serialized.set_data_bytes_received(stats.data_bytes_received); serialized.set_connect_time(stats.connect_time); + if (encoding == Input::INPUT_ENCODING_METACUBE) { + serialized.set_is_metacube_encoded(true); + } else { + assert(encoding == Input::INPUT_ENCODING_RAW); + serialized.set_is_metacube_encoded(false); + } return serialized; } @@ -346,7 +352,7 @@ void HTTPInput::do_work() state = CLOSING_SOCKET; } else { state = SENDING_REQUEST; - request = "GET " + path + " HTTP/1.0\r\nUser-Agent: cubemap\r\n\r\n"; + request = "GET " + path + " HTTP/1.0\r\nHost: " + host_header(host, port) + "\r\nUser-Agent: cubemap\r\n\r\n"; request_bytes_sent = 0; } @@ -429,8 +435,14 @@ void HTTPInput::do_work() process_data(&extra_data[0], extra_data.size()); } - log(INFO, "[%s] Connected to '%s', receiving data.", - url.c_str(), url.c_str()); + if (encoding == Input::INPUT_ENCODING_RAW) { + log(INFO, "[%s] Connected to '%s', receiving raw data.", + url.c_str(), url.c_str()); + } else { + assert(encoding == Input::INPUT_ENCODING_METACUBE); + log(INFO, "[%s] Connected to '%s', receiving data.", + url.c_str(), url.c_str()); + } state = RECEIVING_DATA; break; } @@ -489,6 +501,15 @@ void HTTPInput::process_data(char *ptr, size_t bytes) stats.bytes_received += bytes; } + if (encoding == Input::INPUT_ENCODING_RAW) { + for (size_t i = 0; i < stream_indices.size(); ++i) { + servers->add_data(stream_indices[i], ptr, bytes, /*metacube_flags=*/0); + } + return; + } + + assert(encoding == Input::INPUT_ENCODING_METACUBE); + for ( ;; ) { // If we don't have enough data (yet) for even the Metacube header, just return. if (pending_data.size() < sizeof(metacube2_block_header)) { @@ -560,16 +581,9 @@ void HTTPInput::process_data(char *ptr, size_t bytes) for (size_t i = 0; i < stream_indices.size(); ++i) { servers->set_header(stream_indices[i], http_header, stream_header); } - } else { - StreamStartSuitability suitable_for_stream_start; - if (flags & METACUBE_FLAGS_NOT_SUITABLE_FOR_STREAM_START) { - suitable_for_stream_start = NOT_SUITABLE_FOR_STREAM_START; - } else { - suitable_for_stream_start = SUITABLE_FOR_STREAM_START; - } - for (size_t i = 0; i < stream_indices.size(); ++i) { - servers->add_data(stream_indices[i], inner_data, size, suitable_for_stream_start); - } + } + for (size_t i = 0; i < stream_indices.size(); ++i) { + servers->add_data(stream_indices[i], inner_data, size, flags); } // Consume the block. This isn't the most efficient way of dealing with things