Parse the HTTP header (more) properly, and send the headers on to any connecting...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 8 Apr 2013 23:35:03 +0000 (01:35 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 8 Apr 2013 23:35:03 +0000 (01:35 +0200)
input.cpp
input.h
server.cpp
server.h
state.proto

index 58d319f..47c3c01 100644 (file)
--- a/input.cpp
+++ b/input.cpp
@@ -87,6 +87,7 @@ Input::Input(const InputProto &serialized)
          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())
 {
@@ -105,6 +106,7 @@ InputProto Input::serialize() const
        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);
@@ -176,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<string> lines = split_lines(response);
+       if (lines.empty()) {
+               fprintf(stderr, "WARNING: Empty HTTP response from input.\n");
+               return false;
+       }
+
+       vector<string> 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<string, string> 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<string, string>::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<string, string>::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()
 {
@@ -287,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());
@@ -397,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);
                }
diff --git a/input.h b/input.h
index 8b536c4..28bc14b 100644 (file)
--- a/input.h
+++ b/input.h
@@ -32,6 +32,9 @@ private:
        // Open a socket that connects to the given host and port. Does DNS resolving.
        int lookup_and_connect(const std::string &host, const std::string &port);
 
+       // Parses a HTTP response. Returns false if it not a 200.
+       bool parse_response(const std::string &response);
+
        // Stores the given data, looks for Metacube blocks (skipping data if needed),
        // and calls process_block() for each one.
        void process_data(char *ptr, size_t bytes);
@@ -66,6 +69,9 @@ private:
        // The HTTP response we've received so far. Only relevant for RECEIVING_HEADER.
        std::string response;
 
+       // The HTTP respones headers we want to give clients for this input.
+       std::string http_header;
+
        // Data we have received but not fully processed yet.
        std::vector<char> pending_data;
 
index 6b92fc6..e91ba39 100644 (file)
@@ -538,8 +538,7 @@ int Server::parse_request(Client *client)
 
 void Server::construct_header(Client *client)
 {
-       client->header_or_error = "HTTP/1.0 200 OK\r\nContent-type: video/x-flv\r\nCache-Control: no-cache\r\n\r\n" +
-               find_stream(client->stream_id)->header;
+       client->header_or_error = find_stream(client->stream_id)->header;
 
        // Switch states.
        client->state = Client::SENDING_HEADER;
index ce9365d..5ebc060 100644 (file)
--- a/server.h
+++ b/server.h
@@ -105,7 +105,8 @@ public:
 
        // Stop the thread.
        void stop();
-       
+
+       // Set header (both HTTP header and any stream headers) for the given stream.
        void set_header(const std::string &stream_id, const std::string &header);
 
        // These will be deferred until the next time an iteration in do_work() happens,
index 8fc6321..3d8ea99 100644 (file)
@@ -25,6 +25,7 @@ message InputProto {
        optional bytes request = 4;
        optional int32 request_bytes_sent = 5;
        optional bytes response = 6;
+       optional bytes http_header = 10;
        optional bytes pending_data = 7;
        optional bool has_metacube_header = 8;
        optional int32 sock = 9;