WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
framework::HTTPRequest Class Reference

Parsing HTTP request. More...

#include <HTTPRequest.h>

Public Member Functions

 HTTPRequest (SessionsManager &session, const web::BaseTCPServer &serverReference, interfaces::IStaticFile &staticResources, interfaces::IDynamicFile &dynamicResources, sqlite::SQLiteManager &database, sockaddr clientAddr, streams::IOSocketStream &stream)
 
 HTTPRequest (HTTPRequest &&) noexcept=default
 
 HTTPRequest (const HTTPRequest &)=default
 
HTTPRequestoperator= (HTTPRequest &&) noexcept=default
 
HTTPRequestoperator= (const HTTPRequest &)=default
 
const std::string & getRawParameters () const
 Parameters string from HTTP.
 
const std::string & getMethod () const
 HTTP request method.
 
const std::unordered_map< std::string, std::string > & getKeyValueParameters () const
 GET parameters.
 
std::string getHTTPVersion () const
 HTTP version.
 
const web::HeadersMap & getHeaders () const
 All HTTP headers.
 
const std::string & getBody () const
 HTTP request body.
 
void setAttribute (const std::string &name, const std::string &value)
 Session wrapper.
 
std::string getAttribute (const std::string &name)
 Session wrapper.
 
void deleteSession ()
 Session wrapper.
 
void deleteAttribute (const std::string &name)
 Session wrapper.
 
web::HeadersMap getCookies () const
 Client's cookies.
 
void sendAssetFile (const std::string &filePath, HTTPResponse &response, const std::unordered_map< std::string, std::string > &variables={}, bool isBinary=true, const std::string &fileName="")
 ResourceExecutor wrapper.
 
void sendStaticFile (const std::string &filePath, HTTPResponse &response, bool isBinary=true, const std::string &fileName="")
 
void sendDynamicFile (const std::string &filePath, HTTPResponse &response, const std::unordered_map< std::string, std::string > &variables, bool isBinary=false, const std::string &fileName="")
 
void streamFile (std::string_view filePath, HTTPResponse &response, std::string_view fileName, size_t chunkSize=defaultChunkSize)
 
void registerDynamicFunction (const std::string &functionName, std::function< std::string(const std::vector< std::string > &)> &&function)
 Add new function in .wfdp interpreter.
 
void unregisterDynamicFunction (const std::string &functionName)
 Remove function from .wfdp interpreter.
 
bool isDynamicFunctionRegistered (const std::string &functionName)
 Check if function is registered.
 
const json::JSONParser & getJSON () const
 Getter for JSONParser.
 
const std::vector< std::string > & getChunks () const
 Get chunks.
 
const web::HTTPParser & getParser () const
 HTTP parser getter.
 
std::string getClientIpV4 () const
 Get client's address.
 
std::string getServerIpV4 () const
 Get server's address.
 
uint16_t getClientPort () const
 Get client's port.
 
uint16_t getServerPort () const
 Get server's port.
 
template<utility::concepts::RouteParameterType T>
const T & getRouteParameter (const std::string &routeParameterName)
 Getter for route parameters.
 
template<std::derived_from< sqlite::SQLiteDatabaseModel > T, typename... Args>
std::shared_ptr< T > createModel (Args &&... args)
 First call creates model with arguments other calls returns created model.
 
template<std::derived_from< sqlite::SQLiteDatabaseModel > T>
std::shared_ptr< T > getModel () const
 First call creates model without arguments other calls returns created model.
 
template<std::derived_from< utility::ChunkGenerator > T, typename... Args>
void sendChunks (HTTPResponse &response, Args &&... args)
 Send runtime generated content.
 
template<std::derived_from< utility::ChunkGenerator > T, typename... Args>
void sendFileChunks (HTTPResponse &response, std::string_view fileName, Args &&... args)
 Send file.
 
template<>
WEB_FRAMEWORK_API const string & getRouteParameter (const string &routeParameterName)
 
template<>
WEB_FRAMEWORK_API const int64_t & getRouteParameter (const string &routeParameterName)
 
template<>
WEB_FRAMEWORK_API const double & getRouteParameter (const string &routeParameterName)
 

Static Public Member Functions

