forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsf_http_response.h
More file actions
186 lines (166 loc) · 5.08 KB
/
Copy pathsf_http_response.h
File metadata and controls
186 lines (166 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* @version 1.0.0
* @author skyfire
* @mail skyfireitdiy@hotmail.com
* @see http://github.com/skyfireitdiy/sflib
* @file sf_http_response.h
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
#pragma once
#include <string>
#include "sf_type.hpp"
#include "sf_http_utils.h"
#include "sf_http_header.h"
namespace skyfire
{
class sf_http_base_server;
/**
* @brief HTTP响应
*/
class sf_http_response
{
public:
/**
* @brief 响应类型
*/
enum class response_type
{
normal, // 普通类型
file, // 文件类型
multipart // multipart类型
};
/**
* @brief 响应文件信息(当响应类型为file或者响应类型为multipart,子类型为file时,使用改数据结构声明文件)
*/
struct response_file_info_t
{
std::string filename;
long long begin;
long long end;
};
/**
* @brief multipart类型信息
*/
struct multipart_info_t
{
/**
* @brief multipart子类型
*/
enum class multipart_info_type
{
form, // 表单
file // 文件
};
/**
* @brief 表单信息,当type为multipart_info_type::form时有效
*/
struct form_info_t
{
sf_http_header_t header; // 响应头
byte_array body; // 响应体
};
multipart_info_type type; // multipart子类型,当响应类型为multipart时有效
form_info_t form_info; // 表单信息,当type为multipart_info_type::form时有效
response_file_info_t file_info; // 响应文件信息,当type为multipart_info_type::file时有效
};
private:
int status__ = 200;
std::string http_version__ = "HTTP/1.1";
std::string status_desc__ = "OK";
sf_http_header header__;
byte_array body__;
response_type type__ = {response_type ::normal};
std::map<std::string,sf_http_cookie_t> cookies__;
response_file_info_t file_info__;
std::vector<multipart_info_t> multipart_info_vec__;
public:
/**
* 设置http状态码
* @param status
*/
void set_status(int status);
/**
* 设置http版本(当前只支持HTTP/1.1,默认也是此值)
* @param http_version
*/
void set_http_version(const std::string& http_version);
/**
* 设置状态码描述信息,一般无需特意设置,在设置status的时候会自动设置
* @param desc 描述信息
*/
void set_status_desc(const std::string& desc);
/**
* 设置http响应头
* @param header 响应头
*/
void set_header(const sf_http_header& header);
/**
* 设置http响应(会设置类型为normal)
* @param body 响应体
*/
void set_body(const byte_array & body);
/**
* 设置响应文件(会设置类型为file)
* @param file_info 文件信息
*/
void set_file(const response_file_info_t &file_info);
/**
* 设置分块响应(会设置类型为multipart)
* @param multipart_info_vec 分块信息
*/
void set_multipart(const std::vector<multipart_info_t>& multipart_info_vec);
/**
* 添加cookie
* @param cookie_data cookie信息
*/
void add_cookie(const sf_http_cookie_t &cookie_data);
/**
* 移除cookie
* @param key cookie名称
*/
void remove_cookie(const std::string& key);
/**
* 获取cookies
* @return cookies
*/
std::map<std::string,sf_http_cookie_t> get_cookies() const;
/**
* 获取响应类型
* @return 响应类型
*/
response_type get_type() const ;
/**
* 获取响应文件
* @return 响应文件信息
*/
response_file_info_t get_file() const;
/**
* 获取分块响应信息
* @return 分块响应信息
*/
std::vector<multipart_info_t> get_multipart() const;
/**
* 获取响应长度
* @return 响应长度
*/
unsigned long long get_length() const;
/**
* 获取响应头
* @return 响应头
*/
sf_http_header& get_header();
/**
* 转换头部为供发送的字节数组
* @return 字节数组
*/
byte_array to_header_package() const;
/**
* 转换为供发送的字节数组(在类型为normal时有用)
* @return 字节数组
*/
byte_array to_package() const;
friend sf_http_base_server;
};
}