Skip to content

Commit

Permalink
feat(gzip): add gzip configurable mime types inside JSON config
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuri committed Apr 10, 2018
1 parent d709cb7 commit dc0d2a5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions bproxy.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"port": 8080,
"gzip_mime_types": ["text/css", "application/javascript", "application/x-javascript"],
"proxies": [
{
"hosts": [
Expand Down
6 changes: 5 additions & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions include/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/bproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 25 additions & 9 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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");
}
}
}
9 changes: 6 additions & 3 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dc0d2a5

Please sign in to comment.