Always lowering both http field names in search

This commit is contained in:
2020-12-27 19:36:48 +01:00
parent 885ec2226f
commit 1733ad3c1d
4 changed files with 23 additions and 24 deletions

View File

@ -95,9 +95,9 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
goto respond; goto respond;
} }
hdr_connection = http_get_header_field(&req.hdr, "Connection", HTTP_LOWER); hdr_connection = http_get_header_field(&req.hdr, "Connection");
client_keep_alive = hdr_connection != NULL && strncmp(hdr_connection, "keep-alive", 10) == 0; client_keep_alive = hdr_connection != NULL && strncmp(hdr_connection, "keep-alive", 10) == 0;
host = http_get_header_field(&req.hdr, "Host", HTTP_LOWER); host = http_get_header_field(&req.hdr, "Host");
if (host == NULL || strchr(host, '/') != NULL) { if (host == NULL || strchr(host, '/') != NULL) {
res.status = http_get_status(400); res.status = http_get_status(400);
sprintf(err_msg, "The client provided no or an invalid Host header field."); sprintf(err_msg, "The client provided no or an invalid Host header field.");
@ -200,8 +200,8 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
http_add_header_field(&res.hdr, "Cache-Control", "public, max-age=86400"); http_add_header_field(&res.hdr, "Cache-Control", "public, max-age=86400");
} }
char *if_modified_since = http_get_header_field(&req.hdr, "If-Modified-Since", HTTP_LOWER); char *if_modified_since = http_get_header_field(&req.hdr, "If-Modified-Since");
char *if_none_match = http_get_header_field(&req.hdr, "If-None-Match", HTTP_LOWER); char *if_none_match = http_get_header_field(&req.hdr, "If-None-Match");
if ((if_none_match != NULL && strstr(if_none_match, uri.meta->etag) == NULL) || (accept_if_modified_since && if ((if_none_match != NULL && strstr(if_none_match, uri.meta->etag) == NULL) || (accept_if_modified_since &&
if_modified_since != NULL && strncmp(if_modified_since, last_modified, strlen(last_modified)) == 0)) { if_modified_since != NULL && strncmp(if_modified_since, last_modified, strlen(last_modified)) == 0)) {
res.status = http_get_status(304); res.status = http_get_status(304);
@ -209,7 +209,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
} }
// TODO Ranges // TODO Ranges
char *accept_encoding = http_get_header_field(&req.hdr, "Accept-Encoding", HTTP_LOWER); char *accept_encoding = http_get_header_field(&req.hdr, "Accept-Encoding");
if (uri.meta->filename_comp[0] != 0 && accept_encoding != NULL && strstr(accept_encoding, "deflate") != NULL) { if (uri.meta->filename_comp[0] != 0 && accept_encoding != NULL && strstr(accept_encoding, "deflate") != NULL) {
file = fopen(uri.meta->filename_comp, "rb"); file = fopen(uri.meta->filename_comp, "rb");
if (file == NULL) { if (file == NULL) {
@ -238,7 +238,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
fastcgi_close_stdin(&php_fpm); fastcgi_close_stdin(&php_fpm);
} }
char *accept_encoding = http_get_header_field(&req.hdr, "Accept-Encoding", HTTP_LOWER); char *accept_encoding = http_get_header_field(&req.hdr, "Accept-Encoding");
if (accept_encoding != NULL && strstr(accept_encoding, "deflate") != NULL) { if (accept_encoding != NULL && strstr(accept_encoding, "deflate") != NULL) {
//http_add_header_field(&res.hdr, "Content-Encoding", "deflate"); //http_add_header_field(&res.hdr, "Content-Encoding", "deflate");
} }
@ -249,14 +249,14 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
content_length = -1; content_length = -1;
use_fastcgi = 1; use_fastcgi = 1;
if (http_get_header_field(&res.hdr, "Content-Length", HTTP_PRESERVE_UPPER) == NULL) { if (http_get_header_field(&res.hdr, "Content-Length") == NULL) {
http_add_header_field(&res.hdr, "Transfer-Encoding", "chunked"); http_add_header_field(&res.hdr, "Transfer-Encoding", "chunked");
} }
} }
respond: respond:
if (http_get_header_field(&res.hdr, "Accept-Ranges", HTTP_PRESERVE_UPPER) == NULL) { if (http_get_header_field(&res.hdr, "Accept-Ranges") == NULL) {
http_add_header_field(&res.hdr, "Accept-Ranges", "none"); http_add_header_field(&res.hdr, "Accept-Ranges", "none");
} }
if (!use_fastcgi && file == NULL && res.status->code >= 400 && res.status->code < 600) { if (!use_fastcgi && file == NULL && res.status->code >= 400 && res.status->code < 600) {
@ -271,7 +271,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
if (content_length >= 0) { if (content_length >= 0) {
sprintf(buf0, "%li", content_length); sprintf(buf0, "%li", content_length);
http_add_header_field(&res.hdr, "Content-Length", buf0); http_add_header_field(&res.hdr, "Content-Length", buf0);
} else if (http_get_header_field(&res.hdr, "Transfer-Encoding", HTTP_PRESERVE_UPPER) == NULL) { } else if (http_get_header_field(&res.hdr, "Transfer-Encoding") == NULL) {
server_keep_alive = 0; server_keep_alive = 0;
} }
if (server_keep_alive && client_keep_alive) { if (server_keep_alive && client_keep_alive) {
@ -284,7 +284,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
http_send_response(client, &res); http_send_response(client, &res);
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");
unsigned long micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000; unsigned long micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000;
print("%s%03i %s%s%s (%s)%s", http_get_status_color(res.status), res.status->code, res.status->msg, print("%s%03i %s%s%s (%s)%s", http_get_status_color(res.status), res.status->code, res.status->msg,
location != NULL ? " -> " : "", location != NULL ? location : "", format_duration(micros, buf0), CLR_STR); location != NULL ? " -> " : "", location != NULL ? location : "", format_duration(micros, buf0), CLR_STR);
@ -330,9 +330,9 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
snd_len += ret; snd_len += ret;
} }
} else if (use_fastcgi) { } else if (use_fastcgi) {
char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding", HTTP_PRESERVE_UPPER); char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
int chunked = transfer_encoding != NULL && strncmp(transfer_encoding, "chunked", 7) == 0; int chunked = transfer_encoding != NULL && strncmp(transfer_encoding, "chunked", 7) == 0;
char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding", HTTP_PRESERVE_UPPER); char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding");
int comp = content_encoding != NULL && strncmp(content_encoding, "deflate", 7) == 0; int comp = content_encoding != NULL && strncmp(content_encoding, "deflate", 7) == 0;
fastcgi_send(&php_fpm, client, (chunked ? FASTCGI_CHUNKED : 0) | (comp ? FASTCGI_COMPRESS : 0)); fastcgi_send(&php_fpm, client, (chunked ? FASTCGI_CHUNKED : 0) | (comp ? FASTCGI_COMPRESS : 0));
} }

