STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | List of all members
stt::network::HttpServerFDHandler Class Reference

Operation class for parsing and responding to Http/https requests Only pass in the socket, then use this class for Http operations. More...

#include <sttnet_English.h>

Inheritance diagram for stt::network::HttpServerFDHandler:
stt::network::TcpFDHandler

Public Member Functions

void setFD (const int &fd, SSL *ssl=nullptr, const bool &flag1=false, const bool &flag2=true)
 Initialize the object, pass in socket and other parameters. More...
 
int solveRequest (TcpFDInf &TcpInf, HttpRequestInformation &HttpInf, const unsigned long &buffer_size, const int &times=1)
 Parse Http/Https request. More...
 
bool sendBack (const std::string &data, const std::string &header="", const std::string &code="200 OK", const std::string &header1="")
 Send Http/Https response. More...
 
bool sendBack (const char *data, const size_t &length, const char *header="\0", const char *code="200 OK\0", const char *header1="\0", const size_t &header_length=50)
 Send Http/Https response. More...
 
- Public Member Functions inherited from stt::network::TcpFDHandler
void setFD (const int &fd, SSL *ssl, const bool &flag1=false, const bool &flag2=false, const int &sec=-1)
 Initialize the object with a socket. More...
 
int getFD ()
 Get the socket of this object. More...
 
SSL * getSSL ()
 Get the encrypted SSL handle of this object. More...
 
void close (const bool &cle=true)
 Close the object. More...
 
void blockSet (const int &sec=-1)
 Set the socket in the object to blocking mode. More...
 
void unblockSet ()
 Set the socket in the object to non-blocking mode. More...
 
bool multiUseSet ()
 Set the socket in the object to SO_REUSEADDR mode. More...
 
bool isConnect ()
 Determine if the object has a socket bound. More...
 
int sendData (const std::string &data, const bool &block=true)
 Send string data to the connected socket. More...
 
int sendData (const char *data, const uint64_t &length, const bool &block=true)
 Send a specified length of binary data to the connected socket. More...
 
int recvDataByLength (std::string &data, const uint64_t &length, const int &sec=2)
 Blocking receive of specified length of data from a connected socket into a string. More...
 
int recvDataByLength (char *data, const uint64_t &length, const int &sec=2)
 Blocking receive of specified length of data from a connected socket into a char* container. More...
 
int recvData (std::string &data, const uint64_t &length)
 Receive data once from a connected socket into a string container. More...
 
int recvData (char *data, const uint64_t &length)
 Receive data once from a connected socket into a char* container. More...
 

Additional Inherited Members

- Public Attributes inherited from stt::network::TcpFDHandler
bool flag3 = false
 if the connection lost while using sendData function with block=true,this flag will be set to true More...
 
- Protected Attributes inherited from stt::network::TcpFDHandler
int fd = -1
 
bool flag1 = false
 
bool flag2 = false
 
SSL * ssl = nullptr
 
int sec =-1
 

Detailed Description

Operation class for parsing and responding to Http/https requests Only pass in the socket, then use this class for Http operations.

Member Function Documentation

bool stt::network::HttpServerFDHandler::sendBack ( const std::string &  data,
const std::string &  header = "",
const std::string &  code = "200 OK",
const std::string &  header1 = "" 
)

Send Http/Https response.

Parameters
dataString container with response body data
codeHttp response status code and status description (default is "200 OK")
headerHttp request header; if not generated by createHeader, remember to add
at the end.
header1Additional item for the HTTP request header; if needed, must fill in a valid item; no need to add
at the end (cannot use createHeader). (For example, you can fill in the keepalive field by default)
Returns
true: Response sent successfully, false: Response sending failed
bool stt::network::HttpServerFDHandler::sendBack ( const char *  data,
const size_t &  length,
const char *  header = "\0",
const char *  code = "200 OK\0",
const char *  header1 = "\0",
const size_t &  header_length = 50 
)

Send Http/Https response.

Parameters
dataChar * container with response body data
lengthData length in the char* container
codeHttp response status code and status description (default is "200 OK")
headerHttp request header; if not generated by createHeader, remember to add
at the end.
header1Additional item for the HTTP request header; if needed, must fill in a valid item; no need to add
at the end (cannot use createHeader). (For example, you can fill in the keepalive field by default)
header_lengthMaximum length of the response header added up (default is 50)
Warning
The reserved space must be accurate, otherwise the delivery may fail
All data pointed to by char must be ensured that 0 ends with 0 is not counted in the length, otherwise there is a risk of crashing
Returns
true: The response was successfully sent false: The response failed to be sent
void stt::network::HttpServerFDHandler::setFD ( const int &  fd,
SSL *  ssl = nullptr,
const bool &  flag1 = false,
const bool &  flag2 = true 
)
inline

Initialize the object, pass in socket and other parameters.

Parameters
fdSocket
sslTLS encrypted SSL handle (default is nullptr)
flag1true: Enable non-blocking mode, false: Enable blocking mode (default is false, i.e., blocking mode)
flag2true: Enable SO_REUSEADDR mode, false: Do not enable SO_REUSEADDR mode (default is true, i.e., enable mode)
int stt::network::HttpServerFDHandler::solveRequest ( TcpFDInf TcpInf,
HttpRequestInformation HttpInf,
const unsigned long &  buffer_size,
const int &  times = 1 
)

Parse Http/Https request.

Parameters
TcpInfstores the state information of the underlying TCP processing socket
HttpInfstores the information of Http
buffer_sizeThe size of the server-defined parsing buffer (in bytes)
timessometims will be used to record solve times
Returns
1: Parsing completed 0: Parsing still needs to be continued -1: Parsing failed
Note
TcpInf.status

0 Initial status 1 Receive request header 2 Receive request body (chunk mode) 3 Receive request body (non-chunk mode)


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