~ubuntu-branches/ubuntu/wily/nginx/wily-proposed

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Dmitry Shachnev
  • Date: 2013-05-11 14:47:53 UTC
  • mfrom: (4.3.1 sid)
  • Revision ID: package-import@ubuntu.com-20130511144753-a65vqwrxy58omej4
Tags: 1.4.1-1ubuntu1
* Merge with Debian unstable (LP: #1177919). Remaining changes:
  - debian/conf/sites-available/default:
    + Modify default site configuration file to correct a typo
      that prevented out-of-the-box usability (LP: #1162177).
  - debian/patches/ubuntu-branding.patch:
    + Add ubuntu branding to server_tokens.
* Refresh all patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * Copyright (C) Yichun Zhang (agentzh)
 
4
 */
 
5
 
 
6
 
1
7
#ifndef DDEBUG
2
8
#define DDEBUG 0
3
9
#endif
121
127
    lua_setfield(L, -2, "tcp");
122
128
 
123
129
    {
124
 
        const char    buf[] = "local sock = ngx.socket.tcp()"
125
 
                   " local ok, err = sock:connect(...)"
126
 
                   " if ok then return sock else return nil, err end";
 
130
        const char  buf[] = "local sock = ngx.socket.tcp()"
 
131
                            " local ok, err = sock:connect(...)"
 
132
                            " if ok then return sock else return nil, err end";
127
133
 
128
134
        rc = luaL_loadbuffer(L, buf, sizeof(buf) - 1, "ngx.socket.connect");
129
135
    }
231
237
 
232
238
    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
233
239
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
234
 
                               | NGX_HTTP_LUA_CONTEXT_CONTENT);
 
240
                               | NGX_HTTP_LUA_CONTEXT_CONTENT
 
241
                               | NGX_HTTP_LUA_CONTEXT_TIMER);
235
242
 
236
243
    lua_createtable(L, 3 /* narr */, 1 /* nrec */);
237
244
    lua_pushlightuserdata(L, &ngx_http_lua_tcp_socket_metatable_key);
291
298
 
292
299
    ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE
293
300
                               | NGX_HTTP_LUA_CONTEXT_ACCESS
294
 
                               | NGX_HTTP_LUA_CONTEXT_CONTENT);
 
301
                               | NGX_HTTP_LUA_CONTEXT_CONTENT
 
302
                               | NGX_HTTP_LUA_CONTEXT_TIMER);
295
303
 
296
304
    luaL_checktype(L, 1, LUA_TTABLE);
297
305
 
441
449
        u->connect_timeout = u->conf->connect_timeout;
442
450
    }
443
451
 
444
 
    r->connection->single_connection = 0;
445
 
 
446
452
    rc = ngx_http_lua_get_keepalive_peer(r, L, key_index, u);
447
453
 
448
454
    if (rc == NGX_OK) {
577
583
 
578
584
    if (ctx->entered_content_phase) {
579
585
        r->write_event_handler = ngx_http_lua_content_wev_handler;
 
586
 
 
587
    } else {
 
588
        r->write_event_handler = ngx_http_core_run_phases;
580
589
    }
581
590
 
582
591
    return lua_yield(L, 0);
587
596
ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
588
597
{
589
598
    ngx_http_request_t                  *r;
 
599
    ngx_connection_t                    *c;
590
600
    ngx_http_upstream_resolved_t        *ur;
591
601
    ngx_http_lua_ctx_t                  *lctx;
592
602
    lua_State                           *L;
599
609
 
600
610
    u = ctx->data;
601
611
    r = u->request;
 
612
    c = r->connection;
602
613
    ur = u->resolved;
603
614
 
604
 
    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
 
615
    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
605
616
                   "lua tcp socket resolve handler");
606
617
 
607
618
    lctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
618
629
    waiting = u->waiting;
619
630
 
620
631
    if (ctx->state) {
621
 
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
 
632
        ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
622
633
                       "lua tcp socket resolver error: %s (waiting: %d)",
623
634
                       ngx_resolver_strerror(ctx->state), (int) u->waiting);
624
635
 
632
643
        u->prepare_retvals = ngx_http_lua_socket_error_retval_handler;
633
644
        ngx_http_lua_socket_handle_error(r, u,
634
645
                                         NGX_HTTP_LUA_SOCKET_FT_RESOLVER);
 
646
 
 
647
        if (waiting) {
 
648
            ngx_http_run_posted_requests(c);
 
649
        }
 
650
 
635
651
        return;
636
652
    }
637
653
 
648
664
 
649
665
        addr = ntohl(ctx->addrs[i]);
650
666
 
651
 
        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
 
667
        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
652
668
                       "name was resolved to %ud.%ud.%ud.%ud",
653
669
                       (addr >> 24) & 0xff, (addr >> 16) & 0xff,
654
670
                       (addr >> 8) & 0xff, addr & 0xff);
662
678
 
663
679
        lua_pushnil(L);
664
680
        lua_pushliteral(L, "name cannot be resolved to a address");
 
