Skip to content

Commit 7e2fc08

Browse files
authored
Merge pull request #3 from v0lker/binary
allow for reception and transmission of generic binary data
2 parents 65d41b5 + 18a4f60 commit 7e2fc08

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

build_test_resources/cfuntest.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ void ngx_http_c_func_init(ngx_http_c_func_ctx_t* ctx) {
2626
void my_app_simple_get_greeting(ngx_http_c_func_ctx_t *ctx) {
2727
ngx_http_c_func_log_info(ctx, "Calling back and log from my_app_simple_get");
2828

29+
char *rep = "greeting from ngx_http_c_func testing";
2930
ngx_http_c_func_write_resp(
3031
ctx,
3132
200,
3233
"200 OK",
3334
"text/plain",
34-
"greeting from ngx_http_c_func testing"
35+
rep,
36+
strlen(rep)
3537
);
3638
}
3739

@@ -45,7 +47,8 @@ void my_app_simple_get_args(ngx_http_c_func_ctx_t *ctx) {
4547
200,
4648
"200 OK",
4749
"text/plain",
48-
ctx->req_args
50+
ctx->req_args,
51+
strlen(ctx->req_args)
4952
);
5053
}
5154

@@ -61,7 +64,8 @@ void my_app_simple_get_calloc_from_pool(ngx_http_c_func_ctx_t *ctx) {
6164
200,
6265
"200 OK",
6366
"text/plain",
64-
my_log_message
67+
my_log_message,
68+
strlen(my_log_message)
6569
);
6670
}
6771

@@ -76,7 +80,8 @@ void my_app_simple_get_header_param(ngx_http_c_func_ctx_t *ctx) {
7680
200,
7781
"200 OK",
7882
"text/plain",
79-
req_content_type
83+
req_content_type,
84+
strlen(req_content_type)
8085
);
8186
}
8287
}
@@ -86,20 +91,23 @@ void my_app_simple_get_token_args(ngx_http_c_func_ctx_t *ctx) {
8691

8792
char * tokenArgs = ngx_http_c_func_get_query_param(ctx, "token");
8893
if (! tokenArgs) {
94+
char *resp = "Token Not Found";
8995
ngx_http_c_func_write_resp(
9096
ctx,
9197
401,
9298
"401 unauthorized",
9399
"text/plain",
94-
"Token Not Found"
100+
resp,
101+
strlen(resp)
95102
);
96103
} else {
97104
ngx_http_c_func_write_resp(
98105
ctx,
99106
401,
100107
"401 unauthorized",
101108
"text/plain",
102-
tokenArgs
109+
tokenArgs,
110+
strlen(tokenArgs)
103111
);
104112
}
105113
}
@@ -110,12 +118,14 @@ void my_app_simple_post(ngx_http_c_func_ctx_t *ctx) {
110118
if (!ctx->req_body) {
111119
ngx_http_c_func_log_info(ctx, "no request body");
112120

121+
char *resp = "\n";
113122
ngx_http_c_func_write_resp(
114123
ctx,
115124
202,
116125
"202 Accepted and Processing",
117126
"text/plain",
118-
"\n"
127+
resp,
128+
strlen(resp)
119129
);
120130
} else {
121131

@@ -124,7 +134,8 @@ void my_app_simple_post(ngx_http_c_func_ctx_t *ctx) {
124134
202,
125135
"202 Accepted and Processing",
126136
"text/plain",
127-
ctx->req_body
137+
ctx->req_body,
138+
strlen(ctx->req_body)
128139
);
129140
}
130141
}
@@ -140,7 +151,8 @@ void my_app_simple_get_cache(ngx_http_c_func_ctx_t *ctx) {
140151
200,
141152
"200 OK",
142153
"text/plain",
143-
(char*)my_cache_value
154+
my_cache_value,
155+
strlen(my_cache_value)
144156
);
145157
}
146158
}
@@ -159,4 +171,4 @@ void ngx_http_c_func_exit(ngx_http_c_func_ctx_t* ctx) {
159171
ngx_http_c_func_log(info, ctx, "%s\n", "Shutting down The Application");
160172

161173
is_service_on = 0;
162-
}
174+
}

