+ 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;
+
+ // Joinable is already the default, but it's good to be certain.
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
+ pthread_create(&worker_thread, &attr, Input::do_work_thunk, this);
+}
+
+void Input::stop()
+{
+ should_stop = true;
+
+ if (pthread_join(worker_thread, NULL) == -1) {
+ perror("pthread_join");
+ exit(1);
+ }
+}
+
+void *Input::do_work_thunk(void *arg)
+{
+ Input *input = static_cast<Input *>(arg);
+ input->do_work();
+ return NULL;
+}
+
+int Input::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) {
+ fprintf(stderr, "WARNING: Lookup of '%s' failed (%s).\n",
+ host.c_str(), gai_strerror(err));
+ freeaddrinfo(ai);
+ return -1;
+ }
+
+ // Connect to everything in turn until we have a socket.
+ while (ai && !should_stop) {
+ int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
+ if (sock == -1) {
+ // Could be e.g. EPROTONOSUPPORT. The show must go on.
+ continue;
+ }
+
+ do {
+ err = connect(sock, ai->ai_addr, ai->ai_addrlen);
+ } while (err == -1 && errno == EINTR);
+
+ if (err != -1) {
+ freeaddrinfo(ai);
+ return sock;
+ }
+
+ ai = ai->ai_next;
+ }
+
+ // Give the last one as error.
+ fprintf(stderr, "WARNING: Connect to '%s' failed (%s)\n",
+ host.c_str(), strerror(errno));
+ 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;