]> git.sesse.net Git - cubemap/blobdiff - httpinput.cpp
Add suppor for raw (non-Metacube) inputs over HTTP. Only really useful for TS.
[cubemap] / httpinput.cpp
index 4980fc170c2e721a07abb80fe6635916537cca71..5fb55b378610e846f2d74f6074f82f1f95ee58b7 100644 (file)
@@ -24,6 +24,7 @@
 #include "serverpool.h"
 #include "state.pb.h"
 #include "stream.h"
+#include "timespec.h"
 #include "util.h"
 #include "version.h"
 
@@ -31,27 +32,10 @@ using namespace std;
 
 extern ServerPool *servers;
 
-namespace {
-
-// Compute b-a.
-timespec clock_diff(const timespec &a, const timespec &b)
-{
-       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;
-       }
-       assert(ret.tv_nsec >= 0);
-       return ret;
-}
-
-}  // namespace
-
-HTTPInput::HTTPInput(const string &url)
+HTTPInput::HTTPInput(const string &url, Input::Encoding encoding)
        : state(NOT_CONNECTED),
          url(url),
+         encoding(encoding),
          has_metacube_header(false),
          sock(-1)
 {
@@ -65,6 +49,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 +104,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;
 }
 
@@ -328,6 +321,13 @@ void HTTPInput::do_work()
                                        log(WARNING, "[%s] Failed to parse URL '%s'", url.c_str(), url.c_str());
                                        break;
                                }
+
+                               // Remove the brackets around IPv6 address literals.
+                               // TODO: See if we can join this with the code in parse_ip_address(),
+                               // or maybe even more it into parse_url().
+                               if (!host.empty() && host[0] == '[' && host[host.size() - 1] == ']') {
+                                       host = host.substr(1, host.size() - 2);
+                               }
                        }
 
                        sock = lookup_and_connect(host, port);
@@ -422,8 +422,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;
                }
@@ -482,6 +488,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, SUITABLE_FOR_STREAM_START);
+               }
+               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)) {