FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/http/Request.cpp at main · ssh352/rstudio · GitHub
ssh352
/
rstudio
Public
forked from
rstudio/rstudio
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
rstudio
/
src
/
cpp
/
core
/
http
/
Request.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
437 lines (373 loc) · 11.3 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
http
/
Request.cpp
Copy path
File metadata and controls
437 lines (373 loc) · 11.3 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Request.cpp
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/http/Request.hpp
>
#
include
<
core/http/URL.hpp
>
#
include
<
gsl/gsl
>
#
include
<
boost/regex.hpp
>
#
include
<
boost/tokenizer.hpp
>
#
include
<
boost/asio/buffer.hpp
>
#
include
<
boost/algorithm/string/predicate.hpp
>
#
include
<
boost/algorithm/string/join.hpp
>
#
include
<
core/Log.hpp
>
#
include
<
core/Thread.hpp
>
namespace
rstudio
{
namespace
core
{
namespace
http
{
Request::Request
()
: remoteUid_(-
1
),
parsedCookies_
(
false
),
parsedFormFields_(
false
),
parsedQueryParams_(
false
)
{
}
Request::~Request
()
{
}
bool
Request::isSecure
()
const
{
//
the request is secure if the browser is using HTTPS to
//
reach the server which may depend on the presence of a proxy
return
URL
(
proxiedUri
()).
protocol
() ==
"
https
"
;
}
std::string
Request::baseUri
(BaseUriUse use
/*
= BaseUriUse::Internal
*/
)
const
{
//
When the root path is defined as other than the default ("/")
//
we can guess with precision the actual URL show by the browser's
//
address bar when we return the proxied URI. Without it either
//
it doesn't matter because there's no proxy and the internal
//
URI is the same address visible in the browser or the proxy is
//
taking care of the path-rewrite which is something we asked
//
customers to do before v1.4
if
(
rootPath
() !=
kRequestDefaultRootPath
)
return
proxiedUri
();
if
(use == BaseUriUse::Internal)
return
internalUri
();
//
When BaseUriUse::External, we omit the internal URI
return
"
"
;
}
std::string
Request::internalUri
()
const
{
//
ignore the proxy for the most part except by the scheme
std::string scheme =
URL
(
proxiedUri
()).
protocol
();
return
scheme +
"
://
"
+
host
() +
uri
();
}
std::string
Request::rootPath
()
const
{
//
if there's no proxy defining the header resort
//
to the externally defined value for root path
std::string rootPathHeader =
headerValue
(
"
X-RStudio-Root-Path
"
);
if
(rootPathHeader ==
"
"
)
{
rootPathHeader = rootPath_;
}
//
be sure the root path start with slash but doesn't end with one (unless literally only "/")
if
(rootPathHeader.
empty
() || rootPathHeader[
0
] !=
'
/
'
)
rootPathHeader =
'
/
'
+ rootPathHeader;
if
(rootPathHeader.
length
() >
1
&& rootPathHeader[rootPathHeader.
length
() -
1
] ==
'
/
'
)
rootPathHeader = rootPathHeader.
substr
(
0
, rootPathHeader.
length
() -
1
);
return
rootPathHeader;
}
std::string
firstInList
(
const
std::string& input)
{
std::string res;
//
Take the first if there's a list
std::
size_t
pos = input.
find
(
'
,
'
);
if
(pos != std::string::npos)
res = input.
substr
(
0
, pos);
else
res = input;
return
res;
}
std::string
Request::proxiedUri
()
const
{
//
if using the product-specific header use it
//
it should contain the exact scheme/host/port/path
//
of the original request as send by the browser
std::string overrideHeader =
headerValue
(
"
X-RStudio-Request
"
);
if
(!overrideHeader.
empty
())
{
return
overrideHeader;
}
std::string root =
rootPath
();
//
some internal requests may contain a virtual path any attempt to
//
compose the same address as seen in browser must be include this path
std::string vPath =
virtualPath
();
if
(!vPath.
empty
())
{
root += vPath;
}
//
might be using new Forwarded header
std::string forwarded =
headerValue
(
"
Forwarded
"
);
if
(!forwarded.
empty
())
{
std::string forwardedHost;
boost::regex
reHost
(
"
host=([
\\
w.:]+);?
"
);
boost::smatch matches;
if
(
boost::regex_search
(forwarded, matches, reHost))
forwardedHost = matches[
1
];
std::string protocol =
"
http
"
;
boost::regex
reProto
(
"
proto=([
\\
w.:]+);?
"
);
if
(
boost::regex_search
(forwarded, matches, reProto))
protocol = matches[
1
];
return
URL::complete
(protocol +
"
://
"
+ forwardedHost, root +
'
/
'
+
uri
());
}
//
get the protocol that was specified in the request
//
it might have been specified by rserver-http w/ ssl-enabled=1
std::string protocol =
headerValue
(
"
X-Forwarded-Proto
"
);
if
(protocol.
empty
())
{
protocol =
"
http
"
;
}
else
{
protocol =
firstInList
(protocol);
}
//
might be using the legacy X-Forwarded headers
std::string forwardedHost =
headerValue
(
"
X-Forwarded-Host
"
);
if
(!forwardedHost.
empty
())
{
forwardedHost =
firstInList
(forwardedHost);
//
get the port that may be specified in the request
std::string port =
headerValue
(
"
X-Forwarded-Port
"
);
if
(!port.
empty
())
{
port =
firstInList
(port);
std::
size_t
pos = forwardedHost.
find
(
'
:
'
);
if
(pos == std::string::npos)
{
forwardedHost +=
"
:
"
+ port;
}
else
{
forwardedHost = forwardedHost.
substr
(
0
, pos +
1
) + port;
}
}
return
URL::complete
(protocol +
"
://
"
+ forwardedHost, root +
'
/
'
+
uri
());
}
//
use the protocol that may have been set by X-Forwarded-Proto
return
URL::complete
(protocol +
"
://
"
+
host
(), root +
'
/
'
+
uri
());
}
bool
Request::acceptsContentType
(
const
std::string& contentType)
const
{
return
headerValue
(
"
Accept
"
).
find
(contentType) != std::string::npos;
}
bool
Request::acceptsEncoding
(
const
std::string& encoding)
const
{
//
read , separated fields
using
namespace
boost
;
char_separator<
char
>
comma
(
"
,
"
);
std::string accepted =
acceptEncoding
();
tokenizer<char_separator<
char
>>
tokens
(accepted, comma);
return
std::find
(tokens.
begin
(), tokens.
end
(), encoding) != tokens.
end
();
}
boost::posix_time::ptime
Request::ifModifiedSince
()
const
{
using
namespace
boost
::posix_time
;
std::string modifiedSinceDate =
headerValue
(
"
If-Modified-Since
"
);
if
(!modifiedSinceDate.
empty
())
{
return
util::parseHttpDate
(modifiedSinceDate);
}
else
{
return
ptime
(not_a_date_time);
}
}
std::string
Request::path
()
const
{
std::string::size_type pos =
uri
().
find
(
'
?
'
);
if
(pos != std::string::npos)
return
uri
().
substr
(
0
, pos);
else
return
uri
();
}
std::string
Request::queryString
()
const
{
//
find ? in uri()
std::string::size_type pos =
uri
().
find
(
'
?
'
);
if
(pos != std::string::npos)
{
std::string::size_type qsPos = pos +
1
;
if
(
uri
().
length
() > qsPos)
return
uri
().
substr
(qsPos);
else
return
std::string
();
}
else
{
return
std::string
();
}
}
const
Fields&
Request::queryParams
()
const
{
if
(!parsedQueryParams_)
{
util::parseQueryString
(
queryString
(), &queryParams_);
parsedQueryParams_ =
true
;
}
return
queryParams_;
}
std::string
Request::cookieValue
(
const
std::string& name)
const
{
//
parse cookies on demand
if
( !parsedCookies_ )
{
for
(Headers::const_iterator it =
headers
().
begin
(); it !=
headers
().
end
(); ++it )
{
scanHeaderForCookie
(it->
name
, it->
value
);
}
parsedCookies_ =
true
;
}
//
lookup the cookie
std::string cookie =
util::fieldValue
(cookies_, name);
//
when embedded into an iFrame a legacy cookie
//
may be present for old, non-conforming browsers
if
(cookie.
empty
())
{
cookie =
util::fieldValue
(cookies_, name +
kLegacyCookieSuffix
);
}
return
cookie;
}
void
Request::addCookie
(
const
std::string& name,
const
std::string& value)
{
cookies_.
push_back
(
std::make_pair
(name, value));
std::vector<std::string> cookies;
for
(
const
auto
& cookie : cookies_)
{
cookies.
push_back
(cookie.
first
+
"
=
"
+ cookie.
second
);
}
setHeader
(
"
Cookie
"
,
boost::algorithm::join
(cookies,
"
;
"
));
}
std::string
Request::cookieValueFromHeader
(
const
std::string& headerName)
const
{
std::string value =
headerValue
(headerName);
Fields cookie;
util::parseFields
(value,
"
;,
"
,
"
=
"
, &cookie, util::FieldDecodeNone);
if
(cookie.
size
() >
0
)
return
cookie.
at
(
0
).
second
;
else
return
std::string
();
}
std::string
Request::formFieldValue
(
const
std::string& name)
const
{
ensureFormFieldsParsed
();
//
lookup the form field
return
util::fieldValue
(formFields_, name);
}
const
Fields&
Request::formFields
()
const
{
ensureFormFieldsParsed
();
return
formFields_;
}
const
File&
Request::uploadedFile
(
const
std::string& name)
const
{
ensureFormFieldsParsed
();
//
lookup the file
for
(Files::const_iterator it = files_.
begin
(); it != files_.
end
(); ++it)
{
if
(it->
first
== name)
return
it->
second
;
}
//
not found
return
emptyFile_;
}
std::string
Request::queryParamValue
(
const
std::string& name)
const
{
//
lookup the query param
return
util::fieldValue
(
queryParams
(), name);
}
void
Request::setBody
(
const
std::string& body)
{
body_ = body;
setContentLength
(gsl::narrow_cast<
int
>(body_.
length
()));
}
void
Request::debugPrintUri
(
const
std::string& caption)
const
{
static
boost::mutex printMutex;
LOCK_MUTEX
(printMutex)
{
std::cerr << caption <<
"
:
"
<<
uri
() << std::endl;
}
END_LOCK_MUTEX
}
void
Request::resetMembers
()
{
method_.
clear
();
uri_.
clear
();
parsedCookies_ =
false
;
cookies_.
clear
();
parsedFormFields_ =
false
;
formFields_.
clear
();
parsedQueryParams_ =
false
;
queryParams_.
clear
();
}
void
Request::appendFirstLineBuffers
(
std::vector<boost::asio::const_buffer>& buffers)
const
{
using
boost::asio::buffer;
//
request line
buffers.
push_back
(
buffer
(method_));
appendSpaceBuffer
(buffers);
buffers.
push_back
(
buffer
(uri_));
appendSpaceBuffer
(buffers);
appendHttpVersionBuffers
(buffers);
}
void
Request::ensureFormFieldsParsed
()
const
{
//
parase form fields on demand
if
( !parsedFormFields_ )
{
std::string contentType =
headerValue
(
"
Content-Type
"
);
if
(contentType ==
"
application/x-www-form-urlencoded
"
)
{
util::parseFields
(
body
(),
"
&
"
,
"
=
"
,
&formFields_,
util::FieldDecodeForm);
}
else
if
(contentType.
find
(
"
multipart/form-data
"
) ==
0
)
{
util::parseMultipartForm
(contentType,
body
(), &formFields_, &files_);
}
else
{
//
no form fields available
}
parsedFormFields_ =
true
;
}
}
void
Request::scanHeaderForCookie
(
const
std::string& name,
const
std::string& value)
const
{
if
(
boost::iequals
(name,
"
cookie
"
))
util::parseFields
(value,
"
;,
"
,
"
=
"
, &cookies_, util::FieldDecodeNone);
}
std::ostream&
operator
<< (std::ostream& stream,
const
Request& r)
{
//
output request line
stream << r.
method
() <<
"
"
<< r.
uri
()
<<
"
HTTP/
"
<< r.
httpVersionMajor
() <<
"
.
"
<< r.
httpVersionMinor
()
<< std::endl;
//
output headers and body
const
Message& m = r;
stream << m;
return
stream;
}
}
//
namespacce http
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL