Programmable HTTP Access Phase and Request Body Reading#1044
Draft
xeioex wants to merge 2 commits intonginx:masterfrom
Draft
Programmable HTTP Access Phase and Request Body Reading#1044xeioex wants to merge 2 commits intonginx:masterfrom
xeioex wants to merge 2 commits intonginx:masterfrom
Conversation
The js_access directive registers a JavaScript handler in the HTTP ACCESS phase.
Added async methods
- r.readRequestText() as string
- r.readRequestBuffer() as ArrayBuffer
- r.readRequestJSON() as object.
that return Promises resolving with the request body wrapped
as a corresponding type.
930f611 to
3145d7c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Programmable HTTP Access Phase and Request Body Reading
Overview
Two new capabilities let JavaScript handlers participate in request
processing before the content phase begins:
js_access-- registers a handler in the HTTP access phase forauthorization, routing, and request preprocessing.
r.readRequestText(),r.readRequestArrayBuffer(),r.readRequestJSON()-- async methods that read and cache therequest body, available in any HTTP handler.
Together they enable decisions based on headers, arguments, variables,
and the request body -- all resolved before content generation or
proxying starts.
js_access Directive
Context:
http,server,locationThe handler runs in
NGX_HTTP_ACCESS_PHASE, before content handlers(
js_content,proxy_pass,fastcgi_pass, etc.) and after built-inaccess checkers (
allow/deny,auth_basic,auth_request).Configuration inherits from outer to inner blocks.
r.return(status)to reject the request, or simply return to continue to the next phase.
the Promise settles, enabling
ngx.fetch(),r.subrequest(),setTimeout(), and body reading without blocking the event loop.500 Internal Server Error.r.return(), processing continuesnormally to the content phase.
Request Body Reading
Three async methods read and cache the request body:
r.readRequestText()Promise<string>r.readRequestArrayBuffer()Promise<ArrayBuffer>r.readRequestJSON()Promise<object>proxy_passforwards it unchanged).
client_body_buffer_size),client_body_in_file_only, andclient_max_body_sizeenforcement.js_accessandjs_contenthandlers.Examples
Authentication with an External Service
The access handler calls an auth service via
ngx.fetch(). On failurethe request is rejected immediately; on success a variable is set for
downstream use. The content handler (
proxy_pass) only runs afterauthentication succeeds.
Authentication with a Subrequest
Same pattern using an internal subrequest instead of an outbound fetch.
Dynamic Upstream Routing
The access handler computes a routing variable synchronously;
proxy_passevaluates it after the access phase completes.
Body-Based Access Control
The request body is parsed as JSON in the access phase. The policy
decision is made before the request reaches the backend. The body is
preserved and forwarded to
proxy_passunchanged.Body-Driven Routing
Combines body reading with dynamic routing: the request is parsed once
and the upstream is selected based on a field in the payload.
Questions
This is a draft. We are collecting feedback before finalizing the API.
Please share your thoughts on any of the following:
Naming -- are
readRequestText(),readRequestArrayBuffer(), andreadRequestJSON()clear and consistent? This follow elements of Fetch API.Use cases -- which of the examples above match problems you are
solving today? Are there scenarios you would need that are not
covered?
Error handling -- the handler can call
r.return(status)toreject a request or let an unhandled exception produce 500. Is this
sufficient, or do you need more control over error responses from
access handlers?
Missing features -- is there anything you would expect from an
access-phase handler that is not present here?