Skip to content

Commit eef81d1

Browse files
committed
add on server properties feature
1 parent 44dcf93 commit eef81d1

File tree

4 files changed

+106
-11
lines changed

4 files changed

+106
-11
lines changed

build_test_resources/linkfuntest.c

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
#include <ngx_link_func_module.h>
44

55

6-
/***gcc -shared -o liblinkfuntest.so -fPIC linkfuntest.c***/
7-
/***cp liblinkfuntest.so /etc/nginx/***/
6+
/* gcc -shared -o liblinkfuntest.so -fPIC linkfuntest.c */
7+
/* cp liblinkfuntest.so /etc/nginx/ */
8+
9+
/* for Darwin */
10+
/*
11+
*
12+
* clang -dynamiclib -o liblinkfuntest.dylib -fPIC linkfuntest.c -Wl,-undefined,dynamic_lookup
13+
*
14+
*/
815

916
int is_service_on = 0;
1017

@@ -14,7 +21,6 @@ void ngx_link_func_init(ngx_link_func_ctx_t* ctx) {
1421
is_service_on = 1;
1522
}
1623

17-
1824
void my_app_simple_get_greeting(ngx_link_func_ctx_t *ctx) {
1925
ngx_link_func_log_info(ctx, "Calling back and log from my_app_simple_get");
2026

@@ -30,7 +36,44 @@ void my_app_simple_get_greeting(ngx_link_func_ctx_t *ctx) {
3036
);
3137
}
3238

39+
void my_app_simple_get_delay_greeting(ngx_link_func_ctx_t *ctx) {
40+
ngx_link_func_log_info(ctx, "Calling back and log from my_app_simple_get");
41+
42+
char *rep = "2 second delay greeting from ngx_link_func testing";
43+
sleep(2);
44+
ngx_link_func_write_resp(
45+
ctx,
46+
200,
47+
"200 OK",
48+
"text/plain",
49+
rep,
50+
strlen(rep)
51+
);
52+
}
3353

54+
void my_app_simple_get_prop_greeting(ngx_link_func_ctx_t *ctx) {
55+
ngx_link_func_log_info(ctx, "Calling back and log from my_app_simple_get");
56+
u_char *defaultGreeting = ngx_link_func_get_prop(ctx, "defaultGreeting", sizeof("defaultGreeting") - 1);
57+
if(defaultGreeting) {
58+
ngx_link_func_write_resp(
59+
ctx,
60+
200,
61+
"200 OK",
62+
"text/plain",
63+
(char*) defaultGreeting,
64+
strlen(defaultGreeting)
65+
);
66+
} else {
67+
ngx_link_func_write_resp(
68+
ctx,
69+
404,
70+
"404 NOT FOUND",
71+
"text/plain",
72+
NULL,
73+
0
74+
);
75+
}
76+
}
3477

3578
void my_app_simple_get_args(ngx_link_func_ctx_t *ctx) {
3679
ngx_link_func_log(info, ctx, "Calling back and log from my_app_simple_get_args");

config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ngx_feature_incs="#include <ngx_link_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_link_func_module_current_version_=ngx_link_func_module_version_31;"
27+
ngx_feature_test="int ngx_link_func_module_current_version_=ngx_link_func_module_version_32;"
2828
. auto/feature
2929

3030
if [ $ngx_found != yes ]; then

src/ngx_link_func_module.c

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ typedef struct {
9292
ngx_str_t _headers;
9393
ngx_str_t _ca_cart;
9494
ngx_queue_t *_link_func_locs_queue;
95+
ngx_array_t *_props;
9596
} ngx_http_link_func_srv_conf_t;
9697

9798
typedef struct {
@@ -206,7 +207,8 @@ void ngx_link_func_log_info(ngx_link_func_ctx_t *ctx, const char* msg);
206207
void ngx_link_func_log_warn(ngx_link_func_ctx_t *ctx, const char* msg);
207208
void ngx_link_func_log_err(ngx_link_func_ctx_t *ctx, const char* msg);
208209
char *ngx_link_func_strdup(ngx_link_func_ctx_t *ctx, const char *src);
209-
u_char* ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char*key, size_t keylen);
210+
u_char* ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen);
211+
u_char* ngx_link_func_get_prop(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen);
210212
int ngx_link_func_add_header_in(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen, const char *value, size_t val_len );
211213
int ngx_link_func_add_header_out(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen, const char *value, size_t val_len );
212214
void* ngx_link_func_get_query_param(ngx_link_func_ctx_t *ctx, const char *key);
@@ -294,6 +296,13 @@ static ngx_command_t ngx_http_link_func_commands[] = {
294296
offsetof(ngx_http_link_func_loc_conf_t, _method_name), /* No offset when storing the module configuration on struct. */
295297
NULL
296298
},
299+
{ ngx_string("ngx_link_func_add_prop"),
300+
NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_TAKE2,
301+
ngx_conf_set_keyval_slot,
302+
NGX_HTTP_SRV_CONF_OFFSET,
303+
offsetof(ngx_http_link_func_srv_conf_t, _props),
304+
NULL
305+
},
297306
ngx_null_command /* command termination */
298307
};
299308

