From 38a2bb28fd8dcb5bb1e0cb56028936a35f20f503 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 16 Aug 2015 00:27:33 +0200 Subject: [PATCH 1/1] Rename header_or_error to header_or_short_response, as it will soon be able to contain pongs. Similarly, rename the state from SENDING_ERROR to SENDING_SHORT_RESPONSE. --- client.cpp | 10 +++++----- client.h | 16 +++++++++------- server.cpp | 34 +++++++++++++++++----------------- server.h | 2 +- state.proto | 4 ++-- 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/client.cpp b/client.cpp index 786e178..77fad72 100644 --- a/client.cpp +++ b/client.cpp @@ -19,7 +19,7 @@ Client::Client(int sock) : sock(sock), state(Client::READING_REQUEST), stream(NULL), - header_or_error_bytes_sent(0), + header_or_short_response_bytes_sent(0), stream_pos(0), bytes_sent(0), bytes_lost(0), @@ -70,8 +70,8 @@ Client::Client(const ClientProto &serialized, Stream *stream) request(serialized.request()), url(serialized.url()), stream(stream), - header_or_error(serialized.header_or_error()), - header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()), + header_or_short_response(serialized.header_or_short_response()), + header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()), stream_pos(serialized.stream_pos()), bytes_sent(serialized.bytes_sent()), bytes_lost(serialized.bytes_lost()), @@ -100,8 +100,8 @@ ClientProto Client::serialize() const serialized.set_state(state); serialized.set_request(request); serialized.set_url(url); - serialized.set_header_or_error(header_or_error); - serialized.set_header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()); + serialized.set_header_or_short_response(header_or_short_response); + serialized.set_header_or_short_response_bytes_sent(serialized.header_or_short_response_bytes_sent()); serialized.set_stream_pos(stream_pos); serialized.set_bytes_sent(bytes_sent); serialized.set_bytes_lost(bytes_lost); diff --git a/client.h b/client.h index 954ad41..2ee4783 100644 --- a/client.h +++ b/client.h @@ -44,7 +44,7 @@ struct Client { std::string referer; std::string user_agent; - enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_ERROR, WAITING_FOR_KEYFRAME, PREBUFFERING }; + enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_SHORT_RESPONSE, WAITING_FOR_KEYFRAME, PREBUFFERING }; State state; // The HTTP request, as sent by the client. If we are in READING_REQUEST, @@ -56,14 +56,16 @@ struct Client { std::string url; Stream *stream; - // The header we want to send. This is nominally a copy of Stream::header, - // but since that might change on reconnects etc., we keep a local copy here. - // Only relevant for SENDING_HEADER or SENDING_ERROR; blank otherwise. - std::string header_or_error; + // The header we want to send, or the response with headers if we know + // it in its entirety after reading the request (typically an error). + // This is nominally a copy of Stream::header, but since that might + // change on reconnects etc., we keep a local copy here. Only relevant + // for SENDING_HEADER or SENDING_SHORT_RESPONSE; blank otherwise. + std::string header_or_short_response; // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER - // or SENDING_ERROR. - size_t header_or_error_bytes_sent; + // or SENDING_SHORT_RESPONSE. + size_t header_or_short_response_bytes_sent; // Number of bytes we are into the stream (ie., the end of last send). // -1 means we want to send from the end of the backlog (the normal case), diff --git a/server.cpp b/server.cpp index 72664e3..615e8fb 100644 --- a/server.cpp +++ b/server.cpp @@ -423,17 +423,17 @@ read_request_again: } // We've changed states, so fall through. - assert(client->state == Client::SENDING_ERROR || + assert(client->state == Client::SENDING_SHORT_RESPONSE || client->state == Client::SENDING_HEADER); } - case Client::SENDING_ERROR: + case Client::SENDING_SHORT_RESPONSE: case Client::SENDING_HEADER: { -sending_header_or_error_again: +sending_header_or_short_response_again: int ret; do { ret = write(client->sock, - client->header_or_error.data() + client->header_or_error_bytes_sent, - client->header_or_error.size() - client->header_or_error_bytes_sent); + client->header_or_short_response.data() + client->header_or_short_response_bytes_sent, + client->header_or_short_response.size() - client->header_or_short_response_bytes_sent); } while (ret == -1 && errno == EINTR); if (ret == -1 && errno == EAGAIN) { @@ -451,18 +451,18 @@ sending_header_or_error_again: return; } - client->header_or_error_bytes_sent += ret; - assert(client->header_or_error_bytes_sent <= client->header_or_error.size()); + client->header_or_short_response_bytes_sent += ret; + assert(client->header_or_short_response_bytes_sent <= client->header_or_short_response.size()); - if (client->header_or_error_bytes_sent < client->header_or_error.size()) { + if (client->header_or_short_response_bytes_sent < client->header_or_short_response.size()) { // We haven't sent all yet. Fine; go another round. - goto sending_header_or_error_again; + goto sending_header_or_short_response_again; } // We're done sending the header or error! Clear it to release some memory. - client->header_or_error.clear(); + client->header_or_short_response.clear(); - if (client->state == Client::SENDING_ERROR) { + if (client->state == Client::SENDING_SHORT_RESPONSE) { // We're done sending the error, so now close. // This is postcondition #1. close_client(client); @@ -670,11 +670,11 @@ void Server::construct_header(Client *client) { Stream *stream = client->stream; if (stream->encoding == Stream::STREAM_ENCODING_RAW) { - client->header_or_error = stream->http_header + + client->header_or_short_response = stream->http_header + "\r\n" + stream->stream_header; } else if (stream->encoding == Stream::STREAM_ENCODING_METACUBE) { - client->header_or_error = stream->http_header + + client->header_or_short_response = stream->http_header + "Content-encoding: metacube\r\n" + "\r\n"; if (!stream->stream_header.empty()) { @@ -683,10 +683,10 @@ void Server::construct_header(Client *client) hdr.size = htonl(stream->stream_header.size()); hdr.flags = htons(METACUBE_FLAGS_HEADER); hdr.csum = htons(metacube2_compute_crc(&hdr)); - client->header_or_error.append( + client->header_or_short_response.append( string(reinterpret_cast(&hdr), sizeof(hdr))); } - client->header_or_error.append(stream->stream_header); + client->header_or_short_response.append(stream->stream_header); } else { assert(false); } @@ -709,10 +709,10 @@ void Server::construct_error(Client *client, int error_code) char error[256]; snprintf(error, 256, "HTTP/1.0 %d Error\r\nContent-type: text/plain\r\n\r\nSomething went wrong. Sorry.\r\n", error_code); - client->header_or_error = error; + client->header_or_short_response = error; // Switch states. - client->state = Client::SENDING_ERROR; + client->state = Client::SENDING_SHORT_RESPONSE; epoll_event ev; ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP; diff --git a/server.h b/server.h index 79c35f8..cfc9d25 100644 --- a/server.h +++ b/server.h @@ -138,7 +138,7 @@ private: void construct_header(Client *client); // Construct a generic error with the given line, and set the client into - // the SENDING_ERROR state. + // the SENDING_SHORT_RESPONSE state. void construct_error(Client *client, int error_code); void process_queued_data(); diff --git a/state.proto b/state.proto index 9422540..c7c7e70 100644 --- a/state.proto +++ b/state.proto @@ -8,8 +8,8 @@ message ClientProto { optional int32 state = 2; optional bytes request = 3; optional string url = 4; - optional bytes header_or_error = 5; - optional int64 header_or_error_bytes_sent = 6; + optional bytes header_or_short_response = 5; + optional int64 header_or_short_response_bytes_sent = 6; optional int64 stream_pos = 7; optional int64 bytes_sent = 10; optional int64 bytes_lost = 11; -- 2.39.2