diff --git a/src/client.c b/src/client.c index f999054..9e666e8 100644 --- a/src/client.c +++ b/src/client.c @@ -498,9 +498,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int const char *content_type = http_get_header_field(&res.hdr, "Content-Type"); const char *content_length_f = http_get_header_field(&res.hdr, "Content-Length"); const char *content_encoding = http_get_header_field(&res.hdr, "Content-Encoding"); - if (content_encoding == NULL && content_type != NULL && content_length_f != NULL && - strncmp(content_type, "text/html", 9) == 0) - { + if (content_encoding == NULL && content_type != NULL && content_length_f != NULL && strncmp(content_type, "text/html", 9) == 0) { long content_len = strtol(content_length_f, NULL, 10); if (content_len <= sizeof(msg_content) - 1) { ctx.status = res.status->code; diff --git a/src/lib/http.c b/src/lib/http.c index a73cb3f..a4c60ce 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -48,22 +48,25 @@ const char *http_field_get_value(const http_field *field) { return NULL; } +void http_free_field(http_field *f) { + if (f->type == HTTP_FIELD_NORMAL) { + f->normal.name[0] = 0; + f->normal.value[0] = 0; + } else if (f->type == HTTP_FIELD_EX_VALUE) { + f->ex_value.name[0] = 0; + free(f->ex_value.value); + f->ex_value.value = NULL; + } else if (f->type == HTTP_FIELD_EX_NAME) { + free(f->ex_name.name); + free(f->ex_name.value); + f->ex_name.name = NULL; + f->ex_name.value = NULL; + } +} + void http_free_hdr(http_hdr *hdr) { for (int i = 0; i < hdr->field_num; i++) { - http_field *f = &hdr->fields[i]; - if (f->type == HTTP_FIELD_NORMAL) { - f->normal.name[0] = 0; - f->normal.value[0] = 0; - } else if (f->type == HTTP_FIELD_EX_VALUE) { - f->ex_value.name[0] = 0; - free(f->ex_value.value); - f->ex_value.value = NULL; - } else if (f->type == HTTP_FIELD_EX_NAME) { - free(f->ex_name.name); - free(f->ex_name.value); - f->ex_name.name = NULL; - f->ex_name.value = NULL; - } + http_free_field(&hdr->fields[i]); } hdr->field_num = 0; hdr->last_field_num = -1; @@ -326,6 +329,7 @@ void http_remove_header_field(http_hdr *hdr, const char *field_name, int mode) { strcpy(field_name_2, http_field_get_name(&hdr->fields[i])); http_to_camel_case(field_name_2, HTTP_LOWER); if (strcmp(field_name_1, field_name_2) == 0) { + http_free_field(&hdr->fields[i]); memmove(&hdr->fields[i], &hdr->fields[i + 1], sizeof(hdr->fields[0]) * (hdr->field_num - i)); hdr->field_num--; if (mode == HTTP_REMOVE_ALL) { diff --git a/src/lib/http.h b/src/lib/http.h index c624396..4ce1ff7 100644 --- a/src/lib/http.h +++ b/src/lib/http.h @@ -130,6 +130,8 @@ const char *http_field_get_name(const http_field *field); const char *http_field_get_value(const http_field *field); +void http_free_field(http_field *f); + void http_free_hdr(http_hdr *hdr); void http_free_req(http_req *req); diff --git a/src/lib/rev_proxy.c b/src/lib/rev_proxy.c index 45e1171..7ab8259 100644 --- a/src/lib/rev_proxy.c +++ b/src/lib/rev_proxy.c @@ -29,8 +29,7 @@ int rev_proxy_preload() { } int rev_proxy_request_header(http_req *req, int enc) { - char buf1[256]; - char buf2[256]; + char buf1[256], buf2[256]; int p_len; http_remove_header_field(&req->hdr, "Connection", HTTP_REMOVE_ALL); http_add_header_field(&req->hdr, "Connection", "keep-alive"); @@ -52,7 +51,7 @@ int rev_proxy_request_header(http_req *req, int enc) { const char *host = http_get_header_field(&req->hdr, "Host"); const char *forwarded = http_get_header_field(&req->hdr, "Forwarded"); int client_ipv6 = strchr(client_addr_str, ':') != NULL; - int server_ipv6 = strchr(server_addr_str, ':') != NULL; + int server_ipv6 = strchr(server_addr_str, ':') != NULL; p_len = snprintf(buf1, sizeof(buf1), "by=%s%s%s;for=%s%s%s;host=%s;proto=%s", server_ipv6 ? "\"[" : "", server_addr_str, server_ipv6 ? "]\"" : "", @@ -62,6 +61,7 @@ int rev_proxy_request_header(http_req *req, int enc) { print(ERR_STR "Appended part of header field 'Forwarded' too long" CLR_STR); return -1; } + if (forwarded == NULL) { http_add_header_field(&req->hdr, "Forwarded", buf1); } else { @@ -129,8 +129,7 @@ int rev_proxy_request_header(http_req *req, int enc) { } int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) { - char buf1[256]; - char buf2[256]; + char buf1[256], buf2[256]; int p_len; const char *via = http_get_header_field(&res->hdr, "Via"); @@ -172,20 +171,19 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) { if (0) { match: + strcpy(buf1, location + p_len - 1); http_remove_header_field(&res->hdr, "Location", HTTP_REMOVE_ALL); - http_add_header_field(&res->hdr, "Location", location + p_len - 1); + http_add_header_field(&res->hdr, "Location", buf1); } } return 0; } -int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config *conf, sock *client, - http_status *custom_status, char *err_msg) { +int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config *conf, sock *client, http_status *custom_status, char *err_msg) { char buffer[CHUNK_SIZE]; long ret; - int tries = 0; - int retry = 0; + int tries = 0, retry = 0; if (rev_proxy.socket != 0 && strcmp(rev_proxy_host, conf->name) == 0 && sock_check(&rev_proxy) == 0) { goto rev_proxy; @@ -455,13 +453,9 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) { // TODO handle websockets - long ret = 0; - char buffer[CHUNK_SIZE]; - char comp_out[CHUNK_SIZE]; - char buf[256]; - long len, snd_len; + char buffer[CHUNK_SIZE], comp_out[CHUNK_SIZE], buf[256], *ptr; + long ret = 0, len, snd_len; int finish_comp = 0; - char *ptr; compress_ctx comp_ctx; if (flags & REV_PROXY_COMPRESS_BR) {