Skip to content

Commit

Permalink
http: Compress application responses
Browse files Browse the repository at this point in the history
Co-authored-by: Alejandro Colomar <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
Signed-off-by: Andrew Clayton <[email protected]>
  • Loading branch information
ac000 and alejandro-colomar committed Dec 4, 2024
1 parent ab66623 commit 78d32c3
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
81 changes: 81 additions & 0 deletions src/nxt_http_compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,57 @@ nxt_http_comp_bound(size_t size)
}


nxt_int_t
nxt_http_comp_compress_app_response(nxt_http_request_t *r)
{
bool last;
size_t buf_len, in_len;
ssize_t cbytes;
nxt_buf_t *buf, *b = r->out;
nxt_http_comp_ctx_t *ctx = &compressor_ctx;

printf("%s: \n", __func__);

if (ctx->idx == NXT_HTTP_COMP_SCHEME_IDENTITY) {
printf("%s: NXT_HTTP_COMP_SCHEME_IDENTITY [skipping/identity]\n",
__func__);
return NXT_OK;
}

if (b->mem.pos == NULL) {
return NXT_OK;
}

in_len = b->mem.free - b->mem.pos;

last = !b->next || b->next->is_last == 1;

buf_len = nxt_http_comp_bound(in_len);

buf = nxt_buf_mem_alloc(r->mem_pool, buf_len, 0);
if (nxt_slow_path(buf == NULL)) {
return NXT_ERROR;
}

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

cbytes = nxt_http_comp_compress(b->mem.pos, in_len, buf->mem.start,
buf->mem.end - buf->mem.start, last);
printf("%s: cbytes = %ld\n", __func__, cbytes);
if (cbytes == -1) {
return NXT_ERROR;
}

// if (cbytes != -1) {
// b->mem.free = nxt_cpymem(b->mem.pos, tmp->mem.start, cbytes);
// }
b = buf;

return NXT_OK;
}


nxt_int_t
nxt_http_comp_compress_static_response(nxt_task_t *task, nxt_file_t **f,
nxt_file_info_t *fi,
Expand Down Expand Up @@ -401,6 +452,14 @@ nxt_http_comp_set_header(nxt_http_request_t *r, nxt_uint_t comp_idx)
static const nxt_str_t content_encoding_str =
nxt_string("Content-Encoding");

printf("%s: \n", __func__);

#if 0
if (comp_idx == NXT_HTTP_COMP_SCHEME_IDENTITY) {
return NXT_OK;
}
#endif

f = nxt_list_add(r->resp.fields);
if (nxt_slow_path(f == NULL)) {
return NXT_ERROR;
Expand All @@ -415,6 +474,28 @@ nxt_http_comp_set_header(nxt_http_request_t *r, nxt_uint_t comp_idx)
f->value = token->start;
f->value_length = token->length;

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

if (r->resp.mime_type == NULL) {
nxt_http_field_t *f;

/*
* As per RFC 2616 section 4.4 item 3, you should not send
* Content-Length when a Transfer-Encoding header is present.
*/
nxt_list_each(f, r->resp.fields) {
if (nxt_strcasecmp(f->name,
(const u_char *)"Content-Length") == 0)
{
printf("%s: Found (%s: %s), marking as 'skip'\n", __func__,
f->name, f->value);
f->skip = true;
break;
}
} nxt_list_loop;
}

return NXT_OK;
}

Expand Down
1 change: 1 addition & 0 deletions src/nxt_http_compression.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ extern const nxt_http_comp_operations_t nxt_comp_brotli_ops;
#endif


extern nxt_int_t nxt_http_comp_compress_app_response(nxt_http_request_t *r);
extern nxt_int_t nxt_http_comp_compress_static_response(nxt_task_t *task,
nxt_file_t **f, nxt_file_info_t *fi, size_t static_buf_len,
size_t *out_total);
Expand Down
2 changes: 2 additions & 0 deletions src/nxt_port_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ nxt_port_queue_recv(nxt_port_queue_t volatile *q, void *p)
nxt_nncq_atomic_t i;
nxt_port_queue_item_t *qi;

// printf("%s \n", __func__);

i = nxt_nncq_dequeue(&q->queue);
if (i == nxt_nncq_empty(&q->queue)) {
return -1;
Expand Down
13 changes: 12 additions & 1 deletion src/nxt_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -4136,6 +4136,8 @@ nxt_router_response_ready_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
nxt_unit_response_t *resp;
nxt_request_rpc_data_t *req_rpc_data;

printf("%s: \n", __func__);

req_rpc_data = data;

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

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

/* XXX Do compression here */
nxt_http_comp_compress_app_response(r);

nxt_http_request_send_body(task, r, NULL);
} else {
b_size = nxt_buf_is_mem(b) ? nxt_buf_mem_used_size(&b->mem) : 0;

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

/* XXX Check compression / modify headers here */
ret = nxt_http_comp_check_compression(task, r);
if (ret != NXT_OK) {
goto fail;
}

nxt_http_request_header_send(task, r, nxt_http_request_send_body, NULL);

if (r->websocket_handshake
Expand Down

0 comments on commit 78d32c3

Please sign in to comment.