output/httpd: reject some well-known request paths

Return `404 not found` for some common well-known paths, as clients requesting them usually do that automatically and don't expect endless audio stram.

Closes #572
This commit is contained in:
cathugger 2019-06-05 00:49:15 +03:00 committed by Max Kellermann
parent 4b81cf0c2c
commit f9ca2f52c1
3 changed files with 27 additions and 2 deletions

2
NEWS
View File

@ -1,4 +1,6 @@
ver 0.21.10 (not yet released) ver 0.21.10 (not yet released)
* output
- httpd: reject some well-known URIs
* fix crash bug (0.21.9 regression) * fix crash bug (0.21.9 regression)
ver 0.21.9 (2019/05/20) ver 0.21.9 (2019/05/20)

View File

@ -83,6 +83,17 @@ HttpdClient::HandleLine(const char *line) noexcept
return false; return false;
} }
/* blacklist some well-known request paths */
if ((strncmp(line, "favicon.ico", 11) == 0 &&
(line[11] == '\0' || line[11] == ' ')) ||
(strncmp(line, "robots.txt", 10) == 0 &&
(line[10] == '\0' || line[10] == ' ')) ||
(strncmp(line, "sitemap.xml", 11) == 0 &&
(line[11] == '\0' || line[11] == ' ')) ||
(strncmp(line, ".well-known/", 12) == 0)) {
should_reject = true;
}
line = strchr(line, ' '); line = strchr(line, ' ');
if (line == nullptr || strncmp(line + 1, "HTTP/", 5) != 0) { if (line == nullptr || strncmp(line + 1, "HTTP/", 5) != 0) {
/* HTTP/0.9 without request headers */ /* HTTP/0.9 without request headers */
@ -129,7 +140,14 @@ HttpdClient::SendResponse() noexcept
assert(state == State::RESPONSE); assert(state == State::RESPONSE);
if (metadata_requested) { if (should_reject) {
response =
"HTTP/1.1 404 not found\r\n"
"Content-Type: text/plain\r\n"
"Connection: close\r\n"
"\r\n"
"404 not found";
} else if (metadata_requested) {
allocated = allocated =
icy_server_metadata_header(httpd.name, httpd.genre, icy_server_metadata_header(httpd.name, httpd.genre,
httpd.website, httpd.website,
@ -415,7 +433,7 @@ HttpdClient::OnSocketInput(void *data, size_t length) noexcept
if (!SendResponse()) if (!SendResponse())
return InputResult::CLOSED; return InputResult::CLOSED;
if (head_method) { if (head_method || should_reject) {
LockClose(); LockClose();
return InputResult::CLOSED; return InputResult::CLOSED;
} }

View File

@ -83,6 +83,11 @@ class HttpdClient final
*/ */
bool head_method = false; bool head_method = false;
/**
* Should we reject this request?
*/
bool should_reject = false;
/* ICY */ /* ICY */
/** /**