FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
esp32_https_server/src/HTTPMultipartBodyParser.cpp at master · phrabos/esp32_https_server · GitHub
phrabos
esp32_https_server
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
esp32_https_server
/
src
/
HTTPMultipartBodyParser.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
288 lines (270 loc) · 7.44 KB
Breadcrumbs
esp32_https_server
/
src
/
HTTPMultipartBodyParser.cpp
Copy path
File metadata and controls
288 lines (270 loc) · 7.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
HTTPMultipartBodyParser.hpp
"
#
include
<
sstream
>
const
size_t
MAXLINESIZE
=
256
;
namespace
httpsserver
{
HTTPMultipartBodyParser::HTTPMultipartBodyParser
(HTTPRequest * req):
HTTPBodyParser
(req),
peekBuffer
(
NULL
),
peekBufferSize
(
0
),
boundary
(
"
"
),
lastBoundary
(
"
"
),
fieldName
(
"
"
),
fieldMimeType
(
"
"
),
fieldFilename
(
"
"
)
{
auto
contentType = _request->
getHeader
(
"
Content-Type
"
);
#
ifdef
DEBUG_MULTIPART_PARSER
Serial.
print
(
"
Content type:
"
);
Serial.
println
(contentType.
c_str
());
#
endif
auto
boundaryIndex = contentType.
find
(
"
boundary=
"
);
if
(boundaryIndex == std::string::npos) {
HTTPS_LOGE
(
"
Multipart: missing boundary=
"
);
discardBody
();
return
;
}
boundary = contentType.
substr
(boundaryIndex +
9
);
//
"boundary="
auto
commaIndex = boundary.
find
(
'
;
'
);
boundary =
"
--
"
+ boundary.
substr
(
0
, commaIndex);
if
(boundary.
size
() >
72
) {
HTTPS_LOGE
(
"
Multipart: boundary string too long
"
);
discardBody
();
}
lastBoundary = boundary +
"
--
"
;
}
HTTPMultipartBodyParser::~HTTPMultipartBodyParser
() {
if
(peekBuffer) {
free
(peekBuffer);
peekBuffer =
NULL
;
}
}
void
HTTPMultipartBodyParser::discardBody
() {
if
(peekBuffer) {
free
(peekBuffer);
}
peekBuffer =
NULL
;
peekBufferSize =
0
;
_request->
discardRequestBody
();
}
bool
HTTPMultipartBodyParser::endOfBody
() {
return
peekBufferSize ==
0
&& _request->
requestComplete
();
}
void
HTTPMultipartBodyParser::fillBuffer
(
size_t
maxLen) {
//
Fill the buffer with up to maxLen bytes (total length, including
//
what was already in the buffer), but stop reading ahead once
//
we have a CR in the buffer (because the upper layers will
//
stop consuming there anyway, to forestall overrunning
//
a boundary)
char
*bufPtr;
if
(peekBuffer ==
NULL
) {
//
Nothing in the buffer. Allocate one of the wanted size
peekBuffer = (
char
*)
malloc
(maxLen);
if
(peekBuffer ==
NULL
) {
HTTPS_LOGE
(
"
Multipart: out of memory
"
);
discardBody
();
return
;
}
bufPtr = peekBuffer;
peekBufferSize =
0
;
}
else
if
(peekBufferSize < maxLen) {
//
Something in the buffer, but not enough
char
*newPeekBuffer = (
char
*)
realloc
(peekBuffer, maxLen);
if
(newPeekBuffer ==
NULL
) {
HTTPS_LOGE
(
"
Multipart: out of memory
"
);
discardBody
();
return
;
}
peekBuffer = newPeekBuffer;
bufPtr = peekBuffer + peekBufferSize;
}
else
{
//
We already have enough data in the buffer.
return
;
}
while
(bufPtr < peekBuffer+maxLen) {
size_t
didRead = _request->
readChars
(bufPtr, peekBuffer+maxLen-bufPtr);
if
(didRead ==
0
) {
break
;
}
bufPtr += didRead;
//
We stop buffering once we have a CR in the buffer
if
(
memchr
(peekBuffer,
'
\r
'
, bufPtr-peekBuffer) !=
NULL
) {
break
;
}
}
peekBufferSize = bufPtr - peekBuffer;
if
(peekBufferSize ==
0
) {
HTTPS_LOGE
(
"
Multipart incomplete
"
);
}
}
void
HTTPMultipartBodyParser::consumedBuffer
(
size_t
consumed) {
if
(consumed ==
0
) {
return
;
}
if
(consumed == peekBufferSize) {
free
(peekBuffer);
peekBuffer =
NULL
;
peekBufferSize =
0
;
}
else
{
memmove
(peekBuffer, peekBuffer+consumed, peekBufferSize-consumed);
peekBufferSize -= consumed;
}
}
bool
HTTPMultipartBodyParser::skipCRLF
() {
if
(peekBufferSize <
2
) {
fillBuffer
(
2
);
}
if
(peekBufferSize <
2
) {
return
false
;
}
if
(peekBuffer[
0
] !=
'
\r
'
) {
return
false
;
}
if
(peekBuffer[
1
] !=
'
\n
'
) {
HTTPS_LOGE
(
"
Multipart incorrect line terminator
"
);
discardBody
();
return
false
;
}
consumedBuffer
(
2
);
return
true
;
}
std::string
HTTPMultipartBodyParser::readLine
() {
fillBuffer
(
MAXLINESIZE
);
if
(peekBufferSize ==
0
) {
return
"
"
;
}
char
*crPtr = (
char
*)
memchr
(peekBuffer,
'
\r
'
, peekBufferSize);
if
(crPtr ==
NULL
) {
HTTPS_LOGE
(
"
Multipart line too long
"
);
discardBody
();
return
"
"
;
}
size_t
lineLength = crPtr-peekBuffer;
std::string
rv
(peekBuffer, lineLength);
consumedBuffer
(lineLength);
skipCRLF
();
return
rv;
}
//
Returns true if the buffer contains a boundary (or possibly lastBoundary)
bool
HTTPMultipartBodyParser::peekBoundary
() {
if
(peekBuffer ==
NULL
|| peekBufferSize < boundary.
size
()) {
return
false
;
}
char
*ptr = peekBuffer;
if
(*ptr ==
'
\r
'
) {
ptr++;
}
if
(*ptr ==
'
\n
'
) {
ptr++;
}
return
memcmp
(ptr, boundary.
c_str
(), boundary.
size
()) ==
0
;
}
bool
HTTPMultipartBodyParser::nextField
() {
fillBuffer
(
MAXLINESIZE
);
while
(!
peekBoundary
()) {
std::string dummy =
readLine
();
if
(
endOfBody
()) {
HTTPS_LOGE
(
"
Multipart missing last boundary
"
);
return
false
;
}
fillBuffer
(
MAXLINESIZE
);
}
skipCRLF
();
std::string line =
readLine
();
if
(line == lastBoundary) {
discardBody
();
return
false
;
}
if
(line != boundary) {
HTTPS_LOGE
(
"
Multipart incorrect boundary
"
);
return
false
;
}
//
Read header lines up to and including blank line
fieldName =
"
"
;
fieldMimeType =
"
text/plain
"
;
fieldFilename =
"
"
;
while
(
true
) {
line =
readLine
();
if
(line ==
"
"
) {
break
;
}
if
(line.
substr
(
0
,
14
) ==
"
Content-Type:
"
) {
fieldMimeType = line.
substr
(
14
);
}
if
(line.
substr
(
0
,
31
) ==
"
Content-Disposition: form-data;
"
) {
//
Parse name=value; or name="value"; fields.
std::string field;
line = line.
substr
(
31
);
while
(
true
) {
size_t
pos = line.
find_first_not_of
(
'
'
);
if
(pos != std::string::npos) {
line = line.
substr
(pos);
}
if
(line ==
"
"
)
break
;
pos = line.
find
(
'
;
'
);
if
(pos == std::string::npos) {
field = line;
line =
"
"
;
}
else
{
field = line.
substr
(
0
, pos);
line = line.
substr
(pos+
1
);
}
pos = field.
find
(
'
=
'
);
if
(pos == std::string::npos) {
HTTPS_LOGE
(
"
Multipart ill-formed form-data header
"
);
return
false
;
}
std::string headerName = field.
substr
(
0
, pos);
std::string headerValue = field.
substr
(pos+
1
);
if
(headerValue.
substr
(
0
,
1
) ==
"
\"
"
) {
headerValue = headerValue.
substr
(
1
, headerValue.
size
()-
2
);
}
if
(headerName ==
"
name
"
) {
fieldName = headerValue;
}
if
(headerName ==
"
filename
"
) {
fieldFilename = headerValue;
}
}
}
}
if
(fieldName ==
"
"
) {
HTTPS_LOGE
(
"
Multipart missing name
"
);
return
false
;
}
return
true
;
}
std::string
HTTPMultipartBodyParser::getFieldName
() {
return
fieldName;
}
std::string
HTTPMultipartBodyParser::getFieldFilename
() {
return
fieldFilename;
}
std::string
HTTPMultipartBodyParser::getFieldMimeType
() {
return
fieldMimeType;
}
bool
HTTPMultipartBodyParser::endOfField
() {
return
peekBoundary
();
}
size_t
HTTPMultipartBodyParser::read
(byte* buffer,
size_t
bufferSize) {
if
(
peekBoundary
()) {
return
0
;
}
size_t
readSize =
std::min
(bufferSize,
MAXLINESIZE
);
fillBuffer
(readSize);
if
(
peekBoundary
()) {
return
0
;
}
//
We read at most up to a CR (so we don't miss a boundary that has been partially buffered)
//
but we always read at least one byte so if the first byte in the buffer is a CR we do read it.
if
(peekBufferSize >
1
) {
char
*crPtr = (
char
*)
memchr
(peekBuffer+
1
,
'
\r
'
, peekBufferSize-
1
);
if
(crPtr !=
NULL
&& crPtr - peekBuffer < bufferSize) {
bufferSize = crPtr - peekBuffer;
}
}
size_t
copySize =
std::min
(bufferSize, peekBufferSize);
memcpy
(buffer, peekBuffer, copySize);
consumedBuffer
(copySize);
return
copySize;
}
}
/*
namespace httpsserver
*/
Back
|
FazBrowse Home
|
New Git URL