forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultipart_parser.h
More file actions
368 lines (344 loc) · 9.24 KB
/
Copy pathmultipart_parser.h
File metadata and controls
368 lines (344 loc) · 9.24 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCMS_IMPL_MULTIPART_PARSER_H
#define CPPCMS_IMPL_MULTIPART_PARSER_H
#include <cppcms/defs.h>
#include <booster/noncopyable.h>
#include <booster/shared_ptr.h>
#include <cppcms/http_file.h>
#include <cppcms/http_content_type.h>
#include <vector>
#include "http_protocol.h"
namespace cppcms {
namespace impl {
class multipart_parser : public booster::noncopyable {
public:
typedef booster::shared_ptr<http::file> file_ptr;
typedef std::vector<file_ptr> files_type;
files_type get_files()
{
files_type result;
result.swap(files_);
return result;
}
bool set_content_type(std::string const &ct)
{
http::content_type t(ct);
return set_content_type(t);
}
bool set_content_type(http::content_type const &content_type)
{
std::string bkey = content_type.parameter_by_key("boundary");
if(bkey.empty())
return false;
boundary_ = "\r\n--" + bkey;
crlfcrlf_ = "\r\n\r\n";
position_ = 2; // First CRLF does not appear if first state
state_ = expecting_first_boundary;
file_.reset(new http::file());
file_->set_temporary_directory(temp_dir_);
if(memory_limit_ != -1) {
file_->set_memory_limit(memory_limit_);
}
return true;
}
multipart_parser(std::string const &temp_dir = std::string(),size_t memory_limit = -1) :
state_ ( expecting_first_boundary ),
position_(0),
temp_dir_(temp_dir),
memory_limit_(memory_limit),
file_is_ready_(false)
{
}
~multipart_parser()
{
}
typedef enum {
parsing_error,
meta_ready,
content_partial,
content_ready,
continue_input,
eof,
no_room_left
} parsing_result_type;
static bool is_ok(parsing_result_type r) {
switch(r) {
case meta_ready:
case content_partial:
case content_ready:
case continue_input:
return true;
default:
return false;
}
}
bool has_file() { return file_is_ready_; }
http::file &get_file()
{
if(!file_is_ready_)
throw booster::logic_error("Invalid state for get_file");
return *file_;
}
http::file &last_file()
{
if(files_.empty())
throw booster::logic_error("No file was uploaded");
return *files_.back();
}
parsing_result_type consume(char const *&buffer,char const *buffer_end)
{
for(;buffer!=buffer_end;buffer++) {
#ifdef DEBUG_MULTIPART_PARSER
std::cerr << "[";
if(*buffer < 32)
std::cerr << int(*buffer);
else
std::cerr << *buffer ;
std::cerr <<"]" << state_str_[state_] << ", pos="<<position_ << std::endl;
#endif
switch(state_) {
case expecting_first_boundary:
if(*buffer != boundary_[position_])
return parsing_error;
position_++;
if(position_ == boundary_.size()) {
state_ = expecting_one_crlf_or_eof;
position_ = 0;
}
break;
case expecting_one_crlf_or_eof:
if(*buffer=='\r')
state_=expecting_lf;
else if(*buffer=='-')
state_=expecting_minus;
else
return parsing_error;
break;
case expecting_minus:
if(*buffer!='-')
return parsing_error;
state_=expecting_eof_cr;
break;
case expecting_eof_cr:
if(*buffer!='\r')
return parsing_error;
state_=expecting_eof_lf;
break;
case expecting_eof_lf:
if(*buffer!='\n')
return parsing_error;
if(buffer + 1 == buffer_end) {
buffer++;
return eof;
}
else
return parsing_error;
case expecting_lf:
if(*buffer!='\n')
return parsing_error;
state_=expecting_crlfcrlf;
break;
case expecting_crlfcrlf:
header_+=*buffer;
if(*buffer == crlfcrlf_[position_])
position_++;
else
position_=0;
if(position_ == crlfcrlf_.size()) {
if(!process_header(header_))
return parsing_error;
header_.clear();
position_ = 0;
state_ = expecting_separator_boundary;
buffer++;
file_is_ready_=true;
return meta_ready;
}
break;
case expecting_separator_boundary:
{
std::streambuf *out=file_->write_data().rdbuf();
char const *this_boundary = boundary_.c_str();
size_t boundary_size = boundary_.size();
while(buffer != buffer_end) {
char c=*buffer;
if(c == this_boundary[position_])
position_++;
else if(position_ > 0) {
std::streamsize expected = position_;
std::streamsize s=out->sputn(this_boundary,position_);
position_ = 0;
if(c == boundary_[0])
position_=1;
if(s!=expected)
return no_room_left;
}
if(position_ == 0) {
if(out->sputc(c)==EOF)
return no_room_left;
}
else if(position_ == boundary_size) {
state_ = expecting_one_crlf_or_eof;
position_ = 0;
file_->data().seekg(0);
files_.push_back(file_);
file_.reset(new http::file());
file_->set_temporary_directory(temp_dir_);
if(memory_limit_ != -1) {
file_->set_memory_limit(memory_limit_);
}
buffer++;
file_is_ready_=false;
return content_ready;
}
buffer++;
} // end while
return content_partial;
}
break;
}
}
if(file_is_ready_)
return content_partial;
else
return continue_input;
}
private:
bool process_header(std::string const &hdr)
{
size_t pos = 0;
while(pos < hdr.size()) {
size_t next = hdr.find("\r\n",pos);
if(next==std::string::npos) {
return false;
}
if(next == pos) {
return true;
}
std::string one_header=hdr.substr(pos,next-pos);
pos=next+2;
std::string::iterator p=one_header.begin(),e=one_header.end();
p=http::protocol::skip_ws(p,e);
std::string::iterator start=p;
std::string::iterator end=http::protocol::tocken(p,e);
std::string header_name(start,end);
p=http::protocol::skip_ws(end,e);
if(p==e || *p!=':')
return false;
p=http::protocol::skip_ws(p+1,e);
if(http::protocol::compare(header_name,"Content-Disposition")==0) {
start = p;
end=http::protocol::tocken(p,e);
if(http::protocol::compare(std::string(start,end),"form-data")!=0)
return false;
p=end;
if(!parse_content_disposition(p,e))
return false;
}
else if(http::protocol::compare(header_name,"Content-Type")==0) {
http::content_type ct(std::string(p,e));
file_->mime(ct.media_type());
}
}
return false;
}
bool parse_content_disposition(std::string::iterator p,std::string::iterator e)
{
p=http::protocol::skip_ws(p,e);
while(p!=e) {
std::string name,value;
if(!parse_pair(p,e,name,value))
return false;
for(unsigned i=0;i<name.size();i++)
name[i]=http::protocol::ascii_to_lower(name[i]);
if(name=="filename")
file_->filename(value);
else if(name=="name")
file_->name(value);
p=http::protocol::skip_ws(p,e);
}
return true;
}
bool parse_pair(std::string::iterator &p,std::string::iterator e,std::string &name,std::string &value)
{
if(p==e)
return false;
if(*p!=';')
return false;
p=http::protocol::skip_ws(p+1,e);
if(p==e)
return false;
std::string::iterator start = p;
std::string::iterator end = http::protocol::tocken(p,e);
if(start==end)
return false;
if(end==e)
return false;
if(*end!='=')
return false;
name.assign(start,end);
p=http::protocol::skip_ws(end+1,e);
if(p==e)
return false;
if(*p=='"') {
std::string::iterator tmp=p;
value=http::protocol::unquote(tmp,e);
if(p==tmp)
return false;
p=tmp;
return true;
}
else {
std::string::iterator tmp=http::protocol::tocken(p,e);
if(tmp==p)
return false;
value.assign(p,tmp);
p=tmp;
return true;
}
}
typedef enum {
expecting_first_boundary,
expecting_one_crlf_or_eof,
expecting_minus,
expecting_eof_cr,
expecting_eof_lf,
expecting_lf,
expecting_crlfcrlf,
expecting_separator_boundary
} states_type;
#ifdef DEBUG_MULTIPART_PARSER
static char const * const state_str_[8];
#endif
states_type state_;
file_ptr file_;
files_type files_;
size_t position_;
std::string header_;
std::string boundary_;
std::string crlfcrlf_;
std::string temp_dir_;
int memory_limit_;
bool file_is_ready_;
};
#ifdef DEBUG_MULTIPART_PARSER
char const * const multipart_parser::state_str_[8] = {
"expecting_first_boundary",
"expecting_one_crlf_or_eof",
"expecting_minus",
"expecting_eof_cr",
"expecting_eof_lf",
"expecting_lf",
"expecting_crlfcrlf",
"expecting_separator_boundary"
};
#endif
}
}
#endif