Line data Source code
1 :
2 : /*
3 : * Copyright (C) Xiaozhe Wang (chaoslawful)
4 : * Copyright (C) Yichun Zhang (agentzh)
5 : */
6 :
7 :
8 : #ifndef DDEBUG
9 : #define DDEBUG 0
10 : #endif
11 : #include "ddebug.h"
12 :
13 :
14 : #include "ngx_http_lua_uri.h"
15 : #include "ngx_http_lua_util.h"
16 :
17 :
18 : static int ngx_http_lua_ngx_req_set_uri(lua_State *L);
19 :
20 :
21 : void
22 18 : ngx_http_lua_inject_req_uri_api(ngx_log_t *log, lua_State *L)
23 : {
24 18 : lua_pushcfunction(L, ngx_http_lua_ngx_req_set_uri);
25 18 : lua_setfield(L, -2, "set_uri");
26 18 : }
27 :
28 :
29 : static int
30 0 : ngx_http_lua_ngx_req_set_uri(lua_State *L)
31 : {
32 : ngx_http_request_t *r;
33 : size_t len;
34 : u_char *p;
35 : int n;
36 0 : int jump = 0;
37 : ngx_http_lua_ctx_t *ctx;
38 :
39 0 : n = lua_gettop(L);
40 :
41 0 : if (n != 1 && n != 2) {
42 0 : return luaL_error(L, "expecting 1 or 2 arguments but seen %d", n);
43 : }
44 :
45 0 : r = ngx_http_lua_get_req(L);
46 0 : if (r == NULL) {
47 0 : return luaL_error(L, "no request found");
48 : }
49 :
50 0 : ngx_http_lua_check_fake_request(L, r);
51 :
52 0 : p = (u_char *) luaL_checklstring(L, 1, &len);
53 :
54 0 : if (len == 0) {
55 0 : return luaL_error(L, "attempt to use zero-length uri");
56 : }
57 :
58 0 : if (n == 2) {
59 :
60 0 : luaL_checktype(L, 2, LUA_TBOOLEAN);
61 0 : jump = lua_toboolean(L, 2);
62 :
63 0 : if (jump) {
64 :
65 0 : ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
66 0 : if (ctx == NULL) {
67 0 : return luaL_error(L, "no ctx found");
68 : }
69 :
70 : dd("rewrite: %d, access: %d, content: %d",
71 : (int) ctx->entered_rewrite_phase,
72 : (int) ctx->entered_access_phase,
73 : (int) ctx->entered_content_phase);
74 :
75 0 : ngx_http_lua_check_context(L, ctx, NGX_HTTP_LUA_CONTEXT_REWRITE);
76 :
77 0 : ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
78 : "lua set uri jump to \"%*s\"", len, p);
79 :
80 0 : ngx_http_lua_check_if_abortable(L, ctx);
81 : }
82 : }
83 :
84 0 : r->uri.data = ngx_palloc(r->pool, len);
85 0 : if (r->uri.data == NULL) {
86 0 : return luaL_error(L, "no memory");
87 : }
88 :
89 0 : ngx_memcpy(r->uri.data, p, len);
90 :
91 0 : r->uri.len = len;
92 :
93 0 : r->internal = 1;
94 0 : r->valid_unparsed_uri = 0;
95 :
96 0 : ngx_http_set_exten(r);
97 :
98 0 : if (jump) {
99 0 : r->uri_changed = 1;
100 :
101 0 : return lua_yield(L, 0);
102 : }
103 :
104 0 : r->valid_location = 0;
105 0 : r->uri_changed = 0;
106 :
107 0 : return 0;
108 : }
109 :
110 : /* vi:set ft=c ts=4 sw=4 et fdm=marker: */
|