FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/session/SessionContentUrls.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
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
/
session
/
SessionContentUrls.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
261 lines (215 loc) · 7.62 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
session
/
SessionContentUrls.cpp
Copy path
File metadata and controls
261 lines (215 loc) · 7.62 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
/*
* SessionContentUrls.cpp
*
* Copyright (C) 2009-19 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, 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
<
session/SessionContentUrls.hpp
>
#
include
<
boost/algorithm/string/predicate.hpp
>
#
include
<
shared_core/Error.hpp
>
#
include
<
core/Log.hpp
>
#
include
<
shared_core/FilePath.hpp
>
#
include
<
core/FileSerializer.hpp
>
#
include
<
core/Exec.hpp
>
#
include
<
core/system/System.hpp
>
#
include
<
core/http/Util.hpp
>
#
include
<
core/http/Response.hpp
>
#
include
<
core/http/Request.hpp
>
#
include
<
core/json/JsonRpc.hpp
>
extern
"
C
"
const
char
*
locale2charset
(
const
char
*);
#
include
<
session/SessionModuleContext.hpp
>
using
namespace
rstudio
::core
;
namespace
rstudio
{
namespace
session
{
namespace
content_urls
{
namespace
{
FilePath
contentUrlPath
()
{
FilePath filePath =
module_context::userScratchPath
().
completePath
(
"
content_urls
"
);
Error error = filePath.
ensureDirectory
();
if
(error)
LOG_ERROR
(error);
return
filePath;
}
std::string
buildContentUrl
(
const
std::string& title,
const
std::string& contentFile)
{
//
sanitize and encode title
std::string encodedTitle =
http::util::urlEncode
(
string_utils::htmlEscape
(title),
true
);
//
calculate and return content url
std::string url =
std::string
(
"
content
"
) +
"
?title=
"
+ encodedTitle +
"
&file=
"
+
http::util::urlEncode
(contentFile,
true
);
return
url;
}
Error
contentFileInfo
(
const
std::string& contentUrl,
std::string* pTitle,
core::FilePath* pFilePath)
{
//
extract and parse query string
std::string queryString;
std::string::size_type pos = contentUrl.
find
(
'
?
'
);
if
(pos != std::string::npos)
{
if
((pos+
1
) < contentUrl.
length
())
queryString = contentUrl.
substr
(pos+
1
);
else
return
systemError
(
ENOENT
,
ERROR_LOCATION
);
}
else
{
return
systemError
(
ENOENT
,
ERROR_LOCATION
);
}
http::Fields fields;
http::util::parseQueryString
(queryString, &fields);
//
get title parameter
*pTitle = http::util::fieldValue<std::string>(fields,
"
title
"
,
"
(Untitled)
"
);
//
use only the first line of the title
size_t
newline = pTitle->
find
(
"
\n
"
);
if
(newline != std::string::npos)
*pTitle = pTitle->
substr
(
0
, newline);
//
get file parameter
std::string contentFile =
http::util::fieldValue
(fields,
"
file
"
);
if
(contentFile.
empty
())
return
systemError
(
ENOENT
,
ERROR_LOCATION
);
*pFilePath =
contentUrlPath
().
completePath
(contentFile);
//
return success
return
Success
();
}
}
//
anonymous namespace
std::string
provision
(
const
std::string& title,
const
FilePath& filePath)
{
//
calculate content path
std::string contentFile =
core::system::generateUuid
(
false
) +
filePath.
getExtension
();
FilePath contentPath =
contentUrlPath
().
completePath
(contentFile);
//
copy the file
Error error = filePath.
copy
(contentPath);
if
(error)
LOG_ERROR
(error);
//
calculate and return content url
return
buildContentUrl
(title, contentFile);
}
std::string
provision
(
const
std::string& title,
const
std::string& content,
const
std::string& extension)
{
//
calculate content path
std::string contentFile =
core::system::generateUuid
(
false
) + extension;
FilePath contentPath =
contentUrlPath
().
completePath
(contentFile);
//
write the file
Error error =
writeStringToFile
(contentPath, content);
if
(error)
LOG_ERROR
(error);
//
calculate and return content url
return
buildContentUrl
(title, contentFile);
}
void
handleContentRequest
(
const
http::Request& request, http::Response* pResponse)
{
//
get content file info
std::string title;
FilePath contentFilePath;
Error error =
contentFileInfo
(request.
uri
(), &title, &contentFilePath);
if
(error)
{
pResponse->
setError
(error);
return
;
}
//
set private cache forever headers
pResponse->
setPrivateCacheForeverHeaders
();
//
read file
std::string contents;
error =
core::readStringFromFile
(contentFilePath, &contents);
if
(error)
{
pResponse->
setError
(error);
return
;
}
//
Ensure we are over the Qt iframe (?) minimum of 513 bytes for our custom
//
network reply handler to work correctly (for this case and apparently
//
this case only if bytesAvailable is less than 512 then Qt never calls
//
readData after the call to bytesAvailable). This is the behavior we used
//
to see in Qt 4.X and for that case we had a special workaround (see
//
https://github.com/rstudio/rstudio/blob/master/src/cpp/desktop/DesktopNetworkReply.cpp#L186-L195)
//
//
Note that we can't just restore the workaround because the IDE didn't
//
even come up with the workaround in place! (due to RPC calls that
//
were less than 512 bytes). I'm not sure why this case and this case
//
only presents problems -- it may be because it's a request in an
//
iframe? (I tried to find other iframes where we might have less than
//
512 bytes -- e.g. Plots, Help but couldn't even construct a case
//
where the payload was that small)
if
(contents.
length
() <=
512
)
{
std::string
fill
(
512
- contents.
length
() +
1
,
'
'
);
contents.
append
(fill);
}
//
set contents
pResponse->
setContentType
(contentFilePath.
getMimeContentType
());
pResponse->
setBody
(contents);
bool
isUtf8 =
true
;
if
(
boost::algorithm::starts_with
(contentFilePath.
getMimeContentType
(),
"
text/
"
))
{
//
If the content looks like valid UTF-8, assume it is. Otherwise, assume
//
it's the system encoding.
std::string contents;
error =
core::readStringFromFile
(contentFilePath, &contents);
if
(!error)
{
for
(std::string::iterator pos = contents.
begin
(); pos != contents.
end
(); )
{
error =
string_utils::utf8Advance
(pos,
1
, contents.
end
(), &pos);
if
(error)
{
isUtf8 =
false
;
break
;
}
}
}
}
//
reset content-type with charset
pResponse->
setContentType
(contentFilePath.
getMimeContentType
() +
std::string
(
"
; charset=
"
) +
(isUtf8 ?
"
UTF-8
"
: ::
locale2charset
(
nullptr
)));
//
set title header
pResponse->
setHeader
(
"
Title
"
, title);
}
Error
removeContentUrl
(
const
json::JsonRpcRequest& request,
json::JsonRpcResponse* pResponse)
{
//
get content url
std::string contentUrl;
Error error =
json::readParams
(request.
params
, &contentUrl);
if
(error)
return
error;
//
get content file info
std::string title;
FilePath contentFilePath;
error =
contentFileInfo
(contentUrl, &title, &contentFilePath);
if
(error)
return
error;
//
remove it
return
contentFilePath.
removeIfExists
();
}
Error
initialize
()
{
using
boost::bind;
using
namespace
session
::module_context
;
ExecBlock initBlock ;
initBlock.
addFunctions
()
(
bind
(registerUriHandler,
"
/content
"
, handleContentRequest))
(
bind
(registerRpcMethod,
"
remove_content_url
"
, removeContentUrl));
return
initBlock.
execute
();
}
}
//
namespace content_urls
}
//
namespace session
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL