![]() |
TSDuck v3.43-4426
MPEG Transport Stream Toolkit
|
Basic helper for REST API servers. More...
#include <tsRestServer.h>
Public Member Functions | |
RestServer (const RestArgs &args, Report &report) | |
Constructor. | |
void | addResponseHeader (const UString &name, const UString &value) |
Add a header which will be sent with the response. | |
bool | getPostJSON (json::ValuePtr &value) const |
Get the POST data from the request in JSON format. | |
void | getPostText (UString &data) const |
Get the POST data from the request in text format. | |
bool | getRequest (TCPConnection &conn) |
Accept and decode one client request. | |
bool | hasParameter (const UString &name) const |
Check if the request's query parameters contains a parameter. | |
UString | header (const UString &name, const UString &def_value=UString()) const |
Get the first value of a given request header. | |
const UStringToUStringMultiMap & | headers () const |
Get a multimap of all request headers. | |
const UString & | method () const |
Return the request method of the previous getRequest(). | |
UString | parameter (const UString &name, const UString &def_value=UString()) const |
Get the value of a given request's query parameter. | |
const UStringToUStringMultiMap & | parameters () const |
Get a multimap of all request's query parameters. | |
const UString & | path () const |
Get the path of the last received request. | |
const UString & | postContentType () const |
Get the MIME type of the POST data, if specified. | |
const ByteBlock & | postData () const |
Get the POST data from the request. | |
void | replaceResponseHeader (const UString &name, const UString &value) |
Replace a header which will be sent with the response. | |
void | reset () |
Cleanup request data, restart from an empty state. | |
bool | sendResponse (TCPConnection &conn, int http_status, bool close) |
Send the response to the last client request. | |
void | setResponse (const ByteBlock &data, const UString &mime_type=u"application/octet-stream") |
Store the data to be sent with the response. | |
void | setResponse (const json::Value &value, const UString &mime_type=u"application/json") |
Store the data to be sent with the response. | |
void | setResponse (const UString &text, const UString &mime_type=u"text/plain; charset=utf-8") |
Store the data to be sent with the response. | |
const UString & | token () const |
Get the authentication token of the last received request, if any. | |
Basic helper for REST API servers.
Constructor.
[in] | args | Initial REST operation arguments. This instance will keep a copy of it. |
[in,out] | report | Where to report errors. This instance will keep a reference to it. |
bool ts::RestServer::getRequest | ( | TCPConnection & | conn | ) |
Accept and decode one client request.
If the RestArgs contains a non-empty authentication token, this token is checked in the request headers. If there is no matching token in the request, the request is rejected and the connection is closed.
[in,out] | conn | Client connection. This can be any TCPConnection but usually this is a TLSConnection. |
|
inline |
Return the request method of the previous getRequest().
|
inline |
Get the path of the last received request.
|
inline |
Get a multimap of all request's query parameters.
bool ts::RestServer::hasParameter | ( | const UString & | name | ) | const |
Check if the request's query parameters contains a parameter.
[in] | name | Name of the parameter to check. |
UString ts::RestServer::parameter | ( | const UString & | name, |
const UString & | def_value = UString() |
||
) | const |
Get the value of a given request's query parameter.
[in] | name | Name of the parameter to fetch. |
[in] | def_value | Value to return if the parameter name is not present. |
|
inline |
Get a multimap of all request headers.
UString ts::RestServer::header | ( | const UString & | name, |
const UString & | def_value = UString() |
||
) | const |
Get the first value of a given request header.
[in] | name | Name of the header to fetch. |
[in] | def_value | Value to return if the header name is not present. |
|
inline |
Get the authentication token of the last received request, if any.
Note that if the RestArgs contains a non-empty authentication token, this token was already checked by getRequest() and the request was rejected it the token didn't match.
|
inline |
Get the MIME type of the POST data, if specified.
|
inline |
Get the POST data from the request.
void ts::RestServer::getPostText | ( | UString & | data | ) | const |
Get the POST data from the request in text format.
[out] | data | The POST data. |
bool ts::RestServer::getPostJSON | ( | json::ValuePtr & | value | ) | const |
Get the POST data from the request in JSON format.
[out] | value | The POST data. |
Add a header which will be sent with the response.
If the same header already exists with another value, a new header is added.
[in] | name | The header name. |
[in] | value | The header value. |
Replace a header which will be sent with the response.
If the same header already exists with another value, it is replaced.
[in] | name | The header name. |
[in] | value | The header value. |
void ts::RestServer::setResponse | ( | const ByteBlock & | data, |
const UString & | mime_type = u"application/octet-stream" |
||
) |
Store the data to be sent with the response.
[in] | data | Returned data in binary form. |
[in] | mime_type | Optional MIME type for these data. |
void ts::RestServer::setResponse | ( | const UString & | text, |
const UString & | mime_type = u"text/plain; charset=utf-8" |
||
) |
Store the data to be sent with the response.
[in] | text | Returned data in text format. |
[in] | mime_type | Optional MIME type for these data. |
void ts::RestServer::setResponse | ( | const json::Value & | value, |
const UString & | mime_type = u"application/json" |
||
) |
Store the data to be sent with the response.
[in] | value | Returned data in JSON format. |
[in] | mime_type | Optional MIME type for these data. |
bool ts::RestServer::sendResponse | ( | TCPConnection & | conn, |
int | http_status, | ||
bool | close | ||
) |
Send the response to the last client request.
[in,out] | conn | Client connection from which the request was received. |
[in] | http_status | HTTP status code (200, 404, etc.) |
[in] | close | If true, the connection is closed after sending the response. |