Skip to content

Commit 2fd2cd6

Browse files
http: Compress application responses
Co-authored-by: Alejandro Colomar <alx@kernel.org> Signed-off-by: Alejandro Colomar <alx@kernel.org> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
1 parent 7671844 commit 2fd2cd6

4 files changed

+101
-1
lines changed

src/nxt_http_compression.c

+86
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,62 @@ static void print_comp_config(size_t n)
141141
}
142142
}
143143

144+
nxt_int_t
145+
nxt_http_comp_compress_app_response(nxt_http_request_t *r)
146+
{
147+
bool last;
148+
size_t buf_len, in_len;
149+
ssize_t cbytes;
150+
nxt_buf_t *buf, *b = r->out;
151+
nxt_http_comp_ctx_t *ctx = &compressor_ctx;
152+
nxt_http_comp_compressor_t *compressor;
153+
const nxt_http_comp_operations_t *cops;
154+
155+
printf("%s: \n", __func__);
156+
157+
if (ctx->idx == NXT_HTTP_COMP_SCHEME_IDENTITY) {
158+
printf("%s: NXT_HTTP_COMP_SCHEME_IDENTITY [skipping/identity]\n",
159+
__func__);
160+
return NXT_OK;
161+
}
162+
163+
if (b->mem.pos == NULL) {
164+
return NXT_OK;
165+
}
166+
167+
compressor = &enabled_compressors[ctx->idx];
168+
169+
in_len = b->mem.free - b->mem.pos;
170+
171+
last = !b->next || b->next->is_last == 1;
172+
173+
cops = compressor->type->cops;
174+
175+
buf_len = cops->bound(&ctx->ctx, in_len);
176+
177+
buf = nxt_buf_mem_alloc(r->mem_pool, buf_len, 0);
178+
if (nxt_slow_path(buf == NULL)) {
179+
return NXT_ERROR;
180+
}
181+
182+
nxt_memcpy(buf, b, offsetof(nxt_buf_t, mem));
183+
buf->data = r->mem_pool;
184+
185+
cbytes = cops->deflate(&ctx->ctx, b->mem.pos, in_len, buf->mem.start,
186+
buf->mem.end - buf->mem.start, last);
187+
printf("%s: cbytes = %ld\n", __func__, cbytes);
188+
if (cbytes == -1) {
189+
return NXT_ERROR;
190+
}
191+
192+
// if (cbytes != -1) {
193+
// b->mem.free = nxt_cpymem(b->mem.pos, tmp->mem.start, cbytes);
194+
// }
195+
b = buf;
196+
197+
return NXT_OK;
198+
}
199+
144200
bool
145201
nxt_http_comp_wants_compression(void)
146202
{
@@ -287,6 +343,14 @@ nxt_http_comp_set_header(nxt_http_request_t *r, nxt_uint_t comp_idx)
287343
static const nxt_str_t content_encoding_str =
288344
nxt_string("Content-Encoding");
289345

346+
printf("%s: \n", __func__);
347+
348+
#if 0
349+
if (comp_idx == NXT_HTTP_COMP_SCHEME_IDENTITY) {
350+
return NXT_OK;
351+
}
352+
#endif
353+
290354
f = nxt_list_add(r->resp.fields);
291355
if (nxt_slow_path(f == NULL)) {
292356
return NXT_ERROR;
@@ -301,6 +365,28 @@ nxt_http_comp_set_header(nxt_http_request_t *r, nxt_uint_t comp_idx)
301365
f->value = token->start;
302366
f->value_length = token->length;
303367

368+
r->resp.content_length = NULL;
369+
r->resp.content_length_n = -1;
370+
371+
if (r->resp.mime_type == NULL) {
372+
nxt_http_field_t *f;
373+
374+
/*
375+
* As per RFC 2616 section 4.4 item 3, you should not send
376+
* Content-Length when a Transfer-Encoding header is present.
377+
*/
378+
nxt_list_each(f, r->resp.fields) {
379+
if (nxt_strcasecmp(f->name,
380+
(const u_char *)"Content-Length") == 0)
381+
{
382+
printf("%s: Found (%s: %s), marking as 'skip'\n", __func__,
383+
f->name, f->value);
384+
f->skip = true;
385+
break;
386+
}
387+
} nxt_list_loop;
388+
}
389+
304390
return NXT_OK;
305391
}
306392

src/nxt_http_compression.h

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extern const nxt_http_comp_operations_t nxt_comp_brotli_ops;
8585
#endif
8686

8787

88+
extern nxt_int_t nxt_http_comp_compress_app_response(nxt_http_request_t *r);
8889
extern bool nxt_http_comp_wants_compression(void);
8990
extern size_t nxt_http_comp_bound(size_t size);
9091
extern ssize_t nxt_http_comp_compress(uint8_t *dst, size_t dst_size,

src/nxt_port_queue.h

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ nxt_port_queue_recv(nxt_port_queue_t volatile *q, void *p)
8181
nxt_nncq_atomic_t i;
8282
nxt_port_queue_item_t *qi;
8383

84+
// printf("%s \n", __func__);
85+
8486
i = nxt_nncq_dequeue(&q->queue);
8587
if (i == nxt_nncq_empty(&q->queue)) {
8688
return -1;

src/nxt_router.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -4136,6 +4136,8 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
41364136
nxt_unit_response_t *resp;
41374137
nxt_request_rpc_data_t *req_rpc_data;
41384138

4139+
printf("%s: \n", __func__);
4140+
41394141
req_rpc_data = data;
41404142

41414143
r = req_rpc_data->request;
@@ -4191,8 +4193,11 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
41914193

41924194
if (r->header_sent) {
41934195
nxt_buf_chain_add(&r->out, b);
4194-
nxt_http_request_send_body(task, r, NULL);
41954196

4197+
/* XXX Do compression here */
4198+
nxt_http_comp_compress_app_response(r);
4199+
4200+
nxt_http_request_send_body(task, r, NULL);
41964201
} else {
41974202
b_size = nxt_buf_is_mem(b) ? nxt_buf_mem_used_size(&b->mem) : 0;
41984203

@@ -4272,6 +4277,12 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
42724277
nxt_buf_chain_add(&r->out, b);
42734278
}
42744279

4280+
/* XXX Check compression / modify headers here */
4281+
ret = nxt_http_comp_check_compression(task, r);
4282+
if (ret != NXT_OK) {
4283+
goto fail;
4284+
}
4285+
42754286
nxt_http_request_header_send(task, r, nxt_http_request_send_body, NULL);
42764287

42774288
if (r->websocket_handshake

0 commit comments

Comments
 (0)