|
STTNet 0.7.0
|
解析,响应Http/https请求的操作类 仅传入套接字,然后使用这个类进行Http的操作 More...
#include <sttnet.h>
Public Member Functions | |
| void | setFD (const int &fd, SSL *ssl=nullptr, const bool &flag1=false, const bool &flag2=true) |
| 初始化对象,传入套接字等参数 | |
| int | solveRequest (TcpFDInf &TcpInf, HttpRequestInformation &HttpInf, const unsigned long &buffer_size, const int ×=1, const unsigned long &max_header_size=64UL *1024UL) |
| 解析Http/Https请求 | |
| bool | sendBack (const std::string &data, const std::string &header="", const std::string &code="200 OK", const std::string &header1="") |
| 发送Http/Https响应 | |
| bool | sendText (const std::string &data, const std::string &code="200 OK", const std::string &contentType="text/plain; charset=utf-8", const std::string &extraHeaders="") |
| 发送文本响应,自动补充 Content-Type 和 Content-Length。 | |
| bool | sendJson (const Json::Value &data, const std::string &code="200 OK", const std::string &extraHeaders="") |
| 序列化 Json::Value 并发送 application/json UTF-8 响应。 | |
| bool | redirect (const std::string &location, const std::string &code="302 Found") |
| 发送 HTTP 重定向响应。 | |
| 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) |
| 发送Http/Https响应 | |
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) |
| 传入套接字初始化对象 | |
| void | setTransportFunctions (std::function< int(std::string)> sendFunction, std::function< void()> closeFunction={}) |
| 由服务端 Reactor 注入异步发送和关闭通道。 | |
| int | getFD () |
| 获取该对象的套接字 | |
| SSL * | getSSL () |
| 获取该对象的加密SSL句柄 | |
| void | close (const bool &cle=true) |
| 关闭对象 | |
| void | blockSet (const int &sec=-1) |
| 设置对象中的套接字为阻塞模式 | |
| void | unblockSet () |
| 设置对象中的套接字为非阻塞模式 | |
| bool | multiUseSet () |
| 设置对象中的套接字为SO_REUSEADDR模式 | |
| bool | isConnect () |
| 判断对象是否有套接字绑定 | |
| int | sendData (const std::string &data, const bool &block=true) |
| 向已连接的套接字发送字符串数据。 | |
| int | sendData (const char *data, const uint64_t &length, const bool &block=true) |
| 向已连接的套接字发送指定长度的二进制数据。 | |
| int | recvDataByLength (std::string &data, const uint64_t &length, const int &sec=2) |
| 从已连接的套接字中阻塞接收指定长度的数据到字符串 | |
| int | recvDataByLength (char *data, const uint64_t &length, const int &sec=2) |
| 从已连接的套接字中阻塞接收指定长度的数据到char*容器 | |
| int | recvData (std::string &data, const uint64_t &length) |
| 从已连接的套接字中接收一次数据到string字符串容器 | |
| int | recvData (char *data, const uint64_t &length) |
| 从已连接的套接字中接收一次数据到char*容器 | |
Additional Inherited Members | |
Public Attributes inherited from stt::network::TcpFDHandler | |
| bool | flag3 =false |
| 如果sendData的block=true,如果发送过程中连接断开,这个标志位会置为true | |
Protected Attributes inherited from stt::network::TcpFDHandler | |
| int | fd =-1 |
| bool | flag1 =false |
| bool | flag2 =false |
| SSL * | ssl =nullptr |
| int | sec =-1 |
| std::function< int(std::string)> | queuedSendFunction |
| std::function< void()> | queuedCloseFunction |
解析,响应Http/https请求的操作类 仅传入套接字,然后使用这个类进行Http的操作
| bool stt::network::HttpServerFDHandler::redirect | ( | const std::string & | location, |
| const std::string & | code = "302 Found" ) |
发送 HTTP 重定向响应。
| location | Location 目标;包含 CR/LF 时会拒绝,避免响应头注入。 |
| code | 默认 302 Found,也可使用 301 Moved Permanently。 |
| 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 ) |
发送Http/Https响应
| data | 装着响应体的数据的char *容器 |
| length | char*容器中的数据长度 |
| code | Http响应状态码和状态说明 (默认是 200 OK) |
| header | Http 响应头;如果不是用 createHeader 生成,末尾应包含 CRLF。 |
| header1 | HTTP 响应头附加项;末尾不需要加入 CRLF。 |
| header_length | 响应头部长度的预估值,仅用于减少内存扩容;不准确不会影响发送正确性(默认50)。 |
| bool stt::network::HttpServerFDHandler::sendBack | ( | const std::string & | data, |
| const std::string & | header = "", | ||
| const std::string & | code = "200 OK", | ||
| const std::string & | header1 = "" ) |
发送Http/Https响应
| data | 装着响应体的数据的string容器 |
| code | Http响应状态码和状态说明 (默认是 200 OK) |
| header | Http 响应头;如果不是用 createHeader 生成,末尾应包含 CRLF。 |
| header1 | HTTP 响应头附加项;末尾不需要加入 CRLF。 |
| bool stt::network::HttpServerFDHandler::sendJson | ( | const Json::Value & | data, |
| const std::string & | code = "200 OK", | ||
| const std::string & | extraHeaders = "" ) |
序列化 Json::Value 并发送 application/json UTF-8 响应。
| bool stt::network::HttpServerFDHandler::sendText | ( | const std::string & | data, |
| const std::string & | code = "200 OK", | ||
| const std::string & | contentType = "text/plain; charset=utf-8", | ||
| const std::string & | extraHeaders = "" ) |
发送文本响应,自动补充 Content-Type 和 Content-Length。
| data | 响应体。 |
| code | HTTP 状态,例如 200 OK。 |
| contentType | MIME 类型,默认 UTF-8 纯文本。 |
| extraHeaders | 额外响应头,可以是多行。 |
|
inline |
初始化对象,传入套接字等参数
| fd | 套接字 |
| ssl | TLS加密的SSL句柄(默认为nullptr) |
| flag1 | true:启用非阻塞模式 false:启用阻塞模式 (默认为false,即启用阻塞模式) |
| flag2 | true:启用SO_REUSEADDR模式 false:不启用SO_REUSEADDR模式 (默认为true,即启用SO_REUSEADDR模式) |
| int stt::network::HttpServerFDHandler::solveRequest | ( | TcpFDInf & | TcpInf, |
| HttpRequestInformation & | HttpInf, | ||
| const unsigned long & | buffer_size, | ||
| const int & | times = 1, | ||
| const unsigned long & | max_header_size = 64UL *1024UL ) |
解析Http/Https请求
| TcpInf | 存放底层tcp处理套接字的信息 |
| HttpInf | 存放Http协议的信息 |
| buffer_size | 服务器定义的解析缓冲区的大小(单位为字节) |
| times | 记录解析的次数,某些场景会用上 |
| max_header_size | HTTP 请求头最大字节数,默认 64 KiB,且不会超过 buffer_size。 |