MySQL 8.0.39
Source Code Documentation
rest_api_utils.cc File Reference
#include "mysqlrouter/rest_api_utils.h"
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include "mysql/harness/utility/string.h"
#include "mysqlrouter/http_auth_realm_component.h"
#include "mysqlrouter/http_server_component.h"

Functions

void send_json_document (HttpRequest &req, HttpStatusCode::key_type status_code, const rapidjson::Document &json_doc)
 send json document as HTTP response. More...
 
void send_rfc7807_error (HttpRequest &req, HttpStatusCode::key_type status_code, const std::map< std::string, std::string > &fields)
 send a JsonProblem HTTP response. More...
 
void send_rfc7807_not_found_error (HttpRequest &req)
 send a JsonProblem "Not Found" error. More...
 
bool ensure_http_method (HttpRequest &req, HttpMethod::Bitset allowed_methods)
 ensure HTTP method is allowed. More...
 
bool ensure_auth (HttpRequest &req, const std::string require_realm)
 ensure request is authenticated. More...
 
bool ensure_no_params (HttpRequest &req)
 ensure request has no parameters. More...
 
bool ensure_modified_since (HttpRequest &req, time_t last_modified)
 ensure resource has modified since client received it. More...
 

Function Documentation

◆ ensure_auth()

bool ensure_auth ( HttpRequest req,
const std::string  require_realm 
)

ensure request is authenticated.

sends HTTP-response with status 401 if authentication was not successful.

Returns
success
Return values
trueif request is authenticaticated
falseif authentication was not successful and HTTP response has been sent

◆ ensure_http_method()

bool ensure_http_method ( HttpRequest req,
HttpMethod::Bitset  allowed_methods 
)

ensure HTTP method is allowed.

sends HTTP-response with status 405 if method is not allowed and sets Allow HTTP header.

Returns
success
Return values
trueif HTTP method is allowed
falseif HTTP is not allowed and HTTP response has been sent

◆ ensure_modified_since()

bool ensure_modified_since ( HttpRequest req,
time_t  last_modified 
)

ensure resource has modified since client received it.

sends HTTP-response with status 304 if client has a newer version that

Returns
success
Return values
trueif resource is modified since client received it.
falseif client has the same resource and HTTP response has been sent

◆ ensure_no_params()

bool ensure_no_params ( HttpRequest req)

ensure request has no parameters.

sends HTTP-response with status 400 if request contained a query string.

Returns
success
Return values
trueif request did not contain a query-string
falseif request contained a query-string and HTTP response has been sent

◆ send_json_document()

void send_json_document ( HttpRequest req,
HttpStatusCode::key_type  status_code,
const rapidjson::Document &  json_doc 
)

send json document as HTTP response.

Content-Type must be sent before the function is called.

Parameters
reqHttpRequest object to send error-msg
status_codeHTTP Status code of the problem message
json_docjson document to send as response

◆ send_rfc7807_error()

void send_rfc7807_error ( HttpRequest req,
HttpStatusCode::key_type  status_code,
const std::map< std::string, std::string > &  fields 
)

send a JsonProblem HTTP response.

RFC 7807 defines application/problem+json:

  • title
  • description
  • instance
  • detail
Parameters
reqHttpRequest object to send error-msg
status_codeHTTP Status code of the problem message
fieldsfields on the problem message

◆ send_rfc7807_not_found_error()

void send_rfc7807_not_found_error ( HttpRequest req)

send a JsonProblem "Not Found" error.

Parameters
reqHttpRequest object to send error-msg