src/ngx_http_c_func_module.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ void* ngx_http_c_func_cache_put(void *shared_mem, const char* key, void* value);
183183
void* ngx_http_c_func_cache_new(void *shared_mem, const char* key, size_t size);
184184
void* ngx_http_c_func_cache_remove(void *shared_mem, const char* key);
185185
void ngx_http_c_func_set_resp_var(ngx_http_c_func_ctx_t *ctx, const char* resp_content);
186-
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);
186+
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);
187187
void ngx_http_c_func_write_resp_l(ngx_http_c_func_ctx_t *ctx, uintptr_t status_code, const char* status_line,
188188
size_t status_line_len, const char* content_type, size_t content_type_len,
189189
const char* resp_content, size_t resp_content_len);
@@ -1060,12 +1060,13 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) {
10601060
args is %V\n \
10611061
extern is %V\n \
10621062
unparsed_uri is %V\n \
1063-
Size is %d\n \
1064-
Request body is %s", &r->request_line, &r->uri, &r->args, &r->exten, &r->unparsed_uri, len, buf);
1063+
Size is %zu", &r->request_line, &r->uri, &r->args, &r->exten, &r->unparsed_uri, len);
10651064

10661065
new_ctx->req_body = buf;
1066+
new_ctx->req_body_len = len;
10671067
} else {
10681068
new_ctx->req_body = NULL;
1069+
new_ctx->req_body_len = 0;
10691070
}
10701071
} else { //if (!(r->method & (NGX_HTTP_POST | NGX_HTTP_PUT | NGX_HTTP_PATCH))) {
10711072
if (ngx_http_discard_request_body(r) != NGX_OK) {
@@ -1078,6 +1079,7 @@ ngx_http_c_func_precontent_handler(ngx_http_request_t *r) {
10781079
extern is %V\n \
10791080
unparsed_uri is %V\n", &r->request_line, &r->uri, &r->args, &r->exten, &r->unparsed_uri);
10801081
new_ctx->req_body = NULL;
1082+
new_ctx->req_body_len = 0;
10811083
}
10821084

10831085
#if (NGX_THREADS) && (nginx_version > 1013003)
@@ -1600,14 +1602,15 @@ ngx_http_c_func_write_resp(
16001602
uintptr_t status_code,
16011603
const char* status_line,
16021604
const char* content_type,
1603-
const char* resp_content
1605+
const char* resp_content,
1606+
size_t resp_len
16041607
) {
16051608
ngx_http_c_func_write_resp_l(appctx, status_code,
16061609
status_line, status_line ? ngx_strlen(status_line) : 0,
16071610
content_type,
16081611
content_type ? ngx_strlen(content_type) : 0,
16091612
resp_content,
1610-
resp_content ? ngx_strlen(resp_content) : 0
1613+
resp_len
16111614
);
16121615
}
16131616

src/ngx_http_c_func_module.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct {
5555
char *req_args; // Uri Args
5656
u_char *req_body; // Request Body
5757
void *shared_mem;
58+
size_t req_body_len; // length of body, including terminating \0
5859

5960
/* internal */
6061
void* __r__;
@@ -86,7 +87,8 @@ extern void ngx_http_c_func_write_resp(
8687
uintptr_t status_code,
8788
const char* status_line,
8889
const char* content_type,
89-
const char* resp_content
90+
const char* resp_content,
91+
size_t resp_len
9092
);
9193

9294
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*
118120
extern void* ngx_http_c_func_cache_new(void *shared_mem, const char* key, size_t size);
119121
extern void* ngx_http_c_func_cache_remove(void *shared_mem, const char* key);
120122

121-
#endif /* _NGX_C_FUNC_APP_H_INCLUDED_ */
123+
#endif /* _NGX_C_FUNC_APP_H_INCLUDED_ */

0 commit comments

Comments
 (0)