Skip to content

Commit

Permalink
Update Nginx Get_URI feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Taymindis committed Oct 13, 2020
1 parent 5454265 commit 9d3672c
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 425 deletions.
32 changes: 28 additions & 4 deletions build_test_resources/linkfuntest.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void my_app_simple_get_delay_greeting(ngx_link_func_ctx_t *ctx) {
ngx_link_func_log_info(ctx, "Calling back and log from my_app_simple_get");

char *rep = "2 second delay greeting from ngx_link_func testing";
sleep(2);
sleep(2);
ngx_link_func_write_resp(
ctx,
200,
Expand All @@ -51,10 +51,34 @@ void my_app_simple_get_delay_greeting(ngx_link_func_ctx_t *ctx) {
);
}

void my_app_simple_get_uri(ngx_link_func_ctx_t *ctx) {
ngx_link_func_log_info(ctx, "log from my_app_simple_get_uri");
ngx_link_func_str_t uri;
if (ngx_link_func_get_uri(ctx, &uri) == 0) {
ngx_link_func_write_resp(
ctx,
200,
"200 OK",
"text/plain",
uri.data,
uri.len
);
} else {
ngx_link_func_write_resp(
ctx,
404,
"404 NOT FOUND",
"text/plain",
NULL,
0
);
}
}

void my_app_simple_get_prop_greeting(ngx_link_func_ctx_t *ctx) {
ngx_link_func_log_info(ctx, "Calling back and log from my_app_simple_get");
u_char *defaultGreeting = ngx_link_func_get_prop(ctx, "defaultGreeting", sizeof("defaultGreeting") - 1);
if(defaultGreeting) {
if (defaultGreeting) {
ngx_link_func_write_resp(
ctx,
200,
Expand Down Expand Up @@ -147,7 +171,7 @@ void my_simple_authentication(ngx_link_func_ctx_t *ctx) {
userName = login(userId, userPass);
/** Add input header for downstream response **/
if (userName) {
ngx_link_func_add_header_in(ctx, "userName", sizeof("userName")-1, userName, strlen(userName));
ngx_link_func_add_header_in(ctx, "userName", sizeof("userName") - 1, userName, strlen(userName));
} else {
goto AUTH_FAILED;
}
Expand All @@ -158,7 +182,7 @@ void my_simple_authentication(ngx_link_func_ctx_t *ctx) {
"200 OK",
"text/plain",
"OK",
sizeof("OK")-1
sizeof("OK") - 1
);
}
}
Expand Down
16 changes: 16 additions & 0 deletions build_test_resources/sanity_test_aio_parse.t
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,19 @@ GET /ext_header_foo
foo: foovalue


=== TEST 60: aio threads Get Uri correct
--- main_config eval: $::main_conf
--- config
aio threads=my_thread_pool;
ngx_link_func_lib "NGINX_HTTP_LINK_FUNC_TEST_LIB_PATH";
location = /testLinkFunUri {
ngx_link_func_call "my_app_simple_get_uri";
}
--- request
GET /testLinkFunUri
--- error_code: 200
--- response_headers
Content-Type: text/plain
--- response_body_like eval
qr/testLinkFunUri$/

14 changes: 14 additions & 0 deletions build_test_resources/sanity_test_raw_parse.t
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,17 @@ GET /ext_header_foo
foo: foovalue
=== TEST 10: Get Uri correct
--- config
ngx_link_func_lib "NGINX_HTTP_LINK_FUNC_TEST_LIB_PATH";
location = /testLinkFunUri {
ngx_link_func_call "my_app_simple_get_uri";
}
--- request
GET /testLinkFunUri
--- error_code: 200
--- response_headers
Content-Type: text/plain
--- response_body_like eval
qr/testLinkFunUri$/
4 changes: 2 additions & 2 deletions config
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ ngx_feature_incs="#include <ngx_link_func_module.h>"
ngx_feature_path=
ngx_feature_libs=
# ngx_feature_exit_if_not_found=yes
ngx_feature_test="int ngx_link_func_module_current_version_=ngx_link_func_module_version_33;"
ngx_feature_test="int ngx_link_func_module_current_version_=ngx_link_func_module_version_34;"
. auto/feature

if [ $ngx_found != yes ]; then
echo "ngx_link_func_module.h not found in your system c header path, please copy latest ngx_link_func_module.h to your /usr/include or /usr/local/include or relavent header search path with read and write permission given."
echo "ngx_link_func_module.h not found or version not aligned in your system c header path, please copy latest ngx_link_func_module.h to your /usr/include or /usr/local/include or relavent header search path with read and write permission given."
echo "e.g install -m 644 ../nginx-link-function/src/ngx_link_func_module.h /usr/local/include/"
echo
exit 1
Expand Down
2 changes: 1 addition & 1 deletion freebsd_test/nginx_link_function_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ ngx_http_link_func_pre_configuration(ngx_conf_t *cf) {
return NGX_ERROR;
#endif

#ifndef ngx_link_func_module_version_33
#ifndef ngx_link_func_module_version_34
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, \
please copy latest ngx_http_link_func_module.h to your /usr/include or /usr/local/include or relavent header search path \
with read and write permission.");
Expand Down
31 changes: 22 additions & 9 deletions src/ngx_link_func_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ void ngx_link_func_log_info(ngx_link_func_ctx_t *ctx, const char* msg);
void ngx_link_func_log_warn(ngx_link_func_ctx_t *ctx, const char* msg);
void ngx_link_func_log_err(ngx_link_func_ctx_t *ctx, const char* msg);
char *ngx_link_func_strdup(ngx_link_func_ctx_t *ctx, const char *src);
int ngx_link_func_get_uri(ngx_link_func_ctx_t *ctx, ngx_link_func_str_t *str);
u_char* ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen);
u_char* ngx_link_func_get_prop(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen);
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 );
Expand Down Expand Up @@ -641,7 +642,7 @@ ngx_http_link_func_proceed_init_calls(ngx_cycle_t* cycle, ngx_http_link_func_sr
ngx_http_link_func_app_cycle_handler func;

#if __FreeBSD__
(void) dlerror();
(void) dlerror();
#endif

*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_init_cycle");
Expand Down Expand Up @@ -745,7 +746,7 @@ ngx_http_link_func_pre_configuration(ngx_conf_t *cf) {
return NGX_ERROR;
#endif

#ifndef ngx_link_func_module_version_33
#ifndef ngx_link_func_module_version_34
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, \
please copy latest ngx_http_link_func_module.h to your /usr/include or /usr/local/include or relavent header search path \
with read and write permission.");
Expand Down Expand Up @@ -813,7 +814,7 @@ ngx_http_link_func_application_compatibility_check(ngx_conf_t *cf, ngx_http_core
}

#if __FreeBSD__
(void) dlerror();
(void) dlerror();
#endif
/* * check init function block, this version has to be at least init with empty function * */
ngx_http_link_func_app_cycle_handler func;
Expand All @@ -826,7 +827,7 @@ ngx_http_link_func_application_compatibility_check(ngx_conf_t *cf, ngx_http_core
}

#if __FreeBSD__
(void) dlerror();
(void) dlerror();
#endif

*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_exit_cycle");
Expand All @@ -846,9 +847,9 @@ ngx_http_link_func_application_compatibility_check(ngx_conf_t *cf, ngx_http_core

ngx_http_link_func_loc_conf_t *lcf = cflq->_loc_conf;
if ( lcf && lcf->_method_name.len > 0 ) {

#if __FreeBSD__
(void) dlerror();
(void) dlerror();
#endif

*(void**)(&lcf->_handler) = dlsym(scf->_app, (const char*)lcf->_method_name.data);
Expand Down Expand Up @@ -924,9 +925,9 @@ ngx_http_link_func_module_init(ngx_cycle_t *cycle) {
if ( ( lcf->_handler = ngx_http_link_func_get_duplicate_handler(scf, &lcf->_method_name) ) == NULL ) {

#if __FreeBSD__
(void) dlerror();
(void) dlerror();
#endif

*(void**)(&lcf->_handler) = dlsym(scf->_app, (const char*)lcf->_method_name.data);
if ((error = dlerror()) != NULL) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "Error function load: %s", error);
Expand Down Expand Up @@ -1026,7 +1027,7 @@ ngx_http_link_func_process_exit(ngx_cycle_t *cycle) {
ngx_http_link_func_app_cycle_handler func;

#if __FreeBSD__
(void) dlerror();
(void) dlerror();
#endif

*(void**)(&func) = dlsym(scf->_app, (const char*)"ngx_link_func_exit_cycle");
Expand Down Expand Up @@ -1848,6 +1849,18 @@ ngx_link_func_strdup(ngx_link_func_ctx_t *ctx, const char *src) {
return dst;
}

int
ngx_link_func_get_uri(ngx_link_func_ctx_t *ctx, ngx_link_func_str_t *str) {
ngx_http_request_t *r = (ngx_http_request_t*)ctx->__r__;
size_t len = r->uri.len;
if (len > 0) {
str->len = len;
str->data = r->uri.data;
return 0; // NGX_OK
}
return -1; // NGX_ERROR
}

static u_char*
ngx_http_link_func_strdup_with_p(ngx_pool_t *pool, const char *src, size_t len) {
u_char *dst;
Expand Down
13 changes: 12 additions & 1 deletion src/ngx_link_func_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <stdlib.h>
#include <stdint.h>

#define ngx_link_func_module_version_33 33
#define ngx_link_func_module_version_34 34


#define ngx_link_func_content_type_plaintext "text/plain"
Expand All @@ -53,6 +53,11 @@
typedef unsigned char u_char;
#endif

typedef struct {
size_t len;
u_char *data;
} ngx_link_func_str_t;

typedef struct {
char *req_args; // Uri Args
u_char *req_body; // Request Body
Expand Down Expand Up @@ -92,6 +97,12 @@ extern void ngx_link_func_log_info(ngx_link_func_ctx_t *ctx, const char* msg);
extern void ngx_link_func_log_warn(ngx_link_func_ctx_t *ctx, const char* msg);
extern void ngx_link_func_log_err(ngx_link_func_ctx_t *ctx, const char* msg);

/**
* example: ngx_link_func_str_t uri;
* if(ngx_link_func_get_uri(ctx, &uri) == 0) { TODO success }
*
*/
extern int ngx_link_func_get_uri(ngx_link_func_ctx_t *ctx, ngx_link_func_str_t *str);
extern u_char* ngx_link_func_get_header(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen);
extern u_char* ngx_link_func_get_prop(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen);
extern void* ngx_link_func_get_query_param(ngx_link_func_ctx_t *ctx, const char *key);
Expand Down
Binary file removed t/liblinkfuntest.so
Binary file not shown.
Loading

0 comments on commit 9d3672c

Please sign in to comment.