@@ -711,7 +720,7 @@ ngx_http_link_func_pre_configuration(ngx_conf_t *cf) {
711720
return NGX_ERROR;
712721
#endif
713722

714-
#ifndef ngx_link_func_module_version_31
723+
#ifndef ngx_link_func_module_version_32
715724
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", "the ngx_http_link_func_module.h might not be latest or not found in the c header path, \
716725
please copy latest ngx_http_link_func_module.h to your /usr/include or /usr/local/include or relavent header search path \
717726
with read and write permission.");
@@ -990,8 +999,8 @@ ngx_http_link_func_create_srv_conf(ngx_conf_t *cf) {
990999
static char *
9911000
ngx_http_link_func_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
9921001
{
993-
// ngx_http_link_func_srv_conf_t *prev = parent;
994-
// ngx_http_link_func_srv_conf_t *conf = child;
1002+
ngx_http_link_func_srv_conf_t *prev = parent;
1003+
ngx_http_link_func_srv_conf_t *conf = child;
9951004

9961005

9971006
// ngx_conf_merge_str_value(conf->_libname, prev->_libname, "");
@@ -1010,6 +1019,11 @@ ngx_http_link_func_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
10101019
// "lib_name");
10111020
// return NGX_CONF_ERROR;
10121021
// }
1022+
1023+
if (conf->_props == NULL) {
1024+
conf->_props = prev->_props;
1025+
}
1026+
10131027
return NGX_CONF_OK;
10141028
}
10151029

@@ -1659,7 +1673,7 @@ ngx_http_link_func_strdup_with_p(ngx_pool_t *pool, const char *src, size_t len)
16591673
}
16601674

16611675
u_char*
1662-
ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char*key, size_t keylen) {
1676+
ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen) {
16631677
ngx_http_request_t *r = (ngx_http_request_t*)ctx->__r__;
16641678
ngx_list_part_t *part = &r->headers_in.headers.part;
16651679
ngx_table_elt_t *header = part->elts;
@@ -1685,6 +1699,43 @@ ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char*key, size_t keylen
16851699
}
16861700
}
16871701

1702+
u_char*
1703+
ngx_link_func_get_prop(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen) {
1704+
ngx_http_request_t *r = (ngx_http_request_t*)ctx->__r__;
1705+
ngx_http_link_func_srv_conf_t *scf;
1706+
ngx_uint_t nelts, i;
1707+
ngx_keyval_t *keyval;
1708+
1709+
if (r == NULL) {
1710+
ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "Invalid Session access");
1711+
return NULL;
1712+
}
1713+
1714+
scf = ngx_http_get_module_srv_conf(r, ngx_http_link_func_module);
1715+
1716+
if( scf == NULL ) {
1717+
ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "Invalid link function server config");
1718+
return NULL;
1719+
}
1720+
1721+
if(scf->_props == NULL) {
1722+
return NULL;
1723+
}
1724+
1725+
nelts = scf->_props->nelts;
1726+
keyval = scf->_props->elts;
1727+
1728+
for (i = 0; i < nelts; i++) {
1729+
if ( keyval->key.len == keylen && ngx_strncasecmp(keyval->key.data, (u_char*) key, keylen) == 0) {
1730+
/** it is config memory pool, should not reallocate or overwrite **/
1731+
return keyval->value.data;
1732+
}
1733+
keyval++;
1734+
}
1735+
return NULL;
1736+
}
1737+
1738+
16881739
static int
16891740
strpos(const char *haystack, const char *needle) {
16901741
char *p = ngx_strstr(haystack, needle);

src/ngx_link_func_module.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include <stdlib.h>
4141
#include <stdint.h>
4242

43-
#define ngx_link_func_module_version_31 31
43+
#define ngx_link_func_module_version_32 32
4444

4545

4646
#define ngx_link_func_content_type_plaintext "text/plain"
@@ -70,7 +70,8 @@ extern void ngx_link_func_log_info(ngx_link_func_ctx_t *ctx, const char* msg);
7070
extern void ngx_link_func_log_warn(ngx_link_func_ctx_t *ctx, const char* msg);
7171
extern void ngx_link_func_log_err(ngx_link_func_ctx_t *ctx, const char* msg);
7272

73-
extern u_char* ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char*key, size_t keylen);
73+
extern u_char* ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen);
74+
extern u_char* ngx_link_func_get_prop(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen);
7475
extern void* ngx_link_func_get_query_param(ngx_link_func_ctx_t *ctx, const char *key);
7576
extern void* ngx_link_func_palloc(ngx_link_func_ctx_t *ctx, size_t size);
7677
extern void* ngx_link_func_pcalloc(ngx_link_func_ctx_t *ctx, size_t size);

0 commit comments

Comments
 (0)