diff --git a/src/client.c b/src/client.c index 94b3458..21515f5 100644 --- a/src/client.c +++ b/src/client.c @@ -95,9 +95,9 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int 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; - host = http_get_header_field(&req.hdr, "Host", HTTP_LOWER); + host = http_get_header_field(&req.hdr, "Host"); if (host == NULL || strchr(host, '/') != NULL) { res.status = http_get_status(400); 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"); } - char *if_modified_since = http_get_header_field(&req.hdr, "If-Modified-Since", HTTP_LOWER); - char *if_none_match = http_get_header_field(&req.hdr, "If-None-Match", 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"); 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)) { res.status = http_get_status(304); @@ -209,7 +209,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int } // 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) { file = fopen(uri.meta->filename_comp, "rb"); if (file == NULL) { @@ -238,7 +238,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int 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) { //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; 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"); } } 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"); } 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) { sprintf(buf0, "%li", content_length); 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; } 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); 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; 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); @@ -330,9 +330,9 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int snd_len += ret; } } 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; - 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; fastcgi_send(&php_fpm, client, (chunked ? FASTCGI_CHUNKED : 0) | (comp ? FASTCGI_COMPRESS : 0)); } diff --git a/src/fastcgi.c b/src/fastcgi.c index f16bb05..18cea30 100644 --- a/src/fastcgi.c +++ b/src/fastcgi.c @@ -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, "SERVER_SOFTWARE", SERVER_STR); 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) { 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, "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 : ""); - 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 : ""); for (int i = 0; i < req->hdr.field_num; i++) { diff --git a/src/http.c b/src/http.c index 8b4d77b..e6becc2 100644 --- a/src/http.c +++ b/src/http.c @@ -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) { - size_t len = strlen(field_name); - char *_field_name = malloc(len + 1); - strcpy(_field_name, field_name); - http_to_camel_case(_field_name, strict); +char *http_get_header_field(const http_hdr *hdr, const char *field_name) { + char field_name_1[256], field_name_2[256]; + strcpy(field_name_1, field_name); + http_to_camel_case(field_name_1, HTTP_LOWER); for (int i = 0; i < hdr->field_num; i++) { - if (strncmp(hdr->fields[i][0], _field_name, len) == 0) { - free(_field_name); + strcpy(field_name_2, hdr->fields[i][0]); + http_to_camel_case(field_name_2, HTTP_LOWER); + if (strcmp(field_name_1, field_name_2) == 0) { return hdr->fields[i][1]; } } - free(_field_name); return NULL; } diff --git a/src/http.h b/src/http.h index c4e1726..d7d54bf 100644 --- a/src/http.h +++ b/src/http.h @@ -164,7 +164,7 @@ void http_free_res(http_res *res); 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);