NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
http_server_response结构体 参考

HTTP服务器响应结构 更多...

#include <http_server_message.hpp>

类 http_server_response 继承关系图:
[图例]

Public 成员函数

 http_server_response ()
 默认构造函数
NEFORCE_NODISCARD string_view header (const string &name) const noexcept
 获取响应头值
void set_header (const string &name, string value)
 设置响应头
NEFORCE_NODISCARD bool has_header (const string &name) const noexcept
 检查响应头是否存在
void set_content_type (http_content value)
 设置Content-Type
void set_content_type (string value)
 设置Content-Type
NEFORCE_NODISCARD string to_string () const
 序列化为HTTP响应字符串
Public 成员函数 继承自 istringify< http_server_response >
NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string () const
 转换为字符串

Public 属性

string version {"HTTP/1.1"}
 HTTP版本
http_status status = http_status::S4_NOT_FOUNT
 HTTP状态码
string status_message {}
 状态消息
unordered_map< string, stringheaders
 响应头
vector< http_cookiecookies
 设置的Cookie
string body {}
 响应正文
string redirect_url {}
 重定向URL
string forward_path {}
 转发路径

详细描述

HTTP服务器响应结构

表示一个HTTP响应,包含状态行、头部和正文。 支持重定向、Cookie设置、内容类型设置等。

使用示例:

// 设置状态
response.status_message = "OK";
// 设置内容
response.body = "<html><body>Hello World</body></html>";
response.set_content_type(http_content::HTML());
// 设置Cookie
http_cookie cookie;
cookie.name = "session";
cookie.value = "abc123";
cookie.path = "/";
cookie.http_only = true;
response.cookies.push_back(cookie);
// 重定向
// response.redirect_url = "https://example.com/new-page";
// 序列化响应
string response_str = response.to_string();
NEFORCE_CONSTEXPR20 void push_back(const T &value)
在末尾拷贝插入元素
@ S2_OK
200 OK 请求成功
http_status status
HTTP状态码
NEFORCE_NODISCARD string to_string() const
序列化为HTTP响应字符串
void set_content_type(http_content value)
设置Content-Type
vector< http_cookie > cookies
设置的Cookie
http_server_response()
默认构造函数

在文件 http_server_message.hpp267 行定义.

构造及析构函数说明

◆ http_server_response()

http_server_response::http_server_response ( )
inline

默认构造函数

设置默认Content-Type为text/plain,Connection为close。

在文件 http_server_message.hpp282 行定义.

引用了 headers, http_content::PLAIN_TEXT() , 以及 http_content::to_string().

成员函数说明

◆ has_header()

NEFORCE_NODISCARD bool http_server_response::has_header ( const string & name) const
inlinenoexcept

检查响应头是否存在

参数
name头名称
返回
存在返回true

在文件 http_server_message.hpp309 行定义.

引用了 headers , 以及 name().

◆ header()

NEFORCE_NODISCARD string_view http_server_response::header ( const string & name) const
inlinenoexcept

获取响应头值

参数
name头名称
返回
头值,不存在返回空字符串

在文件 http_server_message.hpp292 行定义.

引用了 headers , 以及 name().

◆ set_content_type() [1/2]

void http_server_response::set_content_type ( http_content value)
inline

设置Content-Type

参数
valueHTTP内容类型对象

在文件 http_server_message.hpp315 行定义.

引用了 headers , 以及 move().

◆ set_content_type() [2/2]

void http_server_response::set_content_type ( string value)
inline

设置Content-Type

参数
value内容类型字符串

在文件 http_server_message.hpp321 行定义.

引用了 headers , 以及 move().

◆ set_header()

void http_server_response::set_header ( const string & name,
string value )
inline

设置响应头

参数
name头名称
value头值

在文件 http_server_message.hpp302 行定义.

引用了 headers, move() , 以及 name().

◆ to_string()

NEFORCE_NODISCARD string http_server_response::to_string ( ) const

序列化为HTTP响应字符串

返回
HTTP响应字符串

支持正常响应和重定向响应。 自动添加Content-Length头。


该结构体的文档由以下文件生成: