Similarly, rename the state from SENDING_ERROR to SENDING_SHORT_RESPONSE.
: 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),
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()),
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);
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,
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),
}
// 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) {
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);
{
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()) {
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<char *>(&hdr), sizeof(hdr)));
}
- client->header_or_error.append(stream->stream_header);
+ client->header_or_short_response.append(stream->stream_header);
} else {
assert(false);
}
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;
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();
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;