Skip to content

Commit 34a59a7

Browse files
authored
feature: add bypass_if_checks method to ngx.resp (#495)
I hereby granted the copyright of the changes in this pull request to the authors of this lua-resty-core project. Signed-off-by: tzssangglass <tzssangglass@gmail.com> * apply comments Signed-off-by: tzssangglass <tzssangglass@gmail.com> --------- Signed-off-by: tzssangglass <tzssangglass@gmail.com>
1 parent 72a113f commit 34a59a7

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

lib/ngx/resp.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ local FFI_BAD_CONTEXT = base.FFI_BAD_CONTEXT
1010
local core_response = require "resty.core.response"
1111
local set_resp_header = core_response.set_resp_header
1212
local get_request = base.get_request
13+
local bypass_if_checks = core_response.bypass_if_checks
1314

1415
ffi.cdef[[
1516
int ngx_http_lua_ffi_set_resp_status_and_reason(ngx_http_request_t *r,
@@ -43,4 +44,9 @@ function _M.set_status(status, reason)
4344
end
4445
end
4546

47+
48+
function _M.bypass_if_checks()
49+
return bypass_if_checks()
50+
end
51+
4652
return _M

lib/ngx/resp.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Table of Contents
1313
* [Methods](#methods)
1414
* [add_header](#add_header)
1515
* [set_status](#set_status)
16+
* [bypass_if_checks](#bypass_if_checks)
1617
* [Community](#community)
1718
* [English Mailing List](#english-mailing-list)
1819
* [Chinese Mailing List](#chinese-mailing-list)
@@ -87,6 +88,23 @@ status with an optional reason. The `reason` should be a string.
8788

8889
[Back to TOC](#table-of-contents)
8990

91+
bypass_if_checks
92+
------------------
93+
**syntax:** *ngx_resp.bypass_if_checks()*
94+
95+
Bypasses the conditional header checks as defined in RFC 9110 for the current request.
96+
This method is useful when you need to skip validation of headers such as `If-Modified-Since`,
97+
`If-None-Match`, `If-Range`, `If-Unmodified-Since`, and `If-Match`, allowing the response to
98+
proceed without evaluating these conditions.
99+
100+
When Nginx's validation of a conditional request fails, it has a specific response.
101+
It will return a 412 error, as per the provisions of Section 13 of RFC 9110. If `error_page 412` is configured,
102+
it will result in an internal redirect to the error handling process, making it impossible to customize the exit status code.
103+
104+
This configuration, designed to bypass Nginx's built-in validation of conditional requests, serves a specific purpose.
105+
106+
[Back to TOC](#table-of-contents)
107+
90108
Community
91109
=========
92110

lib/resty/core/response.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ ffi.cdef[[
4242
const unsigned char *key, size_t key_len,
4343
unsigned char *key_buf, ngx_http_lua_ffi_str_t *values,
4444
int max_nvalues, char **errmsg);
45+
46+
int ngx_http_lua_ffi_bypass_if_checks(ngx_http_request_t *r);
4547
]]
4648

4749

@@ -226,6 +228,18 @@ local function get_resp_header(tb, key)
226228
end
227229

228230

231+
local function bypass_if_checks()
232+
local r = get_request()
233+
if not r then
234+
error("no request found")
235+
end
236+
C.ngx_http_lua_ffi_bypass_if_checks(r)
237+
end
238+
239+
240+
_M.bypass_if_checks = bypass_if_checks
241+
242+
229243
do
230244
local mt = new_tab(0, 2)
231245
mt.__newindex = set_resp_header

0 commit comments

Comments
 (0)