forked from skyfireitdiy/sflib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsf_http_multipart.hpp
134 lines (124 loc) · 3.85 KB
/
sf_http_multipart.hpp
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
/**
* @version 1.0.0
* @author skyfire
* @mail [email protected]
* @see http://github.com/skyfireitdiy/sflib
* @file sf_http_multipart.hpp
* sflib第一版本发布
* 版本号1.0.0
* 发布日期:2018-10-22
*/
#include "sf_http_multipart.h"
#include "sf_http_request.hpp"
#include "sf_type.hpp"
#include "sf_http_utils.hpp"
#include "sf_utils.hpp"
namespace skyfire
{
inline sf_http_multipart::sf_http_multipart(const std::string &boundary_str, const std::string& tmp_path) :
boundary_str__(boundary_str), filename__(sf_path_join(tmp_path,sf_random::get_instance()->get_uuid_str()))
{
}
inline sf_http_header sf_http_multipart::get_header() const
{
return header__;
}
inline bool sf_http_multipart::is_end() const
{
return end__;
}
inline bool sf_http_multipart::is_finished() const
{
return finish__;
}
inline std::string sf_http_multipart::get_filename() const
{
return filename__;
}
inline bool sf_http_multipart::append_data(const byte_array &data, byte_array &ret)
{
std::string new_boundary_str = "----" + boundary_str__;
if(first_block)
{
auto tmp_data = to_string(data);
if (tmp_data.find(new_boundary_str) != 0)
{
ret = data;
return false;
}
std::string request_line;
std::vector<std::string> header_lines;
byte_array body;
if (!sf_http_request::split_request(data, request_line, header_lines, body))
{
ret = data;
return false;
}
if (!sf_http_request::parse_header(header_lines, header__))
{
ret = data;
return false;
}
first_block = false;
fp__ = std::shared_ptr<std::ofstream>(new std::ofstream(filename__,std::ios::binary | std::ios::out),[](std::ofstream *p){
if(p->good()){
p->close();
}
delete p;
});
// NOTE 暂时忽略掉打开失败的情况
auto body_str = to_string(body);
auto finish_pos = body_str.find(new_boundary_str);
if(finish_pos == std::string::npos){
fp__->write(body.data(),body.size());
ret = byte_array();
return true;
}
else
{
fp__->write(body.data(),finish_pos-2);// 有一个\r\n
finish__ = true;
fp__->close();
if(finish_pos == body_str.find(new_boundary_str+"--"))
{
end__ = true;
ret = byte_array();
return true;
}
else
{
ret = {body.begin()+finish_pos,body.end()};
return true;
}
}
}
else
{
auto body = data;
auto body_str = to_string(body);
auto finish_pos = body_str.find(new_boundary_str);
if(finish_pos == std::string::npos){
fp__->write(body.data(),body.size());
ret = byte_array();
return true;
}
else
{
fp__->write(body.data(),finish_pos-2);// 有一个\r\n
finish__ = true;
fp__->close();
if(finish_pos == body_str.find(new_boundary_str+"--"))
{
end__ = true;
ret = byte_array();
return true;
}
else
{
ret = {body.begin()+finish_pos,body.end()};
return true;
}
}
}
}
}