Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 1 | |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 2 | /* |
Igor Sysoev | ff8da91 | 2004-09-29 16:00:49 +0000 | [diff] [blame] | 3 | * Copyright (C) Igor Sysoev |
Igor Sysoev | d90282d | 2004-09-28 08:34:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 7 | #include <ngx_config.h> |
| 8 | #include <ngx_core.h> |
| 9 | #include <ngx_http.h> |
| 10 | |
| 11 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 12 | #define ngx_http_script_exit (u_char *) &ngx_http_script_exit_code |
| 13 | |
| 14 | static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL; |
| 15 | |
| 16 | |
| 17 | ngx_uint_t |
| 18 | ngx_http_script_variables_count(ngx_str_t *value) |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 19 | { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 20 | ngx_uint_t i, n; |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 21 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 22 | for (n = 0, i = 0; i < value->len; i++) { |
| 23 | if (value->data[i] == '$') { |
| 24 | n++; |
| 25 | } |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 26 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 27 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 28 | return n; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | ngx_int_t |
| 33 | ngx_http_script_compile(ngx_http_script_compile_t *sc) |
| 34 | { |
| 35 | u_char ch; |
| 36 | size_t size; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 37 | ngx_int_t index, *p; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 38 | ngx_str_t name; |
| 39 | uintptr_t *code; |
| 40 | ngx_uint_t i, n, bracket; |
| 41 | ngx_http_script_var_code_t *var_code; |
| 42 | ngx_http_script_copy_code_t *copy; |
| 43 | ngx_http_script_copy_capture_code_t *copy_capture; |
| 44 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 45 | if (sc->flushes && *sc->flushes == NULL) { |
| 46 | n = sc->variables ? sc->variables : 1; |
| 47 | *sc->flushes = ngx_array_create(sc->cf->pool, n, sizeof(ngx_uint_t)); |
| 48 | if (*sc->flushes == NULL) { |
| 49 | return NGX_ERROR; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 54 | if (*sc->lengths == NULL) { |
| 55 | n = sc->variables * (2 * sizeof(ngx_http_script_copy_code_t) |
| 56 | + sizeof(ngx_http_script_var_code_t)) |
| 57 | + sizeof(uintptr_t); |
| 58 | |
| 59 | *sc->lengths = ngx_array_create(sc->cf->pool, n, 1); |
| 60 | if (*sc->lengths == NULL) { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 61 | return NGX_ERROR; |
| 62 | } |
| 63 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 64 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 65 | |
| 66 | if (*sc->values == NULL) { |
| 67 | n = (sc->variables * (2 * sizeof(ngx_http_script_copy_code_t) |
| 68 | + sizeof(ngx_http_script_var_code_t)) |
| 69 | + sizeof(uintptr_t) |
| 70 | + sc->source->len |
| 71 | + sizeof(uintptr_t) - 1) |
| 72 | & ~(sizeof(uintptr_t) - 1); |
| 73 | |
| 74 | *sc->values = ngx_array_create(sc->cf->pool, n, 1); |
| 75 | if (*sc->values == NULL) { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 76 | return NGX_ERROR; |
| 77 | } |
| 78 | } |
| 79 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 80 | sc->variables = 0; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 81 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 82 | for (i = 0; i < sc->source->len; /* void */ ) { |
| 83 | |
| 84 | name.len = 0; |
| 85 | |
| 86 | if (sc->source->data[i] == '$') { |
| 87 | |
| 88 | if (++i == sc->source->len) { |
| 89 | goto invalid_variable; |
| 90 | } |
| 91 | |
| 92 | if (sc->source->data[i] >= '1' && sc->source->data[i] <= '9') { |
| 93 | |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 94 | n = sc->source->data[i] - '0'; |
| 95 | |
| 96 | if (sc->captures_mask & (1 << n)) { |
| 97 | sc->dup_capture = 1; |
| 98 | } |
| 99 | |
| 100 | sc->captures_mask |= 1 << n; |
| 101 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 102 | copy_capture = ngx_http_script_add_code(*sc->lengths, |
| 103 | sizeof(ngx_http_script_copy_capture_code_t), |
| 104 | NULL); |
| 105 | if (copy_capture == NULL) { |
| 106 | return NGX_ERROR; |
| 107 | } |
| 108 | |
| 109 | copy_capture->code = (ngx_http_script_code_pt) |
| 110 | ngx_http_script_copy_capture_len_code; |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 111 | copy_capture->n = 2 * n; |
| 112 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 113 | |
| 114 | copy_capture = ngx_http_script_add_code(*sc->values, |
| 115 | sizeof(ngx_http_script_copy_capture_code_t), |
| 116 | &sc->main); |
| 117 | if (copy_capture == NULL) { |
| 118 | return NGX_ERROR; |
| 119 | } |
| 120 | |
| 121 | copy_capture->code = ngx_http_script_copy_capture_code; |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 122 | copy_capture->n = 2 * n; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 123 | |
Igor Sysoev | 7f7846d | 2006-04-26 09:52:47 +0000 | [diff] [blame] | 124 | if (sc->ncaptures < n) { |
| 125 | sc->ncaptures = n; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 128 | i++; |
| 129 | |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | if (sc->source->data[i] == '{') { |
| 134 | bracket = 1; |
| 135 | |
| 136 | if (++i == sc->source->len) { |
| 137 | goto invalid_variable; |
| 138 | } |
| 139 | |
| 140 | name.data = &sc->source->data[i]; |
| 141 | |
| 142 | } else { |
| 143 | bracket = 0; |
| 144 | name.data = &sc->source->data[i]; |
| 145 | } |
| 146 | |
| 147 | for ( /* void */ ; i < sc->source->len; i++, name.len++) { |
| 148 | ch = sc->source->data[i]; |
| 149 | |
| 150 | if (ch == '}' && bracket) { |
| 151 | i++; |
| 152 | bracket = 0; |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | if ((ch >= 'A' && ch <= 'Z') |
| 157 | || (ch >= 'a' && ch <= 'z') |
| 158 | || (ch >= '0' && ch <= '9') |
| 159 | || ch == '_') |
| 160 | { |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | if (bracket) { |
| 168 | ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, |
| 169 | "the closing bracket in \"%V\" " |
| 170 | "variable is missing", &name); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 171 | return NGX_ERROR; |
| 172 | } |
| 173 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 174 | if (name.len == 0) { |
| 175 | goto invalid_variable; |
| 176 | } |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 177 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 178 | sc->variables++; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 179 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 180 | index = ngx_http_get_variable_index(sc->cf, &name); |
| 181 | |
| 182 | if (index == NGX_ERROR) { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 183 | return NGX_ERROR; |
| 184 | } |
| 185 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 186 | if (sc->flushes) { |
| 187 | p = ngx_array_push(*sc->flushes); |
| 188 | if (p == NULL) { |
| 189 | return NGX_ERROR; |
| 190 | } |
| 191 | |
| 192 | *p = index; |
| 193 | } |
| 194 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 195 | var_code = ngx_http_script_add_code(*sc->lengths, |
| 196 | sizeof(ngx_http_script_var_code_t), |
| 197 | NULL); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 198 | if (var_code == NULL) { |
| 199 | return NGX_ERROR; |
| 200 | } |
| 201 | |
| 202 | var_code->code = (ngx_http_script_code_pt) |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 203 | ngx_http_script_copy_var_len_code; |
| 204 | var_code->index = (uintptr_t) index; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 205 | |
| 206 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 207 | var_code = ngx_http_script_add_code(*sc->values, |
| 208 | sizeof(ngx_http_script_var_code_t), |
| 209 | &sc->main); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 210 | if (var_code == NULL) { |
| 211 | return NGX_ERROR; |
| 212 | } |
| 213 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 214 | var_code->code = ngx_http_script_copy_var_code; |
| 215 | var_code->index = (uintptr_t) index; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 216 | |
| 217 | continue; |
| 218 | } |
| 219 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 220 | if (sc->source->data[i] == '?' && sc->compile_args) { |
| 221 | sc->args = 1; |
| 222 | sc->compile_args = 0; |
| 223 | |
| 224 | code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), |
| 225 | &sc->main); |
| 226 | if (code == NULL) { |
| 227 | return NGX_ERROR; |
| 228 | } |
| 229 | |
| 230 | *code = (uintptr_t) ngx_http_script_start_args_code; |
| 231 | |
| 232 | i++; |
| 233 | |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | name.data = &sc->source->data[i]; |
| 238 | |
| 239 | while (i < sc->source->len |
| 240 | && sc->source->data[i] != '$' |
| 241 | && !(sc->source->data[i] == '?' && sc->compile_args)) |
| 242 | { |
| 243 | i++; |
| 244 | name.len++; |
| 245 | } |
| 246 | |
| 247 | sc->size += name.len; |
| 248 | |
| 249 | copy = ngx_http_script_add_code(*sc->lengths, |
| 250 | sizeof(ngx_http_script_copy_code_t), |
| 251 | NULL); |
| 252 | if (copy == NULL) { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 253 | return NGX_ERROR; |
| 254 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 255 | |
| 256 | copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code; |
| 257 | copy->len = name.len; |
| 258 | |
| 259 | size = (sizeof(ngx_http_script_copy_code_t) + name.len |
| 260 | + sizeof(uintptr_t) - 1) |
| 261 | & ~(sizeof(uintptr_t) - 1); |
| 262 | |
| 263 | copy = ngx_http_script_add_code(*sc->values, size, &sc->main); |
| 264 | if (copy == NULL) { |
| 265 | return NGX_ERROR; |
| 266 | } |
| 267 | |
| 268 | copy->code = ngx_http_script_copy_code; |
| 269 | copy->len = name.len; |
| 270 | |
| 271 | ngx_memcpy((u_char *) copy + sizeof(ngx_http_script_copy_code_t), |
| 272 | name.data, name.len); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 275 | if (sc->complete_lengths) { |
| 276 | code = ngx_http_script_add_code(*sc->lengths, sizeof(uintptr_t), NULL); |
| 277 | if (code == NULL) { |
| 278 | return NGX_ERROR; |
| 279 | } |
| 280 | |
| 281 | *code = (uintptr_t) NULL; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 284 | if (sc->complete_values) { |
| 285 | code = ngx_http_script_add_code(*sc->values, sizeof(uintptr_t), |
| 286 | &sc->main); |
| 287 | if (code == NULL) { |
| 288 | return NGX_ERROR; |
| 289 | } |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 290 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 291 | *code = (uintptr_t) NULL; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 294 | return NGX_OK; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 295 | |
| 296 | invalid_variable: |
| 297 | |
| 298 | ngx_conf_log_error(NGX_LOG_EMERG, sc->cf, 0, "invalid variable name"); |
| 299 | |
| 300 | return NGX_ERROR; |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
Igor Sysoev | 8fea885 | 2006-03-15 09:53:04 +0000 | [diff] [blame] | 304 | u_char * |
| 305 | ngx_http_script_run(ngx_http_request_t *r, ngx_str_t *value, |
| 306 | void *code_lengths, size_t len, void *code_values) |
| 307 | { |
Igor Sysoev | 2c8f057 | 2007-03-30 19:00:34 +0000 | [diff] [blame] | 308 | ngx_uint_t i; |
| 309 | ngx_http_script_code_pt code; |
| 310 | ngx_http_script_len_code_pt lcode; |
| 311 | ngx_http_script_engine_t e; |
| 312 | ngx_http_core_main_conf_t *cmcf; |
| 313 | |
| 314 | cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); |
| 315 | |
| 316 | for (i = 0; i < cmcf->variables.nelts; i++) { |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 317 | if (r->variables[i].no_cacheable) { |
Igor Sysoev | 2c8f057 | 2007-03-30 19:00:34 +0000 | [diff] [blame] | 318 | r->variables[i].valid = 0; |
| 319 | r->variables[i].not_found = 0; |
| 320 | } |
| 321 | } |
Igor Sysoev | 8fea885 | 2006-03-15 09:53:04 +0000 | [diff] [blame] | 322 | |
| 323 | ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); |
| 324 | |
| 325 | e.ip = code_lengths; |
| 326 | e.request = r; |
| 327 | e.flushed = 1; |
| 328 | |
| 329 | while (*(uintptr_t *) e.ip) { |
| 330 | lcode = *(ngx_http_script_len_code_pt *) e.ip; |
| 331 | len += lcode(&e); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | value->len = len; |
| 336 | value->data = ngx_palloc(r->pool, len); |
| 337 | if (value->data == NULL) { |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | e.ip = code_values; |
| 342 | e.pos = value->data; |
| 343 | |
| 344 | while (*(uintptr_t *) e.ip) { |
| 345 | code = *(ngx_http_script_code_pt *) e.ip; |
| 346 | code((ngx_http_script_engine_t *) &e); |
| 347 | } |
| 348 | |
| 349 | return e.pos; |
| 350 | } |
| 351 | |
| 352 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 353 | void |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 354 | ngx_http_script_flush_no_cacheable_variables(ngx_http_request_t *r, |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 355 | ngx_array_t *indices) |
| 356 | { |
| 357 | ngx_uint_t n, *index; |
| 358 | |
| 359 | if (indices) { |
| 360 | index = indices->elts; |
| 361 | for (n = 0; n < indices->nelts; n++) { |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 362 | if (r->variables[index[n]].no_cacheable) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 363 | r->variables[index[n]].valid = 0; |
| 364 | r->variables[index[n]].not_found = 0; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 371 | void * |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 372 | ngx_http_script_start_code(ngx_pool_t *pool, ngx_array_t **codes, size_t size) |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 373 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 374 | if (*codes == NULL) { |
| 375 | *codes = ngx_array_create(pool, 256, 1); |
| 376 | if (*codes == NULL) { |
| 377 | return NULL; |
| 378 | } |
| 379 | } |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 380 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 381 | return ngx_array_push_n(*codes, size); |
| 382 | } |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 383 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 384 | |
| 385 | void * |
| 386 | ngx_http_script_add_code(ngx_array_t *codes, size_t size, void *code) |
| 387 | { |
| 388 | u_char *elts, **p; |
| 389 | void *new; |
| 390 | |
| 391 | elts = codes->elts; |
| 392 | |
| 393 | new = ngx_array_push_n(codes, size); |
| 394 | if (new == NULL) { |
| 395 | return NGX_CONF_ERROR; |
| 396 | } |
| 397 | |
| 398 | if (code) { |
| 399 | if (elts != codes->elts) { |
| 400 | p = code; |
| 401 | *p += (u_char *) codes->elts - elts; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return new; |
| 406 | } |
Igor Sysoev | 02025fd | 2005-01-18 13:03:58 +0000 | [diff] [blame] | 407 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 408 | |
| 409 | size_t |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 410 | ngx_http_script_copy_len_code(ngx_http_script_engine_t *e) |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 411 | { |
| 412 | ngx_http_script_copy_code_t *code; |
| 413 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 414 | code = (ngx_http_script_copy_code_t *) e->ip; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 415 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 416 | e->ip += sizeof(ngx_http_script_copy_code_t); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 417 | |
| 418 | return code->len; |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 422 | void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 423 | ngx_http_script_copy_code(ngx_http_script_engine_t *e) |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 424 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 425 | ngx_http_script_copy_code_t *code; |
| 426 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 427 | code = (ngx_http_script_copy_code_t *) e->ip; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 428 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 429 | if (!e->skip) { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 430 | e->pos = ngx_copy(e->pos, e->ip + sizeof(ngx_http_script_copy_code_t), |
| 431 | code->len); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 432 | } |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 433 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 434 | e->ip += sizeof(ngx_http_script_copy_code_t) |
| 435 | + ((code->len + sizeof(uintptr_t) - 1) & ~(sizeof(uintptr_t) - 1)); |
| 436 | |
Igor Sysoev | 5836423 | 2006-11-14 12:45:03 +0000 | [diff] [blame] | 437 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 438 | "http script copy: \"%V\"", &e->buf); |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 442 | size_t |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 443 | ngx_http_script_copy_var_len_code(ngx_http_script_engine_t *e) |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 444 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 445 | ngx_http_variable_value_t *value; |
| 446 | ngx_http_script_var_code_t *code; |
| 447 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 448 | code = (ngx_http_script_var_code_t *) e->ip; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 449 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 450 | e->ip += sizeof(ngx_http_script_var_code_t); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 451 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 452 | if (e->flushed) { |
| 453 | value = ngx_http_get_indexed_variable(e->request, code->index); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 454 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 455 | } else { |
| 456 | value = ngx_http_get_flushed_variable(e->request, code->index); |
| 457 | } |
| 458 | |
| 459 | if (value && !value->not_found) { |
| 460 | return value->len; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Igor Sysoev | f6e1fe3 | 2005-10-04 10:38:53 +0000 | [diff] [blame] | 463 | return 0; |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 467 | void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 468 | ngx_http_script_copy_var_code(ngx_http_script_engine_t *e) |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 469 | { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 470 | ngx_http_variable_value_t *value; |
| 471 | ngx_http_script_var_code_t *code; |
| 472 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 473 | code = (ngx_http_script_var_code_t *) e->ip; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 474 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 475 | e->ip += sizeof(ngx_http_script_var_code_t); |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 476 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 477 | if (!e->skip) { |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 478 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 479 | if (e->flushed) { |
| 480 | value = ngx_http_get_indexed_variable(e->request, code->index); |
| 481 | |
| 482 | } else { |
| 483 | value = ngx_http_get_flushed_variable(e->request, code->index); |
| 484 | } |
| 485 | |
| 486 | if (value && !value->not_found) { |
| 487 | e->pos = ngx_copy(e->pos, value->data, value->len); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 488 | |
Igor Sysoev | 5836423 | 2006-11-14 12:45:03 +0000 | [diff] [blame] | 489 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, |
| 490 | e->request->connection->log, 0, |
| 491 | "http script var: \"%V\"", &e->buf); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | |
| 497 | size_t |
| 498 | ngx_http_script_copy_capture_len_code(ngx_http_script_engine_t *e) |
| 499 | { |
| 500 | ngx_http_script_copy_capture_code_t *code; |
| 501 | |
| 502 | code = (ngx_http_script_copy_capture_code_t *) e->ip; |
| 503 | |
| 504 | e->ip += sizeof(ngx_http_script_copy_capture_code_t); |
| 505 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 506 | if (code->n < e->ncaptures) { |
| 507 | if ((e->args || e->quote) |
| 508 | && (e->request->quoted_uri || e->request->plus_in_uri)) |
| 509 | { |
| 510 | return e->captures[code->n + 1] - e->captures[code->n] |
Igor Sysoev | af3b7ea | 2006-05-31 14:11:45 +0000 | [diff] [blame] | 511 | + 2 * ngx_escape_uri(NULL, |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 512 | &e->line.data[e->captures[code->n]], |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 513 | e->captures[code->n + 1] - e->captures[code->n], |
| 514 | NGX_ESCAPE_ARGS); |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 515 | } else { |
| 516 | return e->captures[code->n + 1] - e->captures[code->n]; |
| 517 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 518 | } |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 519 | |
| 520 | return 0; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
| 524 | void |
| 525 | ngx_http_script_copy_capture_code(ngx_http_script_engine_t *e) |
| 526 | { |
| 527 | ngx_http_script_copy_capture_code_t *code; |
| 528 | |
| 529 | code = (ngx_http_script_copy_capture_code_t *) e->ip; |
| 530 | |
| 531 | e->ip += sizeof(ngx_http_script_copy_capture_code_t); |
| 532 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 533 | if (code->n < e->ncaptures) { |
| 534 | if ((e->args || e->quote) |
| 535 | && (e->request->quoted_uri || e->request->plus_in_uri)) |
| 536 | { |
| 537 | e->pos = (u_char *) ngx_escape_uri(e->pos, |
| 538 | &e->line.data[e->captures[code->n]], |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 539 | e->captures[code->n + 1] - e->captures[code->n], |
| 540 | NGX_ESCAPE_ARGS); |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 541 | } else { |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 542 | e->pos = ngx_copy(e->pos, |
| 543 | &e->line.data[e->captures[code->n]], |
| 544 | e->captures[code->n + 1] - e->captures[code->n]); |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 545 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 549 | "http script capture: \"%V\"", &e->buf); |
| 550 | } |
| 551 | |
| 552 | |
| 553 | void |
| 554 | ngx_http_script_start_args_code(ngx_http_script_engine_t *e) |
| 555 | { |
| 556 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 557 | "http script args"); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 558 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 559 | e->args = e->pos; |
| 560 | e->ip += sizeof(uintptr_t); |
| 561 | } |
| 562 | |
| 563 | |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame] | 564 | |
| 565 | #if (NGX_PCRE) |
| 566 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 567 | void |
| 568 | ngx_http_script_regex_start_code(ngx_http_script_engine_t *e) |
| 569 | { |
| 570 | size_t len; |
| 571 | ngx_int_t rc; |
| 572 | ngx_uint_t n; |
| 573 | ngx_http_request_t *r; |
| 574 | ngx_http_script_engine_t le; |
| 575 | ngx_http_script_len_code_pt lcode; |
| 576 | ngx_http_script_regex_code_t *code; |
| 577 | |
| 578 | code = (ngx_http_script_regex_code_t *) e->ip; |
| 579 | |
| 580 | r = e->request; |
| 581 | |
| 582 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 583 | "http script regex: \"%V\"", &code->name); |
| 584 | |
| 585 | if (code->uri) { |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 586 | e->line = r->uri; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 587 | } else { |
| 588 | e->sp--; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 589 | e->line.len = e->sp->len; |
| 590 | e->line.data = e->sp->data; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 593 | rc = ngx_regex_exec(code->regex, &e->line, e->captures, code->ncaptures); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 594 | |
| 595 | if (rc == NGX_REGEX_NO_MATCHED) { |
| 596 | if (e->log) { |
| 597 | ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 598 | "\"%V\" does not match \"%V\"", |
| 599 | &code->name, &e->line); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 602 | e->ncaptures = 0; |
| 603 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 604 | if (code->test) { |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 605 | if (code->negative_test) { |
| 606 | e->sp->len = 1; |
| 607 | e->sp->data = (u_char *) "1"; |
| 608 | |
| 609 | } else { |
| 610 | e->sp->len = 0; |
| 611 | e->sp->data = (u_char *) ""; |
| 612 | } |
| 613 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 614 | e->sp++; |
| 615 | |
| 616 | e->ip += sizeof(ngx_http_script_regex_code_t); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | e->ip += code->next; |
Igor Sysoev | 02f742b | 2005-04-08 15:18:55 +0000 | [diff] [blame] | 621 | return; |
| 622 | } |
| 623 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 624 | if (rc < 0) { |
| 625 | ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, |
| 626 | ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"", |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 627 | rc, &e->line, &code->name); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 628 | |
| 629 | e->ip = ngx_http_script_exit; |
| 630 | e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (e->log) { |
| 635 | ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 636 | "\"%V\" matches \"%V\"", &code->name, &e->line); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 639 | e->ncaptures = code->ncaptures; |
| 640 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 641 | if (code->test) { |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 642 | if (code->negative_test) { |
| 643 | e->sp->len = 0; |
| 644 | e->sp->data = (u_char *) ""; |
| 645 | |
| 646 | } else { |
| 647 | e->sp->len = 1; |
| 648 | e->sp->data = (u_char *) "1"; |
| 649 | } |
| 650 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 651 | e->sp++; |
| 652 | |
| 653 | e->ip += sizeof(ngx_http_script_regex_code_t); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | if (code->status) { |
| 658 | e->status = code->status; |
| 659 | |
| 660 | if (!code->redirect) { |
| 661 | e->ip = ngx_http_script_exit; |
| 662 | return; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (code->uri) { |
| 667 | r->internal = 1; |
| 668 | r->valid_unparsed_uri = 0; |
| 669 | |
| 670 | if (code->break_cycle) { |
| 671 | r->valid_location = 0; |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 672 | r->uri_changed = 0; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 673 | |
| 674 | } else { |
| 675 | r->uri_changed = 1; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | if (code->lengths == NULL) { |
| 680 | e->buf.len = code->size; |
| 681 | |
| 682 | if (code->uri) { |
| 683 | if (rc && (r->quoted_uri || r->plus_in_uri)) { |
| 684 | e->buf.len += 2 * ngx_escape_uri(NULL, r->uri.data, r->uri.len, |
| 685 | NGX_ESCAPE_ARGS); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | for (n = 1; n < (ngx_uint_t) rc; n++) { |
| 690 | e->buf.len += e->captures[2 * n + 1] - e->captures[2 * n]; |
| 691 | } |
| 692 | |
| 693 | } else { |
| 694 | ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); |
| 695 | |
| 696 | le.ip = code->lengths->elts; |
Igor Sysoev | 3f8dc59 | 2006-08-28 16:57:48 +0000 | [diff] [blame] | 697 | le.line = e->line; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 698 | le.request = r; |
| 699 | le.captures = e->captures; |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 700 | le.ncaptures = e->ncaptures; |
Igor Sysoev | 3f8dc59 | 2006-08-28 16:57:48 +0000 | [diff] [blame] | 701 | le.quote = code->redirect; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 702 | |
| 703 | len = 1; /* reserve 1 byte for possible "?" */ |
| 704 | |
| 705 | while (*(uintptr_t *) le.ip) { |
| 706 | lcode = *(ngx_http_script_len_code_pt *) le.ip; |
| 707 | len += lcode(&le); |
| 708 | } |
| 709 | |
| 710 | e->buf.len = len; |
| 711 | } |
| 712 | |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 713 | if (code->add_args && r->args.len) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 714 | e->buf.len += r->args.len + 1; |
| 715 | } |
| 716 | |
| 717 | e->buf.data = ngx_palloc(r->pool, e->buf.len); |
| 718 | if (e->buf.data == NULL) { |
| 719 | e->ip = ngx_http_script_exit; |
| 720 | e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 721 | return; |
| 722 | } |
| 723 | |
| 724 | e->quote = code->redirect; |
| 725 | |
| 726 | e->pos = e->buf.data; |
| 727 | |
| 728 | e->ip += sizeof(ngx_http_script_regex_code_t); |
| 729 | } |
| 730 | |
| 731 | |
| 732 | void |
| 733 | ngx_http_script_regex_end_code(ngx_http_script_engine_t *e) |
| 734 | { |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 735 | u_char *dst, *src; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 736 | ngx_http_request_t *r; |
| 737 | ngx_http_script_regex_end_code_t *code; |
| 738 | |
| 739 | code = (ngx_http_script_regex_end_code_t *) e->ip; |
| 740 | |
| 741 | r = e->request; |
| 742 | |
| 743 | e->quote = 0; |
| 744 | |
| 745 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 746 | "http script regex end"); |
| 747 | |
| 748 | if (code->redirect) { |
| 749 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 750 | dst = e->buf.data; |
| 751 | src = e->buf.data; |
| 752 | |
Igor Sysoev | ae33d01 | 2006-01-17 20:04:32 +0000 | [diff] [blame] | 753 | ngx_unescape_uri(&dst, &src, e->pos - e->buf.data, NGX_UNESCAPE_URI); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 754 | |
| 755 | if (src < e->pos) { |
| 756 | dst = ngx_copy(dst, src, e->pos - src); |
| 757 | } |
| 758 | |
| 759 | e->pos = dst; |
| 760 | |
| 761 | if (code->add_args && r->args.len) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 762 | *e->pos++ = (u_char) (code->args ? '&' : '?'); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 763 | e->pos = ngx_copy(e->pos, r->args.data, r->args.len); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | e->buf.len = e->pos - e->buf.data; |
| 767 | |
| 768 | if (e->log) { |
| 769 | ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, |
| 770 | "rewritten redirect: \"%V\"", &e->buf); |
| 771 | } |
| 772 | |
| 773 | r->headers_out.location = ngx_list_push(&r->headers_out.headers); |
| 774 | if (r->headers_out.location == NULL) { |
| 775 | e->ip = ngx_http_script_exit; |
| 776 | e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 777 | return; |
| 778 | } |
| 779 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 780 | r->headers_out.location->hash = 1; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 781 | r->headers_out.location->key.len = sizeof("Location") - 1; |
| 782 | r->headers_out.location->key.data = (u_char *) "Location"; |
| 783 | r->headers_out.location->value = e->buf; |
| 784 | |
| 785 | e->ip += sizeof(ngx_http_script_regex_end_code_t); |
| 786 | return; |
| 787 | } |
| 788 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 789 | if (e->args) { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 790 | e->buf.len = e->args - e->buf.data; |
| 791 | |
| 792 | if (code->add_args && r->args.len) { |
| 793 | *e->pos++ = '&'; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 794 | e->pos = ngx_copy(e->pos, r->args.data, r->args.len); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | r->args.len = e->pos - e->args; |
| 798 | r->args.data = e->args; |
| 799 | |
| 800 | e->args = NULL; |
| 801 | |
| 802 | } else { |
| 803 | e->buf.len = e->pos - e->buf.data; |
Igor Sysoev | 08e63d4 | 2006-08-14 15:09:38 +0000 | [diff] [blame] | 804 | |
| 805 | if (!code->add_args) { |
| 806 | r->args.len = 0; |
| 807 | } |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | if (e->log) { |
| 811 | ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, |
| 812 | "rewritten data: \"%V\", args: \"%V\"", |
| 813 | &e->buf, &r->args); |
| 814 | } |
| 815 | |
| 816 | if (code->uri) { |
| 817 | r->uri = e->buf; |
| 818 | |
Igor Sysoev | b85fd59 | 2005-08-23 15:36:54 +0000 | [diff] [blame] | 819 | if (r->uri.len == 0) { |
| 820 | ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
| 821 | "the rewritten URI has a zero length"); |
| 822 | e->ip = ngx_http_script_exit; |
| 823 | e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 824 | return; |
| 825 | } |
| 826 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 827 | if (ngx_http_set_exten(r) != NGX_OK) { |
| 828 | e->ip = ngx_http_script_exit; |
| 829 | e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 830 | return; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | e->ip += sizeof(ngx_http_script_regex_end_code_t); |
| 835 | } |
| 836 | |
Igor Sysoev | 4959ec4 | 2005-05-23 12:07:45 +0000 | [diff] [blame] | 837 | #endif |
| 838 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 839 | |
| 840 | void |
| 841 | ngx_http_script_return_code(ngx_http_script_engine_t *e) |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 842 | { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 843 | ngx_http_script_return_code_t *code; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 844 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 845 | code = (ngx_http_script_return_code_t *) e->ip; |
| 846 | |
| 847 | e->status = code->status; |
| 848 | |
Igor Sysoev | afd7ec5 | 2006-05-29 17:28:12 +0000 | [diff] [blame] | 849 | if (code->status == NGX_HTTP_NO_CONTENT) { |
| 850 | e->request->header_only = 1; |
Igor Sysoev | 8fd830a | 2006-10-02 10:22:51 +0000 | [diff] [blame] | 851 | e->request->zero_body = 1; |
Igor Sysoev | afd7ec5 | 2006-05-29 17:28:12 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 854 | e->ip += sizeof(ngx_http_script_return_code_t) - sizeof(uintptr_t); |
| 855 | } |
| 856 | |
| 857 | |
| 858 | void |
Igor Sysoev | 5192b36 | 2005-07-08 14:34:20 +0000 | [diff] [blame] | 859 | ngx_http_script_break_code(ngx_http_script_engine_t *e) |
| 860 | { |
| 861 | e->request->uri_changed = 0; |
| 862 | |
| 863 | e->ip = ngx_http_script_exit; |
| 864 | } |
| 865 | |
| 866 | |
| 867 | void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 868 | ngx_http_script_if_code(ngx_http_script_engine_t *e) |
| 869 | { |
| 870 | ngx_http_script_if_code_t *code; |
| 871 | |
| 872 | code = (ngx_http_script_if_code_t *) e->ip; |
| 873 | |
| 874 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 875 | "http script if"); |
| 876 | |
| 877 | e->sp--; |
| 878 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 879 | if (e->sp->len && e->sp->data[0] != '0') { |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 880 | if (code->loc_conf) { |
| 881 | e->request->loc_conf = code->loc_conf; |
Igor Sysoev | b85fd59 | 2005-08-23 15:36:54 +0000 | [diff] [blame] | 882 | ngx_http_update_location_config(e->request); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | e->ip += sizeof(ngx_http_script_if_code_t); |
| 886 | return; |
| 887 | } |
| 888 | |
| 889 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 890 | "http script if: false"); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 891 | |
| 892 | e->ip += code->next; |
| 893 | } |
| 894 | |
| 895 | |
| 896 | void |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 897 | ngx_http_script_equal_code(ngx_http_script_engine_t *e) |
| 898 | { |
| 899 | ngx_http_variable_value_t *val, *res; |
| 900 | |
| 901 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 902 | "http script equal"); |
| 903 | |
| 904 | e->sp--; |
| 905 | val = e->sp; |
| 906 | res = e->sp - 1; |
| 907 | |
| 908 | e->ip += sizeof(uintptr_t); |
| 909 | |
| 910 | if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len) |
| 911 | == 0) |
| 912 | { |
| 913 | *res = ngx_http_variable_true_value; |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 918 | "http script equal: no"); |
| 919 | |
| 920 | *res = ngx_http_variable_null_value; |
| 921 | } |
| 922 | |
| 923 | |
| 924 | void |
| 925 | ngx_http_script_not_equal_code(ngx_http_script_engine_t *e) |
| 926 | { |
| 927 | ngx_http_variable_value_t *val, *res; |
| 928 | |
| 929 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 930 | "http script not equal"); |
| 931 | |
| 932 | e->sp--; |
| 933 | val = e->sp; |
| 934 | res = e->sp - 1; |
| 935 | |
| 936 | e->ip += sizeof(uintptr_t); |
| 937 | |
| 938 | if (val->len == res->len && ngx_strncmp(val->data, res->data, res->len) |
| 939 | == 0) |
| 940 | { |
| 941 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 942 | "http script not equal: no"); |
| 943 | |
| 944 | *res = ngx_http_variable_null_value; |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | *res = ngx_http_variable_true_value; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | void |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 953 | ngx_http_script_file_code(ngx_http_script_engine_t *e) |
| 954 | { |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 955 | ngx_str_t path; |
| 956 | ngx_http_request_t *r; |
| 957 | ngx_open_file_info_t of; |
| 958 | ngx_http_core_loc_conf_t *clcf; |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 959 | ngx_http_variable_value_t *value; |
| 960 | ngx_http_script_file_code_t *code; |
| 961 | |
| 962 | value = e->sp - 1; |
| 963 | |
| 964 | code = (ngx_http_script_file_code_t *) e->ip; |
| 965 | e->ip += sizeof(ngx_http_script_file_code_t); |
| 966 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 967 | path.len = value->len - 1; |
| 968 | path.data = value->data; |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 969 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 970 | r = e->request; |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 971 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 972 | ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
| 973 | "http script file op %p \"%V\"", code->op, &path); |
| 974 | |
| 975 | clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); |
| 976 | |
| 977 | of.test_dir = 0; |
| 978 | of.retest = clcf->open_file_cache_retest; |
| 979 | of.errors = clcf->open_file_cache_errors; |
Igor Sysoev | 9afd58f | 2007-09-03 08:41:42 +0000 | [diff] [blame] | 980 | of.events = clcf->open_file_cache_events; |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 981 | |
| 982 | if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool) |
| 983 | == NGX_ERROR) |
| 984 | { |
| 985 | if (of.err != NGX_ENOENT && of.err != NGX_ENOTDIR) { |
| 986 | ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err, |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 987 | ngx_file_info_n " \"%s\" failed", value->data); |
| 988 | } |
| 989 | |
| 990 | switch (code->op) { |
Igor Sysoev | c55a104 | 2006-08-09 19:59:45 +0000 | [diff] [blame] | 991 | |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 992 | case ngx_http_script_file_plain: |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 993 | case ngx_http_script_file_dir: |
| 994 | case ngx_http_script_file_exists: |
| 995 | case ngx_http_script_file_exec: |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 996 | goto false; |
Igor Sysoev | c55a104 | 2006-08-09 19:59:45 +0000 | [diff] [blame] | 997 | |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 998 | case ngx_http_script_file_not_plain: |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 999 | case ngx_http_script_file_not_dir: |
Igor Sysoev | c55a104 | 2006-08-09 19:59:45 +0000 | [diff] [blame] | 1000 | case ngx_http_script_file_not_exists: |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1001 | case ngx_http_script_file_not_exec: |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 1002 | goto true; |
| 1003 | } |
| 1004 | |
| 1005 | goto false; |
| 1006 | } |
| 1007 | |
| 1008 | switch (code->op) { |
| 1009 | case ngx_http_script_file_plain: |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1010 | if (of.is_file) { |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 1011 | goto true; |
| 1012 | } |
| 1013 | goto false; |
| 1014 | |
| 1015 | case ngx_http_script_file_not_plain: |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1016 | if (of.is_file) { |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 1017 | goto false; |
| 1018 | } |
| 1019 | goto true; |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1020 | |
| 1021 | case ngx_http_script_file_dir: |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1022 | if (of.is_dir) { |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1023 | goto true; |
| 1024 | } |
| 1025 | goto false; |
| 1026 | |
| 1027 | case ngx_http_script_file_not_dir: |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1028 | if (of.is_dir) { |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1029 | goto false; |
| 1030 | } |
| 1031 | goto true; |
| 1032 | |
| 1033 | case ngx_http_script_file_exists: |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1034 | if (of.is_file || of.is_dir || of.is_link) { |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1035 | goto true; |
| 1036 | } |
| 1037 | goto false; |
| 1038 | |
| 1039 | case ngx_http_script_file_not_exists: |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1040 | if (of.is_file || of.is_dir || of.is_link) { |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1041 | goto false; |
| 1042 | } |
| 1043 | goto true; |
| 1044 | |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1045 | case ngx_http_script_file_exec: |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1046 | if (of.is_exec) { |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1047 | goto true; |
| 1048 | } |
| 1049 | goto false; |
| 1050 | |
| 1051 | case ngx_http_script_file_not_exec: |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1052 | if (of.is_exec) { |
Igor Sysoev | b71c690 | 2006-08-04 16:04:04 +0000 | [diff] [blame] | 1053 | goto false; |
| 1054 | } |
| 1055 | goto true; |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | false: |
| 1059 | |
Igor Sysoev | 140c755 | 2007-09-01 12:12:48 +0000 | [diff] [blame] | 1060 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
Igor Sysoev | 94e32ce | 2006-04-07 14:08:04 +0000 | [diff] [blame] | 1061 | "http script file op false"); |
| 1062 | |
| 1063 | *value = ngx_http_variable_null_value; |
| 1064 | return; |
| 1065 | |
| 1066 | true: |
| 1067 | |
| 1068 | *value = ngx_http_variable_true_value; |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | |
| 1073 | void |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1074 | ngx_http_script_complex_value_code(ngx_http_script_engine_t *e) |
| 1075 | { |
| 1076 | size_t len; |
| 1077 | ngx_http_script_engine_t le; |
| 1078 | ngx_http_script_len_code_pt lcode; |
| 1079 | ngx_http_script_complex_value_code_t *code; |
| 1080 | |
| 1081 | code = (ngx_http_script_complex_value_code_t *) e->ip; |
| 1082 | |
| 1083 | e->ip += sizeof(ngx_http_script_complex_value_code_t); |
| 1084 | |
| 1085 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 1086 | "http script complex value"); |
| 1087 | |
| 1088 | ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); |
| 1089 | |
| 1090 | le.ip = code->lengths->elts; |
Igor Sysoev | af3b7ea | 2006-05-31 14:11:45 +0000 | [diff] [blame] | 1091 | le.line = e->line; |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1092 | le.request = e->request; |
| 1093 | le.captures = e->captures; |
| 1094 | le.ncaptures = e->ncaptures; |
Igor Sysoev | af3b7ea | 2006-05-31 14:11:45 +0000 | [diff] [blame] | 1095 | le.quote = e->quote; |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1096 | |
| 1097 | for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) { |
| 1098 | lcode = *(ngx_http_script_len_code_pt *) le.ip; |
| 1099 | } |
| 1100 | |
| 1101 | e->buf.len = len; |
| 1102 | e->buf.data = ngx_palloc(e->request->pool, len); |
| 1103 | if (e->buf.data == NULL) { |
| 1104 | e->ip = ngx_http_script_exit; |
| 1105 | e->status = NGX_HTTP_INTERNAL_SERVER_ERROR; |
| 1106 | return; |
| 1107 | } |
| 1108 | |
| 1109 | e->pos = e->buf.data; |
| 1110 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1111 | e->sp->len = e->buf.len; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1112 | e->sp->data = e->buf.data; |
Igor Sysoev | e31e90b | 2005-05-19 13:25:22 +0000 | [diff] [blame] | 1113 | e->sp++; |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1118 | ngx_http_script_value_code(ngx_http_script_engine_t *e) |
| 1119 | { |
| 1120 | ngx_http_script_value_code_t *code; |
| 1121 | |
| 1122 | code = (ngx_http_script_value_code_t *) e->ip; |
| 1123 | |
| 1124 | e->ip += sizeof(ngx_http_script_value_code_t); |
| 1125 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1126 | e->sp->len = code->text_len; |
| 1127 | e->sp->data = (u_char *) code->text_data; |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1128 | |
| 1129 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
Igor Sysoev | 5ba6739 | 2007-10-09 18:44:59 +0000 | [diff] [blame] | 1130 | "http script value: \"%v\"", e->sp); |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1131 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1132 | e->sp++; |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | void |
| 1137 | ngx_http_script_set_var_code(ngx_http_script_engine_t *e) |
| 1138 | { |
| 1139 | ngx_http_request_t *r; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1140 | ngx_http_script_var_code_t *code; |
| 1141 | |
| 1142 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 1143 | "http script set var"); |
| 1144 | |
| 1145 | code = (ngx_http_script_var_code_t *) e->ip; |
| 1146 | |
| 1147 | e->ip += sizeof(ngx_http_script_var_code_t); |
| 1148 | |
| 1149 | r = e->request; |
| 1150 | |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1151 | e->sp--; |
| 1152 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1153 | r->variables[code->index].len = e->sp->len; |
| 1154 | r->variables[code->index].valid = 1; |
Igor Sysoev | 2d3f3f6 | 2007-10-14 18:56:15 +0000 | [diff] [blame] | 1155 | r->variables[code->index].no_cacheable = 0; |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1156 | r->variables[code->index].not_found = 0; |
| 1157 | r->variables[code->index].data = e->sp->data; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | void |
Igor Sysoev | 7bdb720 | 2006-04-19 15:30:56 +0000 | [diff] [blame] | 1162 | ngx_http_script_var_set_handler_code(ngx_http_script_engine_t *e) |
| 1163 | { |
| 1164 | ngx_http_script_var_handler_code_t *code; |
| 1165 | |
| 1166 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 1167 | "http script set var handler"); |
| 1168 | |
| 1169 | code = (ngx_http_script_var_handler_code_t *) e->ip; |
| 1170 | |
| 1171 | e->ip += sizeof(ngx_http_script_var_handler_code_t); |
| 1172 | |
| 1173 | e->sp--; |
| 1174 | |
| 1175 | code->handler(e->request, e->sp, code->data); |
| 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | void |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1180 | ngx_http_script_var_code(ngx_http_script_engine_t *e) |
| 1181 | { |
| 1182 | ngx_http_variable_value_t *value; |
| 1183 | ngx_http_script_var_code_t *code; |
| 1184 | |
| 1185 | ngx_log_debug0(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
| 1186 | "http script var"); |
| 1187 | |
| 1188 | code = (ngx_http_script_var_code_t *) e->ip; |
| 1189 | |
| 1190 | e->ip += sizeof(ngx_http_script_var_code_t); |
| 1191 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1192 | value = ngx_http_get_flushed_variable(e->request, code->index); |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1193 | |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1194 | if (value && !value->not_found) { |
| 1195 | ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0, |
Igor Sysoev | 0d4b372 | 2007-08-20 09:57:19 +0000 | [diff] [blame] | 1196 | "http script var: \"%v\"", value); |
Igor Sysoev | 09c684b | 2005-11-09 17:25:55 +0000 | [diff] [blame] | 1197 | |
Igor Sysoev | f6e1fe3 | 2005-10-04 10:38:53 +0000 | [diff] [blame] | 1198 | *e->sp = *value; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1199 | e->sp++; |
| 1200 | |
| 1201 | return; |
| 1202 | } |
| 1203 | |
Igor Sysoev | 0e5dc5c | 2005-11-15 13:30:52 +0000 | [diff] [blame] | 1204 | *e->sp = ngx_http_variable_null_value; |
Igor Sysoev | 899b44e | 2005-05-12 14:58:06 +0000 | [diff] [blame] | 1205 | e->sp++; |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | void |
| 1210 | ngx_http_script_nop_code(ngx_http_script_engine_t *e) |
| 1211 | { |
| 1212 | e->ip += sizeof(uintptr_t); |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 1213 | } |