~spuul/nginx/trunk

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/src/ngx_http_lua_variable.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-02-15 03:05:42 UTC
  • mfrom: (4.3.10 sid)
  • Revision ID: package-import@ubuntu.com-20140215030542-71ubtowl24vf7nfn
Tags: 1.4.5-1ubuntu1
* Resynchronise with Debian (LP: #1280511).  Remaining changes:
  - debian/patches/ubuntu-branding.patch:
    + Add Ubuntu branding to server_tokens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
    int                         *cap;
61
61
#endif
62
62
 
63
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
64
 
    lua_rawget(L, LUA_GLOBALSINDEX);
65
 
    r = lua_touserdata(L, -1);
66
 
    lua_pop(L, 1);
67
 
 
 
63
    r = ngx_http_lua_get_req(L);
68
64
    if (r == NULL) {
69
65
        return luaL_error(L, "no request object found");
70
66
    }
108
104
    }
109
105
#endif
110
106
 
111
 
    p = (u_char *) luaL_checklstring(L, -1, &len);
 
107
    if (lua_type(L, -1) != LUA_TSTRING) {
 
108
        return luaL_error(L, "bad variable name");
 
109
    }
 
110
 
 
111
    p = (u_char *) lua_tolstring(L, -1, &len);
112
112
 
113
113
    lowcase = lua_newuserdata(L, len);
114
114
 
150
150
    int                          value_type;
151
151
    const char                  *msg;
152
152
 
153
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
154
 
    lua_rawget(L, LUA_GLOBALSINDEX);
155
 
    r = lua_touserdata(L, -1);
156
 
    lua_pop(L, 1);
157
 
 
 
153
    r = ngx_http_lua_get_req(L);
158
154
    if (r == NULL) {
159
155
        return luaL_error(L, "no request object found");
160
156
    }
165
161
 
166
162
    /* we read the variable name */
167
163
 
168
 
    p = (u_char *) luaL_checklstring(L, 2, &len);
169
 
 
170
 
    lowcase = lua_newuserdata(L, len);
 
164
    if (lua_type(L, 2) != LUA_TSTRING) {
 
165
        return luaL_error(L, "bad variable name");
 
166
    }
 
167
 
 
168
    p = (u_char *) lua_tolstring(L, 2, &len);
 
169
 
 
170
    lowcase = lua_newuserdata(L, len + 1);
171
171
 
172
172
    hash = ngx_hash_strlow(lowcase, p, len);
 
173
    lowcase[len] = '\0';
173
174
 
174
175
    name.len = len;
175
176
    name.data = lowcase;
284
285
                      lowcase, lowcase);
285
286
}
286
287
 
 
288
 
 
289
#ifndef NGX_HTTP_LUA_NO_FFI_API
 
290
int
 
291
ngx_http_lua_ffi_var_get(ngx_http_request_t *r, u_char *name_data,
 
292
    size_t name_len, u_char *lowcase_buf, int capture_id, u_char **value,
 
293
    size_t *value_len, char **err)
 
294
{
 
295
    ngx_uint_t                   hash;
 
296
    ngx_str_t                    name;
 
297
    ngx_http_variable_value_t   *vv;
 
298
 
 
299
#if (NGX_PCRE)
 
300
    u_char                      *p;
 
301
    ngx_uint_t                   n;
 
302
    int                         *cap;
 
303
#endif
 
304
 
 
305
    if (r == NULL) {
 
306
        *err = "no request object found";
 
307
        return NGX_ERROR;
 
308
    }
 
309
 
 
310
    if ((r)->connection->fd == -1) {
 
311
        *err = "API disabled in the current context";
 
312
        return NGX_ERROR;
 
313
    }
 
314
 
 
315
#if (NGX_PCRE)
 
316
    if (name_data == 0) {
 
317
        if (capture_id <= 0) {
 
318
            return NGX_DECLINED;
 
319
        }
 
320
 
 
321
        /* it is a regex capturing variable */
 
322
 
 
323
        n = (ngx_uint_t) capture_id * 2;
 
324
 
 
325
        dd("n = %d, ncaptures = %d", (int) n, (int) r->ncaptures);
 
326
 
 
327
        if (r->captures == NULL
 
328
            || r->captures_data == NULL
 
329
            || n >= r->ncaptures)
 
330
        {
 
331
            return NGX_DECLINED;
 
332
        }
 
333
 
 
334
        /* n >= 0 && n < r->ncaptures */
 
335
 
 
336
        cap = r->captures;
 
337
        p = r->captures_data;
 
338
 
 
339
        *value = &p[cap[n]];
 
340
        *value_len = (size_t) (cap[n + 1] - cap[n]);
 
341
 
 
342
        return NGX_OK;
 
343
    }
 
344
#endif
 
345
 
 
346
    hash = ngx_hash_strlow(lowcase_buf, name_data, name_len);
 
347
 
 
348
    name.data = lowcase_buf;
 
349
    name.len = name_len;
 
350
 
 
351
    dd("variable name: %.*s", (int) name_len, lowcase_buf);
 
352
 
 
353
    vv = ngx_http_get_variable(r, &name, hash);
 
354
    if (vv == NULL || vv->not_found) {
 
355
        return NGX_DECLINED;
 
356
    }
 
357
 
 
358
    *value = vv->data;
 
359
    *value_len = vv->len;
 
360
    return NGX_OK;
 
361
}
 
362
 
 
363
 
 
364
int
 
365
ngx_http_lua_ffi_var_set(ngx_http_request_t *r, u_char *name_data,
 
366
    size_t name_len, u_char *lowcase_buf, u_char *value, size_t value_len,
 
367
    u_char *errbuf, size_t errlen)
 
368
{
 
369
    u_char                      *p;
 
370
    ngx_uint_t                   hash;
 
371
    ngx_http_variable_t         *v;
 
372
    ngx_http_variable_value_t   *vv;
 
373
    ngx_http_core_main_conf_t   *cmcf;
 
374
 
 
375
    if (r == NULL) {
 
376
        ngx_snprintf(errbuf, errlen, "no request object found");
 
377
        return NGX_ERROR;
 
378
    }
 
379
 
 
380
    if ((r)->connection->fd == -1) {
 
381
        ngx_snprintf(errbuf, errlen, "API disabled in the current context");
 
382
        return NGX_ERROR;
 
383
    }
 
384
 
 
385
    hash = ngx_hash_strlow(lowcase_buf, name_data, name_len);
 
386
 
 
387
    dd("variable name: %.*s", (int) name_len, lowcase_buf);
 
388
 
 
389
    /* we fetch the variable itself */
 
390
 
 
391
    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
 
392
 
 
393
    v = ngx_hash_find(&cmcf->variables_hash, hash, lowcase_buf, name_len);
 
394
 
 
395
    if (v) {
 
396
        if (!(v->flags & NGX_HTTP_VAR_CHANGEABLE)) {
 
397
            dd("variable not changeable");
 
398
            ngx_snprintf(errbuf, errlen, "variable \"%*s\" not changeable",
 
399
                         name_len, lowcase_buf);
 
400
            return NGX_ERROR;
 
401
        }
 
402
 
 
403
        if (v->set_handler) {
 
404
 
 
405
            dd("set variables with set_handler");
 
406
 
 
407
            if (value != NULL && value_len) {
 
408
                vv = ngx_pnalloc(r->pool, sizeof(ngx_http_variable_value_t)
 
409
                                 + value_len);
 
410
                if (vv == NULL) {
 
411
                    goto nomem;
 
412
                }
 
413
 
 
414
                p = (u_char *) vv + sizeof(ngx_http_variable_value_t);
 
415
                ngx_memcpy(p, value, value_len);
 
416
                value = p;
 
417
 
 
418
            } else {
 
419
                vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t));
 
420
                if (vv == NULL) {
 
421
                    goto nomem;
 
422
                }
 
423
            }
 
424
 
 
425
            if (value == NULL) {
 
426
                vv->valid = 0;
 
427
                vv->not_found = 1;
 
428
                vv->no_cacheable = 0;
 
429
                vv->data = NULL;
 
430
                vv->len = 0;
 
431
 
 
432
            } else {
 
433
                vv->valid = 1;
 
434
                vv->not_found = 0;
 
435
                vv->no_cacheable = 0;
 
436
 
 
437
                vv->data = value;
 
438
                vv->len = value_len;
 
439
            }
 
440
 
 
441
            v->set_handler(r, vv, v->data);
 
442
            return NGX_OK;
 
443
        }
 
444
 
 
445
        if (v->flags & NGX_HTTP_VAR_INDEXED) {
 
446
            vv = &r->variables[v->index];
 
447
 
 
448
            dd("set indexed variable");
 
449
 
 
450
            if (value == NULL) {
 
451
                vv->valid = 0;
 
452
                vv->not_found = 1;
 
453
                vv->no_cacheable = 0;
 
454
 
 
455
                vv->data = NULL;
 
456
                vv->len = 0;
 
457
 
 
458
            } else {
 
459
                p = ngx_palloc(r->pool, value_len);
 
460
                if (p == NULL) {
 
461
                    goto nomem;
 
462
                }
 
463
                ngx_memcpy(p, value, value_len);
 
464
                value = p;
 
465
 
 
466
                vv->valid = 1;
 
467
                vv->not_found = 0;
 
468
                vv->no_cacheable = 0;
 
469
 
 
470
                vv->data = value;
 
471
                vv->len = value_len;
 
472
            }
 
473
 
 
474
            return NGX_OK;
 
475
        }
 
476
 
 
477
        ngx_snprintf(errbuf, errlen, "variable \"%*s\" cannot be assigned "
 
478
                     "a value", name_len, lowcase_buf);
 
479
        return NGX_ERROR;
 
480
    }
 
481
 
 
482
    /* variable not found */
 
483
 
 
484
    ngx_snprintf(errbuf, errlen, "variable \"%*s\" not found for writing; "
 
485
                 "maybe it is a built-in variable that is not changeable "
 
486
                 "or you forgot to use \"set $%*s '';\" "
 
487
                 "in the config file to define it first",
 
488
                 name_len, lowcase_buf, name_len, lowcase_buf);
 
489
    return NGX_ERROR;
 
490
 
 
491
nomem:
 
492
 
 
493
    ngx_snprintf(errbuf, errlen, "no memory");
 
494
    return NGX_ERROR;
 
495
}
 
496
#endif /* NGX_HTTP_LUA_NO_FFI_API */
 
497
 
 
498
 
287
499
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */