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 all commits
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
4 changes: 4 additions & 0 deletions auto/make
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ libunit-install: $NXT_BUILD_DIR/lib/$NXT_LIB_UNIT_STATIC
test -d \$(DESTDIR)$NXT_INCLUDEDIR \
|| install -d \$(DESTDIR)$NXT_INCLUDEDIR
install -p -m u=rw,go=r src/nxt_unit.h \
src/nxt_unit_bit.h \
src/nxt_unit_cdefs.h \
src/nxt_unit_field.h \
src/nxt_unit_request.h \
src/nxt_unit_response.h \
Expand All @@ -423,6 +425,8 @@ libunit-uninstall:
rm -f \$(DESTDIR)$NXT_PKGCONFIGDIR/unit.pc
@rmdir -p \$(DESTDIR)$NXT_PKGCONFIGDIR 2>/dev/null || true
rm -f \$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit.h \
\$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_bit.h \
\$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_cdefs.h \
\$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_field.h \
\$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_request.h \
\$(DESTDIR)$NXT_INCLUDEDIR/nxt_unit_response.h \
Expand Down
4 changes: 4 additions & 0 deletions auto/options
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ NXT_PCRE_LIB=
NXT_REGEX=YES
NXT_TRY_PCRE2=YES

NXT_ZLIB=YES

NXT_TLS=NO
NXT_OPENSSL=NO
NXT_GNUTLS=NO
Expand Down Expand Up @@ -104,6 +106,8 @@ do
--no-regex) NXT_REGEX=NO ;;
--no-pcre2) NXT_TRY_PCRE2=NO ;;

--no-zlib) NXT_ZLIB=NO ;;

--openssl) NXT_OPENSSL=YES ;;
--gnutls) NXT_GNUTLS=YES ;;
--cyassl) NXT_CYASSL=YES ;;
Expand Down
2 changes: 2 additions & 0 deletions auto/sources
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ 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_compress_gzip.c \
src/nxt_http_request.c \
src/nxt_http_response.c \
src/nxt_http_error.c \
Expand Down
1 change: 1 addition & 0 deletions auto/summary
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Unit configuration summary:
Unix domain sockets support: $NXT_UNIX_DOMAIN
TLS support: ............... $NXT_OPENSSL
Regex support: ............. $NXT_REGEX
zlib support: .............. $NXT_ZLIB
NJS support: ............... $NXT_NJS

process isolation: ......... $NXT_ISOLATION
Expand Down
34 changes: 34 additions & 0 deletions auto/zlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

# Copyright (C) Alejandro Colomar
# Copyright (C) NGINX, Inc.


NXT_HAVE_ZLIB=no
NXT_ZLIB_CFLAGS=
NXT_ZLIB_LIBS=


if [ $NXT_ZLIB = YES ]; then

NXT_ZLIB_CFLAGS="$(pkgconf --cflags-only-I zlib 2>/dev/null || echo "")"
NXT_ZLIB_LIBS="$(pkgconf --libs zlib 2>/dev/null || echo "-lz")"

nxt_feature="zlib"
nxt_feature_name=NXT_HAVE_ZLIB
nxt_feature_run=no
nxt_feature_incs=$NXT_ZLIB_CFLAGS
nxt_feature_libs=$NXT_ZLIB_LIBS
nxt_feature_test="#include <stdio.h>

#include <zlib.h>

int main(void) {
puts(zlibVersion());
return 0;
}"
. auto/feature

if [ $nxt_found = yes ]; then
NXT_HAVE_ZLIB=YES
fi
fi
5 changes: 3 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ NXT_LIBRT=
. auto/unix
. auto/os/conf
. auto/ssltls
. auto/zlib

if [ $NXT_REGEX = YES ]; then
. auto/pcre
Expand Down Expand Up @@ -168,11 +169,11 @@ END

NXT_LIB_AUX_CFLAGS="$NXT_OPENSSL_CFLAGS $NXT_GNUTLS_CFLAGS \\
$NXT_CYASSL_CFLAGS $NXT_POLARSSL_CFLAGS \\
$NXT_PCRE_CFLAGS"
$NXT_PCRE_CFLAGS $NXT_ZLIB_CFLAGS"

NXT_LIB_AUX_LIBS="$NXT_OPENSSL_LIBS $NXT_GNUTLS_LIBS \\
$NXT_CYASSL_LIBS $NXT_POLARSSL_LIBS \\
$NXT_PCRE_LIB"
$NXT_PCRE_LIB $NXT_ZLIB_LIBS"

if [ $NXT_NJS != NO ]; then
. auto/njs
Expand Down
17 changes: 4 additions & 13 deletions src/nxt_clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#define _NXT_CLANG_H_INCLUDED_


#include <nxt_unit_cdefs.h>


#define nxt_inline static inline __attribute__((always_inline))
#define nxt_noinline __attribute__((noinline))
#define nxt_cdecl
Expand Down Expand Up @@ -210,24 +213,12 @@ nxt_popcount(unsigned int x)
*(type *) ((u_char *) p + offset)


#define nxt_nitems(x) \
(sizeof(x) / sizeof((x)[0]))


/* GCC and Clang use __builtin_abs() instead of libc abs(). */

#define nxt_abs(val) \
abs(val)


#define nxt_max(val1, val2) \
((val1 < val2) ? (val2) : (val1))


