SOIL C++
C++ Unified Device Interface
HTTP::Resource Class Reference

HTTP Resource base class. More...

#include <Resource.h>

Inheritance diagram for HTTP::Resource:
SOIL::Element SOIL::Figure< T, -1, -1 > SOIL::Figure< T, x, y > SOIL::Function SOIL::Object SOIL::Parameter< T, x, y > SOIL::Variable< T, x, y >

Public Member Functions

 Resource ()
 Constructor. More...
 
 ~Resource ()
 Default Destructor. More...
 
virtual Response handle (Request message, std::smatch match=std::smatch())
 HTTP Handler. More...
 
virtual Response handle_get (Request message, std::smatch match=std::smatch())
 HTTP GET Handler. More...
 
virtual Response handle_put (Request message, std::smatch match=std::smatch())
 HTTP PUT Handler. More...
 
virtual Response handle_post (Request message, std::smatch match=std::smatch())
 HTTP POST Handler. More...
 
virtual Response handle_delete (Request message, std::smatch match=std::smatch())
 HTTP DELETE Handler. More...
 
virtual Response handle_patch (Request message, std::smatch match=std::smatch())
 HTTP PATCH Handler. More...
 
virtual Response handle_options (Request message, std::smatch match=std::smatch())
 HTTP OPTIONS Handler. More...
 
virtual Response handle_head (Request message, std::smatch match=std::smatch())
 HTTP HEAD Handler. More...
 
virtual Response handle_exception (Request message, std::exception &exception, std::smatch match=std::smatch())
 HTTP Exception handler. More...
 

Static Public Member Functions

static web::json::value request_info (Request message, std::smatch match=std::smatch())
 Request Info. More...
 

Protected Member Functions

void apply_headers (Response &response)
 Apply headers. More...
 

Protected Attributes

std::vector< web::http::method > allowed_methods
 Allowed methods. More...
 
std::string content_type
 Content type. More...
 
std::string allowed_origins
 Allowed Origins. More...
 

Detailed Description

Base class for any resource that is implemented using this library. It should always be subclassed by the class implmenting the resource itself, and the relevant functions should be overriden. All HTTP business logic internally relies on the cpprestsdk library. The former internally uses widestrings, which leads to the need for conversion work at some places.

Definition at line 20 of file Resource.h.

Constructor & Destructor Documentation

◆ Resource()

HTTP::Resource::Resource ( )

Default Constructor, sets the above mentioned defaults for allowed_methods, content_type and allowed_origins

Definition at line 67 of file Resource.cpp.

◆ ~Resource()

HTTP::Resource::~Resource ( )

Default destructor, does no custom business logic.

Definition at line 74 of file Resource.cpp.

Member Function Documentation

◆ apply_headers()

void HTTP::Resource::apply_headers ( Response response)
protected

Apply headers to the HTTP response object. This function shoud be called in the handler before returning the repsonse object to the server.

Parameters
[in,out]responseResponse object to act on.

Definition at line 50 of file Resource.cpp.

◆ handle()

HTTP::Response HTTP::Resource::handle ( Request  message,
std::smatch  match = std::smatch() 
)
virtual

Handler that is called by the server on HTTP requests. By default, this function redirects to the more specifc ones based on the respective HTTP Verb. In case an exception occurs, it calls the exception handler.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Reimplemented in SOIL::Element.

Definition at line 78 of file Resource.cpp.

◆ handle_delete()

HTTP::Response HTTP::Resource::handle_delete ( Request  message,
std::smatch  match = std::smatch() 
)
virtual

Handler that is called by the server on HTTP requests on a DELETE method. This function should be overridden by the implementation of the resource. Per default, it resorts to the internal fallback function.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Reimplemented in SOIL::Object.

Definition at line 136 of file Resource.cpp.

◆ handle_exception()

HTTP::Response HTTP::Resource::handle_exception ( Request  message,
std::exception &  exception,
std::smatch  match = std::smatch() 
)
virtual

