Skip to content

Commit 9223cb6

Browse files
feature: add ngx.resp.set_status(status, reason).
1 parent 24cafc6 commit 9223cb6

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/ngx_http_lua_misc.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ ngx_http_lua_ffi_get_resp_status(ngx_http_request_t *r)
6464

6565

6666
int
67-
ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int status)
67+
ngx_http_lua_ffi_set_resp_status_and_reason(ngx_http_request_t *r, int status,
68+
const char *reason, size_t reason_len)
6869
{
70+
u_char *buf;
71+
6972
if (r->connection->fd == (ngx_socket_t) -1) {
7073
return NGX_HTTP_LUA_FFI_BAD_CONTEXT;
7174
}
@@ -77,6 +80,14 @@ ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int status)
7780
return NGX_DECLINED;
7881
}
7982

83+
/* per RFC-7230 sec 3.1.2, the status line must be 3 digits, it also makes
84+
* buffer size calculation easier */
85+
if (status < 100 || status > 999) {
86+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
87+
"invalid HTTP status code %d", status);
88+
return NGX_DECLINED;
89+
}
90+
8091
r->headers_out.status = status;
8192

8293
if (r->err_status) {
@@ -91,6 +102,18 @@ ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int status)
91102

92103
ngx_str_set(&r->headers_out.status_line, "101 Switching Protocols");
93104

105+
} else if (reason != NULL && reason_len > 0) {
106+
reason_len += 5; /* "ddd <reason>\0" */
107+
buf = ngx_palloc(r->pool, reason_len);
108+
if (buf == NULL) {
109+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no memory");
110+
return NGX_DECLINED;
111+
}
112+
113+
ngx_snprintf(buf, reason_len, "%d %s", status, reason);
114+
r->headers_out.status_line.len = reason_len;
115+
r->headers_out.status_line.data = buf;
116+
94117
} else {
95118
r->headers_out.status_line.len = 0;
96119
}
@@ -99,6 +122,13 @@ ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int status)
99122
}
100123

101124

125+
int
126+
ngx_http_lua_ffi_set_resp_status(ngx_http_request_t *r, int status)
127+
{
128+
return ngx_http_lua_ffi_set_resp_status_and_reason(r, status, NULL, 0);
129+
}
130+
131+
102132
int
103133
ngx_http_lua_ffi_req_is_internal(ngx_http_request_t *r)
104134
{

t/015-status.t

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,57 @@ ngx.status: 654
293293
--- no_error_log
294294
[error]
295295
--- error_code: 654
296+
297+
298+
299+
=== TEST 17: set status and reason
300+
--- config
301+
location = /upstream {
302+
content_by_lua_block {
303+
local resp = require "ngx.resp"
304+
resp.set_status(500, "user defined reason")
305+
ngx.say("set_status_and_reason")
306+
}
307+
}
308+
309+
location /t {
310+
content_by_lua_block {
311+
local sock = ngx.socket.tcp()
312+
local port = ngx.var.server_port
313+
local ok, err = sock:connect("127.0.0.1", port)
314+
if not ok then
315+
ngx.say("failed to connect: ", err)
316+
return
317+
end
318+
319+
local req = "GET /upstream HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\n\r\n"
320+
321+
local bytes, err = sock:send(req)
322+
if not bytes then
323+
ngx.say("failed to send request: ", err)
324+
return
325+
end
326+
327+
local found = false
328+
while true do
329+
local line, err, part = sock:receive()
330+
if line then
331+
if ngx.re.find(line, "HTTP/1.1 500 user defined reason") then
332+
ngx.say("match")
333+
end
334+
else
335+
break
336+
end
337+
end
338+
339+
sock:close()
340+
}
341+
}
342+
--- request
343+
GET /t
344+
--- response_body
345+
match
346+
--- no_error_log
347+
[error]
348+
[crit]
349+
--- error_code: 200

0 commit comments

Comments
 (0)