Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gzip filter #914

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions auto/sources
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ NXT_LIB_SRCS=" \
src/nxt_router_access_log.c \
src/nxt_h1proto.c \
src/nxt_status.c \
src/nxt_http_compress.c \
src/nxt_http_request.c \
src/nxt_http_response.c \
src/nxt_http_error.c \
Expand Down
16 changes: 16 additions & 0 deletions src/nxt_conf_validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_php_options_members[];
static nxt_conf_vldt_object_t nxt_conf_vldt_php_target_members[];
static nxt_conf_vldt_object_t nxt_conf_vldt_wasm_access_members[];
static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[];
static nxt_conf_vldt_object_t nxt_conf_vldt_compress_members[];
static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[];
static nxt_conf_vldt_object_t nxt_conf_vldt_app_processes_members[];
static nxt_conf_vldt_object_t nxt_conf_vldt_app_isolation_members[];
Expand Down Expand Up @@ -697,6 +698,11 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_action_common_members[] = {
.validator = nxt_conf_vldt_object_iterator,
.u.object = nxt_conf_vldt_response_header,
},
{
.name = nxt_string("compress"),
.type = NXT_CONF_VLDT_OBJECT,
.u.members = nxt_conf_vldt_compress_members,
},

NXT_CONF_VLDT_END
};
Expand Down Expand Up @@ -1148,6 +1154,16 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_common_members[] = {
};


static nxt_conf_vldt_object_t nxt_conf_vldt_compress_members[] = {
{
.name = nxt_string("encoding"),
.type = NXT_CONF_VLDT_STRING,
},

NXT_CONF_VLDT_END
};