681
 
 
682
        if (waiting) {
 
683
            ngx_http_run_posted_requests(c);
 
684
        }
 
685
 
665
686
        return;
666
687
    }
667
688
 
683
704
 
684
705
        lua_pushnil(L);
685
706
        lua_pushliteral(L, "out of memory");
 
707
 
 
708
        if (waiting) {
 
709
            ngx_http_run_posted_requests(c);
 
710
        }
 
711
 
686
712
        return;
687
713
    }
688
714
 
712
738
    if (waiting) {
713
739
        lctx->resume_handler = ngx_http_lua_socket_tcp_resume;
714
740
        r->write_event_handler(r);
 
741
        ngx_http_run_posted_requests(c);
715
742
 
716
743
    } else {
717
744
        (void) ngx_http_lua_socket_resolve_retval_handler(r, u, L);
870
897
 
871
898
    if (ctx->entered_content_phase) {
872
899
        r->write_event_handler = ngx_http_lua_content_wev_handler;
 
900
 
 
901
    } else {
 
902
        r->write_event_handler = ngx_http_core_run_phases;
873
903
    }
874
904
 
875
905
    u->co_ctx = ctx->cur_co_ctx;
880
910
 
881
911
    if (ctx->entered_content_phase) {
882
912
        r->write_event_handler = ngx_http_lua_content_wev_handler;
 
913
 
 
914
    } else {
 
915
        r->write_event_handler = ngx_http_core_run_phases;
883
916
    }
884
917
 
885
918
    return NGX_AGAIN;
1030
1063
            pat.data = (u_char *) luaL_checklstring(L, 2, &pat.len);
1031
1064
            if (pat.len != 2 || pat.data[0] != '*') {
1032
1065
                p = (char *) lua_pushfstring(L, "bad pattern argument: %s",
1033
 
                                    (char *) pat.data);
 
1066
                                             (char *) pat.data);
1034
1067
 
1035
1068
                return luaL_argerror(L, 2, p);
1036
1069
            }
1131
1164
 
1132
1165
    if (ctx->entered_content_phase) {
1133
1166
        r->write_event_handler = ngx_http_lua_content_wev_handler;
 
1167
 
 
1168
    } else {
 
1169
        r->write_event_handler = ngx_http_core_run_phases;
1134
1170
    }
1135
1171
 
1136
1172
    u->co_ctx = ctx->cur_co_ctx;
1374
1410
 
1375
1411
            if (rc == NGX_ERROR) {
1376
1412
                dd("input filter error: ft_type:%d waiting:%d",
1377
 
                        (int) u->ft_type, (int) u->waiting);
 
1413
                   (int) u->ft_type, (int) u->waiting);
1378
1414
 
1379
1415
                ngx_http_lua_socket_handle_error(r, u,
1380
1416
                                                 NGX_HTTP_LUA_SOCKET_FT_ERROR);
1630
1666
 
1631
1667
        default:
1632
1668
            msg = lua_pushfstring(L, "string, number, boolean, nil, "
1633
 
                    "or array table expected, got %s",
1634
 
                    lua_typename(L, type));
 
1669
                                  "or array table expected, got %s",
 
1670
                                  lua_typename(L, type));
1635
1671
 
1636
1672
            return luaL_argerror(L, 2, msg);
1637
1673
    }
1696
1732
 
1697
1733
    if (ctx->entered_content_phase) {
1698
1734
        r->write_event_handler = ngx_http_lua_content_wev_handler;
 
1735
 
 
1736
    } else {
 
1737
        r->write_event_handler = ngx_http_core_run_phases;
1699
1738
    }
1700
1739
 
1701
1740
    u->co_ctx = ctx->cur_co_ctx;
2622
2661
 
2623
2662
    if (ctx->entered_content_phase) {
2624
2663
        r->write_event_handler = ngx_http_lua_content_wev_handler;
 
2664
 
 
2665
    } else {
 
2666
        r->write_event_handler = ngx_http_core_run_phases;
2625
2667
    }
2626
2668
 
2627
2669
    u->co_ctx = ctx->cur_co_ctx;
2968
3010
 
2969
3011
    if (lua_gettop(L) != 0) {
2970
3012
        return luaL_error(L, "expecting zero arguments, but got %d",
2971
 
                lua_gettop(L));
 
3013
                          lua_gettop(L));
2972
3014
    }
2973
3015
 
2974
3016
    lua_pushlightuserdata(L, &ngx_http_lua_request_key);
2981
3023
                          "subrequest");
2982
3024
    }
2983
3025
 
 
3026
#if nginx_version >= 1003009
 
3027
    if (r->headers_in.chunked) {
 
3028
        lua_pushnil(L);
 
3029
        lua_pushliteral(L, "chunked request bodies not supported yet");
 
3030
        return 2;
 
3031
    }
 
3032
#endif
 
3033
 
2984
3034
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
2985
3035
    if (ctx == NULL) {
2986
3036
        return luaL_error(L, "no ctx found");
3002
3052
        return 2;
3003
3053
    }
