diff --git a/bproxy.json b/bproxy.json index 052e5d5..bde0767 100644 --- a/bproxy.json +++ b/bproxy.json @@ -1,5 +1,6 @@ { "port": 8080, + "gzip_mime_types": ["text/css", "application/javascript", "application/x-javascript"], "proxies": [ { "hosts": [ diff --git a/include/config.h b/include/config.h index b9ded3c..90db135 100644 --- a/include/config.h +++ b/include/config.h @@ -15,6 +15,8 @@ #include "jsmn.h" #define CONFIG_MAX_HOSTS 10 +#define CONFIG_MAX_GZIP_MIME_TYPES 20 +#define CONFIG_MAX_PROXIES 100 typedef struct proxy_config_t { @@ -27,7 +29,9 @@ typedef struct proxy_config_t typedef struct config_t { unsigned short port; - proxy_config_t *proxies[100]; + char *gzip_mime_types[CONFIG_MAX_GZIP_MIME_TYPES]; + int num_gzip_mime_types; + proxy_config_t *proxies[CONFIG_MAX_PROXIES]; int num_proxies; } config_t; diff --git a/include/http.h b/include/http.h index 01152b2..2b11cd4 100644 --- a/include/http.h +++ b/include/http.h @@ -17,6 +17,7 @@ #include "uv.h" #include "http_parser.h" #include "version.h" +#include "config.h" #define MAX_HEADERS 20 #define MAX_ELEMENT_SIZE 500 @@ -63,6 +64,7 @@ typedef struct http_response_t char headers[MAX_HEADERS][2][MAX_ELEMENT_SIZE]; char status_line[256]; boolean enable_compression; + config_t *server_config; } http_response_t; typedef struct conn_t diff --git a/src/bproxy.c b/src/bproxy.c index 8a75067..9c71e16 100644 --- a/src/bproxy.c +++ b/src/bproxy.c @@ -201,6 +201,7 @@ void proxy_connect_cb(uv_connect_t *req, int status) proxy_conn->conn->proxy_handle = req->handle; http_parser_init(&proxy_conn->response.parser, HTTP_RESPONSE); + proxy_conn->response.server_config = server->config; proxy_conn->response.parser.data = &proxy_conn->response; uv_read_start(req->handle, alloc_cb, proxy_read_cb); diff --git a/src/config.c b/src/config.c index fd99f01..da8ebb4 100644 --- a/src/config.c +++ b/src/config.c @@ -6,6 +6,7 @@ * found in the LICENSE file at https://github.com/bleenco/bproxy */ #include "config.h" +#include "log.h" char *read_file(char *path) { @@ -16,7 +17,7 @@ char *read_file(char *path) if (!f) { - fprintf(stderr, "could not open file %s for reading!\n", path); + log_error("could not open file %s for reading!", path); exit(1); } @@ -47,14 +48,14 @@ void parse_config(const char *json_string, config_t *config) int r = jsmn_parse(&p, json_string, strlen(json_string), t, sizeof(t) / sizeof(t[0])); if (r < 0) { - fprintf(stderr, "failed to parse config JSON: %d\n", r); + log_error("failed to parse config JSON: %d", r); exit(1); } // assume top-level element is an object if (r < 1 || t[0].type != JSMN_OBJECT) { - fprintf(stderr, "wrong configuration!\n"); + log_error("wrong JSON configuration!"); exit(1); } @@ -70,11 +71,30 @@ void parse_config(const char *json_string, config_t *config) free(port); i++; } + else if (jsoneq(json_string, &t[i], "gzip_mime_types") == 0) + { + if (t[i + 1].type != JSMN_ARRAY) + { + log_error("wrong configuration, gzip_mime_types should be an array!"); + exit(1); + } + + config->num_gzip_mime_types = 0; + for (int j = 0; j < t[i + 1].size; j++) + { + jsmntok_t *mime_type = &t[i + j + 2]; + char *mime = malloc(50); + sprintf(mime, "%.*s", mime_type->end - mime_type->start, json_string + mime_type->start); + config->gzip_mime_types[config->num_gzip_mime_types] = malloc(sizeof(char) * 50); + memcpy(config->gzip_mime_types[config->num_gzip_mime_types], mime, strlen(mime)); + config->num_gzip_mime_types++; + } + } else if (jsoneq(json_string, &t[i], "proxies") == 0) { if (t[i + 1].type != JSMN_ARRAY) { - fprintf(stderr, "wrong configuration, proxies should be array!\n"); + log_error("wrong configuration, proxies should be an array!"); exit(1); } @@ -88,7 +108,7 @@ void parse_config(const char *json_string, config_t *config) int rr = jsmn_parse(&pp, proxy_str, strlen(proxy_str), token, sizeof(token) / sizeof(token[0])); if (rr < 0) { - fprintf(stderr, "failed to parse JSON (proxies): %d\n", rr); + log_error("failed to parse JSON (proxies): %d", rr); exit(1); } @@ -143,9 +163,5 @@ void parse_config(const char *json_string, config_t *config) free(proxy_str); return; } - else if (jsoneq(json_string, &t[i], "gzip-types") == 0) - { - printf("yeey"); - } } } diff --git a/src/http.c b/src/http.c index aea6cdd..df42f1e 100644 --- a/src/http.c +++ b/src/http.c @@ -238,10 +238,13 @@ int response_headers_complete_cb(http_parser *p) { if (strcasecmp(response->headers[i][0], "Content-Type") == 0) { - // TODO: allow multiple types - if (strstr(response->headers[i][1], "text/") != NULL) + for (int j = 0; j < response->server_config->num_gzip_mime_types; j++) { - response->enable_compression = true; + if (strstr(response->headers[i][1], response->server_config->gzip_mime_types[j])) + { + response->enable_compression = true; + continue; + } } } else if (strcasecmp(response->headers[i][0], "Content-Encoding") == 0)