NAME
Async::MicroserviceReq - async microservice request class
SYNOPSIS
my $this_req = Async::MicroserviceReq->new(
method => $plack_req->method,
headers => $plack_req->headers,
content => $plack_req->content,
path => $plack_req->path_info,
params => $plack_req->parameters,
static_dir => $self->static_dir,
);
...
my $plack_handler_sub = sub {
my ($plack_respond) = @_;
$this_req->plack_respond($plack_respond);
...
DESCRIPTION
This is an object created for each request handled by Async::Microservice. It is passed to all request handling functions as the first argument and it provides request information and response helper methods.
ATTRIBUTES
method
headers
path
params
plack_respond
static_dir
base_url
using_frontend_proxy
server_host
server_port
server_scheme
http_host
http_port
http_schema
want_json
content
json_content
METHODS
text_plain(@text_lines)
Send text plain response.
respond($status, $headers, $payload)
Send a PSGI/Plack response.
$headers must be an array reference of header key/value pairs.
If $payload is not a reference, it is sent as plain text by default. When the request Accept header allows JSON and no explicit Content-Type header is already present, plain scalar payloads are wrapped automatically as JSON:
{ "data": "..." }
For error statuses ($status >= 400), scalar payloads are wrapped as:
{ "error": { err_status => ..., err_msg => ... } }
If $payload is a reference, it is serialized as JSON. When JSONP is enabled and a valid callback parameter is present, the response is emitted as application/javascript instead of application/json.
redirect($location_path)
Send redirect.
using_frontend_proxy
Controls whether proxy-forwarded headers are trusted for base URL parsing.
When true, http_host, http_port, and http_schema are parsed from forwarded headers. If forwarded host headers are missing, parsing falls back to Host and emits a warning.
When false, http_host/http_port/http_schema are taken from trusted server fields (server_host/server_port/server_scheme) when present, otherwise host/port fall back to Host and scheme defaults to http.
server_host
Trusted server host for non-proxy mode (typically SERVER_NAME from PSGI environment).
server_port
Trusted server port for non-proxy mode (typically SERVER_PORT from PSGI environment).
server_scheme
Trusted server scheme for non-proxy mode (typically psgi.url_scheme from PSGI environment).
http_host
Returns parsed host value used to build base_url.
When using_frontend_proxy is true, this comes from forwarded host headers, with Host fallback and warning if forwarded host headers are missing.
When using_frontend_proxy is false, this comes from server_host when available, otherwise from Host.
http_port
Returns parsed port value used to build base_url.
When using_frontend_proxy is true, this comes from forwarded headers (or Host fallback when forwarded host headers are missing).
When using_frontend_proxy is false, this comes from server_port when available, otherwise from Host when present, else defaults to 80.
http_schema
Returns parsed scheme value used to build base_url.
When using_frontend_proxy is true, this is derived from forwarded HTTPS/ proto headers.
When using_frontend_proxy is false, this comes from server_scheme.
static_ft($file_name, $content_cb)
Send static file, can be updated/modified using optional callback.
get_pending_req
Returns number of currently pending async requests.