diff --git a/lib/ngx/resp.lua b/lib/ngx/resp.lua index 28d1ed4aa..2cc9269e5 100644 --- a/lib/ngx/resp.lua +++ b/lib/ngx/resp.lua @@ -10,6 +10,7 @@ local FFI_BAD_CONTEXT = base.FFI_BAD_CONTEXT local core_response = require "resty.core.response" local set_resp_header = core_response.set_resp_header local get_request = base.get_request +local bypass_if_checks = core_response.bypass_if_checks ffi.cdef[[ 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) end end + +function _M.bypass_if_checks() + return bypass_if_checks() +end + return _M diff --git a/lib/ngx/resp.md b/lib/ngx/resp.md index 01f8c4729..d0c3d6419 100644 --- a/lib/ngx/resp.md +++ b/lib/ngx/resp.md @@ -13,6 +13,7 @@ Table of Contents * [Methods](#methods) * [add_header](#add_header) * [set_status](#set_status) + * [bypass_if_checks](#bypass_if_checks) * [Community](#community) * [English Mailing List](#english-mailing-list) * [Chinese Mailing List](#chinese-mailing-list) @@ -87,6 +88,23 @@ status with an optional reason. The `reason` should be a string. [Back to TOC](#table-of-contents) +bypass_if_checks +------------------ +**syntax:** *ngx_resp.bypass_if_checks()* + +Bypasses the conditional header checks as defined in RFC 9110 for the current request. +This method is useful when you need to skip validation of headers such as `If-Modified-Since`, +`If-None-Match`, `If-Range`, `If-Unmodified-Since`, and `If-Match`, allowing the response to +proceed without evaluating these conditions. + +When Nginx's validation of a conditional request fails, it has a specific response. +It will return a 412 error, as per the provisions of Section 13 of RFC 9110. If `error_page 412` is configured, +it will result in an internal redirect to the error handling process, making it impossible to customize the exit status code. + +This configuration, designed to bypass Nginx's built-in validation of conditional requests, serves a specific purpose. + +[Back to TOC](#table-of-contents) + Community ========= diff --git a/lib/resty/core/response.lua b/lib/resty/core/response.lua index 986de7461..0bd53d14f 100644 --- a/lib/resty/core/response.lua +++ b/lib/resty/core/response.lua @@ -42,6 +42,8 @@ ffi.cdef[[ const unsigned char *key, size_t key_len, unsigned char *key_buf, ngx_http_lua_ffi_str_t *values, int max_nvalues, char **errmsg); + + int ngx_http_lua_ffi_bypass_if_checks(ngx_http_request_t *r); ]] @@ -226,6 +228,18 @@ local function get_resp_header(tb, key) end +local function bypass_if_checks() + local r = get_request() + if not r then + error("no request found") + end + C.ngx_http_lua_ffi_bypass_if_checks(r) +end + + +_M.bypass_if_checks = bypass_if_checks + + do local mt = new_tab(0, 2) mt.__newindex = set_resp_header