static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[] = {
{
.name = nxt_string("timeout"),
Expand Down
5 changes: 5 additions & 0 deletions src/nxt_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <nxt_regex.h>


typedef struct nxt_http_compress_conf_s nxt_http_compress_conf_t;


typedef enum {
NXT_HTTP_UNSET = -1,
NXT_HTTP_INVALID = 0,
Expand Down Expand Up @@ -233,6 +236,7 @@ typedef struct nxt_http_route_addr_rule_s nxt_http_route_addr_rule_t;
typedef struct {
nxt_conf_value_t *rewrite;
nxt_conf_value_t *set_headers;
nxt_conf_value_t *compress;
nxt_conf_value_t *pass;
nxt_conf_value_t *ret;
nxt_conf_value_t *location;
Expand Down Expand Up @@ -262,6 +266,7 @@ struct nxt_http_action_s {

nxt_tstr_t *rewrite;
nxt_array_t *set_headers; /* of nxt_http_field_t */
nxt_http_compress_conf_t *compress;
nxt_http_action_t *fallback;
};

Expand Down
88 changes: 88 additions & 0 deletions src/nxt_http_compress.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) Alejandro Colomar
* Copyright (C) NGINX, Inc.
*/


#include "nxt_http_compress.h"

#include <stddef.h>

#include <nxt_unit_cdefs.h>

#include "nxt_clang.h"
#include "nxt_conf.h"
#include "nxt_errno.h"
#include "nxt_http.h"
#include "nxt_list.h"
#include "nxt_main.h"
#include "nxt_mp.h"
#include "nxt_router.h"
#include "nxt_string.h"
#include "nxt_types.h"


static nxt_conf_map_t nxt_http_compress_conf[] = {
{
nxt_string("encoding"),
NXT_CONF_MAP_STR,
offsetof(nxt_http_compress_conf_t, encoding),
},
};


nxt_int_t
nxt_http_compress_init(nxt_router_conf_t *rtcf, nxt_http_action_t *action,
nxt_http_action_conf_t *acf)
{
nxt_mp_t *mp;
nxt_int_t ret;
nxt_http_compress_conf_t *conf;

mp = rtcf->mem_pool;

conf = nxt_mp_zget(mp, sizeof(nxt_http_compress_conf_t));
if (nxt_slow_path(conf == NULL)) {
return NXT_ERROR;
}

ret = nxt_conf_map_object(mp, acf->compress, nxt_http_compress_conf,
nxt_nitems(nxt_http_compress_conf), conf);
if (nxt_slow_path(ret == NXT_ERROR)) {
return NXT_ERROR;
}

if (0) {

} else {
return NXT_ERROR;
}
alejandro-colomar marked this conversation as resolved.
Show resolved Hide resolved

action->compress = conf;

return NXT_OK;
}


nxt_int_t
nxt_http_compress_append_field(nxt_task_t *task, nxt_http_request_t *r,
nxt_str_t *field, nxt_str_t *value)
{
nxt_http_field_t *f;

f = nxt_list_add(r->resp.fields);
if (nxt_slow_path(f == NULL)) {
return NXT_ERROR;
}

f->hash = 0;
f->skip = 0;
f->hopbyhop = 0;

f->name_length = field->length;
f->value_length = value->length;
f->name = field->start;
f->value = value->start;

return NXT_OK;
}
35 changes: 35 additions & 0 deletions src/nxt_http_compress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) Alejandro Colomar
* Copyright (C) NGINX, Inc.
*/

#ifndef NXT_HTTP_COMPRESS_H_INCLUDED_
#define NXT_HTTP_COMPRESS_H_INCLUDED_


#include "nxt_router.h"

#include "nxt_http.h"
#include "nxt_main.h"
#include "nxt_router.h"
#include "nxt_string.h"
#include "nxt_types.h"


struct nxt_http_compress_conf_s {
nxt_str_t encoding;

nxt_int_t (*handler)(nxt_task_t *task,
nxt_http_request_t *r,
nxt_http_compress_conf_t *conf);
};


nxt_int_t nxt_http_compress_init(nxt_router_conf_t *rtcf,
nxt_http_action_t *action, nxt_http_action_conf_t *acf);

nxt_int_t nxt_http_compress_append_field(nxt_task_t *task,
nxt_http_request_t *r, nxt_str_t *field, nxt_str_t *value);


#endif /* NXT_HTTP_COMPRESS_H_INCLUDED_ */
18 changes: 14 additions & 4 deletions src/nxt_http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <nxt_router.h>
#include <nxt_http.h>

#include "nxt_http_compress.h"
#include "nxt_http_filter.h"


Expand Down Expand Up @@ -635,10 +636,19 @@ nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r)
void
nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r)
{
u_char *p, *end, *server_string;
nxt_int_t ret;
nxt_http_field_t *server, *date, *content_length;
nxt_socket_conf_t *skcf;
u_char *p, *end, *server_string;
nxt_int_t ret;
nxt_http_field_t *server, *date, *content_length;
nxt_socket_conf_t *skcf;
nxt_http_compress_conf_t *compress;

compress = r->action->compress;
if (compress != NULL) {
ret = compress->handler(task, r, compress);
if (nxt_slow_path(ret == NXT_ERROR)) {
goto fail;
}
}

ret = nxt_http_set_headers(r);
if (nxt_slow_path(ret != NXT_OK)) {
Expand Down
13 changes: 13 additions & 0 deletions src/nxt_http_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <nxt_sockaddr.h>
#include <nxt_http_route_addr.h>
#include <nxt_regex.h>
#include "nxt_http_compress.h"


typedef enum {
Expand Down Expand Up @@ -588,6 +589,11 @@ static nxt_conf_map_t nxt_http_route_action_conf[] = {
NXT_CONF_MAP_PTR,
offsetof(nxt_http_action_conf_t, set_headers)
},
{
nxt_string("compress"),
NXT_CONF_MAP_PTR,
offsetof(nxt_http_action_conf_t, compress)
},
{
nxt_string("pass"),
NXT_CONF_MAP_PTR,
Expand Down Expand Up @@ -683,6 +689,13 @@ nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
}
}

if (acf.compress != NULL) {
ret = nxt_http_compress_init(rtcf, action, &acf);
if (nxt_slow_path(ret != NXT_OK)) {
return ret;
}
}

if (acf.ret != NULL) {
return nxt_http_return_init(rtcf, action, &acf);
}
Expand Down