Skip to content

Commit dc0d2a5

Browse files
committed
feat(gzip): add gzip configurable mime types inside JSON config
1 parent d709cb7 commit dc0d2a5

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

bproxy.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"port": 8080,
3+
"gzip_mime_types": ["text/css", "application/javascript", "application/x-javascript"],
34
"proxies": [
45
{
56
"hosts": [

include/config.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "jsmn.h"
1616

1717
#define CONFIG_MAX_HOSTS 10
18+
#define CONFIG_MAX_GZIP_MIME_TYPES 20
19+
#define CONFIG_MAX_PROXIES 100
1820

1921
typedef struct proxy_config_t
2022
{
@@ -27,7 +29,9 @@ typedef struct proxy_config_t
2729
typedef struct config_t
2830
{
2931
unsigned short port;
30-
proxy_config_t *proxies[100];
32+
char *gzip_mime_types[CONFIG_MAX_GZIP_MIME_TYPES];
33+
int num_gzip_mime_types;
34+
proxy_config_t *proxies[CONFIG_MAX_PROXIES];
3135
int num_proxies;
3236
} config_t;
3337

include/http.h

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "uv.h"
1818
#include "http_parser.h"
1919
#include "version.h"
20+
#include "config.h"
2021

2122
#define MAX_HEADERS 20
2223
#define MAX_ELEMENT_SIZE 500
@@ -63,6 +64,7 @@ typedef struct http_response_t
6364
char headers[MAX_HEADERS][2][MAX_ELEMENT_SIZE];
6465
char status_line[256];
6566
boolean enable_compression;
67+
config_t *server_config;
6668
} http_response_t;
6769

6870
typedef struct conn_t

src/bproxy.c

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ void proxy_connect_cb(uv_connect_t *req, int status)
201201
proxy_conn->conn->proxy_handle = req->handle;
202202

203203
http_parser_init(&proxy_conn->response.parser, HTTP_RESPONSE);
204+
proxy_conn->response.server_config = server->config;
204205
proxy_conn->response.parser.data = &proxy_conn->response;
205206

206207
uv_read_start(req->handle, alloc_cb, proxy_read_cb);

src/config.c

+25-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://github.com/bleenco/bproxy
77
*/
88
#include "config.h"
9+
#include "log.h"
910

1011
char *read_file(char *path)
1112
{
@@ -16,7 +17,7 @@ char *read_file(char *path)
1617

1718
if (!f)
1819
{
19-
fprintf(stderr, "could not open file %s for reading!\n", path);
20+
log_error("could not open file %s for reading!", path);
2021
exit(1);
2122
}
2223

@@ -47,14 +48,14 @@ void parse_config(const char *json_string, config_t *config)
4748
int r = jsmn_parse(&p, json_string, strlen(json_string), t, sizeof(t) / sizeof(t[0]));
4849
if (r < 0)
4950
{
50-
fprintf(stderr, "failed to parse config JSON: %d\n", r);
51+
log_error("failed to parse config JSON: %d", r);
5152
exit(1);
5253
}
5354

5455
// assume top-level element is an object
5556
if (r < 1 || t[0].type != JSMN_OBJECT)
5657
{
57-
fprintf(stderr, "wrong configuration!\n");
58+
log_error("wrong JSON configuration!");
5859
exit(1);
5960
}
6061

@@ -70,11 +71,30 @@ void parse_config(const char *json_string, config_t *config)
7071
free(port);
7172
i++;
7273
}
74+
else if (jsoneq(json_string, &t[i], "gzip_mime_types") == 0)
75+
{
76+
if (t[i + 1].type != JSMN_ARRAY)
77+
{
78+
log_error("wrong configuration, gzip_mime_types should be an array!");
79+
exit(1);
80+
}
81+
82+
config->num_gzip_mime_types = 0;
83+
for (int j = 0; j < t[i + 1].size; j++)
84+
{
85+
jsmntok_t *mime_type = &t[i + j + 2];
86+
char *mime = malloc(50);
87+
sprintf(mime, "%.*s", mime_type->end - mime_type->start, json_string + mime_type->start);
88+
config->gzip_mime_types[config->num_gzip_mime_types] = malloc(sizeof(char) * 50);
89+
memcpy(config->gzip_mime_types[config->num_gzip_mime_types], mime, strlen(mime));
90+
config->num_gzip_mime_types++;
91+
}
92+
}
7393
else if (jsoneq(json_string, &t[i], "proxies") == 0)
7494
{
7595
if (t[i + 1].type != JSMN_ARRAY)
7696
{
77-
fprintf(stderr, "wrong configuration, proxies should be array!\n");
97+
log_error("wrong configuration, proxies should be an array!");
7898
exit(1);
7999
}
80100

@@ -88,7 +108,7 @@ void parse_config(const char *json_string, config_t *config)
88108
int rr = jsmn_parse(&pp, proxy_str, strlen(proxy_str), token, sizeof(token) / sizeof(token[0]));
89109
if (rr < 0)
90110
{
91-
fprintf(stderr, "failed to parse JSON (proxies): %d\n", rr);
111+
log_error("failed to parse JSON (proxies): %d", rr);
92112
exit(1);
93113
}
94114

@@ -143,9 +163,5 @@ void parse_config(const char *json_string, config_t *config)
143163
free(proxy_str);
144164
return;
145165
}
146-
else if (jsoneq(json_string, &t[i], "gzip-types") == 0)
147-
{
148-
printf("yeey");
149-
}
150166
}
151167
}

src/http.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,13 @@ int response_headers_complete_cb(http_parser *p)
238238
{
239239
if (strcasecmp(response->headers[i][0], "Content-Type") == 0)
240240
{
241-
// TODO: allow multiple types
242-
if (strstr(response->headers[i][1], "text/") != NULL)
241+
for (int j = 0; j < response->server_config->num_gzip_mime_types; j++)
243242
{
244-
response->enable_compression = true;
243+
if (strstr(response->headers[i][1], response->server_config->gzip_mime_types[j]))
244+
{
245+
response->enable_compression = true;
246+
continue;
247+
}
245248
}
246249
}
247250
else if (strcasecmp(response->headers[i][0], "Content-Encoding") == 0)

0 commit comments

Comments
 (0)