From 18a4f608ac0dd15ad9576ff7f5635731c2dad972 Mon Sep 17 00:00:00 2001 From: volker Date: Sun, 2 Sep 2018 12:28:40 +0100 Subject: [PATCH] allow for reception and transmit of generic binary data --- build_test_resources/cfuntest.c | 32 ++++++++++++++++++++++---------- src/ngx_http_c_func_module.c | 13 ++++++++----- src/ngx_http_c_func_module.h | 6 ++++-- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/build_test_resources/cfuntest.c b/build_test_resources/cfuntest.c index 8b15ec5..c21050d 100755 --- a/build_test_resources/cfuntest.c +++ b/build_test_resources/cfuntest.c @@ -26,12 +26,14 @@ void ngx_http_c_func_init(ngx_http_c_func_ctx_t* ctx) { void my_app_simple_get_greeting(ngx_http_c_func_ctx_t *ctx) { ngx_http_c_func_log_info(ctx, "Calling back and log from my_app_simple_get"); + char *rep = "greeting from ngx_http_c_func testing"; ngx_http_c_func_write_resp( ctx, 200, "200 OK", "text/plain", - "greeting from ngx_http_c_func testing" + rep, + strlen(rep) ); } @@ -45,7 +47,8 @@ void my_app_simple_get_args(ngx_http_c_func_ctx_t *ctx) { 200, "200 OK", "text/plain", - ctx->req_args + ctx->req_args, + strlen(ctx->req_args) ); } @@ -61,7 +64,8 @@ void my_app_simple_get_calloc_from_pool(ngx_http_c_func_ctx_t *ctx) { 200, "200 OK", "text/plain", - my_log_message + my_log_message, + strlen(my_log_message) ); } @@ -76,7 +80,8 @@ void my_app_simple_get_header_param(ngx_http_c_func_ctx_t *ctx) { 200, "200 OK", "text/plain", - req_content_type + req_content_type, + strlen(req_content_type) ); } } @@ -86,12 +91,14 @@ void my_app_simple_get_token_args(ngx_http_c_func_ctx_t *ctx) { char * tokenArgs = ngx_http_c_func_get_query_param(ctx, "token"); if (! tokenArgs) { + char *resp = "Token Not Found"; ngx_http_c_func_write_resp( ctx, 401, "401 unauthorized", "text/plain", - "Token Not Found" + resp, + strlen(resp) ); } else { ngx_http_c_func_write_resp( @@ -99,7 +106,8 @@ void my_app_simple_get_token_args(ngx_http_c_func_ctx_t *ctx) { 401, "401 unauthorized", "text/plain", - tokenArgs + tokenArgs, + strlen(tokenArgs) ); } } @@ -110,12 +118,14 @@ void my_app_simple_post(ngx_http_c_func_ctx_t *ctx) { if (!ctx->req_body) { ngx_http_c_func_log_info(ctx, "no request body"); + char *resp = "\n"; ngx_http_c_func_write_resp( ctx, 202, "202 Accepted and Processing", "text/plain", - "\n" + resp, + strlen(resp) ); } else { @@ -124,7 +134,8 @@ void my_app_simple_post(ngx_http_c_func_ctx_t *ctx) { 202, "202 Accepted and Processing", "text/plain", - ctx->req_body + ctx->req_body, + strlen(ctx->req_body) ); } } @@ -140,7 +151,8 @@ void my_app_simple_get_cache(ngx_http_c_func_ctx_t *ctx) { 200, "200 OK", "text/plain", - (char*)my_cache_value + my_cache_value, + strlen(my_cache_value) ); } } @@ -159,4 +171,4 @@ void ngx_http_c_func_exit(ngx_http_c_func_ctx_t* ctx) { ngx_http_c_func_log(info, ctx, "%s\n", "Shutting down The Application"); is_service_on = 0; -} \ No newline at end of file +} diff --git a/src/ngx_http_c_func_module.c b/src/ngx_http_c_func_module.c index b3fe8ca..3487b29 100755 --- a/src/ngx_http_c_func_module.c +++ b/src/ngx_http_c_func_module.c @@ -183,7 +183,7 @@ void* ngx_http_c_func_cache_put(void *shared_mem, const char* key, void* value); void* ngx_http_c_func_cache_new(void *shared_mem, const char* key, size_t size); void* ngx_http_c_func_cache_remove(void *shared_mem, const char* key); void ngx_http_c_func_set_resp_var(ngx_http_c_func_ctx_t *ctx, const char* resp_content); -void ngx_http_c_func_write_resp(ngx_http_c_func_ctx_t *ctx, uintptr_t status_code, const char* status_line, const char* content_type, const char* resp_content); +void ngx_http_c_func_write_resp(ngx_http_c_func_ctx_t *ctx, uintptr_t status_code, const char* status_line, const char* content_type, const char* resp_content, size_t resp_len); void ngx_http_c_func_write_resp_l(ngx_http_c_func_ctx_t *ctx, uintptr_t status_code, const char* status_line, size_t status_line_len, const char* content_type, size_t content_type_len, const char* resp_content, size_t resp_content_len); @@ -1060,12 +1060,13 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) { args is %V\n \ extern is %V\n \ unparsed_uri is %V\n \ - Size is %d\n \ - Request body is %s", &r->request_line, &r->uri, &r->args, &r->exten, &r->unparsed_uri, len, buf); + Size is %zu", &r->request_line, &r->uri, &r->args, &r->exten, &r->unparsed_uri, len); new_ctx->req_body = buf; + new_ctx->req_body_len = len; } else { new_ctx->req_body = NULL; + new_ctx->req_body_len = 0; } } else { //if (!(r->method & (NGX_HTTP_POST | NGX_HTTP_PUT | NGX_HTTP_PATCH))) { if (ngx_http_discard_request_body(r) != NGX_OK) { @@ -1078,6 +1079,7 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) { extern is %V\n \ unparsed_uri is %V\n", &r->request_line, &r->uri, &r->args, &r->exten, &r->unparsed_uri); new_ctx->req_body = NULL; + new_ctx->req_body_len = 0; } #if (NGX_THREADS) && (nginx_version > 1013003) @@ -1600,14 +1602,15 @@ ngx_http_c_func_write_resp( uintptr_t status_code, const char* status_line, const char* content_type, - const char* resp_content + const char* resp_content, + size_t resp_len ) { ngx_http_c_func_write_resp_l(appctx, status_code, status_line, status_line ? ngx_strlen(status_line) : 0, content_type, content_type ? ngx_strlen(content_type) : 0, resp_content, - resp_content ? ngx_strlen(resp_content) : 0 + resp_len ); } diff --git a/src/ngx_http_c_func_module.h b/src/ngx_http_c_func_module.h index 478853d..5dec8f9 100755 --- a/src/ngx_http_c_func_module.h +++ b/src/ngx_http_c_func_module.h @@ -55,6 +55,7 @@ typedef struct { char *req_args; // Uri Args u_char *req_body; // Request Body void *shared_mem; + size_t req_body_len; // length of body, including terminating \0 /* internal */ void* __r__; @@ -86,7 +87,8 @@ extern void ngx_http_c_func_write_resp( uintptr_t status_code, const char* status_line, const char* content_type, - const char* resp_content + const char* resp_content, + size_t resp_len ); extern void ngx_http_c_func_write_resp_l( @@ -118,4 +120,4 @@ extern void* ngx_http_c_func_cache_put(void *shared_mem, const char* key, void* extern void* ngx_http_c_func_cache_new(void *shared_mem, const char* key, size_t size); extern void* ngx_http_c_func_cache_remove(void *shared_mem, const char* key); -#endif /* _NGX_C_FUNC_APP_H_INCLUDED_ */ \ No newline at end of file +#endif /* _NGX_C_FUNC_APP_H_INCLUDED_ */