3004
3054
 
3005
 
    if (r->headers_in.content_length_n == 0) {
 
3055
    dd("req content length: %d", (int) r->headers_in.content_length_n);
 
3056
 
 
3057
    if (r->headers_in.content_length_n <= 0) {
3006
3058
        lua_pushnil(L);
3007
 
        lua_pushliteral(L, "request body empty");
 
3059
        lua_pushliteral(L, "no body");
3008
3060
        return 2;
3009
3061
    }
3010
3062
 
3276
3328
                       "lua tcp socket connection pool size: %ui", pool_size);
3277
3329
 
3278
3330
        size = sizeof(ngx_http_lua_socket_pool_t) + key.len
3279
 
                + sizeof(ngx_http_lua_socket_pool_item_t)
3280
 
                * pool_size;
 
3331
               + sizeof(ngx_http_lua_socket_pool_item_t)
 
3332
               * pool_size;
3281
3333
 
3282
3334
        spool = lua_newuserdata(L, size);
3283
3335
        if (spool == NULL) {
3578
3630
    spool->active_connections--;
3579
3631
 
3580
3632
    dd("keepalive: active connections: %u",
3581
 
            (unsigned) spool->active_connections);
 
3633
       (unsigned) spool->active_connections);
3582
3634
 
3583
3635
    if (spool->active_connections == 0) {
3584
3636
        ngx_http_lua_socket_free_pool(ev->log, spool);
3748
3800
        last = ngx_copy(last, b->pos, b->last - b->pos);
3749
3801
 
3750
3802
        dd("copying input data chunk from %p: \"%.*s\"", cl,
3751
 
            (int) (b->last - b->pos), b->pos);
 
3803
           (int) (b->last - b->pos), b->pos);
3752
3804
    }
3753
3805
 
3754
3806
    lua_pushlstring(L, (char *) p, size);
3857
3909
    ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
3858
3910
 
3859
3911
    new_cl = ngx_http_lua_chains_get_free_buf(r->connection->log, r->pool,
3860
 
                                          &ctx->free_recv_bufs,
3861
 
                                          size,
3862
 
                                          (ngx_buf_tag_t)
3863
 
                                          &ngx_http_lua_module);
 
3912
                                              &ctx->free_recv_bufs,
 
3913
                                              size,
 
3914
                                              (ngx_buf_tag_t)
 
3915
                                              &ngx_http_lua_module);
3864
3916
 
3865
3917
    if (new_cl == NULL) {
3866
3918
        return NGX_ERROR;
3871
3923
    b->last = ngx_copy(b->last, pat, prefix);
3872
3924
 
3873
3925
    dd("copy resumed data to %p: %d: \"%.*s\"",
3874
 
        new_cl, (int) (b->last - b->pos), (int) (b->last - b->pos), b->pos);
 
3926
       new_cl, (int) (b->last - b->pos), (int) (b->last - b->pos), b->pos);
3875
3927
 
3876
3928
    dd("before resuming data: bufs_in %p, buf_in %p, buf_in next %p",
3877
 
        u->bufs_in, u->buf_in, u->buf_in->next);
 
3929
       u->bufs_in, u->buf_in, u->buf_in->next);
3878
3930
 
3879
3931
    ll = &u->bufs_in;
3880
3932
    for (cl = u->bufs_in; cl->next; cl = cl->next) {
3885
3937
    new_cl->next = u->buf_in;
3886
3938
 
3887
3939
    dd("after resuming data: bufs_in %p, buf_in %p, buf_in next %p",
3888
 
        u->bufs_in, u->buf_in, u->buf_in->next);
 
3940
       u->bufs_in, u->buf_in, u->buf_in->next);
3889
3941
 
3890
3942
#if (DDEBUG)
3891
3943
    for (cl = u->bufs_in; cl; cl = cl->next) {
3892
3944
        b = cl->buf;
3893
3945
 
3894
3946
        dd("result buf after resuming data: %p: %.*s", cl,
3895
 
            (int) ngx_buf_size(b), b->pos);
 
3947
           (int) ngx_buf_size(b), b->pos);
3896
3948
    }
3897
3949
#endif
3898
3950
 
3955
4007
    }
3956
4008
 
3957
4009
    if (rc == NGX_DONE) {
3958
 
        ngx_http_finalize_request(r, NGX_DONE);
 
4010
        ngx_http_lua_finalize_request(r, NGX_DONE);
3959
4011
        return ngx_http_lua_run_posted_threads(c,lmcf->lua, r, ctx);
3960
4012
    }
3961
4013
 
3962
4014
    if (ctx->entered_content_phase) {
3963
 
        ngx_http_finalize_request(r, rc);
 
4015
        ngx_http_lua_finalize_request(r, rc);
3964
4016
        return NGX_DONE;
3965
4017
    }
3966
4018
 
4007
4059
    ngx_http_lua_socket_tcp_finalize(u->request, u);
4008
4060
}
4009
4061
 
 
4062
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */