summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_http_compress_gzip.c
blob: db675ecf227e14f1f5ddae49f606af76789c5cad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (C) Alejandro Colomar
 * Copyright (C) NGINX, Inc.
 */


#include "nxt_http_compress_gzip.h"

#include <stddef.h>
#include <stdint.h>

#include <nxt_unit_cdefs.h>

#include "nxt_buf.h"
#include "nxt_clang.h"
#include "nxt_errno.h"
#include "nxt_http.h"
#include "nxt_http_compress.h"
#include "nxt_http_filter.h"
#include "nxt_main.h"
#include "nxt_mp.h"
#include "nxt_router.h"
#include "nxt_string.h"
#include "nxt_types.h"

#if (NXT_WITH_ZLIB || __has_include(<zlib.h>))
#include <zlib.h>
#endif


typedef struct nxt_http_compress_gzip_ctx_s  nxt_http_compress_gzip_ctx_t;


struct nxt_http_compress_gzip_ctx_s {
    nxt_http_request_t  *r;
    nxt_buf_t           *b;

    int8_t              level;

#if (NXT_WITH_ZLIB)
    z_stream            z;
#endif
};


static nxt_http_compress_gzip_ctx_t *nxt_http_compress_gzip_ctx(
    nxt_task_t *task, nxt_http_request_t *r, nxt_http_compress_conf_t *conf);

static void nxt_http_compress_gzip_filter(nxt_task_t *task, void *obj,
    void *data);


nxt_int_t
nxt_http_compress_gzip(nxt_task_t *task, nxt_http_request_t *r,
    nxt_http_compress_conf_t *conf)
{
    nxt_int_t                     ret;
    nxt_http_compress_gzip_ctx_t  *ctx;

    static nxt_str_t  ce = nxt_string("Content-Encoding");
    static nxt_str_t  gzip = nxt_string("gzip");

    if (r->body_handler == NULL
        || r->resp.content_length_n == 0
        || (r->resp.content_length != NULL
            && r->resp.content_length->value_length == 1
            && r->resp.content_length->value[0] == '0'))
    {
        return NXT_OK;
    }

    r->resp.content_length = NULL;
    r->resp.content_length_n = -1;

    ret = nxt_http_compress_append_field(task, r, &ce, &gzip);
    if (nxt_slow_path(ret == NXT_ERROR)) {
        return NXT_ERROR;
    }

    ctx = nxt_http_compress_gzip_ctx(task, r, conf);
    if (nxt_slow_path(ctx == NULL)) {
        return NXT_ERROR;
    }

    ret = nxt_http_filter_handler_add(r, nxt_http_compress_gzip_filter, ctx);
    if (nxt_slow_path(ret == NXT_ERROR)) {
        return NXT_ERROR;
    }

    return NXT_OK;
}


static nxt_http_compress_gzip_ctx_t *
nxt_http_compress_gzip_ctx(nxt_task_t *task, nxt_http_request_t *r,
    nxt_http_compress_conf_t *conf)
{
#if (!NXT_WITH_ZLIB)
    return NULL;
#else
    int                           ret;
    z_stream                      *z;
    nxt_http_compress_gzip_ctx_t  *ctx;

    ctx = nxt_mp_zget(r->mem_pool, sizeof(nxt_http_compress_gzip_ctx_t));
    if (nxt_slow_path(ctx == NULL)) {
        return NULL;
    }

    ctx->level = conf->level;
    ctx->r = r;

    z = &ctx->z;
    z->zalloc = NULL;
    z->zfree = NULL;
    z->opaque = NULL;
    ret = deflateInit2(z, ctx->level, Z_DEFLATED, MAX_WBITS + 16, MAX_MEM_LEVEL,
                       Z_DEFAULT_STRATEGY);
    if (nxt_slow_path(ret != 0)) {
        return NULL;
    }

    return ctx;
#endif
}


static void
nxt_http_compress_gzip_filter(nxt_task_t *task, void *obj, void *data)
{
#if (NXT_WITH_ZLIB)
    int                           ret;
    ssize_t                       size;
    z_stream                      *z;
    nxt_buf_t                     **b, *tmp;
    nxt_bool_t                    is_last;
    nxt_http_request_t            *r;
    nxt_http_compress_gzip_ctx_t  *ctx;

    b = obj;
    ctx = data;
    z = &ctx->z;

    r = ctx->r;

    is_last = ((*b)->next != NULL
               && (*b)->next->completion_handler != (*b)->completion_handler);

    z->next_in = (*b)->mem.pos;
    z->avail_in = (*b)->mem.free - (*b)->mem.pos;

    size = deflateBound(z, z->avail_in);

    tmp = nxt_buf_mem_alloc(r->mem_pool, size, 0);
    if (nxt_slow_path(tmp == NULL)) {
        return;
    }

    nxt_memcpy(tmp, *b, offsetof(nxt_buf_t, mem));
    tmp->data = r->mem_pool;

    z->next_out = tmp->mem.start;
    z->avail_out = tmp->mem.end - tmp->mem.start;

    ret = deflate(z, is_last ? Z_FINISH : Z_SYNC_FLUSH);
    if (nxt_slow_path(ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR)) {
        goto fail;
    }

    tmp->mem.free = tmp->mem.end - z->avail_out;
    size = tmp->mem.free - tmp->mem.start;

    if ((*b)->mem.end - (*b)->mem.pos >= size) {
        (*b)->mem.free = nxt_cpymem((*b)->mem.pos, tmp->mem.start, size);

    } else {
        nxt_swap(b, &tmp);
    }

fail:

    nxt_mp_free(tmp->data, tmp);

    if (is_last) {
        ret = deflateEnd(z);
        if (ret != Z_OK) {
            return;
        }
    }

    return;
#endif
}