Skip to content

Commit

Permalink
** DEBUG DO NOT MERGE **
Browse files Browse the repository at this point in the history
  • Loading branch information
ac000 committed Jan 16, 2025
1 parent e9ae4c6 commit 61f85ed
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/nxt_brotli.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
*
*/

#define _GNU_SOURCE
#include <unistd.h>


#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
Expand All @@ -27,6 +31,9 @@ nxt_brotli_init(nxt_http_comp_compressor_ctx_t *ctx)

*brotli = BrotliEncoderCreateInstance(NULL, NULL, NULL);
BrotliEncoderSetParameter(*brotli, BROTLI_PARAM_QUALITY, ctx->level);

printf("%7d %s: brotli compression level [%d]\n", gettid(), __func__,
ctx->level);
}


Expand All @@ -45,6 +52,10 @@ nxt_brotli_compress(nxt_http_comp_compressor_ctx_t *ctx, const uint8_t *in_buf,
size_t out_bytes = out_len;
BrotliEncoderState *brotli = ctx->brotli_ctx;

printf("%7d %s: last/%s\n", gettid(), __func__, last ? "true" : "false");
printf("%7d %s: in_len [%lu] out_len [%lu]\n", gettid(), __func__,
in_len, out_len);

ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_PROCESS,
&in_len, &in_buf, &out_bytes, &out_buf,
NULL);
Expand All @@ -53,13 +64,18 @@ nxt_brotli_compress(nxt_http_comp_compressor_ctx_t *ctx, const uint8_t *in_buf,
&in_len, &in_buf, &out_bytes, &out_buf,
NULL);

printf("%7d %s: in_len [%lu] out_len [%lu] out_bytes [%lu]\n", gettid(),
__func__, in_len, out_len, out_bytes);
if (last) {
ok = BrotliEncoderCompressStream(brotli, BROTLI_OPERATION_FINISH,
&in_len, &in_buf, &out_bytes,
&out_buf, NULL);
nxt_brotli_free(ctx);
}

printf("%7d %s: in_len [%lu] out_len [%lu] out_bytes [%lu]\n", gettid(),
__func__, in_len, out_len, out_bytes);

return out_len - out_bytes;
}

Expand Down
75 changes: 75 additions & 0 deletions src/nxt_http_compression.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ static const nxt_http_comp_type_t compressors[] = {
};


static void print_compressor(const nxt_http_comp_compressor_t *c)
{
printf("token : %s\n", c->type->token.start);
printf("scheme : %d\n", c->type->scheme);
printf("level : %d\n", c->opts.level);
printf("min_len : %ld\n", c->opts.min_len);
}

static void print_comp_config(size_t n)
{
for (size_t i = 0; i < n; i++) {
nxt_http_comp_compressor_t *compr = enabled_compressors + i;

print_compressor(compr);
printf("\n");
}
}


static ssize_t
nxt_http_comp_compress(uint8_t *dst, size_t dst_size, const uint8_t *src,
size_t src_size, bool last)
Expand Down Expand Up @@ -172,7 +191,11 @@ nxt_http_comp_compress_app_response(nxt_http_request_t *r, nxt_buf_t **b)
// nxt_buf_t *buf;
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;
}

Expand All @@ -185,14 +208,19 @@ nxt_http_comp_compress_app_response(nxt_http_request_t *r, nxt_buf_t **b)
in_len = (*b)->mem.free - (*b)->mem.pos;
buf_len = nxt_http_comp_bound(in_len);

printf("%s: in_len [%lu] buf_len [%lu] last [%s]\n", __func__,
in_len, buf_len, last ? "true" : "false");

#if 1
if (buf_len > (size_t)nxt_buf_mem_size(&(*b)->mem)) {
/* XXX Un-skip Content-Length header, or not... */
return NXT_OK;
}

uint8_t *buf = nxt_malloc(buf_len);

