Line data Source code
1 :
2 : /*
3 : * Copyright (C) Yichun Zhang (agentzh)
4 : */
5 :
6 :
7 : #ifndef DDEBUG
8 : #define DDEBUG 0
9 : #endif
10 :
11 :
12 : #include "ddebug.h"
13 : #include "ngx_http_lua_req_method.h"
14 : #include "ngx_http_lua_subrequest.h"
15 : #include "ngx_http_lua_util.h"
16 :
17 :
18 : static int ngx_http_lua_ngx_req_get_method(lua_State *L);
19 : static int ngx_http_lua_ngx_req_set_method(lua_State *L);
20 :
21 :
22 : void
23 18 : ngx_http_lua_inject_req_method_api(lua_State *L)
24 : {
25 18 : lua_pushcfunction(L, ngx_http_lua_ngx_req_get_method);
26 18 : lua_setfield(L, -2, "get_method");
27 :
28 18 : lua_pushcfunction(L, ngx_http_lua_ngx_req_set_method);
29 18 : lua_setfield(L, -2, "set_method");
30 18 : }
31 :
32 :
33 : static int
34 0 : ngx_http_lua_ngx_req_get_method(lua_State *L)
35 : {
36 : int n;
37 : ngx_http_request_t *r;
38 :
39 0 : n = lua_gettop(L);
40 0 : if (n != 0) {
41 0 : return luaL_error(L, "only one argument expected but got %d", n);
42 : }
43 :
44 0 : r = ngx_http_lua_get_req(L);
45 0 : if (r == NULL) {
46 0 : return luaL_error(L, "request object not found");
47 : }
48 :
49 0 : ngx_http_lua_check_fake_request(L, r);
50 :
51 0 : lua_pushlstring(L, (char *) r->method_name.data, r->method_name.len);
52 0 : return 1;
53 : }
54 :
55 :
56 : static int
57 0 : ngx_http_lua_ngx_req_set_method(lua_State *L)
58 : {
59 : int n;
60 : int method;
61 : ngx_http_request_t *r;
62 :
63 0 : n = lua_gettop(L);
64 0 : if (n != 1) {
65 0 : return luaL_error(L, "only one argument expected but got %d", n);
66 : }
67 :
68 0 : method = luaL_checkint(L, 1);
69 :
70 0 : r = ngx_http_lua_get_req(L);
71 0 : if (r == NULL) {
72 0 : return luaL_error(L, "request object not found");
73 : }
74 :
75 0 : ngx_http_lua_check_fake_request(L, r);
76 :
77 0 : switch (method) {
78 0 : case NGX_HTTP_GET:
79 0 : r->method_name = ngx_http_lua_get_method;
80 0 : break;
81 :
82 0 : case NGX_HTTP_POST:
83 0 : r->method_name = ngx_http_lua_post_method;
84 0 : break;
85 :
86 0 : case NGX_HTTP_PUT:
87 0 : r->method_name = ngx_http_lua_put_method;
88 0 : break;
89 :
90 0 : case NGX_HTTP_HEAD:
91 0 : r->method_name = ngx_http_lua_head_method;
92 0 : break;
93 :
94 0 : case NGX_HTTP_DELETE:
95 0 : r->method_name = ngx_http_lua_delete_method;
96 0 : break;
97 :
98 0 : case NGX_HTTP_OPTIONS:
99 0 : r->method_name = ngx_http_lua_options_method;
100 0 : break;
101 :
102 0 : case NGX_HTTP_MKCOL:
103 0 : r->method_name = ngx_http_lua_mkcol_method;
104 0 : break;
105 :
106 0 : case NGX_HTTP_COPY:
107 0 : r->method_name = ngx_http_lua_copy_method;
108 0 : break;
109 :
110 0 : case NGX_HTTP_MOVE:
111 0 : r->method_name = ngx_http_lua_move_method;
112 0 : break;
113 :
114 0 : case NGX_HTTP_PROPFIND:
115 0 : r->method_name = ngx_http_lua_propfind_method;
116 0 : break;
117 :
118 0 : case NGX_HTTP_PROPPATCH:
119 0 : r->method_name = ngx_http_lua_proppatch_method;
120 0 : break;
121 :
122 0 : case NGX_HTTP_LOCK:
123 0 : r->method_name = ngx_http_lua_lock_method;
124 0 : break;
125 :
126 0 : case NGX_HTTP_UNLOCK:
127 0 : r->method_name = ngx_http_lua_unlock_method;
128 0 : break;
129 :
130 0 : case NGX_HTTP_PATCH:
131 0 : r->method_name = ngx_http_lua_patch_method;
132 0 : break;
133 :
134 0 : case NGX_HTTP_TRACE:
135 0 : r->method_name = ngx_http_lua_trace_method;
136 0 : break;
137 :
138 0 : default:
139 0 : return luaL_error(L, "unsupported HTTP method: %d", method);
140 : }
141 :
142 0 : r->method = method;
143 :
144 0 : return 0;
145 : }
146 :
147 :
148 : #ifndef NGX_LUA_NO_FFI_API
149 : int
150 0 : ngx_http_lua_ffi_req_get_method(ngx_http_request_t *r)
151 : {
152 0 : if (r->connection->fd == (ngx_socket_t) -1) {
153 0 : return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
154 : }
155 :
156 0 : return r->method;
157 : }
158 :
159 :
160 : int
161 0 : ngx_http_lua_ffi_req_get_method_name(ngx_http_request_t *r, char *buf,
162 : size_t *len)
163 : {
164 0 : if (r->connection->fd == (ngx_socket_t) -1) {
165 0 : return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
166 : }
167 :
168 0 : *len = ngx_min(r->method_name.len, *len);
169 0 : ngx_memcpy(buf, r->method_name.data, *len);
170 0 : return NGX_OK;
171 : }
172 :
173 :
174 : int
175 0 : ngx_http_lua_ffi_req_set_method(ngx_http_request_t *r, int method)
176 : {
177 0 : if (r->connection->fd == (ngx_socket_t) -1) {
178 0 : return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
179 : }
180 :
181 0 : switch (method) {
182 0 : case NGX_HTTP_GET:
183 0 : r->method_name = ngx_http_lua_get_method;
184 0 : break;
185 :
186 0 : case NGX_HTTP_POST:
187 0 : r->method_name = ngx_http_lua_post_method;
188 0 : break;
189 :
190 0 : case NGX_HTTP_PUT:
191 0 : r->method_name = ngx_http_lua_put_method;
192 0 : break;
193 :
194 0 : case NGX_HTTP_HEAD:
195 0 : r->method_name = ngx_http_lua_head_method;
196 0 : break;
197 :
198 0 : case NGX_HTTP_DELETE:
199 0 : r->method_name = ngx_http_lua_delete_method;
200 0 : break;
201 :
202 0 : case NGX_HTTP_OPTIONS:
203 0 : r->method_name = ngx_http_lua_options_method;
204 0 : break;
205 :
206 0 : case NGX_HTTP_MKCOL:
207 0 : r->method_name = ngx_http_lua_mkcol_method;
208 0 : break;
209 :
210 0 : case NGX_HTTP_COPY:
211 0 : r->method_name = ngx_http_lua_copy_method;
212 0 : break;
213 :
214 0 : case NGX_HTTP_MOVE:
215 0 : r->method_name = ngx_http_lua_move_method;
216 0 : break;
217 :
218 0 : case NGX_HTTP_PROPFIND:
219 0 : r->method_name = ngx_http_lua_propfind_method;
220 0 : break;
221 :
222 0 : case NGX_HTTP_PROPPATCH:
223 0 : r->method_name = ngx_http_lua_proppatch_method;
224 0 : break;
225 :
226 0 : case NGX_HTTP_LOCK:
227 0 : r->method_name = ngx_http_lua_lock_method;
228 0 : break;
229 :
230 0 : case NGX_HTTP_UNLOCK:
231 0 : r->method_name = ngx_http_lua_unlock_method;
232 0 : break;
233 :
234 0 : case NGX_HTTP_PATCH:
235 0 : r->method_name = ngx_http_lua_patch_method;
236 0 : break;
237 :
238 0 : case NGX_HTTP_TRACE:
239 0 : r->method_name = ngx_http_lua_trace_method;
240 0 : break;
241 :
242 0 : default:
243 0 : return NGX_DECLINED;
244 : }
245 :
246 0 : r->method = method;
247 0 : return NGX_OK;
248 : }
249 : #endif
250 :
251 :
252 : /* vi:set ft=c ts=4 sw=4 et fdm=marker: */
|