std::cgi
A utility module for writing CGI scripts in Pyro.
Requests
-
Request() -> Request
-
A utility class for handling CGI requests.
- Automatically parses parameter values from the request's query string.
-
Automatically parses parameter values from the request's body if the
Content-Type
header isapplication/x-www-form-urlencoded
.
The Request
class has the following methods:
-
:body() -> str
-
Returns the request's body as a string.
-
:found(key) -> bool
-
Returns
true
if at least one parameter value forkey
was found, wherekey
is a query-string or form parameter. -
:from_json() -> map|vec|str|i64|f64|bool|null|err
-
Attempts to parse the request's body as JSON, returning the result. Returns an
err
if the body is not valid JSON. -
:params() -> map[string, vec[str]]
-
Returns a map containing parsed parameter values.
-
:value(key) -> str|err
-
Returns the parameter value for
key
wherekey
is a query-string or form parameter.-
Returns an
err
ifkey
was not found. -
If multiple values were found for
key
, returns the final value to be parsed.
-
Returns an
-
:values(key) -> vec[str]
-
Returns the list of parameter values for
key
wherekey
is a query-string or form parameter.Returns an empty vector if
key
was not found.
Responses
-
Response() -> Response
-
A utility class for handling CGI responses.
The Response
class has the following methods:
-
:add_header(header: str) -> Response
-
Adds a HTTP header to the response.
Returns
self
to allow chaining. -
:make_301_redirect(url: str) -> Response
-
Adds the appropriate headers for a
301
(permanent) redirect to the specified URL.Returns
self
to allow chaining. -
:make_302_redirect(url: str) -> Response
-
Adds the appropriate headers for a
302
redirect to the specified URL.Returns
self
to allow chaining. -
:make_303_redirect(url: str) -> Response
-
Adds the appropriate headers for a
303
redirect to the specified URL.Returns
self
to allow chaining. -
:make_307_redirect(url: str) -> Response
-
Adds the appropriate headers for a
307
(temporary) redirect to the specified URL.Returns
self
to allow chaining. -
:send()
-
Writes the response to the standard output stream.
-
:write(arg: any) -> Response
:write(format_string: str, *args: any) -> Response
-
Writes to the response's
body
buffer.-
Calling this method with a single argument is equivalent to calling
$str()
on that argument first and writing the resulting string. -
Calling this method with more than one argument is equivalent to calling
$fmt()
on those arguments first and writing the resulting string.
Returns
self
to allow chaining.You can call this method multiple times to keep appending content to the response.
No data is sent until the
:send()
method is called. -
Calling this method with a single argument is equivalent to calling