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_misc.h"
15 #include "ngx_http_lua_util.h"
16 
17 
18 static int ngx_http_lua_ngx_req_is_internal(lua_State *L);
19 
20 
21 void
ngx_http_lua_inject_req_misc_api(lua_State * L)22 ngx_http_lua_inject_req_misc_api(lua_State *L)
23 {
24     lua_pushcfunction(L, ngx_http_lua_ngx_req_is_internal);
25     lua_setfield(L, -2, "is_internal");
26 }
27 
28 
29 static int
ngx_http_lua_ngx_req_is_internal(lua_State * L)30 ngx_http_lua_ngx_req_is_internal(lua_State *L)
31 {
32     ngx_http_request_t  *r;
33 
34     r = ngx_http_lua_get_req(L);
35     if (r == NULL) {
36         return luaL_error(L, "no request object found");
37     }
38 
39     lua_pushboolean(L, r->internal == 1);
40     return 1;
41 }
42 
43 
44 int
ngx_http_lua_ffi_get_resp_status(ngx_http_request_t * r)45 ngx_http_lua_ffi_get_resp_status(ngx_http_request_t *r)
46 {
47     if (r->connection->fd == (ngx_socket_t) -1) {
48         return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
49     }
50 
51     if (r->err_status) {
52         return r->err_status;
53 
54     } else if (r->headers_out.status) {
55         return r->headers_out.status;
56 
57     } else if (r->http_version == NGX_HTTP_VERSION_9) {
58         return 9;
59 
60     } else {
61         return 0;
62     }
63 }
64 
65 
66 int
ngx_http_lua_ffi_set_resp_status(ngx_http_request_t * r,int status)67 ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int status)
68 {
69     if (r->connection->fd == (ngx_socket_t) -1) {
70         return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
71     }
72 
73     if (r->header_sent) {
74         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
75                       "attempt to set ngx.status after sending out "
76                       "response headers");
77         return NGX_DECLINED;
78     }
79 
80     r->headers_out.status = status;
81 
82     if (r->err_status) {
83         r->err_status = 0;
84     }
85 
86     if (status == 101) {
87         /*
88          * XXX work-around a bug in the Nginx core older than 1.5.5
89          * that 101 does not have a default status line
90          */
91 
92         ngx_str_set(&r->headers_out.status_line, "101 Switching Protocols");
93 
94     } else {
95         r->headers_out.status_line.len = 0;
96     }
97 
98     return NGX_OK;
99 }
100 
101 
102 int
ngx_http_lua_ffi_is_subrequest(ngx_http_request_t * r)103 ngx_http_lua_ffi_is_subrequest(ngx_http_request_t *r)
104 {
105     if (r->connection->fd == (ngx_socket_t) -1) {
106         return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
107     }
108 
109     return r != r->main;
110 }
111 
112 
113 int
ngx_http_lua_ffi_headers_sent(ngx_http_request_t * r)114 ngx_http_lua_ffi_headers_sent(ngx_http_request_t *r)
115 {
116     ngx_http_lua_ctx_t          *ctx;
117 
118     ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
119     if (ctx == NULL) {
120         return NGX_HTTP_LUA_FFI_NO_REQ_CTX;
121     }
122 
123     if (r->connection->fd == (ngx_socket_t) -1) {
124         return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
125     }
126 
127     return r->header_sent ? 1 : 0;
128 }
129 
130 
131 int
ngx_http_lua_ffi_get_conf_env(u_char * name,u_char ** env_buf,size_t * name_len)132 ngx_http_lua_ffi_get_conf_env(u_char *name, u_char **env_buf, size_t *name_len)
133 {
134     ngx_uint_t            i;
135     ngx_str_t            *var;
136     ngx_core_conf_t      *ccf;
137 
138     ccf = (ngx_core_conf_t *) ngx_get_conf(ngx_cycle->conf_ctx,
139                                            ngx_core_module);
140 
141     var = ccf->env.elts;
142 
143     for (i = 0; i < ccf->env.nelts; i++) {
144         if (var[i].data[var[i].len] == '='
145             && ngx_strncmp(name, var[i].data, var[i].len) == 0)
146         {
147             *env_buf = var[i].data;
148             *name_len = var[i].len;
149 
150             return NGX_OK;
151         }
152     }
153 
154     return NGX_DECLINED;
155 }
156 
157 
158 /* vi:set ft=c ts=4 sw=4 et fdm=marker: */
159