]> git.sesse.net Git - cubemap/blobdiff - httpinput.cpp
When reading the HTTP input, skip the \r\n\r\n. Fixes the problem of 4 non-Metacube...
[cubemap] / httpinput.cpp
index c30651ed3e5c42100a7f5cc0d16f56e8b7c7751f..a3a950b03a9876c33fbd1de794e25549eea0b7c9 100644 (file)
@@ -1,4 +1,3 @@
-#include <stdio.h>
 #include <assert.h>
 #include <errno.h>
 #include <netdb.h>
@@ -9,6 +8,7 @@
 #include <string.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
+#include <time.h>
 #include <unistd.h>
 #include <map>
 #include <string>
@@ -62,7 +62,9 @@ HTTPInput::HTTPInput(const InputProto &serialized)
 
 void HTTPInput::close_socket()
 {
-       safe_close(sock);
+       if (sock != -1) {
+               safe_close(sock);
+       }
 }
 
 InputProto HTTPInput::serialize() const
@@ -84,17 +86,16 @@ int HTTPInput::lookup_and_connect(const string &host, const string &port)
 {
        addrinfo *ai;
        int err = getaddrinfo(host.c_str(), port.c_str(), NULL, &ai);
-       if (err == -1) {
+       if (err != 0) {
                log(WARNING, "[%s] Lookup of '%s' failed (%s).",
                        url.c_str(), host.c_str(), gai_strerror(err));
-               freeaddrinfo(ai);
                return -1;
        }
 
        addrinfo *base_ai = ai;
 
        // Connect to everything in turn until we have a socket.
-       while (ai && !should_stop()) {
+       for ( ; ai && !should_stop(); ai = ai->ai_next) {
                int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
                if (sock == -1) {
                        // Could be e.g. EPROTONOSUPPORT. The show must go on.
@@ -152,7 +153,6 @@ int HTTPInput::lookup_and_connect(const string &host, const string &port)
                }
 
                safe_close(sock);
-               ai = ai->ai_next;
        }
 
        // Give the last one as error.
@@ -264,6 +264,7 @@ void HTTPInput::do_work()
                        request_bytes_sent = 0;
                        response.clear();
                        pending_data.clear();
+                       has_metacube_header = false;
                        for (size_t i = 0; i < stream_indices.size(); ++i) {
                                servers->set_header(stream_indices[i], "", "");
                        }
@@ -351,7 +352,7 @@ void HTTPInput::do_work()
                                char *ptr = static_cast<char *>(
                                        memmem(response.data(), response.size(), "\r\n\r\n", 4));
                                assert(ptr != NULL);
-                               extra_data = string(ptr, &response[0] + response.size());
+                               extra_data = string(ptr + 4, &response[0] + response.size());
                                response.resize(ptr - response.data());
                        }
 
@@ -385,7 +386,7 @@ void HTTPInput::do_work()
 
                        if (ret == 0) {
                                // This really shouldn't happen...
-                               log(ERROR, "[%s] Socket unexpectedly closed while reading header",
+                               log(ERROR, "[%s] Socket unexpectedly closed while reading data",
                                           url.c_str());
                                state = CLOSING_SOCKET;
                                continue;
@@ -457,6 +458,11 @@ void HTTPInput::process_data(char *ptr, size_t bytes)
                uint32_t size = ntohl(hdr->size);
                uint32_t flags = ntohl(hdr->flags);
 
+               if (size > 65535) {
+                       log(WARNING, "[%s] Metacube block of %x bytes (flags=%x); corrupted header?",
+                               url.c_str(), size, flags);
+               }
+
                // See if we have the entire block. If not, wait for more data.
                if (pending_data.size() < sizeof(metacube_block_header) + size) {
                        return;
@@ -490,6 +496,7 @@ void HTTPInput::drop_pending_data(size_t num_bytes)
        }
        log(WARNING, "[%s] Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?",
                url.c_str(), (long long)num_bytes);
+       assert(pending_data.size() >= num_bytes);
        pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
 }