output/httpd: make enum strictly-typed

This commit is contained in:
Max Kellermann 2017-11-10 21:25:03 +01:00
parent 83f8eeec44
commit c54a920d13
2 changed files with 15 additions and 15 deletions

View File

@ -54,9 +54,9 @@ HttpdClient::LockClose()
void void
HttpdClient::BeginResponse() HttpdClient::BeginResponse()
{ {
assert(state != RESPONSE); assert(state != State::RESPONSE);
state = RESPONSE; state = State::RESPONSE;
current_page = nullptr; current_page = nullptr;
if (!head_method) if (!head_method)
@ -69,9 +69,9 @@ HttpdClient::BeginResponse()
bool bool
HttpdClient::HandleLine(const char *line) HttpdClient::HandleLine(const char *line)
{ {
assert(state != RESPONSE); assert(state != State::RESPONSE);
if (state == REQUEST) { if (state == State::REQUEST) {
if (memcmp(line, "HEAD /", 6) == 0) { if (memcmp(line, "HEAD /", 6) == 0) {
line += 6; line += 6;
head_method = true; head_method = true;
@ -96,7 +96,7 @@ HttpdClient::HandleLine(const char *line)
} }
/* after the request line, request headers follow */ /* after the request line, request headers follow */
state = HEADERS; state = State::HEADERS;
return true; return true;
} else { } else {
if (*line == 0) { if (*line == 0) {
@ -137,7 +137,7 @@ HttpdClient::SendResponse()
AllocatedString<> allocated = nullptr; AllocatedString<> allocated = nullptr;
const char *response; const char *response;
assert(state == RESPONSE); assert(state == State::RESPONSE);
if (dlna_streaming_requested) { if (dlna_streaming_requested) {
snprintf(buffer, sizeof(buffer), snprintf(buffer, sizeof(buffer),
@ -198,7 +198,7 @@ HttpdClient::HttpdClient(HttpdOutput &_httpd, UniqueSocketDescriptor _fd,
void void
HttpdClient::ClearQueue() HttpdClient::ClearQueue()
{ {
assert(state == RESPONSE); assert(state == State::RESPONSE);
while (!pages.empty()) { while (!pages.empty()) {
#ifndef NDEBUG #ifndef NDEBUG
@ -216,7 +216,7 @@ HttpdClient::ClearQueue()
void void
HttpdClient::CancelQueue() HttpdClient::CancelQueue()
{ {
if (state != RESPONSE) if (state != State::RESPONSE)
return; return;
ClearQueue(); ClearQueue();
@ -257,7 +257,7 @@ HttpdClient::TryWrite()
{ {
const std::lock_guard<Mutex> protect(httpd.mutex); const std::lock_guard<Mutex> protect(httpd.mutex);
assert(state == RESPONSE); assert(state == State::RESPONSE);
if (current_page == nullptr) { if (current_page == nullptr) {
if (pages.empty()) { if (pages.empty()) {
@ -369,7 +369,7 @@ HttpdClient::TryWrite()
void void
HttpdClient::PushPage(PagePtr page) HttpdClient::PushPage(PagePtr page)
{ {
if (state != RESPONSE) if (state != State::RESPONSE)
/* the client is still writing the HTTP request */ /* the client is still writing the HTTP request */
return; return;
@ -410,7 +410,7 @@ HttpdClient::OnSocketReady(unsigned flags) noexcept
BufferedSocket::InputResult BufferedSocket::InputResult
HttpdClient::OnSocketInput(void *data, size_t length) HttpdClient::OnSocketInput(void *data, size_t length)
{ {
if (state == RESPONSE) { if (state == State::RESPONSE) {
LogWarning(httpd_output_domain, LogWarning(httpd_output_domain,
"unexpected input from client"); "unexpected input from client");
LockClose(); LockClose();
@ -435,7 +435,7 @@ HttpdClient::OnSocketInput(void *data, size_t length)
return InputResult::CLOSED; return InputResult::CLOSED;
} }
if (state == RESPONSE) { if (state == State::RESPONSE) {
if (!SendResponse()) if (!SendResponse())
return InputResult::CLOSED; return InputResult::CLOSED;

View File

@ -46,7 +46,7 @@ class HttpdClient final
/** /**
* The current state of the client. * The current state of the client.
*/ */
enum { enum class State {
/** reading the request line */ /** reading the request line */
REQUEST, REQUEST,
@ -55,7 +55,7 @@ class HttpdClient final
/** sending the HTTP response */ /** sending the HTTP response */
RESPONSE, RESPONSE,
} state = REQUEST; } state = State::REQUEST;
/** /**
* A queue of #Page objects to be sent to the client. * A queue of #Page objects to be sent to the client.
@ -160,7 +160,7 @@ public:
bool HandleLine(const char *line); bool HandleLine(const char *line);
/** /**
* Switch the client to the "RESPONSE" state. * Switch the client to #State::RESPONSE.
*/ */
void BeginResponse(); void BeginResponse();