View File

@ -100,7 +100,7 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n
param_ptr = fastcgi_add_param(param_ptr, "GATEWAY_INTERFACE", "CGI/1.1"); param_ptr = fastcgi_add_param(param_ptr, "GATEWAY_INTERFACE", "CGI/1.1");
param_ptr = fastcgi_add_param(param_ptr, "SERVER_SOFTWARE", SERVER_STR); param_ptr = fastcgi_add_param(param_ptr, "SERVER_SOFTWARE", SERVER_STR);
param_ptr = fastcgi_add_param(param_ptr, "SERVER_PROTOCOL", "HTTP/1.1"); param_ptr = fastcgi_add_param(param_ptr, "SERVER_PROTOCOL", "HTTP/1.1");
param_ptr = fastcgi_add_param(param_ptr, "SERVER_NAME", http_get_header_field(&req->hdr, "Host", HTTP_LOWER)); param_ptr = fastcgi_add_param(param_ptr, "SERVER_NAME", http_get_header_field(&req->hdr, "Host"));
if (client->enc) { if (client->enc) {
param_ptr = fastcgi_add_param(param_ptr, "HTTPS", "on"); param_ptr = fastcgi_add_param(param_ptr, "HTTPS", "on");
} }
@ -147,9 +147,9 @@ int fastcgi_init(fastcgi_conn *conn, unsigned int client_num, unsigned int req_n
param_ptr = fastcgi_add_param(param_ptr, "PATH_INFO", buf0); param_ptr = fastcgi_add_param(param_ptr, "PATH_INFO", buf0);
//param_ptr = fastcgi_add_param(param_ptr, "AUTH_TYPE", ""); //param_ptr = fastcgi_add_param(param_ptr, "AUTH_TYPE", "");
char *content_length = http_get_header_field(&req->hdr, "Content-Length", HTTP_LOWER); char *content_length = http_get_header_field(&req->hdr, "Content-Length");
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_LENGTH", content_length != NULL ? content_length : ""); param_ptr = fastcgi_add_param(param_ptr, "CONTENT_LENGTH", content_length != NULL ? content_length : "");
char *content_type = http_get_header_field(&req->hdr, "Content-Type", HTTP_LOWER); char *content_type = http_get_header_field(&req->hdr, "Content-Type");
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_TYPE", content_type != NULL ? content_type : ""); param_ptr = fastcgi_add_param(param_ptr, "CONTENT_TYPE", content_type != NULL ? content_type : "");
for (int i = 0; i < req->hdr.field_num; i++) { for (int i = 0; i < req->hdr.field_num; i++) {

View File

@ -156,18 +156,17 @@ int http_receive_request(sock *client, http_req *req) {
} }
} }
char *http_get_header_field(const http_hdr *hdr, const char *field_name, int strict) { char *http_get_header_field(const http_hdr *hdr, const char *field_name) {
size_t len = strlen(field_name); char field_name_1[256], field_name_2[256];
char *_field_name = malloc(len + 1); strcpy(field_name_1, field_name);
strcpy(_field_name, field_name); http_to_camel_case(field_name_1, HTTP_LOWER);
http_to_camel_case(_field_name, strict);
for (int i = 0; i < hdr->field_num; i++) { for (int i = 0; i < hdr->field_num; i++) {
if (strncmp(hdr->fields[i][0], _field_name, len) == 0) { strcpy(field_name_2, hdr->fields[i][0]);
free(_field_name); http_to_camel_case(field_name_2, HTTP_LOWER);
if (strcmp(field_name_1, field_name_2) == 0) {
return hdr->fields[i][1]; return hdr->fields[i][1];
} }
} }
free(_field_name);
return NULL; return NULL;
} }

View File

@ -164,7 +164,7 @@ void http_free_res(http_res *res);
int http_receive_request(sock *client, http_req *req); int http_receive_request(sock *client, http_req *req);
char *http_get_header_field(const http_hdr *hdr, const char *field_name, int strict); char *http_get_header_field(const http_hdr *hdr, const char *field_name);
void http_add_header_field(http_hdr *hdr, const char *field_name, const char *field_value); void http_add_header_field(http_hdr *hdr, const char *field_name, const char *field_value);