Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Commit 628302a

Browse files
committed
[WIP] Compress application responses
1 parent 46bcc2c commit 628302a

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

src/nxt_http_compression.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,52 @@ static void print_comp_config(size_t n)
140140
}
141141
}
142142

143+
nxt_int_t
144+
nxt_http_comp_compress_response(nxt_http_request_t *r)
145+
{
146+
nxt_buf_t *b = r->out;
147+
size_t in_len;
148+
size_t buf_len;
149+
ssize_t cbytes;
150+
uint8_t *buf;
151+
bool last = false;
152+
nxt_http_comp_compressor_t *compressor;
153+
const nxt_http_comp_operations_t *cops;
154+
const nxt_http_comp_ctx_t *ctx = &compressor_ctx;
155+
156+
printf("%s: \n", __func__);
157+
158+
if (ctx->idx == NXT_HTTP_COMP_SCHEME_IDENTITY) {
159+
printf("%s: NXT_HTTP_COMP_SCHEME_IDENTITY [skipping/identity]\n",
160+
__func__);
161+
return NXT_OK;
162+
}
163+
164+
if (b->mem.pos == NULL) {
165+
return NXT_OK;
166+
}
167+
168+
compressor = &enabled_compressors[ctx->idx];
169+
170+
in_len = b->mem.free - b->mem.pos;
171+
172+
last = !b->next || (b->next && b->next->is_last == 1);
173+
174+
cops = compressor->type->cops;
175+
176+
buf_len = cops->bound(&ctx->ctx, in_len);
177+
buf = nxt_malloc(buf_len);
178+
cbytes = cops->deflate(&ctx->ctx, b->mem.pos, in_len, buf, buf_len, last);
179+
printf("%s: cbytes = %ld\n", __func__, cbytes);
180+
/* TODO handle new buffer is larger than original buffer */
181+
if (cbytes != -1) {
182+
b->mem.free = nxt_cpymem(b->mem.pos, buf, cbytes);
183+
}
184+
nxt_free(buf);
185+
186+
return NXT_OK;
187+
}
188+
143189
bool
144190
nxt_http_comp_wants_compression(void)
145191
{
@@ -284,6 +330,14 @@ nxt_http_comp_set_header(nxt_http_request_t *r, nxt_uint_t comp_idx)
284330
static const nxt_str_t content_encoding_str =
285331
nxt_string("Content-Encoding");
286332

333+
printf("%s: \n", __func__);
334+
335+
#if 0
336+
if (comp_idx == NXT_HTTP_COMP_SCHEME_IDENTITY) {
337+
return NXT_OK;
338+
}
339+
#endif
340+
287341
f = nxt_list_add(r->resp.fields);
288342
if (nxt_slow_path(f == NULL)) {
289343
return NXT_ERROR;
@@ -298,6 +352,30 @@ nxt_http_comp_set_header(nxt_http_request_t *r, nxt_uint_t comp_idx)
298352
f->value = token->start;
299353
f->value_length = token->length;
300354

355+
r->resp.content_length = NULL;
356+
r->resp.content_length_n = -1;
357+
358+
if (r->resp.mime_type == NULL) {
359+
nxt_http_field_t *f;
360+
361+
/*
362+
* As per RFC 2616 section 4.4 item 3, you should not send
363+
* Content-Length when a Transfer-Encoding header is present.
364+
*/
365+
nxt_list_each(f, r->resp.fields) {
366+
if (nxt_strcasecmp(f->name,
367+
(const u_char *)"Content-Length") != 0)
368+
{
369+
continue;
370+
}
371+
372+
printf("%s: Found (%s: %s), marking as 'skip'\n", __func__,
373+
f->name, f->value);
374+
f->skip = true;
375+
break;
376+
} nxt_list_loop;
377+
}
378+
301379
return NXT_OK;
302380
}
303381

src/nxt_http_compression.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ extern const nxt_http_comp_operations_t nxt_comp_zstd_ops;
8181
extern const nxt_http_comp_operations_t nxt_comp_brotli_ops;
8282
#endif
8383

84+
extern nxt_int_t nxt_http_comp_compress_response(nxt_http_request_t *r);
8485
extern bool nxt_http_comp_wants_compression(void);
8586
extern size_t nxt_http_comp_bound(size_t size);
8687
extern ssize_t nxt_http_comp_compress(uint8_t *dst, size_t dst_size,

src/nxt_port_queue.h

Lines changed: 2 additions & 0 deletions
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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4086,6 +4086,8 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
40864086
nxt_unit_response_t *resp;
40874087
nxt_request_rpc_data_t *req_rpc_data;
40884088

4089+
printf("%s: \n", __func__);
4090+
40894091
req_rpc_data = data;
40904092

40914093
r = req_rpc_data->request;
@@ -4141,8 +4143,11 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
41414143

41424144
if (r->header_sent) {
41434145
nxt_buf_chain_add(&r->out, b);
4144-
nxt_http_request_send_body(task, r, NULL);
41454146

4147+
/* XXX Do compression here */
4148+
nxt_http_comp_compress_response(r);
4149+
4150+
nxt_http_request_send_body(task, r, NULL);
41464151
} else {
41474152
b_size = nxt_buf_is_mem(b) ? nxt_buf_mem_used_size(&b->mem) : 0;
41484153

@@ -4222,6 +4227,12 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
42224227
nxt_buf_chain_add(&r->out, b);
42234228
}
42244229

4230+
/* XXX Check compression / modify headers here */
4231+
ret = nxt_http_comp_check_compression(task, r);
4232+
if (nxt_slow_path(ret != NXT_OK)) {
4233+
goto fail;
4234+
}
4235+
42254236
nxt_http_request_header_send(task, r, nxt_http_request_send_body, NULL);
42264237

42274238
if (r->websocket_handshake

0 commit comments

Comments
 (0)