#define nxt_min(val1, val2) \
((val1 > val2) ? (val2) : (val1))


#define nxt_bswap32(val) \
( ((val) >> 24) \
| (((val) & 0x00FF0000) >> 8) \
Expand All @@ -252,7 +243,7 @@ nxt_popcount(unsigned int x)


#define nxt_length(s) \
(sizeof(s) - 1)
(sizeof(s) - 1 + nxt_must_be_array(s))


#endif /* _NXT_CLANG_H_INCLUDED_ */
26 changes: 26 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,26 @@ 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,
}, {
.name = nxt_string("level"),
.type = NXT_CONF_VLDT_INTEGER,
}, {
.name = nxt_string("min_length"),
.type = NXT_CONF_VLDT_INTEGER,
}, {
.name = nxt_string("mime_types"),
.type = NXT_CONF_VLDT_STRING | NXT_CONF_VLDT_ARRAY,
.validator = nxt_conf_vldt_match_patterns,
},

NXT_CONF_VLDT_END
};


static nxt_conf_vldt_object_t nxt_conf_vldt_app_limits_members[] = {
{
.name = nxt_string("timeout"),
Expand Down
12 changes: 5 additions & 7 deletions src/nxt_h1proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ static void nxt_h1p_request_body_read(nxt_task_t *task, nxt_http_request_t *r);
static void nxt_h1p_conn_request_body_read(nxt_task_t *task, void *obj,
void *data);
static void nxt_h1p_request_local_addr(nxt_task_t *task, nxt_http_request_t *r);
static void nxt_h1p_request_header_send(nxt_task_t *task,
nxt_http_request_t *r, nxt_work_handler_t body_handler, void *data);
static void nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r);
static void nxt_h1p_request_send(nxt_task_t *task, nxt_http_request_t *r,
nxt_buf_t *out);
static nxt_buf_t *nxt_h1p_chunk_create(nxt_task_t *task, nxt_http_request_t *r,
Expand Down Expand Up @@ -1204,8 +1203,7 @@ static const nxt_str_t nxt_http_server_error[] = {
#define UNKNOWN_STATUS_LENGTH nxt_length("HTTP/1.1 999 \r\n")

static void
nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
nxt_work_handler_t body_handler, void *data)
nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r)
{
u_char *p;
size_t size;
Expand Down Expand Up @@ -1291,7 +1289,7 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
if (http11) {
if (n != NXT_HTTP_NOT_MODIFIED
&& n != NXT_HTTP_NO_CONTENT
&& body_handler != NULL
&& r->body_handler != NULL
&& !h1p->websocket)
{
h1p->chunked = 1;
Expand Down Expand Up @@ -1379,14 +1377,14 @@ nxt_h1p_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
h1p->conn_write_tail = &header->next;
c->write_state = &nxt_h1p_request_send_state;

if (body_handler != NULL) {
if (r->body_handler != NULL) {
/*
* The body handler will run before c->io->write() handler,
* because the latter was inqueued by nxt_conn_write()
* in engine->write_work_queue.
*/
nxt_work_queue_add(&task->thread->engine->fast_work_queue,
body_handler, task, r, data);
r->body_handler, task, r, r->body_handler_data);

} else {
header->next = nxt_http_buf_last(r);
Expand Down
17 changes: 13 additions & 4 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 @@ -110,6 +113,7 @@ typedef struct {
nxt_http_field_t *content_type;
nxt_http_field_t *content_length;
nxt_off_t content_length_n;
const nxt_str_t *mtype;
} nxt_http_response_t;


Expand Down Expand Up @@ -140,6 +144,10 @@ struct nxt_http_request_s {
nxt_buf_t *out;
const nxt_http_request_state_t *state;

nxt_work_handler_t body_handler;
void *body_handler_data;
nxt_list_t *response_filters;

nxt_nsec_t start_time;

nxt_str_t host;
Expand Down Expand Up @@ -229,6 +237,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 @@ -258,15 +267,15 @@ 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;
};


typedef struct {
void (*body_read)(nxt_task_t *task, nxt_http_request_t *r);
void (*local_addr)(nxt_task_t *task, nxt_http_request_t *r);
void (*header_send)(nxt_task_t *task, nxt_http_request_t *r,
nxt_work_handler_t body_handler, void *data);
void (*header_send)(nxt_task_t *task, nxt_http_request_t *r);
void (*send)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out);
nxt_off_t (*body_bytes_sent)(nxt_task_t *task, nxt_http_proto_t proto);
void (*discard)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *last);
Expand Down Expand Up @@ -326,8 +335,7 @@ nxt_http_request_t *nxt_http_request_create(nxt_task_t *task);
void nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r,
nxt_http_status_t status);
void 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,
nxt_work_handler_t body_handler, void *data);
void nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r);
void nxt_http_request_ws_frame_start(nxt_task_t *task, nxt_http_request_t *r,
nxt_buf_t *ws_frame);
void nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r,
Expand Down Expand Up @@ -438,4 +446,5 @@ nxt_msec_t nxt_h1p_conn_request_timer_value(nxt_conn_t *c, uintptr_t data);

extern const nxt_conn_state_t nxt_h1p_idle_close_state;


#endif /* _NXT_HTTP_H_INCLUDED_ */
Loading