FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
esp32_https_server/src/HTTPURLEncodedBodyParser.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
/
HTTPURLEncodedBodyParser.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
128 lines (115 loc) · 3.32 KB
Breadcrumbs
esp32_https_server
/
src
/
HTTPURLEncodedBodyParser.cpp
Copy path
File metadata and controls
128 lines (115 loc) · 3.32 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
#
include
"
HTTPURLEncodedBodyParser.hpp
"
#
define
CHUNKSIZE
512
#
define
MINCHUNKSIZE
64
namespace
httpsserver
{
HTTPURLEncodedBodyParser::HTTPURLEncodedBodyParser
(HTTPRequest * req):
HTTPBodyParser
(req),
bodyBuffer
(
NULL
),
bodyPtr
(
NULL
),
bodyLength
(
0
),
fieldBuffer
(
"
"
),
fieldPtr
(
NULL
),
fieldRemainingLength
(
0
)
{
bodyLength = _request->
getContentLength
();
if
(bodyLength) {
//
We know the body length. We try to read that much and give an error if it fails.
bodyBuffer = (
char
*)
malloc
(bodyLength+
1
);
if
(bodyBuffer ==
NULL
) {
HTTPS_LOGE
(
"
HTTPURLEncodedBodyParser: out of memory
"
);
return
;
}
bodyPtr = bodyBuffer;
size_t
toRead = bodyLength;
while
(toRead >
0
) {
size_t
didRead = _request->
readChars
(bodyPtr, toRead);
if
(didRead ==
0
) {
HTTPS_LOGE
(
"
HTTPURLEncodedBodyParser: short read
"
);
bodyLength = bodyPtr - bodyBuffer;
break
;
}
bodyPtr += didRead;
toRead -= didRead;
}
}
else
{
//
We don't know the length. Read as much as possible.
bodyBuffer = (
char
*)
malloc
(
CHUNKSIZE
+
1
);
if
(bodyBuffer ==
NULL
) {
HTTPS_LOGE
(
"
HTTPURLEncodedBodyParser: out of memory
"
);
return
;
}
bodyPtr = bodyBuffer;
size_t
bufferUsed =
0
;
size_t
bufferAvailable =
CHUNKSIZE
;
while
(!_request->
requestComplete
()) {
if
(bufferAvailable <
MINCHUNKSIZE
) {
char
*pBuf = (
char
*)
realloc
(bodyBuffer, bufferUsed +
CHUNKSIZE
+
1
);
if
(pBuf ==
NULL
) {
HTTPS_LOGE
(
"
HTTPURLEncodedBodyParser: out of memory
"
);
free
(bodyBuffer);
bodyBuffer =
NULL
;
return
;
}
bodyBuffer = pBuf;
bufferAvailable =
CHUNKSIZE
;
}
size_t
didRead = _request->
readChars
(bodyBuffer+bufferUsed, bufferAvailable);
bufferUsed += didRead;
bufferAvailable -= didRead;
}
bodyLength = bufferUsed;
}
bodyPtr = bodyBuffer;
bodyBuffer[bodyLength] =
'
\0
'
;
}
HTTPURLEncodedBodyParser::~HTTPURLEncodedBodyParser
() {
if
(bodyBuffer) {
free
(bodyBuffer);
}
bodyBuffer =
NULL
;
}
bool
HTTPURLEncodedBodyParser::nextField
() {
fieldBuffer =
"
"
;
fieldPtr =
NULL
;
fieldRemainingLength =
0
;
char
*equalPtr =
index
(bodyPtr,
'
=
'
);
if
(equalPtr ==
NULL
) {
return
false
;
}
fieldName =
std::string
(bodyPtr, equalPtr-bodyPtr);
char
*valuePtr = equalPtr +
1
;
char
*endPtr =
index
(valuePtr,
'
&
'
);
if
(endPtr ==
NULL
) {
endPtr = equalPtr +
strlen
(equalPtr);
bodyPtr = endPtr;
}
else
{
bodyPtr = endPtr+
1
;
}
fieldBuffer =
std::string
(valuePtr, endPtr - valuePtr);
fieldBuffer =
urlDecode
(fieldBuffer);
fieldRemainingLength = fieldBuffer.
size
();
fieldPtr = fieldBuffer.
c_str
();
return
true
;
}
std::string
HTTPURLEncodedBodyParser::getFieldName
() {
return
fieldName;
}
std::string
HTTPURLEncodedBodyParser::getFieldFilename
() {
return
"
"
;
}
std::string
HTTPURLEncodedBodyParser::getFieldMimeType
() {
return
std::string
(
"
text/plain
"
);
}
bool
HTTPURLEncodedBodyParser::endOfField
() {
return
fieldRemainingLength <=
0
;
}
size_t
HTTPURLEncodedBodyParser::read
(byte* buffer,
size_t
bufferSize) {
if
(bufferSize > fieldRemainingLength) {
bufferSize = fieldRemainingLength;
}
memcpy
(buffer, fieldPtr, bufferSize);
fieldRemainingLength -= bufferSize;
fieldPtr += bufferSize;
return
bufferSize;
}
}
/*
namespace httpsserver
*/
Back
|
FazBrowse Home
|
New Git URL