Skip to content

Commit 96f96c5

Browse files
committed
expose ngx_http_c_func_strdup method
1 parent f6ae3c9 commit 96f96c5

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ngx_feature_incs="#include <ngx_http_c_func_module.h>"
2424
ngx_feature_path=
2525
ngx_feature_libs=
2626
# ngx_feature_exit_if_not_found=yes
27-
ngx_feature_test="int ngx_http_c_func_module_current_version_=ngx_http_c_func_module_version_8;"
27+
ngx_feature_test="int ngx_http_c_func_module_current_version_=ngx_http_c_func_module_version_9;"
2828
. auto/feature
2929

3030
if [ $ngx_found != yes ]; then

src/ngx_http_c_func_module.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static ngx_int_t ngx_http_c_func_module_init(ngx_cycle_t *cycle);
118118
static void ngx_http_c_func_master_exit(ngx_cycle_t *cycle);
119119
static void ngx_http_c_func_client_body_handler(ngx_http_request_t *r);
120120
static ngx_int_t ngx_http_c_func_proceed_init_calls(ngx_cycle_t* cycle, ngx_http_c_func_srv_conf_t *scf, ngx_http_c_func_main_conf_t* mcf);
121-
static u_char* ngx_http_c_func_strdup(ngx_pool_t *pool, const char *src, size_t len);
121+
static u_char* ngx_http_c_func_strdup_with_p(ngx_pool_t *pool, const char *src, size_t len);
122122

123123
static ngx_int_t ngx_http_c_func_get_resp_var(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data);
124124
static void ngx_http_c_func_set_resp_var_with_r(ngx_http_request_t *r, ngx_http_c_func_ctx_t *ctx, const char* resp_content);
@@ -149,6 +149,7 @@ void ngx_http_c_func_log_debug(ngx_http_c_func_ctx_t *ctx, const char* msg);
149149
void ngx_http_c_func_log_info(ngx_http_c_func_ctx_t *ctx, const char* msg);
150150
void ngx_http_c_func_log_warn(ngx_http_c_func_ctx_t *ctx, const char* msg);
151151
void ngx_http_c_func_log_err(ngx_http_c_func_ctx_t *ctx, const char* msg);
152+
char *ngx_http_c_func_strdup(ngx_http_c_func_ctx_t *ctx, const char *src);
152153
u_char* ngx_http_c_func_get_header(ngx_http_c_func_ctx_t *ctx, const char*key);
153154
void* ngx_http_c_func_get_query_param(ngx_http_c_func_ctx_t *ctx, const char *key);
154155
void* ngx_http_c_func_palloc(ngx_http_c_func_ctx_t *ctx, size_t size);
@@ -532,7 +533,7 @@ ngx_http_c_func_post_configuration(ngx_conf_t *cf) {
532533
static ngx_int_t
533534
ngx_http_c_func_pre_configuration(ngx_conf_t *cf) {
534535

535-
#ifndef ngx_http_c_func_module_version_8
536+
#ifndef ngx_http_c_func_module_version_9
536537
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", "the latest ngx_http_c_func_module.h not found in the c header path, \
537538
please copy latest ngx_http_c_func_module.h to your /usr/include or /usr/local/include or relavent header search path \
538539
with read and write permission.");
@@ -1103,8 +1104,19 @@ ngx_http_c_func_log_err(ngx_http_c_func_ctx_t *ctx, const char* msg) {
11031104
ngx_log_error(NGX_LOG_ERR, (ngx_log_t *)ctx->__log__, 0, "%s", msg);
11041105
}
11051106

1107+
char*
1108+
ngx_http_c_func_strdup(ngx_http_c_func_ctx_t *ctx, const char *src) {
1109+
char *dst;
1110+
if (src == NULL) return NULL;
1111+
size_t len = ngx_strlen(src);
1112+
dst = (char*) ngx_palloc((ngx_pool_t*)ctx->__pl__, (len + 1) * sizeof(char));
1113+
ngx_memcpy(dst, src, len);
1114+
dst[len] = '\0';
1115+
return dst;
1116+
}
1117+
11061118
static u_char*
1107-
ngx_http_c_func_strdup(ngx_pool_t *pool, const char *src, size_t len) {
1119+
ngx_http_c_func_strdup_with_p(ngx_pool_t *pool, const char *src, size_t len) {
11081120
u_char *dst;
11091121
dst = ngx_pcalloc(pool, len + 1);
11101122
if (dst == NULL) {
@@ -1404,11 +1416,11 @@ ngx_http_c_func_write_resp(
14041416
/* Set the Content-Type header. */
14051417
if (content_type) {
14061418
r->headers_out.content_type.len = ngx_strlen(content_type);
1407-
r->headers_out.content_type.data = ngx_http_c_func_strdup(r->pool, content_type, r->headers_out.content_type.len);
1419+
r->headers_out.content_type.data = ngx_http_c_func_strdup_with_p(r->pool, content_type, r->headers_out.content_type.len);
14081420
} else {
14091421
static const char* plaintext_content_type = ngx_http_c_func_content_type_plaintext;
14101422
r->headers_out.content_type.len = ngx_strlen(plaintext_content_type);
1411-
r->headers_out.content_type.data = ngx_http_c_func_strdup(r->pool, plaintext_content_type, r->headers_out.content_type.len);
1423+
r->headers_out.content_type.data = ngx_http_c_func_strdup_with_p(r->pool, plaintext_content_type, r->headers_out.content_type.len);
14121424
}
14131425

14141426
ngx_buf_t *b;
@@ -1433,7 +1445,7 @@ ngx_http_c_func_write_resp(
14331445

14341446
if (status_line) {
14351447
r->headers_out.status_line.len = ngx_strlen(status_line);
1436-
r->headers_out.status_line.data = ngx_http_c_func_strdup(r->pool, status_line, r->headers_out.status_line.len);
1448+
r->headers_out.status_line.data = ngx_http_c_func_strdup_with_p(r->pool, status_line, r->headers_out.status_line.len);
14371449
}
14381450

14391451
/* Get the content length of the body. */

src/ngx_http_c_func_module.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include <stdlib.h>
3636
#include <stdint.h>
3737

38-
#define ngx_http_c_func_module_version_8 8
38+
#define ngx_http_c_func_module_version_9 9
3939

4040

4141
#define ngx_http_c_func_content_type_plaintext "text/plain"
@@ -68,6 +68,9 @@ extern void* ngx_http_c_func_get_query_param(ngx_http_c_func_ctx_t *ctx, const c
6868
extern void* ngx_http_c_func_palloc(ngx_http_c_func_ctx_t *ctx, size_t size);
6969
extern void* ngx_http_c_func_pcalloc(ngx_http_c_func_ctx_t *ctx, size_t size);
7070

71+
extern char *ngx_http_c_func_strdup(ngx_http_c_func_ctx_t *ctx, const char *src);
72+
73+
7174
#define ngx_http_c_func_log(loglevel, req_context, ...) ({\
7275
char __buff__[200];\
7376
snprintf(__buff__, 200, ##__VA_ARGS__);\

0 commit comments

Comments
 (0)