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())
{
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);
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()
{
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());
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);
}
// 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);
// 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;