diff --git a/src/nxt_http_compression.c b/src/nxt_http_compression.c index ecf6138ea..2738573f1 100644 --- a/src/nxt_http_compression.c +++ b/src/nxt_http_compression.c @@ -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, @@ -400,6 +451,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; @@ -414,6 +473,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; } diff --git a/src/nxt_http_compression.h b/src/nxt_http_compression.h index 9dad11da1..23017bf5b 100644 --- a/src/nxt_http_compression.h +++ b/src/nxt_http_compression.h @@ -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); diff --git a/src/nxt_port_queue.h b/src/nxt_port_queue.h index d2b2326bf..3e364d9a5 100644 --- a/src/nxt_port_queue.h +++ b/src/nxt_port_queue.h @@ -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; diff --git a/src/nxt_router.c b/src/nxt_router.c index d2486c9fd..a9b26fa39 100644 --- a/src/nxt_router.c +++ b/src/nxt_router.c @@ -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; @@ -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; @@ -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