![]() |
TSDuck v3.46-4930
MPEG Transport Stream Toolkit
|
#include <tsRestServer.h>
Public Member Functions | |
| RestServer (const RestArgs &args, Report &report) | |
| void | addResponseHeader (const UString &name, const UString &value) |
| bool | getPostJSON (json::ValuePtr &value) const |
| void | getPostText (UString &data) const |
| bool | getRequest (TCPConnection &conn) |
| bool | hasParameter (const UString &name) const |
| UString | header (const UString &name, const UString &def_value=UString()) const |
| const UStringToUStringMultiMap & | headers () const |
| const UString & | method () const |
| UString | parameter (const UString &name, const UString &def_value=UString()) const |
| const UStringToUStringMultiMap & | parameters () const |
| const UString & | path () const |
| const UString & | postContentType () const |
| const ByteBlock & | postData () const |
| void | replaceResponseHeader (const UString &name, const UString &value) |
| void | reset () |
| bool | sendResponse (TCPConnection &conn, int http_status, bool close) |
| void | setResponse (const ByteBlock &data, const UString &mime_type=u"application/octet-stream") |
| void | setResponse (const json::Value &value, const UString &mime_type=u"application/json") |
| void | setResponse (const UString &text, const UString &mime_type=u"text/plain; charset=utf-8") |
| const UString & | token () const |
Static Public Attributes | |
| static constexpr cn::milliseconds | DEFAULT_SERVER_RECEIVE_TIMEOUT = cn::milliseconds(3000) |
Basic helper for REST API servers.
Constructor.
| [in] | args | Initial REST operation arguments. This instance will keep a copy of it. If no receive timeout is specified for client connection, DEFAULT_SERVER_RECEIVE_TIMEOUT is applied. |
| [in,out] | report | Where to report errors. This instance will keep a reference to it. |
| void ts::RestServer::reset | ( | ) |
Cleanup request data, restart from an empty state.
| 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. |
|
staticconstexpr |
Default receive timeout on the server side. A rest server reads an HTTP request. All lines of the requests are supposed to arrive at once. The server is not expected to wait. On the other hand, a hostile client may want to infinitely wait between request lines as part of a DDoS attack which paralyze the server. Therefore, we impose a strict timeout on reception.