FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
esp32_https_server/src/HTTPRequest.cpp at master · sorianoas/esp32_https_server · GitHub
sorianoas
esp32_https_server
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
esp32_https_server
/
src
/
HTTPRequest.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
185 lines (154 loc) · 4.21 KB
Breadcrumbs
esp32_https_server
/
src
/
HTTPRequest.cpp
Copy path
File metadata and controls
185 lines (154 loc) · 4.21 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
#
include
"
HTTPRequest.hpp
"
namespace
httpsserver
{
HTTPRequest::HTTPRequest
(
ConnectionContext * con,
HTTPHeaders * headers,
HTTPNode * resolvedNode,
std::string method,
ResourceParameters * params,
std::string requestString):
_con
(con),
_headers
(headers),
_resolvedNode
(resolvedNode),
_method
(method),
_params
(params),
_requestString
(requestString) {
HTTPHeader * contentLength = headers->
get
(
"
Content-Length
"
);
if
(contentLength ==
NULL
) {
_remainingContent =
0
;
_contentLengthSet =
false
;
}
else
{
_remainingContent =
parseInt
(contentLength->
_value
);
_contentLengthSet =
true
;
}
}
HTTPRequest::~HTTPRequest
() {
_headers->
clearAll
();
}
ResourceParameters *
HTTPRequest::getParams
() {
return
_params;
}
HTTPHeaders *
HTTPRequest::getHTTPHeaders
() {
return
_headers;
}
std::string
HTTPRequest::getHeader
(std::string
const
&name) {
HTTPHeader * h = _headers->
get
(name);
if
(h !=
NULL
) {
return
h->
_value
;
}
else
{
return
std::string
();
}
}
void
HTTPRequest::setHeader
(std::string
const
&name, std::string
const
&value) {
_headers->
set
(
new
HTTPHeader
(name, value));
}
HTTPNode *
HTTPRequest::getResolvedNode
() {
return
_resolvedNode;
}
IPAddress
HTTPRequest::getClientIP
() {
return
_con->
getClientIP
();
}
size_t
HTTPRequest::readBytes
(byte * buffer,
size_t
length) {
//
Limit reading to content length
if
(_contentLengthSet && length > _remainingContent) {
length = _remainingContent;
}
size_t
bytesRead =
0
;
if
(length >
0
) {
bytesRead = _con->
readBuffer
(buffer, length);
}
if
(_contentLengthSet) {
_remainingContent -= bytesRead;
}
return
bytesRead;
}
size_t
HTTPRequest::readChars
(
char
* buffer,
size_t
length) {
return
readBytes
((byte*)buffer, length);
}
size_t
HTTPRequest::getContentLength
() {
return
_remainingContent;
}
std::string
HTTPRequest::getRequestString
() {
return
_requestString;
}
std::string
HTTPRequest::getMethod
() {
return
_method;
}
std::string
HTTPRequest::getTag
() {
return
_resolvedNode->
_tag
;
}
bool
HTTPRequest::requestComplete
() {
if
(_contentLengthSet) {
//
If we have a content size, rely on it.
return
(_remainingContent ==
0
);
}
else
{
//
If there is no more input...
return
(_con->
pendingBufferSize
() ==
0
);
}
}
/*
*
* This function will drop whatever is remaining of the request body
*/
void
HTTPRequest::discardRequestBody
() {
byte buf[
16
];
while
(!
requestComplete
()) {
readBytes
(buf,
16
);
}
}
std::string
HTTPRequest::getBasicAuthUser
() {
std::string token =
decodeBasicAuthToken
();
size_t
splitpoint = token.
find
(
"
:
"
);
if
(splitpoint != std::string::npos && splitpoint >
0
) {
return
token.
substr
(
0
, splitpoint);
}
else
{
return
std::string
();
}
}
std::string
HTTPRequest::getBasicAuthPassword
() {
std::string token =
decodeBasicAuthToken
();
size_t
splitpoint = token.
find
(
"
:
"
);
if
(splitpoint != std::string::npos && splitpoint >
0
) {
return
token.
substr
(splitpoint+
1
);
}
else
{
return
std::string
();
}
}
std::string
HTTPRequest::decodeBasicAuthToken
() {
std::string basicAuthString =
getHeader
(
"
Authorization
"
);
//
Get the length of the token
size_t
sourceLength = basicAuthString.
length
();
//
Only handle basic auth tokens
if
(basicAuthString.
substr
(
0
,
6
) !=
"
Basic
"
) {
return
std::string
();
}
//
If the token is too long, skip
if
(sourceLength >
100
) {
return
std::string
();
}
else
{
//
Try to decode. As we are using mbedtls anyway, we can use that function
unsigned
char
* bufOut =
new
unsigned
char
[basicAuthString.
length
()];
size_t
outputLength =
0
;
int
res =
mbedtls_base64_decode
(
bufOut,
sourceLength,
&outputLength,
((
const
unsigned
char
*)basicAuthString.
substr
(
6
).
c_str
()),
//
Strip "Basic "
sourceLength -
6
//
Strip "Basic "
);
//
Failure of decoding
if
(res !=
0
) {
delete[]
bufOut;
return
std::string
();
}
std::string tokenRes =
std::string
((
char
*)bufOut, outputLength);
delete[]
bufOut;
return
tokenRes;
}
}
bool
HTTPRequest::isSecure
() {
return
_con->
isSecure
();
}
void
HTTPRequest::setWebsocketHandler
(WebsocketHandler *wsHandler) {
_con->
setWebsocketHandler
(wsHandler);
}
}
/*
namespace httpsserver
*/
Back
|
FazBrowse Home
|
New Git URL