├── config ├── README.md └── ngx_http_lower_upper_case.c /config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_lower_upper_case_module 2 | HTTP_MODULES="$HTTP_MODULES ngx_http_lower_upper_case_module" 3 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_lower_upper_case.c" 4 | CFLAGS="$CFLAGS -I$ngx_addon_dir" 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Nginx lower-/uppercase 2 | ------- 3 | 4 | This module simply uppercases or lowercases a string and saves it into a new variable. 5 | 6 | It knows only two directives: upper and lower 7 | 8 | Example 9 | =============== 10 | 11 | set $var1 "hello"; 12 | set $var2 "WORLD"; 13 | 14 | upper $var3 "$var1 $var2"; # $var3 will be "HELLO WORLD" 15 | lower $var4 "$var1 $var2"; # $var4 will be "hello world" 16 | 17 | -------------------------------------------------------------------------------- /ngx_http_lower_upper_case.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define UPPER 0 6 | #define LOWER 1 7 | 8 | typedef struct { 9 | ngx_conf_t *cf; 10 | ngx_array_t *lucases; 11 | } ngx_http_lower_upper_case_conf_t; 12 | 13 | typedef struct { 14 | uintptr_t action:1; 15 | ngx_str_t *src_variable; 16 | ngx_array_t *src_lengths; 17 | ngx_array_t *src_values; 18 | } ngx_http_lucase_t; 19 | 20 | // create confs 21 | static void *ngx_http_lower_upper_case_create_loc_conf(ngx_conf_t *cf); 22 | static char *ngx_http_lower_upper_case_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child); 23 | 24 | static char *ngx_http_lower_upper_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 25 | static ngx_int_t ngx_http_do_lower_upper(ngx_http_request_t *r, ngx_http_variable_value_t *v, uintptr_t data); 26 | 27 | 28 | static ngx_command_t ngx_http_lower_upper_case_commands[] = { 29 | { ngx_string("upper"), 30 | NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2, 31 | ngx_http_lower_upper_directive, 32 | NGX_HTTP_LOC_CONF_OFFSET, 33 | 0, 34 | NULL }, 35 | { ngx_string("lower"), 36 | NGX_HTTP_LOC_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2, 37 | ngx_http_lower_upper_directive, 38 | NGX_HTTP_LOC_CONF_OFFSET, 39 | 0, 40 | NULL }, 41 | ngx_null_command 42 | }; 43 | 44 | static ngx_http_module_t ngx_http_lower_upper_case_module_ctx = { 45 | NULL, 46 | NULL, 47 | 48 | NULL, 49 | NULL, 50 | 51 | NULL, 52 | NULL, 53 | 54 | ngx_http_lower_upper_case_create_loc_conf, 55 | ngx_http_lower_upper_case_merge_loc_conf 56 | }; 57 | 58 | ngx_module_t ngx_http_lower_upper_case_module = { 59 | NGX_MODULE_V1, 60 | &ngx_http_lower_upper_case_module_ctx, 61 | ngx_http_lower_upper_case_commands, 62 | NGX_HTTP_MODULE, 63 | NULL, 64 | NULL, 65 | NULL, 66 | NULL, 67 | NULL, 68 | NULL, 69 | NULL, 70 | NGX_MODULE_V1_PADDING 71 | }; 72 | 73 | static void * 74 | ngx_http_lower_upper_case_create_loc_conf(ngx_conf_t *cf) 75 | { 76 | ngx_http_lower_upper_case_conf_t *lucf; 77 | 78 | lucf = ngx_pcalloc(cf->pool, sizeof(ngx_http_lower_upper_case_conf_t)); 79 | if (lucf == NULL) 80 | { 81 | return NGX_CONF_ERROR; 82 | } 83 | 84 | lucf->cf = cf; 85 | lucf->lucases = NULL; 86 | 87 | return lucf; 88 | } 89 | 90 | static char * 91 | ngx_http_lower_upper_case_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) 92 | { 93 | ngx_http_lower_upper_case_conf_t *prev = parent; 94 | ngx_http_lower_upper_case_conf_t *conf = child; 95 | 96 | if (prev->lucases != NULL) 97 | { 98 | conf->lucases = prev->lucases; 99 | } 100 | 101 | return NGX_CONF_OK; 102 | } 103 | 104 | static char * 105 | ngx_http_lower_upper_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 106 | { 107 | ngx_http_lower_upper_case_conf_t *lucf = conf; 108 | ngx_http_lucase_t *lucase; 109 | ngx_str_t *variable; 110 | ngx_http_script_compile_t sc; 111 | ngx_http_variable_t *v; 112 | 113 | ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); 114 | 115 | if (lucf->lucases == NULL) { 116 | lucf->lucases = ngx_array_create(cf->pool, 1, sizeof(ngx_http_lucase_t)); 117 | if (lucf->lucases == NULL) { 118 | return NGX_CONF_ERROR; 119 | } 120 | } 121 | 122 | lucase = ngx_array_push(lucf->lucases); 123 | if (lucase == NULL) { 124 | return NGX_CONF_ERROR; 125 | } 126 | 127 | lucase->src_lengths = NULL; 128 | lucase->src_values = NULL; 129 | 130 | variable = cf->args->elts; 131 | if (variable[1].data[0] != '$') { 132 | ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid variable name \"%V\"", variable[1]); 133 | return NGX_CONF_ERROR; 134 | } 135 | 136 | variable[1].len--; 137 | variable[1].data++; 138 | 139 | v = ngx_http_add_variable(cf, &variable[1], NGX_HTTP_VAR_CHANGEABLE); 140 | if (v == NULL) { 141 | return NGX_CONF_ERROR; 142 | } 143 | 144 | lucase->src_variable = &variable[2]; 145 | 146 | sc.cf = cf; 147 | sc.source = lucase->src_variable; 148 | sc.lengths = &lucase->src_lengths; 149 | sc.values = &lucase->src_values; 150 | sc.variables = ngx_http_script_variables_count(lucase->src_variable); 151 | sc.complete_lengths = 1; 152 | sc.complete_values = 1; 153 | 154 | if (ngx_http_script_compile(&sc) != NGX_OK) { 155 | return NGX_CONF_ERROR; 156 | } 157 | 158 | if (variable[0].data[0] == 'u') { 159 | lucase->action = UPPER; 160 | } else { 161 | lucase->action = LOWER; 162 | } 163 | 164 | v->data = lucf->lucases->nelts - 1; 165 | v->get_handler = ngx_http_do_lower_upper; 166 | 167 | return NGX_CONF_OK; 168 | } 169 | 170 | 171 | static ngx_int_t 172 | ngx_http_do_lower_upper(ngx_http_request_t *r, ngx_http_variable_value_t *dst_v, uintptr_t data) 173 | { 174 | ngx_http_lower_upper_case_conf_t *lucf = ngx_http_get_module_loc_conf(r, ngx_http_lower_upper_case_module); 175 | ngx_uint_t i; 176 | //u_char *tmp_void; 177 | ngx_http_lucase_t *lucase; 178 | 179 | //tmp_void = lucf->lucases->elts; 180 | //lucase = (ngx_http_lucase_t*) (tmp_void + (data * lucf->lucases->size)); 181 | 182 | lucase = (ngx_http_lucase_t*) ((u_char*)lucf->lucases->elts + (data * lucf->lucases->size)); 183 | 184 | if (ngx_http_script_run(r, lucase->src_variable, lucase->src_lengths->elts, 0, lucase->src_values->elts) == NULL) { 185 | ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, "source evaluation failed"); 186 | return NGX_ERROR; 187 | } 188 | 189 | dst_v->len = lucase->src_variable->len; 190 | 191 | dst_v->data = ngx_pcalloc(r->pool, lucase->src_variable->len); 192 | if (dst_v->data == NULL) { 193 | return NGX_ERROR; 194 | } 195 | 196 | if (lucase->action == LOWER) { 197 | for (i = 0; i < dst_v->len; i++) { 198 | dst_v->data[i] = ngx_tolower(lucase->src_variable->data[i]); 199 | } 200 | } else { 201 | for (i = 0; i < dst_v->len; i++) { 202 | dst_v->data[i] = ngx_toupper(lucase->src_variable->data[i]); 203 | } 204 | } 205 | 206 | dst_v->valid = 1; 207 | 208 | return NGX_OK; 209 | } 210 | --------------------------------------------------------------------------------