FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
libhttpserver/src/http_request.cpp at master · chendaole/libhttpserver · GitHub
chendaole
/
libhttpserver
Public
forked from
etr/libhttpserver
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
libhttpserver
/
src
/
http_request.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
314 lines (252 loc) · 8.04 KB
Breadcrumbs
libhttpserver
/
src
/
http_request.cpp
Copy path
File metadata and controls
314 lines (252 loc) · 8.04 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
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
/*
This file is part of libhttpserver
Copyright (C) 2011-2019 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
#
include
"
http_utils.hpp
"
#
include
"
http_request.hpp
"
#
include
"
string_utilities.hpp
"
#
include
<
iostream
>
using
namespace
std
;
namespace
httpserver
{
const
std::string http_request::
EMPTY
=
"
"
;
struct
arguments_accumulator
{
unescaper_ptr unescaper;
std::map<std::string, std::string, http::arg_comparator>* arguments;
};
void
http_request::set_method
(
const
std::string& method)
{
this
->
method
=
string_utilities::to_upper_copy
(method);
}
bool
http_request::check_digest_auth
(
const
std::string& realm,
const
std::string& password,
int
nonce_timeout,
bool
& reload_nonce
)
const
{
std::string digested_user =
get_digested_user
();
int
val =
MHD_digest_auth_check
(
underlying_connection,
realm.
c_str
(),
digested_user.
c_str
(),
password.
c_str
(),
nonce_timeout
);
if
(val ==
MHD_INVALID_NONCE
)
{
reload_nonce =
true
;
return
false
;
}
else
if
(val ==
MHD_NO
)
{
reload_nonce =
false
;
return
false
;
}
reload_nonce =
false
;
return
true
;
}
const
std::string
http_request::get_connection_value
(
const
std::string& key,
enum
MHD_ValueKind kind)
const
{
const
char
* header_c =
MHD_lookup_connection_value
(
this
->
underlying_connection
,
kind,
key.
c_str
()
);
if
(header_c ==
NULL
)
return
EMPTY
;
return
header_c;
}
int
http_request::build_request_header
(
void
*cls,
enum
MHD_ValueKind kind,
const
char
*key,
const
char
*value
)
{
std::map<std::string, std::string, http::header_comparator>* dhr =
static_cast
<std::map<std::string, std::string, http::header_comparator>*>(cls);
(*dhr)[key] = value;
return
MHD_YES
;
}
const
std::map<std::string, std::string, http::header_comparator>
http_request::get_headerlike_values
(
enum
MHD_ValueKind kind)
const
{
std::map<std::string, std::string, http::header_comparator> headers;
MHD_get_connection_values
(
this
->
underlying_connection
,
kind,
&build_request_header,
(
void
*) &headers
);
return
headers;
}
const
std::string
http_request::get_header
(
const
std::string& key)
const
{
return
get_connection_value
(key,
MHD_HEADER_KIND
);
}
const
std::map<std::string, std::string, http::header_comparator>
http_request::get_headers
()
const
{
return
get_headerlike_values
(
MHD_HEADER_KIND
);
}
const
std::string
http_request::get_footer
(
const
std::string& key)
const
{
return
get_connection_value
(key,
MHD_FOOTER_KIND
);
}
const
std::map<std::string, std::string, http::header_comparator>
http_request::get_footers
()
const
{
return
get_headerlike_values
(
MHD_FOOTER_KIND
);
}
const
std::string
http_request::get_cookie
(
const
std::string& key)
const
{
return
get_connection_value
(key,
MHD_COOKIE_KIND
);
}
const
std::map<std::string, std::string, http::header_comparator>
http_request::get_cookies
()
const
{
return
get_headerlike_values
(
MHD_COOKIE_KIND
);
}
const
std::string
http_request::get_arg
(
const
std::string& key)
const
{
std::map<std::string, std::string>::const_iterator it =
this
->
args
.
find
(key);
if
(it !=
this
->
args
.
end
())
{
return
it->
second
;
}
return
get_connection_value
(key,
MHD_GET_ARGUMENT_KIND
);
}
const
std::map<std::string, std::string, http::arg_comparator>
http_request::get_args
()
const
{
std::map<std::string, std::string, http::arg_comparator> arguments;
arguments.
insert
(
this
->
args
.
begin
(),
this
->
args
.
end
());
arguments_accumulator aa;
aa.
unescaper
=
this
->
unescaper
;
aa.
arguments
= &arguments;
MHD_get_connection_values
(
this
->
underlying_connection
,
MHD_GET_ARGUMENT_KIND
,
&build_request_args,
(
void
*) &aa
);
return
arguments;
}
const
std::string
http_request::get_querystring
()
const
{
std::string querystring =
"
"
;
MHD_get_connection_values
(
this
->
underlying_connection
,
MHD_GET_ARGUMENT_KIND
,
&build_request_querystring,
(
void
*) &querystring
);
return
querystring;
}
int
http_request::build_request_args
(
void
*cls,
enum
MHD_ValueKind kind,
const
char
*key,
const
char
*arg_value
)
{
arguments_accumulator* aa =
static_cast
<arguments_accumulator*>(cls);
std::string value = ((arg_value ==
NULL
) ?
"
"
: arg_value);
http::base_unescaper
(value, aa->
unescaper
);
(*aa->
arguments
)[key] = value;
return
MHD_YES
;
}
int
http_request::build_request_querystring
(
void
*cls,
enum
MHD_ValueKind kind,
const
char
*key,
const
char
*arg_value
)
{
std::string* querystring =
static_cast
<std::string*>(cls);
std::string value = ((arg_value ==
NULL
) ?
"
"
: arg_value);
{
char
buf[
std::string
(key).
size
() + value.
size
() +
3
];
if
(*querystring ==
"
"
)
{
snprintf
(buf,
sizeof
buf,
"
?%s=%s
"
, key, value.
c_str
());
*querystring = buf;
}
else
{
snprintf
(buf,
sizeof
buf,
"
&%s=%s
"
, key, value.
c_str
());
*querystring +=
string
(buf);
}
}
return
MHD_YES
;
}
const
std::string
http_request::get_user
()
const
{
char
* username =
0x0
;
char
* password =
0x0
;
username =
MHD_basic_auth_get_username_password
(underlying_connection, &password);
if
(password !=
0x0
)
free
(password);
std::string user;
if
(username !=
0x0
) user = username;
free
(username);
return
user;
}
const
std::string
http_request::get_pass
()
const
{
char
* username =
0x0
;
char
* password =
0x0
;
username =
MHD_basic_auth_get_username_password
(underlying_connection, &password);
if
(username !=
0x0
)
free
(username);
std::string pass;
if
(password !=
0x0
) pass = password;
free
(password);
return
pass;
}
const
std::string
http_request::get_digested_user
()
const
{
char
* digested_user_c =
0x0
;
digested_user_c =
MHD_digest_auth_get_username
(underlying_connection);
std::string digested_user =
EMPTY
;
if
(digested_user_c !=
0x0
)
{
digested_user = digested_user_c;
free
(digested_user_c);
}
return
digested_user;
}
const
std::string
http_request::get_requestor
()
const
{
const
MHD_ConnectionInfo * conninfo =
MHD_get_connection_info
(
underlying_connection,
MHD_CONNECTION_INFO_CLIENT_ADDRESS
);
return
http::get_ip_str
(conninfo->
client_addr
);
}
unsigned
short
http_request::get_requestor_port
()
const
{
const
MHD_ConnectionInfo * conninfo =
MHD_get_connection_info
(
underlying_connection,
MHD_CONNECTION_INFO_CLIENT_ADDRESS
);
return
http::get_port
(conninfo->
client_addr
);
}
std::ostream &
operator
<< (std::ostream &os,
const
http_request &r)
{
os << r.
get_method
() <<
"
Request [user:
\"
"
<< r.
get_user
() <<
"
\"
pass:
\"
"
<< r.
get_pass
() <<
"
\"
] path:
\"
"
<< r.
get_path
() <<
"
\"
"
<< std::endl;
http::dump_header_map
(os,
"
Headers
"
,r.
get_headers
());
http::dump_header_map
(os,
"
Footers
"
,r.
get_footers
());
http::dump_header_map
(os,
"
Cookies
"
,r.
get_cookies
());
http::dump_arg_map
(os,
"
Query Args
"
,r.
get_args
());
os <<
"
Version [
"
<< r.
get_version
() <<
"
] Requestor [
"
<< r.
get_requestor
()
<<
"
] Port [
"
<< r.
get_requestor_port
() <<
"
]
"
<< std::endl;
return
os;
}
}
Back
|
FazBrowse Home
|
New Git URL