Skip to content

Commit

Permalink
Fixing webserver to not always return 400 codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasnoble committed Dec 30, 2024
1 parent d2bf4f7 commit 4eca41b
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/core/web-server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -952,11 +952,14 @@ struct PCSX::WebClient::WebClientImpl {
}

void onEOF() {
llhttp_execute(&m_httpParser, nullptr, 0);
if (m_httpParser.upgrade) {
auto error = llhttp_execute(&m_httpParser, nullptr, 0);
if (error == HPE_PAUSED_UPGRADE) {
onUpgrade();
} else if (error != HPE_OK) {
send400(magic_enum::enum_name(error));
} else {
scheduleClose();
}
scheduleClose();
}

void onUpgrade() {}
Expand Down Expand Up @@ -1119,11 +1122,12 @@ struct PCSX::WebClient::WebClientImpl {
const char* ptr = reinterpret_cast<const char*>(slice.data());
auto size = slice.size();

auto parsed = llhttp_execute(&m_httpParser, ptr, size);
auto error = llhttp_execute(&m_httpParser, ptr, size);
if (m_status != OPEN) return;
if (parsed != size) send400();
if (m_httpParser.upgrade) {
if (error == HPE_PAUSED_UPGRADE) {
onUpgrade();
} else if (error != HPE_OK) {
send400(magic_enum::enum_name(error));
}
}

Expand All @@ -1150,8 +1154,10 @@ struct PCSX::WebClient::WebClientImpl {
write(std::move(slice));
}

void send400() {
write("HTTP/1.1 400 Bad Request\r\n\r\nThis request failed to parse properly.\r\n");
void send400(std::string_view code) {
std::string str =
fmt::format("HTTP/1.1 400 Bad Request\r\n\r\Request failed to parse properly. Error: {}\r\n", code);
write(std::move(str));
scheduleClose();
}

Expand Down

0 comments on commit 4eca41b

Please sign in to comment.