cbytes = nxt_http_comp_compress(buf, buf_len, (*b)->mem.pos, in_len, last);
printf("%s: cbytes = %ld\n", __func__, cbytes);
if (cbytes == -1) {
nxt_free(buf);
return NXT_ERROR;
Expand Down Expand Up @@ -307,8 +335,12 @@ nxt_http_comp_compress_static_response(nxt_task_t *task, nxt_file_t **f,

last = n == rest;

printf("%s: out_off [%ld] in_off [%ld] last [%s]\n",
__func__, *out_total, in_size - rest, last ? "true" : "false");

cbytes = nxt_http_comp_compress(out + *out_total, out_size - *out_total,
in + in_size - rest, n, last);
printf("%s: cbytes [%ld]\n", __func__, cbytes);

*out_total += cbytes;
rest -= n;
Expand Down Expand Up @@ -337,6 +369,8 @@ nxt_http_comp_compress_static_response(nxt_task_t *task, nxt_file_t **f,
bool
nxt_http_comp_wants_compression(void)
{
printf("%s: compression [%s]\n", __func__,
compressor_ctx.idx > 0 ? "true" : "false");
return compressor_ctx.idx;
}

Expand Down Expand Up @@ -411,6 +445,9 @@ nxt_http_comp_select_compressor(const nxt_str_t *token)

scheme = enabled_compressors[ecidx].type->scheme;

printf("%s: %.*s [%f] [%d:%d]\n", __func__, (int)enc.length, enc.start,
qval, ecidx, scheme);

if (qval == 0.0 && scheme == NXT_HTTP_COMP_SCHEME_IDENTITY) {
identity_allowed = false;
}
Expand All @@ -425,6 +462,12 @@ nxt_http_comp_select_compressor(const nxt_str_t *token)

free(str);

printf("%s: Selected compressor : %s\n", __func__,
enabled_compressors[idx].type->token.start);

printf("%s: idx [%u], identity_allowed [%s]\n", __func__, idx,
identity_allowed ? "true" : "false");

if (idx == NXT_HTTP_COMP_SCHEME_IDENTITY && !identity_allowed) {
return -1;
}
Expand All @@ -442,6 +485,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 Down Expand Up @@ -470,6 +521,8 @@ nxt_http_comp_set_header(nxt_http_request_t *r, nxt_uint_t comp_idx)
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;
}
Expand All @@ -485,7 +538,10 @@ nxt_http_comp_is_resp_content_encoded(const nxt_http_request_t *r)
{
nxt_http_field_t *f;

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

nxt_list_each(f, r->resp.fields) {
printf("%s: %s: %s\n", __func__, f->name, f->value);
if (nxt_strcasecmp(f->name, (const u_char *)"Content-Encoding") == 0) {
return true;
}
Expand All @@ -504,6 +560,8 @@ nxt_http_comp_check_compression(nxt_task_t *task, nxt_http_request_t *r)
nxt_router_conf_t *rtcf;
nxt_http_comp_compressor_t *compressor;

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

compressor_ctx = (nxt_http_comp_ctx_t){ .resp_clen = -1 };

if (nr_enabled_compressors == 0) {
Expand All @@ -525,10 +583,15 @@ nxt_http_comp_check_compression(nxt_task_t *task, nxt_http_request_t *r)
return NXT_OK;
}

printf("%s: Response Content-Type [%.*s]\n", __func__,
(int)mime_type.length, mime_type.start);

if (mime_types_rule != NULL) {
ret = nxt_http_route_test_rule(r, mime_types_rule,
mime_type.start,
mime_type.length);
printf("%s: mime_type : %d (%.*s)\n", __func__, ret,
(int)mime_type.length, mime_type.start);
if (ret == 0) {
return NXT_OK;
}
Expand Down Expand Up @@ -577,7 +640,11 @@ nxt_http_comp_check_compression(nxt_task_t *task, nxt_http_request_t *r)

min_len = compressor->opts.min_len;

printf("%s: content_length [%ld] min_len [%ld]\n", __func__,
compressor_ctx.resp_clen, min_len);
if (compressor_ctx.resp_clen > -1 && compressor_ctx.resp_clen < min_len) {
printf("%s: %ld < %ld [skipping/clen]\n", __func__,
compressor_ctx.resp_clen , min_len);
return NXT_OK;
}

Expand Down Expand Up @@ -631,6 +698,8 @@ nxt_http_comp_set_compressor(nxt_task_t *task, nxt_router_conf_t *rtcf,

static const nxt_str_t token_str = nxt_string("encoding");

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

obj = nxt_conf_get_object_member(comp, &token_str, NULL);
if (obj == NULL) {
return NXT_ERROR;
Expand All @@ -644,6 +713,7 @@ nxt_http_comp_set_compressor(nxt_task_t *task, nxt_router_conf_t *rtcf,
compr->type = &compressors[cidx];
compr->opts.level = compr->type->def_compr;
compr->opts.min_len = -1;
printf("%s: %s\n", __func__, compr->type->token.start);

ret = nxt_conf_map_object(rtcf->mem_pool, comp, compressors_opts_map,
nxt_nitems(compressors_opts_map), &compr->opts);
Expand Down Expand Up @@ -678,6 +748,8 @@ nxt_http_comp_compression_init(nxt_task_t *task, nxt_router_conf_t *rtcf,
static const nxt_str_t comps_str = nxt_string("compressors");
static const nxt_str_t mimes_str = nxt_string("types");

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

mimes = nxt_conf_get_object_member(comp_conf, &mimes_str, NULL);
if (mimes != NULL) {
mime_types_rule = nxt_http_route_types_rule_create(task,
Expand Down Expand Up @@ -715,6 +787,7 @@ nxt_http_comp_compression_init(nxt_task_t *task, nxt_router_conf_t *rtcf,
.opts.min_len = -1 };

if (nxt_conf_type(comps) == NXT_CONF_OBJECT) {
print_comp_config(nr_enabled_compressors);
return nxt_http_comp_set_compressor(task, rtcf, comps, 1);
}

Expand All @@ -728,5 +801,7 @@ nxt_http_comp_compression_init(nxt_task_t *task, nxt_router_conf_t *rtcf,
}
}

print_comp_config(nr_enabled_compressors);

return NXT_OK;
}
7 changes: 7 additions & 0 deletions src/nxt_http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ nxt_http_request_start(nxt_task_t *task, void *obj, void *data)
nxt_socket_conf_t *skcf;
nxt_http_request_t *r;

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

r = obj;

NXT_OTEL_TRACE();
Expand Down Expand Up @@ -692,6 +694,8 @@ nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r,
nxt_http_field_t *server, *date, *content_length;
nxt_socket_conf_t *skcf;

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

ret = nxt_http_set_headers(r);
if (nxt_slow_path(ret != NXT_OK)) {
goto fail;
Expand Down Expand Up @@ -783,6 +787,9 @@ void
nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out)
{
if (nxt_fast_path(r->proto.any != NULL)) {
printf("%s: sending [%lu] bytes\n", __func__,
nxt_buf_mem_used_size(&out->mem));

nxt_http_proto[r->protocol].send(task, r, out);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/nxt_http_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ nxt_http_action_init(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
nxt_router_conf_t *rtcf;
nxt_http_action_conf_t acf;

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

nxt_memzero(&acf, sizeof(acf));

ret = nxt_conf_map_object(tmcf->mem_pool, cv, nxt_http_route_action_conf,
Expand Down
6 changes: 6 additions & 0 deletions src/nxt_http_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ nxt_http_static_send(nxt_task_t *task, nxt_http_request_t *r,
nxt_work_handler_t body_handler;
nxt_http_static_conf_t *conf;

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

action = ctx->action;
conf = action->u.conf;
rtcf = r->conf->socket_conf->router_conf;
Expand Down Expand Up @@ -821,6 +823,8 @@ nxt_http_static_body_handler(nxt_task_t *task, void *obj, void *data)
nxt_work_queue_t *wq;
nxt_http_request_t *r;

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

r = obj;
fb = r->out;

Expand Down Expand Up @@ -881,6 +885,8 @@ nxt_http_static_buf_completion(nxt_task_t *task, void *obj, void *data)
nxt_off_t rest;
nxt_http_request_t *r;

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

b = obj;
r = data;

Expand Down
2 changes: 2 additions & 0 deletions 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
3 changes: 3 additions & 0 deletions src/nxt_zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ nxt_zlib_deflate(nxt_http_comp_compressor_ctx_t *ctx, const uint8_t *in_buf,
z->avail_out = out_len;
z->next_out = out_buf;

printf("%s: in_len [%lu], out_len [%lu]\n", __func__, in_len, out_len);

compressed_bytes = z->total_out;

ret = deflate(z, last ? Z_FINISH : Z_SYNC_FLUSH);
if (ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR) {
deflateEnd(z);
printf("%s: ret = %d\n", __func__, ret);
return -1;
}

Expand Down
8 changes: 8 additions & 0 deletions src/nxt_zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ nxt_zstd_init(nxt_http_comp_compressor_ctx_t *ctx)

*zstd = ZSTD_createCStream();
ZSTD_initCStream(*zstd, ctx->level);

printf("%s: zstd compression level [%d]\n", __func__, ctx->level);
}


Expand All @@ -46,9 +48,13 @@ nxt_zstd_compress(nxt_http_comp_compressor_ctx_t *ctx, const uint8_t *in_buf,
ZSTD_inBuffer zinb = { .src = in_buf, .size = in_len };
ZSTD_outBuffer zoutb = { .dst = out_buf, .size = out_len };

printf("%s: in_len [%lu] out_len [%lu] last [%s]\n", __func__,
in_len, out_len, last ? "true" : "false");

ret = ZSTD_compressStream(zstd, &zoutb, &zinb);

if (zinb.pos < zinb.size) {
printf("%s: short by [%d]\n", __func__, zinb.pos < zinb.size);
ret = ZSTD_flushStream(zstd, &zoutb);
}

Expand All @@ -57,7 +63,9 @@ nxt_zstd_compress(nxt_http_comp_compressor_ctx_t *ctx, const uint8_t *in_buf,
nxt_zstd_free(ctx);
}

printf("%s: ret [%lu]\n", __func__, ret);
if (ZSTD_isError(ret)) {
printf("%s: [%s]\n", __func__, ZSTD_getErrorName(ret));
return -1;
}

Expand Down

0 comments on commit 61f85ed

Please sign in to comment.