Added HEAD method

This commit is contained in:
2020-12-19 17:13:03 +01:00
parent ba57509461
commit 5e9c98d67f

View File

@ -164,8 +164,14 @@ int client_request_handler(sock *client, int req_num) {
goto respond; goto respond;
} }
if ((int) uri.is_static && uri.filename != NULL) { if (uri.is_static) {
uri_init_cache(&uri); uri_init_cache(&uri);
http_add_header_field(&res.hdr, "Allow", "GET, HEAD");
http_add_header_field(&res.hdr, "Accept-Ranges", "bytes");
if (strncmp(req.method, "GET", 3) != 0 && strncmp(req.method, "HEAD", 4) != 0) {
res.status = http_get_status(405);
goto respond;
}
} }
respond: respond:
@ -176,6 +182,9 @@ int client_request_handler(sock *client, int req_num) {
} else { } else {
http_add_header_field(&res.hdr, "Connection", "close"); http_add_header_field(&res.hdr, "Connection", "close");
} }
if (http_get_header_field(&res.hdr, "Accept-Ranges", HTTP_PRESERVE_UPPER) == NULL) {
http_add_header_field(&res.hdr, "Accept-Ranges", "none");
}
unsigned long len = 0; unsigned long len = 0;
if (res.status->code >= 400 && res.status->code < 600) { if (res.status->code >= 400 && res.status->code < 600) {
http_error_msg *http_msg = http_get_error_msg(res.status->code); http_error_msg *http_msg = http_get_error_msg(res.status->code);
@ -193,6 +202,7 @@ int client_request_handler(sock *client, int req_num) {
} }
http_send_response(client, &res); http_send_response(client, &res);
if (strncmp(req.method, "HEAD", 4) != 0) {
if (res.status->code >= 400 && res.status->code < 600) { if (res.status->code >= 400 && res.status->code < 600) {
int snd_len = 0; int snd_len = 0;
while (snd_len < len) { while (snd_len < len) {
@ -204,6 +214,7 @@ int client_request_handler(sock *client, int req_num) {
snd_len += ret; snd_len += ret;
} }
} }
}
clock_gettime(CLOCK_MONOTONIC, &end); clock_gettime(CLOCK_MONOTONIC, &end);
char *location = http_get_header_field(&res.hdr, "Location", HTTP_PRESERVE_UPPER); char *location = http_get_header_field(&res.hdr, "Location", HTTP_PRESERVE_UPPER);