static web::HTTPParser sendRequestToAnotherServer (std::string_view ip, std::string_view port, std::string_view request, DWORD timeout=30 '000, bool useHTTPS=false)
 

Friends

class ExecutorsManager
 
streams::IOSocketStream & operator>> (streams::IOSocketStream &stream, HTTPRequest &request)
 Reading HTTP request from network.
 
std::ostream & operator<< (std::ostream &stream, const HTTPRequest &request)
 Logging operator.
 

Detailed Description

Parsing HTTP request.

Accessing to sessions

Overriding input stream operator for simplify HTTP request initializing

Definition at line 24 of file HTTPRequest.h.

Constructor & Destructor Documentation

◆ HTTPRequest()

framework::HTTPRequest::HTTPRequest ( SessionsManager & session,
const web::BaseTCPServer & serverReference,
interfaces::IStaticFile & staticResources,
interfaces::IDynamicFile & dynamicResources,
sqlite::SQLiteManager & database,
sockaddr clientAddr,
streams::IOSocketStream & stream )

Definition at line 40 of file HTTPRequest.cpp.

Member Function Documentation

◆ createModel()

template<std::derived_from< sqlite::SQLiteDatabaseModel > T, typename... Args>
std::shared_ptr< T > framework::HTTPRequest::createModel ( Args &&... args)

First call creates model with arguments other calls returns created model.

Template Parameters
...Args
T
Parameters
...args
Returns

Definition at line 284 of file HTTPRequest.h.

◆ deleteAttribute()

void framework::HTTPRequest::deleteAttribute ( const std::string & name)

Session wrapper.

Parameters
name

Definition at line 97 of file HTTPRequest.cpp.

◆ deleteSession()

void framework::HTTPRequest::deleteSession ( )

Session wrapper.

Definition at line 92 of file HTTPRequest.cpp.

◆ getAttribute()

string framework::HTTPRequest::getAttribute ( const std::string & name)

Session wrapper.

Parameters
nameattribute name
Returns
attribute value
Exceptions
std::out_of_range

Definition at line 87 of file HTTPRequest.cpp.

◆ getBody()

const string & framework::HTTPRequest::getBody ( ) const

HTTP request body.

Returns
HTTP request body

Definition at line 77 of file HTTPRequest.cpp.

◆ getChunks()

const vector< string > & framework::HTTPRequest::getChunks ( ) const

Get chunks.

Returns

Definition at line 247 of file HTTPRequest.cpp.

◆ getClientIpV4()

string framework::HTTPRequest::getClientIpV4 ( ) const

Get client's address.

Returns
client's address

Definition at line 257 of file HTTPRequest.cpp.

◆ getClientPort()

uint16_t framework::HTTPRequest::getClientPort ( ) const

Get client's port.

Returns
client's port

Definition at line 267 of file HTTPRequest.cpp.

◆ getCookies()

web::HeadersMap framework::HTTPRequest::getCookies ( ) const

Client's cookies.

Returns
HTTP cookies as map

Definition at line 102 of file HTTPRequest.cpp.

◆ getHeaders()

const web::HeadersMap & framework::HTTPRequest::getHeaders ( ) const

All HTTP headers.

Returns
HTTP headers as map

Definition at line 72 of file HTTPRequest.cpp.

◆ getHTTPVersion()

string framework::HTTPRequest::getHTTPVersion ( ) const

HTTP version.

Returns
HTTP version

Definition at line 67 of file HTTPRequest.cpp.

◆ getJSON()

const json::JSONParser & framework::HTTPRequest::getJSON ( ) const

Getter for JSONParser.

Returns
JSONParser

Definition at line 242 of file HTTPRequest.cpp.

◆ getKeyValueParameters()

const unordered_map< string, string > & framework::HTTPRequest::getKeyValueParameters ( ) const

GET parameters.

Returns
GET parameters as map

Definition at line 62 of file HTTPRequest.cpp.

◆ getMethod()

const string & framework::HTTPRequest::getMethod ( ) const

HTTP request method.

Returns
HTTP method

Definition at line 57 of file HTTPRequest.cpp.

◆ getModel()

template<std::derived_from< sqlite::SQLiteDatabaseModel > T>
std::shared_ptr< T > framework::HTTPRequest::getModel ( ) const

First call creates model without arguments other calls returns created model.

Template Parameters
T
Returns

Definition at line 290 of file HTTPRequest.h.

◆ getParser()

const web::HTTPParser & framework::HTTPRequest::getParser ( ) const

HTTP parser getter.

Returns

Definition at line 252 of file HTTPRequest.cpp.

◆ getRawParameters()

const string & framework::HTTPRequest::getRawParameters ( ) const

Parameters string from HTTP.

Returns
HTTP parameters

Definition at line 52 of file HTTPRequest.cpp.

◆ getRouteParameter() [1/4]

template<utility::concepts::RouteParameterType T>
const T & framework::HTTPRequest::getRouteParameter ( const std::string & routeParameterName)

Getter for route parameters.

Template Parameters
TRouteParameterType type
Parameters
routeParameterNameName of route parameter
Returns
route parameter
Exceptions
std::out_of_rangeCan't find route parameter with this routeParameterName

◆ getRouteParameter() [2/4]

template<>
WEB_FRAMEWORK_API const string & framework::HTTPRequest::getRouteParameter ( const string & routeParameterName)

Definition at line 278 of file HTTPRequest.cpp.

◆ getRouteParameter() [3/4]

template<>
WEB_FRAMEWORK_API const int64_t & framework::HTTPRequest::getRouteParameter ( const string & routeParameterName)

Definition at line 284 of file HTTPRequest.cpp.

◆ getRouteParameter() [4/4]

template<>
WEB_FRAMEWORK_API const double & framework::HTTPRequest::getRouteParameter ( const string & routeParameterName)

Definition at line 290 of file HTTPRequest.cpp.

◆ getServerIpV4()

string framework::HTTPRequest::getServerIpV4 ( ) const

Get server's address.

Returns
server's address

Definition at line 262 of file HTTPRequest.cpp.

◆ getServerPort()

uint16_t framework::HTTPRequest::getServerPort ( ) const

Get server's port.

Returns
server's port

Definition at line 272 of file HTTPRequest.cpp.

◆ isDynamicFunctionRegistered()

bool framework::HTTPRequest::isDynamicFunctionRegistered ( const std::string & functionName)

Check if function is registered.

Parameters
functionNameName of function
Returns
true if function is registered, false otherwise

Definition at line 237 of file HTTPRequest.cpp.

◆ registerDynamicFunction()

void framework::HTTPRequest::registerDynamicFunction ( const std::string & functionName,
std::function< std::string(const std::vector< std::string > &)> && function )

Add new function in .wfdp interpreter.

Parameters
functionNameName of new function
functionFunction implementation

Definition at line 227 of file HTTPRequest.cpp.

◆ sendAssetFile()

void framework::HTTPRequest::sendAssetFile ( const std::string & filePath,
HTTPResponse & response,
const std::unordered_map< std::string, std::string > & variables = {},
bool isBinary = true,
const std::string & fileName = "" )

ResourceExecutor wrapper.

Parameters
filePathpath to asset file from assets folder
fileNameOptional parameter for specifying name of file in Content-Disposition HTTP header, ASCII name required
Exceptions
framework::exceptions::DynamicPagesSyntaxException
std::exception

Definition at line 135 of file HTTPRequest.cpp.

◆ sendChunks()

template<std::derived_from< utility::ChunkGenerator > T, typename... Args>
void framework::HTTPRequest::sendChunks ( HTTPResponse & response,
Args &&... args )

Send runtime generated content.

Template Parameters
...Args
T
Parameters
response
...args

Definition at line 313 of file HTTPRequest.h.

◆ sendDynamicFile()

void framework::HTTPRequest::sendDynamicFile ( const std::string & filePath,
HTTPResponse & response,
const std::unordered_map< std::string, std::string > & variables,
bool isBinary = false,
const std::string & fileName = "" )

Send dynamic file(.wfdp)

Parameters
filePathPath to asset file from assets folder
fileNameOptional parameter for specifying name of file in Content-Disposition HTTP header, ASCII name required
Exceptions
framework::exceptions::DynamicPagesSyntaxException
std::exception

Definition at line 147 of file HTTPRequest.cpp.

◆ sendFileChunks()

template<std::derived_from< utility::ChunkGenerator > T, typename... Args>
void framework::HTTPRequest::sendFileChunks ( HTTPResponse & response,
std::string_view fileName,
Args &&... args )

Send file.

Template Parameters
...Args
T
Parameters
response
fileName
...args

Definition at line 319 of file HTTPRequest.h.

◆ sendRequestToAnotherServer()

web::HTTPParser framework::HTTPRequest::sendRequestToAnotherServer ( std::string_view ip,
std::string_view port,
std::string_view request,
DWORD timeout = 30'000,
bool useHTTPS = false )
static

Definition at line 24 of file HTTPRequest.cpp.

◆ sendStaticFile()

void framework::HTTPRequest::sendStaticFile ( const std::string & filePath,
HTTPResponse & response,
bool isBinary = true,
const std::string & fileName = "" )

Send non dynamic file

Parameters
filePathPath to asset file from assets folder
fileNameOptional parameter for specifying name of file in Content-Disposition HTTP header, ASCII name required
Exceptions
std::exception

Definition at line 142 of file HTTPRequest.cpp.

◆ setAttribute()

void framework::HTTPRequest::setAttribute ( const std::string & name,
const std::string & value )

Session wrapper.

Parameters
nameattribute name
valueattribute value

Definition at line 82 of file HTTPRequest.cpp.

◆ streamFile()

void framework::HTTPRequest::streamFile ( std::string_view filePath,
HTTPResponse & response,
std::string_view fileName,
size_t chunkSize = defaultChunkSize )

Send large files

Parameters
filePathPath to asset file from assets folder
fileNameName of file in Content-Disposition HTTP header, ASCII name required
chunkSizeDesired size of read data before sending

Definition at line 152 of file HTTPRequest.cpp.

◆ unregisterDynamicFunction()

void framework::HTTPRequest::unregisterDynamicFunction ( const std::string & functionName)

Remove function from .wfdp interpreter.

Parameters
functionNameName of function

Definition at line 232 of file HTTPRequest.cpp.

Friends And Related Symbol Documentation

◆ ExecutorsManager

friend class ExecutorsManager
friend

Definition at line 278 of file HTTPRequest.h.

◆ operator<<

std::ostream & operator<< ( std::ostream & stream,
const HTTPRequest & request )
friend

Logging operator.

Parameters
streamany output source
requestclass instance
Returns
self for builder pattern

◆ operator>>

streams::IOSocketStream & operator>> ( streams::IOSocketStream & stream,
HTTPRequest & request )
friend

Reading HTTP request from network.

Parameters
streamspecial class for taking data from network
requestclass instance
Returns
self for builder pattern
Exceptions
web::WebException

Definition at line 295 of file HTTPRequest.cpp.


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