~spuul/nginx/trunk

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/src/ngx_http_lua_headers.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:
32
32
{
33
33
    ngx_http_request_t          *r;
34
34
 
35
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
36
 
    lua_rawget(L, LUA_GLOBALSINDEX);
37
 
    r = lua_touserdata(L, -1);
38
 
    lua_pop(L, 1);
39
 
 
 
35
    r = ngx_http_lua_get_req(L);
40
36
    if (r == NULL) {
41
37
        return luaL_error(L, "no request object found");
42
38
    }
85
81
 
86
82
    dd("no req line: %d", (int) no_req_line);
87
83
 
88
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
89
 
    lua_rawget(L, LUA_GLOBALSINDEX);
90
 
    r = lua_touserdata(L, -1);
91
 
    lua_pop(L, 1);
92
 
 
 
84
    r = ngx_http_lua_get_req(L);
93
85
    if (r == NULL) {
94
86
        return luaL_error(L, "no request object found");
95
87
    }
294
286
 
295
287
 
296
288
static int
297
 
ngx_http_lua_ngx_req_get_headers(lua_State *L) {
 
289
ngx_http_lua_ngx_req_get_headers(lua_State *L)
 
290
{
298
291
    ngx_list_part_t              *part;
299
292
    ngx_table_elt_t              *header;
300
293
    ngx_http_request_t           *r;
322
315
        max = NGX_HTTP_LUA_MAX_HEADERS;
323
316
    }
324
317
 
325
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
326
 
    lua_rawget(L, LUA_GLOBALSINDEX);
327
 
    r = lua_touserdata(L, -1);
328
 
    lua_pop(L, 1);
329
 
 
 
318
    r = ngx_http_lua_get_req(L);
330
319
    if (r == NULL) {
331
320
        return luaL_error(L, "no request object found");
332
321
    }
333
322
 
334
323
    ngx_http_lua_check_fake_request(L, r);
335
324
 
336
 
    lua_createtable(L, 0, 4);
 
325
    part = &r->headers_in.headers.part;
 
326
    count = part->nelts;
 
327
    while (part->next) {
 
328
        part = part->next;
 
329
        count += part->nelts;
 
330
    }
 
331
 
 
332
    if (max > 0 && count > max) {
 
333
        count = max;
 
334
        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
 
335
                       "lua exceeding request header limit %d", max);
 
336
    }
 
337
 
 
338
    lua_createtable(L, 0, count);
337
339
 
338
340
    if (!raw) {
339
341
        lua_pushlightuserdata(L, &ngx_http_lua_req_get_headers_metatable_key);
377
379
                       "lua request header: \"%V: %V\"",
378
380
                       &header[i].key, &header[i].value);
379
381
 
380
 
        if (max > 0 && ++count == max) {
381
 
            ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
382
 
                           "lua hit request header limit %d", max);
383
 
 
 
382
        if (--count == 0) {
384
383
            return 1;
385
384
        }
386
385
    }
399
398
    size_t                       len;
400
399
    ngx_http_lua_loc_conf_t     *llcf;
401
400
 
402
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
403
 
    lua_rawget(L, LUA_GLOBALSINDEX);
404
 
    r = lua_touserdata(L, -1);
405
 
    lua_pop(L, 1);
406
 
 
 
401
    r = ngx_http_lua_get_req(L);
407
402
    if (r == NULL) {
408
403
        return luaL_error(L, "no request object found");
409
404
    }
415
410
 
416
411
    dd("key: %.*s, len %d", (int) len, p, (int) len);
417
412
 
 
413
    key.data = ngx_palloc(r->pool, len + 1);
 
414
    if (key.data == NULL) {
 
415
        return luaL_error(L, "out of memory");
 
416
    }
 
417
 
 
418
    ngx_memcpy(key.data, p, len);
 
419
 
418
420
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
419
421
 
420
422
    if (llcf->transform_underscores_in_resp_headers) {
421
423
        /* replace "_" with "-" */
422
424
        for (i = 0; i < len; i++) {
423
 
            if (p[i] == '_') {
424
 
                p[i] = '-';
 
425
            if (key.data[i] == '_') {
 
426
                key.data[i] = '-';
425
427
            }
426
428
        }
427
429
    }
428
430
 
429
 
    key.data = ngx_palloc(r->pool, len + 1);
430
 
    if (key.data == NULL) {
431
 
        return luaL_error(L, "out of memory");
432
 
    }
433
 
 
434
 
    ngx_memcpy(key.data, p, len);
435
 
 
436
431
    key.data[len] = '\0';
437
432
 
438
433
    key.len = len;
455
450
    ngx_uint_t                   n;
456
451
    ngx_http_lua_loc_conf_t     *llcf;
457
452
 
458
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
459
 
    lua_rawget(L, LUA_GLOBALSINDEX);
460
 
    r = lua_touserdata(L, -1);
461
 
    lua_pop(L, 1);
462
 
 
 
453
    r = ngx_http_lua_get_req(L);
463
454
    if (r == NULL) {
464
455
        return luaL_error(L, "no request object found");
465
456
    }
469
460
        return luaL_error(L, "no ctx");
470
461
    }