Handler function which can be called inside other handler functions to manage exceptions. It returns the request's information and exeption error text while setting the HTTP response to 500 (Internal Error).

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]exceptionException to handle and to copy the error message from.
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Definition at line 158 of file Resource.cpp.

◆ handle_get()

HTTP::Response HTTP::Resource::handle_get ( Request  message,
std::smatch  match = std::smatch() 
)
virtual

Handler that is called by the server on HTTP requests on a GET method. This function should be overridden by the implementation of the resource. Per default, it resorts to the internal fallback function.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Reimplemented in SOIL::Object, SOIL::Parameter< T, x, y >, SOIL::Variable< T, x, y >, and SOIL::Function.

Definition at line 120 of file Resource.cpp.

◆ handle_head()

HTTP::Response HTTP::Resource::handle_head ( Request  message,
std::smatch  match = std::smatch() 
)
virtual

Handler that is called by the server on HTTP requests on a HEAD method. This function should be overridden by the implementation of the resource. Per default, it resorts to the internal fallback function.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Definition at line 153 of file Resource.cpp.

◆ handle_options()

HTTP::Response HTTP::Resource::handle_options ( Request  message,
std::smatch  match = std::smatch() 
)
virtual

Handler that is called by the server on HTTP requests on an OPTIONS method. This function should be overridden by the implementation of the resource. Per default, it resorts to the internal fallback function.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Reimplemented in SOIL::Variable< T, x, y >.

Definition at line 148 of file Resource.cpp.

◆ handle_patch()

HTTP::Response HTTP::Resource::handle_patch ( Request  message,
std::smatch  match = std::smatch() 
)
virtual

Handler that is called by the server on HTTP requests on a PATCH method. This function should be overridden by the implementation of the resource. Per default, it resorts to the internal fallback function.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Reimplemented in SOIL::Parameter< T, x, y >.

Definition at line 130 of file Resource.cpp.

◆ handle_post()

HTTP::Response HTTP::Resource::handle_post ( Request  message,
std::smatch  match = std::smatch() 
)
virtual

Handler that is called by the server on HTTP requests on a POST method. This function should be overridden by the implementation of the resource. Per default, it resorts to the internal fallback function.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Reimplemented in SOIL::Function.

Definition at line 125 of file Resource.cpp.

◆ handle_put()

HTTP::Response HTTP::Resource::handle_put ( Request  message,
std::smatch  match = std::smatch() 
)
virtual

Handler that is called by the server on HTTP requests on a PUT method. This function should be overridden by the implementation of the resource. Per default, it resorts to the internal fallback function.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Outgoing HTTP response to be processed by cpprestdsk

Reimplemented in SOIL::Object.

Definition at line 142 of file Resource.cpp.

◆ request_info()

web::json::value HTTP::Resource::request_info ( Request  message,
std::smatch  match = std::smatch() 
)
static

Extracts all sort of information from the request and returns it in JSON format. This is useful for fallback methods.

Parameters
[in]messageIncoming HTTP request as preprocessed by cpprestsdk
[in]matchMatch result of the request path that led to this resource
Returns
Request information in JSON format

Definition at line 3 of file Resource.cpp.

Member Data Documentation

◆ allowed_methods

std::vector<web::http::method> HTTP::Resource::allowed_methods
protected

List of allowed HTTP methods for this resource implemented as std::string Defaults to all methods (GET, DELETE, PATCH, POST, OPTIONS, HEAD, PUT)

Definition at line 42 of file Resource.h.

◆ allowed_origins

std::string HTTP::Resource::allowed_origins
protected

Origins that are allowed, relavant when implementing web clients and running into CORS issues. Defaults to * .

Postcondition
If you want to be serious about security, you should limit the origins here.

Definition at line 60 of file Resource.h.

◆ content_type

std::string HTTP::Resource::content_type
protected

Content type that is delivered by this resource. Defaults to application/json

Definition at line 50 of file Resource.h.


The documentation for this class was generated from the following files: