Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions build_test_resources/cfuntest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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)
);
}

Expand All @@ -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)
);
}
}
Expand All @@ -86,20 +91,23 @@ 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(
ctx,
401,
"401 unauthorized",
"text/plain",
tokenArgs
tokenArgs,
strlen(tokenArgs)
);
}
}
Expand All @@ -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 {

Expand All @@ -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)
);
}
}
Expand All @@ -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)
);
}
}
Expand All @@ -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;
}
}
13 changes: 8 additions & 5 deletions src/ngx_http_c_func_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/ngx_http_c_func_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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_ */
#endif /* _NGX_C_FUNC_APP_H_INCLUDED_ */