471
462
 
472
 
    ngx_http_lua_check_fake_request2(L, r, ctx);
 
463
    ngx_http_lua_check_fake_request(L, r);
473
464
 
474
 
    if (ctx->headers_sent) {
 
465
    if (r->header_sent) {
475
466
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "attempt to "
476
467
                      "set ngx.header.HEADER after sending out "
477
468
                      "response headers");
505
496
    }
506
497
 
507
498
    if (!ctx->headers_set) {
508
 
        rc = ngx_http_set_content_type(r);
 
499
        rc = ngx_http_lua_set_content_type(r);
509
500
        if (rc != NGX_OK) {
510
501
            return luaL_error(L,
511
502
                              "failed to set default content type: %d",
616
607
    ngx_int_t                    rc;
617
608
    ngx_uint_t                   n;
618
609
 
619
 
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
620
 
    lua_rawget(L, LUA_GLOBALSINDEX);
621
 
    r = lua_touserdata(L, -1);
622
 
    lua_pop(L, 1);
623
 
 
 
610
    r = ngx_http_lua_get_req(L);
624
611
    if (r == NULL) {
625
612
        return luaL_error(L, "no request object found");
626
613
    }
627
614
 
628
615
    ngx_http_lua_check_fake_request(L, r);
629
616
 
 
617
    if (r->http_version < NGX_HTTP_VERSION_10) {
 
618
        return 0;
 
619
    }
 
620
 
630
621
    p = (u_char *) luaL_checklstring(L, 1, &len);
631
622
 
632
623
    dd("key: %.*s, len %d", (int) len, p, (int) len);
633
624
 
 
625
#if 0
634
626
    /* replace "_" with "-" */
635
627
    for (i = 0; i < len; i++) {
636
628
        if (p[i] == '_') {
637
629
            p[i] = '-';
638
630
        }
639
631
    }
 
632
#endif
640
633
 
641
634
    key.data = ngx_palloc(r->pool, len + 1);
642
635
    if (key.data == NULL) {
786
779
    lua_rawset(L, LUA_REGISTRYINDEX);
787
780
}
788
781
 
 
782
 
 
783
#ifndef NGX_HTTP_LUA_NO_FFI_API
 
784
int
 
785
ngx_http_lua_ffi_req_get_headers_count(ngx_http_request_t *r, int max)
 
786
{
 
787
    int                           count;
 
788
    ngx_list_part_t              *part;
 
789
 
 
790
    if (r->connection->fd == -1) {
 
791
        return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
 
792
    }
 
793
 
 
794
    if (max < 0) {
 
795
        max = NGX_HTTP_LUA_MAX_HEADERS;
 
796
    }
 
797
 
 
798
    part = &r->headers_in.headers.part;
 
799
    count = part->nelts;
 
800
    while (part->next) {
 
801
        part = part->next;
 
802
        count += part->nelts;
 
803
    }
 
804
 
 
805
    if (max > 0 && count > max) {
 
806
        count = max;
 
807
        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
 
808
                       "lua exceeding request header limit %d", max);
 
809
    }
 
810
 
 
811
    return count;
 
812
}
 
813
 
 
814
 
 
815
int
 
816
ngx_http_lua_ffi_req_get_headers(ngx_http_request_t *r,
 
817
    ngx_http_lua_ffi_table_elt_t *out, int count, int raw)
 
818
{
 
819
    int                           n;
 
820
    ngx_uint_t                    i;
 
821
    ngx_list_part_t              *part;
 
822
    ngx_table_elt_t              *header;
 
823
 
 
824
    if (count <= 0) {
 
825
        return NGX_OK;
 
826
    }
 
827
 
 
828
    n = 0;
 
829
    part = &r->headers_in.headers.part;
 
830
    header = part->elts;
 
831
 
 
832
    for (i = 0; /* void */; i++) {
 
833
 
 
834
        if (i >= part->nelts) {
 
835
            if (part->next == NULL) {
 
836
                break;
 
837
            }
 
838
 
 
839
            part = part->next;
 
840
            header = part->elts;
 
841
            i = 0;
 
842
        }
 
843
 
 
844
        if (raw) {
 
845
            out[n].key.data = header[i].key.data;
 
846
            out[n].key.len = (int) header[i].key.len;
 
847
 
 
848
        } else {
 
849
            out[n].key.data = header[i].lowcase_key;
 
850
            out[n].key.len = (int) header[i].key.len;
 
851
        }
 
852
 
 
853
        out[n].value.data = header[i].value.data;
 
854
        out[n].value.len = (int) header[i].value.len;
 
855
 
 
856
        if (++n == count) {
 
857
            return NGX_OK;
 
858
        }
 
859
    }
 
860
 
 
861
    return NGX_OK;
 
862
}
 
863
 
 
864
 
 
865
int
 
866
ngx_http_lua_ffi_set_resp_header(ngx_http_request_t *r, const u_char *key_data,
 
867
    size_t key_len, int is_nil, const u_char *sval, size_t sval_len,
 
868
    ngx_str_t *mvals, size_t mvals_len, char **errmsg)
 
869
{
 
870
    u_char                      *p;
 
871
    ngx_str_t                    value, key;
 
872
    ngx_uint_t                   i;
 
873
    size_t                       len;
 
874
    ngx_http_lua_ctx_t          *ctx;
 
875
    ngx_int_t                    rc;
 
876
    ngx_http_lua_loc_conf_t     *llcf;
 
877
 
 
878
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
 
879
    if (ctx == NULL) {
 
880
        return NGX_HTTP_LUA_FFI_NO_REQ_CTX;
 
881
    }
 
882
 
 
883
    if (r->connection->fd == -1) {
 
884
        return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
 
885
    }
 
886
 
 
887
    if (r->header_sent) {
 
888
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "attempt to "
 
889
                      "set ngx.header.HEADER after sending out "
 
890
                      "response headers");
 
891
        return NGX_DECLINED;
 
892
    }
 
893
 
 
894
    key.data = ngx_palloc(r->pool, key_len + 1);
 
895
    if (key.data == NULL) {
 
896
        goto nomem;
 
897
    }
 
898
 
 
899
    ngx_memcpy(key.data, key_data, key_len);
 
900
    key.data[key_len] = '\0';
 
901
    key.len = key_len;
 
902
 
 
903
    llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
 
904
 
 
905
    if (llcf->transform_underscores_in_resp_headers) {
 
906
        /* replace "_" with "-" */
 
907
        p = key.data;
 
908
        for (i = 0; i < key_len; i++) {
 
909
            if (p[i] == '_') {
 
910
                p[i] = '-';
 
911
            }
 
912
        }
 
913
    }
 
914
 
 
915
    if (!ctx->headers_set) {
 
916
        rc = ngx_http_lua_set_content_type(r);
 
917
        if (rc != NGX_OK) {
 
918
            *errmsg = "failed to set default content type";
 
919
            return NGX_ERROR;
 
920
        }
 
921
 
 
922
        ctx->headers_set = 1;
 
923
    }
 
924
 
 
925
    if (is_nil) {
 
926
        value.data = NULL;
 
927
        value.len = 0;
 
928
 
 
929
    } else if (mvals) {
 
930
 
 
931
        if (mvals_len == 0) {
 
932
            value.data = NULL;
 
933
            value.len = 0;
 
934
 
 
935
        } else {
 
936
            for (i = 0; i < mvals_len; i++) {
 
937
                dd("header value table index %d", (int) i);
 
938
 
 
939
                p = mvals[i].data;
 
940
                len = mvals[i].len;
 
941
 
 
942
                value.data = ngx_palloc(r->pool, len);
 
943
                if (value.data == NULL) {
 
944
                    goto nomem;
 
945
                }
 
946
 
 
947
                ngx_memcpy(value.data, p, len);
 
948
                value.len = len;
 
949
 
 
950
                rc = ngx_http_lua_set_output_header(r, key, value,
 
951
                                                    i == 0 /* override */);
 
952
 
 
953
                if (rc == NGX_ERROR) {
 
954
                    *errmsg = "failed to set header";
 
955
                    return NGX_ERROR;
 
956
                }
 
957
            }
 
958
 
 
959
            return NGX_OK;
 
960
        }
 
961
 
 
962
    } else {
 
963
        p = (u_char *) sval;
 
964
        value.data = ngx_palloc(r->pool, sval_len);
 
965
        if (value.data == NULL) {
 
966
            goto nomem;
 
967
        }
 
968
 
 
969
        ngx_memcpy(value.data, p, sval_len);
 
970
        value.len = sval_len;
 
971
    }
 
972
 
 
973
    dd("key: %.*s, value: %.*s",
 
974
       (int) key.len, key.data, (int) value.len, value.data);
 
975
 
 
976
    rc = ngx_http_lua_set_output_header(r, key, value, 1 /* override */);
 
977
 
 
978
    if (rc == NGX_ERROR) {
 
979
        *errmsg = "failed to set header";
 
980
        return NGX_ERROR;
 
981
    }
 
982
 
 
983
    return 0;
 
984
 
 
985
nomem:
 
986
 
 
987
    *errmsg = "no memory";
 
988
    return NGX_ERROR;
 
989
}
 
990
#endif /* NGX_HTTP_LUA_NO_FFI_API */
 
991
 
 
992
 
789
993
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */