]> git.sesse.net Git - cubemap/blobdiff - input.cpp
Parse the HTTP header (more) properly, and send the headers on to any connecting...
[cubemap] / input.cpp
index 401aa6a366f8e555a8adeb3bf2e7761551e50762..47c3c0193472d8cfa56747fd2461cdf5595632cd 100644 (file)
--- a/input.cpp
+++ b/input.cpp
 #include "server.h"
 #include "serverpool.h"
 #include "parse.h"
+#include "state.pb.h"
 
 using namespace std;
 
 extern ServerPool *servers;
+         
+// Extremely rudimentary URL parsing.
+bool parse_url(const string &url, string *host, string *port, string *path)
+{
+       if (url.find("http://") != 0) {
+               return false;
+       }
+       
+       string rest = url.substr(strlen("http://"));
+       size_t split = rest.find_first_of(":/");
+       if (split == string::npos) {
+               // http://foo
+               *host = rest;
+               *port = "http";
+               *path = "/";
+               return true;
+       }
+
+       *host = string(rest.begin(), rest.begin() + split);
+       char ch = rest[split];  // Colon or slash.
+       rest = string(rest.begin() + split + 1, rest.end());
+
+       if (ch == ':') {
+               // Parse the port.
+               split = rest.find_first_of('/');
+               if (split == string::npos) {
+                       // http://foo:1234
+                       *port = rest;
+                       *path = "/";
+                       return true;
+               } else {
+                       // http://foo:1234/bar
+                       *port = string(rest.begin(), rest.begin() + split);
+                       *path = string(rest.begin() + split, rest.end());
+                       return true;
+               }
+       }
+
+       // http://foo/bar
+       *port = "http";
+       *path = rest;
+       return true;
+}
 
 Input::Input(const string &stream_id, const string &url)
        : state(NOT_CONNECTED),
          stream_id(stream_id),
          url(url),
-         // TODO
-         host("gruessi.zrh.sesse.net"),
-         port("4013"),
-         path("/test.flv"),
          has_metacube_header(false),
          sock(-1)
 {
 }
 
+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;
@@ -105,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()
 {
@@ -132,6 +283,11 @@ void Input::do_work()
                        request.clear();
                        request_bytes_sent = 0;
                        response.clear();
+       
+                       if (!parse_url(url, &host, &port, &path)) {
+                               fprintf(stderr, "Failed to parse URL '%s'\n", url.c_str());
+                               break;
+                       }
 
                        sock = lookup_and_connect(host, port);
                        if (sock != -1) {
@@ -211,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());
@@ -321,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);
                }