4 #include <netinet/in.h>
10 #include <sys/socket.h>
19 #include "httpinput.h"
21 #include "metacube2.h"
22 #include "mutexlock.h"
24 #include "serverpool.h"
35 string host_header(const string &host, const string &port)
37 if (port == "http" || atoi(port.c_str()) == 80) {
40 return host + ":" + port;
46 extern ServerPool *servers;
48 HTTPInput::HTTPInput(const string &url, Input::Encoding encoding)
49 : state(NOT_CONNECTED),
52 has_metacube_header(false),
54 num_connection_attempts(0),
55 suppress_logging(false)
57 pthread_mutex_init(&stats_mutex, NULL);
59 stats.bytes_received = 0;
60 stats.data_bytes_received = 0;
61 stats.connect_time = -1;
63 last_verbose_connection.tv_sec = -3600;
64 last_verbose_connection.tv_nsec = 0;
67 HTTPInput::HTTPInput(const InputProto &serialized)
68 : state(State(serialized.state())),
69 url(serialized.url()),
70 encoding(serialized.is_metacube_encoded() ?
71 Input::INPUT_ENCODING_METACUBE :
72 Input::INPUT_ENCODING_RAW),
73 request(serialized.request()),
74 request_bytes_sent(serialized.request_bytes_sent()),
75 response(serialized.response()),
76 http_header(serialized.http_header()),
77 stream_header(serialized.stream_header()),
78 has_metacube_header(serialized.has_metacube_header()),
79 sock(serialized.sock())
81 pending_data.resize(serialized.pending_data().size());
82 memcpy(&pending_data[0], serialized.pending_data().data(), serialized.pending_data().size());
84 string protocol, user;
85 parse_url(url, &protocol, &user, &host, &port, &path); // Don't care if it fails.
87 pthread_mutex_init(&stats_mutex, NULL);
89 stats.bytes_received = serialized.bytes_received();
90 stats.data_bytes_received = serialized.data_bytes_received();
91 if (serialized.has_connect_time()) {
92 stats.connect_time = serialized.connect_time();
94 stats.connect_time = time(NULL);
98 void HTTPInput::close_socket()
105 MutexLock lock(&stats_mutex);
106 stats.connect_time = -1;
109 InputProto HTTPInput::serialize() const
111 InputProto serialized;
112 serialized.set_state(state);
113 serialized.set_url(url);
114 serialized.set_request(request);
115 serialized.set_request_bytes_sent(request_bytes_sent);
116 serialized.set_response(response);
117 serialized.set_http_header(http_header);
118 serialized.set_stream_header(stream_header);
119 serialized.set_pending_data(string(pending_data.begin(), pending_data.end()));
120 serialized.set_has_metacube_header(has_metacube_header);
121 serialized.set_sock(sock);
122 serialized.set_bytes_received(stats.bytes_received);
123 serialized.set_data_bytes_received(stats.data_bytes_received);
124 serialized.set_connect_time(stats.connect_time);
125 if (encoding == Input::INPUT_ENCODING_METACUBE) {
126 serialized.set_is_metacube_encoded(true);
128 assert(encoding == Input::INPUT_ENCODING_RAW);
129 serialized.set_is_metacube_encoded(false);
134 int HTTPInput::lookup_and_connect(const string &host, const string &port)
137 int err = getaddrinfo(host.c_str(), port.c_str(), NULL, &ai);
139 if (!suppress_logging) {
140 log(WARNING, "[%s] Lookup of '%s' failed (%s).",
141 url.c_str(), host.c_str(), gai_strerror(err));
146 addrinfo *base_ai = ai;
148 // Connect to everything in turn until we have a socket.
149 for ( ; ai && !should_stop(); ai = ai->ai_next) {
150 int sock = socket(ai->ai_family, SOCK_STREAM, IPPROTO_TCP);
152 // Could be e.g. EPROTONOSUPPORT. The show must go on.
156 // Now do a non-blocking connect. This is important because we want to be able to be
157 // woken up, even though it's rather cumbersome.
159 // Set the socket as nonblocking.
161 if (ioctl(sock, FIONBIO, &one) == -1) {
162 log_perror("ioctl(FIONBIO)");
167 // Do a non-blocking connect.
169 err = connect(sock, ai->ai_addr, ai->ai_addrlen);
170 } while (err == -1 && errno == EINTR);
172 if (err == -1 && errno != EINPROGRESS) {
173 log_perror("connect");
178 // Wait for the connect to complete, or an error to happen.
180 bool complete = wait_for_activity(sock, POLLIN | POLLOUT, NULL);
190 // Check whether it ended in an error or not.
191 socklen_t err_size = sizeof(err);
192 if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &err_size) == -1) {
193 log_perror("getsockopt");
201 // Successful connect.
202 freeaddrinfo(base_ai);
209 // Give the last one as error.
210 if (!suppress_logging) {
211 log(WARNING, "[%s] Connect to '%s' failed (%s)",
212 url.c_str(), host.c_str(), strerror(errno));
214 freeaddrinfo(base_ai);
218 bool HTTPInput::parse_response(const string &request)
220 vector<string> lines = split_lines(response);
222 if (!suppress_logging) {
223 log(WARNING, "[%s] Empty HTTP response from input.", url.c_str());
228 vector<string> first_line_tokens = split_tokens(lines[0]);
229 if (first_line_tokens.size() < 2) {
230 if (!suppress_logging) {
231 log(WARNING, "[%s] Malformed response line '%s' from input.",
232 url.c_str(), lines[0].c_str());
237 int response = atoi(first_line_tokens[1].c_str());
238 if (response != 200) {
239 if (!suppress_logging) {
240 log(WARNING, "[%s] Non-200 response '%s' from input.",
241 url.c_str(), lines[0].c_str());
246 multimap<string, string> parameters = extract_headers(lines, url);
248 // Remove “Content-encoding: metacube”.
249 // TODO: Make case-insensitive.
250 multimap<string, string>::iterator encoding_it =
251 parameters.find("Content-encoding");
252 if (encoding_it != parameters.end() && encoding_it->second == "metacube") {
253 parameters.erase(encoding_it);
256 // Change “Server: foo” to “Server: metacube/0.1 (reflecting: foo)”
257 // TODO: Make case-insensitive.
258 // XXX: Use a Via: instead?
259 if (parameters.count("Server") == 0) {
260 parameters.insert(make_pair("Server", SERVER_IDENTIFICATION));
262 for (multimap<string, string>::iterator it = parameters.begin();
263 it != parameters.end();
265 if (it->first != "Server") {
268 it->second = SERVER_IDENTIFICATION " (reflecting: " + it->second + ")";
272 // Set “Connection: close”.
273 // TODO: Make case-insensitive.
274 parameters.erase("Connection");
275 parameters.insert(make_pair("Connection", "close"));
277 // Construct the new HTTP header.
278 http_header = "HTTP/1.0 200 OK\r\n";
279 for (multimap<string, string>::iterator it = parameters.begin();
280 it != parameters.end();
282 http_header.append(it->first + ": " + it->second + "\r\n");
285 for (size_t i = 0; i < stream_indices.size(); ++i) {
286 servers->set_header(stream_indices[i], http_header, stream_header);
292 void HTTPInput::do_work()
294 timespec last_activity;
296 // TODO: Make the timeout persist across restarts.
297 if (state == SENDING_REQUEST || state == RECEIVING_HEADER || state == RECEIVING_DATA) {
298 int err = clock_gettime(CLOCK_MONOTONIC_COARSE, &last_activity);
302 while (!should_stop()) {
303 if (state == SENDING_REQUEST || state == RECEIVING_HEADER || state == RECEIVING_DATA) {
304 // Give the socket 30 seconds since last activity before we time out.
305 static const int timeout_secs = 30;
308 int err = clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
311 timespec elapsed = clock_diff(last_activity, now);
312 if (elapsed.tv_sec >= timeout_secs) {
314 if (!suppress_logging) {
315 log(ERROR, "[%s] Timeout after %d seconds, closing.", url.c_str(), elapsed.tv_sec);
317 state = CLOSING_SOCKET;
321 // Basically calculate (30 - (now - last_activity)) = (30 + (last_activity - now)).
322 // Add a second of slack to account for differences between clocks.
323 timespec timeout = clock_diff(now, last_activity);
324 timeout.tv_sec += timeout_secs + 1;
325 assert(timeout.tv_sec > 0 || (timeout.tv_sec >= 0 && timeout.tv_nsec > 0));
327 bool activity = wait_for_activity(sock, (state == SENDING_REQUEST) ? POLLOUT : POLLIN, &timeout);
329 err = clock_gettime(CLOCK_MONOTONIC_COARSE, &last_activity);
332 // OK. Most likely, should_stop was set, or we have timed out.
340 request_bytes_sent = 0;
342 pending_data.clear();
343 has_metacube_header = false;
344 for (size_t i = 0; i < stream_indices.size(); ++i) {
345 servers->set_header(stream_indices[i], "", "");
349 string protocol, user; // Thrown away.
350 if (!parse_url(url, &protocol, &user, &host, &port, &path)) {
351 if (!suppress_logging) {
352 log(WARNING, "[%s] Failed to parse URL '%s'", url.c_str(), url.c_str());
357 // Remove the brackets around IPv6 address literals.
358 // TODO: See if we can join this with the code in parse_ip_address(),
359 // or maybe even more it into parse_url().
360 if (!host.empty() && host[0] == '[' && host[host.size() - 1] == ']') {
361 host = host.substr(1, host.size() - 2);
365 if (suppress_logging) {
366 // See if there's more than one minute since last time we made a connection
367 // with logging enabled. If so, turn it on again.
369 int err = clock_gettime(CLOCK_MONOTONIC_COARSE, &now);
372 double elapsed = now.tv_sec - last_verbose_connection.tv_sec +
373 1e-9 * (now.tv_nsec - last_verbose_connection.tv_nsec);
374 if (elapsed > 60.0) {
375 suppress_logging = false;
378 if (!suppress_logging) {
379 int err = clock_gettime(CLOCK_MONOTONIC_COARSE, &last_verbose_connection);
382 ++num_connection_attempts;
383 sock = lookup_and_connect(host, port);
385 // Yay, successful connect. Try to set it as nonblocking.
387 if (ioctl(sock, FIONBIO, &one) == -1) {
388 log_perror("ioctl(FIONBIO)");
389 state = CLOSING_SOCKET;
391 state = SENDING_REQUEST;
392 request = "GET " + path + " HTTP/1.0\r\nHost: " + host_header(host, port) + "\r\nUser-Agent: cubemap\r\n\r\n";
393 request_bytes_sent = 0;
396 MutexLock lock(&stats_mutex);
397 stats.connect_time = time(NULL);
398 clock_gettime(CLOCK_MONOTONIC_COARSE, &last_activity);
401 case SENDING_REQUEST: {
402 size_t to_send = request.size() - request_bytes_sent;
406 ret = write(sock, request.data() + request_bytes_sent, to_send);
407 } while (ret == -1 && errno == EINTR);
411 state = CLOSING_SOCKET;
416 request_bytes_sent += ret;
418 if (request_bytes_sent == request.size()) {
419 state = RECEIVING_HEADER;
423 case RECEIVING_HEADER: {
428 ret = read(sock, buf, sizeof(buf));
429 } while (ret == -1 && errno == EINTR);
433 state = CLOSING_SOCKET;
438 // This really shouldn't happen...
439 if (!suppress_logging) {
440 log(ERROR, "[%s] Socket unexpectedly closed while reading header",
443 state = CLOSING_SOCKET;
447 RequestParseStatus status = wait_for_double_newline(&response, buf, ret);
449 if (status == RP_OUT_OF_SPACE) {
450 if (!suppress_logging) {
451 log(WARNING, "[%s] Server sent overlong HTTP response!", url.c_str());
453 state = CLOSING_SOCKET;
455 } else if (status == RP_NOT_FINISHED_YET) {
459 // OK, so we're fine, but there might be some of the actual data after the response.
460 // We'll need to deal with that separately.
462 if (status == RP_EXTRA_DATA) {
463 char *ptr = static_cast<char *>(
464 memmem(response.data(), response.size(), "\r\n\r\n", 4));
466 extra_data = string(ptr + 4, &response[0] + response.size());
467 response.resize(ptr - response.data());
470 if (!parse_response(response)) {
471 state = CLOSING_SOCKET;
475 if (!extra_data.empty()) {
476 process_data(&extra_data[0], extra_data.size());
479 if (!suppress_logging) {
480 if (encoding == Input::INPUT_ENCODING_RAW) {
481 log(INFO, "[%s] Connected to '%s', receiving raw data.",
482 url.c_str(), url.c_str());
484 assert(encoding == Input::INPUT_ENCODING_METACUBE);
485 log(INFO, "[%s] Connected to '%s', receiving data.",
486 url.c_str(), url.c_str());
489 state = RECEIVING_DATA;
492 case RECEIVING_DATA: {
497 ret = read(sock, buf, sizeof(buf));
498 } while (ret == -1 && errno == EINTR);
502 state = CLOSING_SOCKET;
507 // This really shouldn't happen...
508 if (!suppress_logging) {
509 log(ERROR, "[%s] Socket unexpectedly closed while reading data",
512 state = CLOSING_SOCKET;
516 num_connection_attempts = 0; // Reset, since we have a successful read.
517 if (suppress_logging) {
518 // This was suppressed earlier, so print it out now.
519 if (encoding == Input::INPUT_ENCODING_RAW) {
520 log(INFO, "[%s] Connected to '%s', receiving raw data.",
521 url.c_str(), url.c_str());
523 assert(encoding == Input::INPUT_ENCODING_METACUBE);
524 log(INFO, "[%s] Connected to '%s', receiving data.",
525 url.c_str(), url.c_str());
527 suppress_logging = false;
530 process_data(buf, ret);
533 case CLOSING_SOCKET: {
535 state = NOT_CONNECTED;
542 // If we are still in NOT_CONNECTED, either something went wrong,
543 // or the connection just got closed.
544 // The earlier steps have already given the error message, if any.
545 if (state == NOT_CONNECTED && !should_stop()) {
546 if (!suppress_logging) {
547 log(INFO, "[%s] Waiting 0.2 seconds and restarting...", url.c_str());
550 if (num_connection_attempts >= 3 && !suppress_logging) {
551 log(INFO, "[%s] %d failed connection attempts, suppressing logging for one minute.",
552 url.c_str(), num_connection_attempts);
553 suppress_logging = true;
556 timeout_ts.tv_sec = 0;
557 timeout_ts.tv_nsec = 200000000;
558 wait_for_wakeup(&timeout_ts);
563 void HTTPInput::process_data(char *ptr, size_t bytes)
566 MutexLock mutex(&stats_mutex);
567 stats.bytes_received += bytes;
570 if (encoding == Input::INPUT_ENCODING_RAW) {
571 for (size_t i = 0; i < stream_indices.size(); ++i) {
572 servers->add_data(stream_indices[i], ptr, bytes, /*metacube_flags=*/0);
577 assert(encoding == Input::INPUT_ENCODING_METACUBE);
578 pending_data.insert(pending_data.end(), ptr, ptr + bytes);
581 // If we don't have enough data (yet) for even the Metacube header, just return.
582 if (pending_data.size() < sizeof(metacube2_block_header)) {
586 // Make sure we have the Metacube sync header at the start.
587 // We may need to skip over junk data (it _should_ not happen, though).
588 if (!has_metacube_header) {
589 char *ptr = static_cast<char *>(
590 memmem(pending_data.data(), pending_data.size(),
591 METACUBE2_SYNC, strlen(METACUBE2_SYNC)));
593 // OK, so we didn't find the sync marker. We know then that
594 // we do not have the _full_ marker in the buffer, but we
595 // could have N-1 bytes. Drop everything before that,
597 drop_pending_data(pending_data.size() - (strlen(METACUBE2_SYNC) - 1));
600 // Yay, we found the header. Drop everything (if anything) before it.
601 drop_pending_data(ptr - pending_data.data());
602 has_metacube_header = true;
604 // Re-check that we have the entire header; we could have dropped data.
605 if (pending_data.size() < sizeof(metacube2_block_header)) {
611 // Now it's safe to read the header.
612 metacube2_block_header hdr;
613 memcpy(&hdr, pending_data.data(), sizeof(hdr));
614 assert(memcmp(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync)) == 0);
615 uint32_t size = ntohl(hdr.size);
616 uint16_t flags = ntohs(hdr.flags);
617 uint16_t expected_csum = metacube2_compute_crc(&hdr);
619 if (expected_csum != ntohs(hdr.csum)) {
620 log(WARNING, "[%s] Metacube checksum failed (expected 0x%x, got 0x%x), "
621 "not reading block claiming to be %d bytes (flags=%x).",
622 url.c_str(), expected_csum, ntohs(hdr.csum),
625 // Drop only the first byte, and let the rest of the code handle resync.
626 pending_data.erase(pending_data.begin(), pending_data.begin() + 1);
627 has_metacube_header = false;
630 if (size > 10485760) {
631 log(WARNING, "[%s] Metacube block of %d bytes (flags=%x); corrupted header??",
632 url.c_str(), size, flags);
635 // See if we have the entire block. If not, wait for more data.
636 if (pending_data.size() < sizeof(metacube2_block_header) + size) {
640 // Send this block on to the servers.
642 MutexLock lock(&stats_mutex);
643 stats.data_bytes_received += size;
645 char *inner_data = pending_data.data() + sizeof(metacube2_block_header);
646 if (flags & METACUBE_FLAGS_HEADER) {
647 stream_header = string(inner_data, inner_data + size);
648 for (size_t i = 0; i < stream_indices.size(); ++i) {
649 servers->set_header(stream_indices[i], http_header, stream_header);
652 for (size_t i = 0; i < stream_indices.size(); ++i) {
653 servers->add_data(stream_indices[i], inner_data, size, flags);
656 // Consume the block. This isn't the most efficient way of dealing with things
657 // should we have many blocks, but these routines don't need to be too efficient
659 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube2_block_header) + size);
660 has_metacube_header = false;
664 void HTTPInput::drop_pending_data(size_t num_bytes)
666 if (num_bytes == 0) {
669 log(WARNING, "[%s] Dropping %lld junk bytes; not a Metacube2 stream, or data was dropped from the middle of the stream.",
670 url.c_str(), (long long)num_bytes);
671 assert(pending_data.size() >= num_bytes);
672 pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
675 void HTTPInput::add_destination(int stream_index)
677 stream_indices.push_back(stream_index);
678 servers->set_header(stream_index, http_header, stream_header);
681 InputStats HTTPInput::get_stats() const
683 MutexLock lock(&stats_mutex);