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
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "webkit/plugins/ppapi/ppb_url_request_info_impl.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "googleurl/src/gurl.h"
#include "net/http/http_util.h"
#include "ppapi/c/pp_var.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebData.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebHTTPBody.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/plugin_module.h"
#include "webkit/plugins/ppapi/ppb_file_ref_impl.h"
#include "webkit/plugins/ppapi/string.h"
#include "webkit/plugins/ppapi/var.h"
#include "webkit/glue/webkit_glue.h"
using WebKit::WebData;
using WebKit::WebHTTPBody;
using WebKit::WebString;
using WebKit::WebFrame;
using WebKit::WebURL;
using WebKit::WebURLRequest;
namespace webkit {
namespace ppapi {
namespace {
const int32_t kDefaultPrefetchBufferUpperThreshold = 100 * 1000 * 1000;
const int32_t kDefaultPrefetchBufferLowerThreshold = 50 * 1000 * 1000;
// A header string containing any of the following fields will cause
// an error. The list comes from the XMLHttpRequest standard.
// http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method
const char* const kForbiddenHeaderFields[] = {
"accept-charset",
"accept-encoding",
"connection",
"content-length",
"cookie",
"cookie2",
"content-transfer-encoding",
"date",
"expect",
"host",
"keep-alive",
"origin",
"referer",
"te",
"trailer",
"transfer-encoding",
"upgrade",
"user-agent",
"via",
};
bool IsValidHeaderField(const std::string& name) {
for (size_t i = 0; i < arraysize(kForbiddenHeaderFields); ++i) {
if (LowerCaseEqualsASCII(name, kForbiddenHeaderFields[i]))
return false;
}
if (StartsWithASCII(name, "proxy-", false))
return false;
if (StartsWithASCII(name, "sec-", false))
return false;
return true;
}
bool AreValidHeaders(const std::string& headers) {
net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n");
while (it.GetNext()) {
if (!IsValidHeaderField(it.name()))
return false;
}
return true;
}
PP_Resource Create(PP_Instance instance_id) {
PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
if (!instance)
return 0;
PPB_URLRequestInfo_Impl* request = new PPB_URLRequestInfo_Impl(instance);
return request->GetReference();
}
PP_Bool IsURLRequestInfo(PP_Resource resource) {
return BoolToPPBool(!!Resource::GetAs<PPB_URLRequestInfo_Impl>(resource));
}
PP_Bool SetProperty(PP_Resource request_id,
PP_URLRequestProperty property,
PP_Var var) {
scoped_refptr<PPB_URLRequestInfo_Impl> request(
Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id));
if (!request)
return PP_FALSE;
PP_Bool result = PP_FALSE;
switch (var.type) {
case PP_VARTYPE_UNDEFINED:
result = BoolToPPBool(request->SetUndefinedProperty(property));
break;
case PP_VARTYPE_BOOL:
result = BoolToPPBool(
request->SetBooleanProperty(property,
PPBoolToBool(var.value.as_bool)));
break;
case PP_VARTYPE_INT32:
result = BoolToPPBool(
request->SetIntegerProperty(property, var.value.as_int));
break;
case PP_VARTYPE_STRING: {
scoped_refptr<StringVar> string(StringVar::FromPPVar(var));
if (string)
result = BoolToPPBool(request->SetStringProperty(property,
string->value()));
break;
}
default:
break;
}
return result;
}
PP_Bool AppendDataToBody(PP_Resource request_id,
const void* data,
uint32_t len) {
scoped_refptr<PPB_URLRequestInfo_Impl> request(
Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id));
if (!request)
return PP_FALSE;
return BoolToPPBool(request->AppendDataToBody(std::string(
static_cast<const char*>(data), len)));
}
PP_Bool AppendFileToBody(PP_Resource request_id,
PP_Resource file_ref_id,
int64_t start_offset,
int64_t number_of_bytes,
PP_Time expected_last_modified_time) {
scoped_refptr<PPB_URLRequestInfo_Impl> request(
Resource::GetAs<PPB_URLRequestInfo_Impl>(request_id));
if (!request)
return PP_FALSE;
scoped_refptr<PPB_FileRef_Impl> file_ref(
Resource::GetAs<PPB_FileRef_Impl>(file_ref_id));
if (!file_ref)
return PP_FALSE;
return BoolToPPBool(request->AppendFileToBody(file_ref,
start_offset,
number_of_bytes,
expected_last_modified_time));
}
const PPB_URLRequestInfo ppb_urlrequestinfo = {
&Create,
&IsURLRequestInfo,
&SetProperty,
&AppendDataToBody,
&AppendFileToBody
};
} // namespace
struct PPB_URLRequestInfo_Impl::BodyItem {
BodyItem(const std::string& data)
: data(data),
start_offset(0),
number_of_bytes(-1),
expected_last_modified_time(0.0) {
}
BodyItem(PPB_FileRef_Impl* file_ref,
int64_t start_offset,
int64_t number_of_bytes,
PP_Time expected_last_modified_time)
: file_ref(file_ref),
start_offset(start_offset),
number_of_bytes(number_of_bytes),
expected_last_modified_time(expected_last_modified_time) {
}
std::string data;
scoped_refptr<PPB_FileRef_Impl> file_ref;
int64_t start_offset;
int64_t number_of_bytes;
PP_Time expected_last_modified_time;
};
PPB_URLRequestInfo_Impl::PPB_URLRequestInfo_Impl(PluginInstance* instance)
: Resource(instance),
stream_to_file_(false),
follow_redirects_(true),
record_download_progress_(false),
record_upload_progress_(false),
has_custom_referrer_url_(false),
allow_cross_origin_requests_(false),
allow_credentials_(false),
has_custom_content_transfer_encoding_(false),
prefetch_buffer_upper_threshold_(kDefaultPrefetchBufferUpperThreshold),
prefetch_buffer_lower_threshold_(kDefaultPrefetchBufferLowerThreshold) {
}
PPB_URLRequestInfo_Impl::~PPB_URLRequestInfo_Impl() {
}
// static
const PPB_URLRequestInfo* PPB_URLRequestInfo_Impl::GetInterface() {
return &ppb_urlrequestinfo;
}
PPB_URLRequestInfo_Impl* PPB_URLRequestInfo_Impl::AsPPB_URLRequestInfo_Impl() {
return this;
}
bool PPB_URLRequestInfo_Impl::SetUndefinedProperty(
PP_URLRequestProperty property) {
switch (property) {
case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL:
has_custom_referrer_url_ = false;
custom_referrer_url_ = std::string();
return true;
case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING:
has_custom_content_transfer_encoding_ = false;
custom_content_transfer_encoding_ = std::string();
return true;
default:
return false;
}
}
bool PPB_URLRequestInfo_Impl::SetBooleanProperty(PP_URLRequestProperty property,
bool value) {
switch (property) {
case PP_URLREQUESTPROPERTY_STREAMTOFILE:
stream_to_file_ = value;
return true;
case PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS:
follow_redirects_ = value;
return true;
case PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS:
record_download_progress_ = value;
return true;
case PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS:
record_upload_progress_ = value;
return true;
case PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS:
allow_cross_origin_requests_ = value;
return true;
case PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS:
allow_credentials_ = value;
return true;
default:
return false;
}
}
bool PPB_URLRequestInfo_Impl::SetIntegerProperty(PP_URLRequestProperty property,
int32_t value) {
switch (property) {
case PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD:
prefetch_buffer_upper_threshold_ = value;
return true;
case PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD:
prefetch_buffer_lower_threshold_ = value;
return true;
default:
return false;
}
}
bool PPB_URLRequestInfo_Impl::SetStringProperty(PP_URLRequestProperty property,
const std::string& value) {
// TODO(darin): Validate input. Perhaps at a different layer?
switch (property) {
case PP_URLREQUESTPROPERTY_URL:
url_ = value; // NOTE: This may be a relative URL.
return true;
case PP_URLREQUESTPROPERTY_METHOD:
method_ = value;
return true;
case PP_URLREQUESTPROPERTY_HEADERS:
if (!AreValidHeaders(value))
return false;
headers_ = value;
return true;
case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL:
has_custom_referrer_url_ = true;
custom_referrer_url_ = value;
return true;
case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING:
has_custom_content_transfer_encoding_ = true;
custom_content_transfer_encoding_ = value;
return true;
default:
return false;
}
}
bool PPB_URLRequestInfo_Impl::AppendDataToBody(const std::string& data) {
if (!data.empty())
body_.push_back(BodyItem(data));
return true;
}
bool PPB_URLRequestInfo_Impl::AppendFileToBody(
PPB_FileRef_Impl* file_ref,
int64_t start_offset,
int64_t number_of_bytes,
PP_Time expected_last_modified_time) {
// Ignore a call to append nothing.
if (number_of_bytes == 0)
return true;
// Check for bad values. (-1 means read until end of file.)
if (start_offset < 0 || number_of_bytes < -1)
return false;
body_.push_back(BodyItem(file_ref,
start_offset,
number_of_bytes,
expected_last_modified_time));
return true;
}
WebURLRequest PPB_URLRequestInfo_Impl::ToWebURLRequest(WebFrame* frame) const {
WebURLRequest web_request;
web_request.initialize();
web_request.setURL(frame->document().completeURL(WebString::fromUTF8(url_)));
web_request.setDownloadToFile(stream_to_file_);
web_request.setReportUploadProgress(record_upload_progress());
if (!method_.empty())
web_request.setHTTPMethod(WebString::fromUTF8(method_));
if (!headers_.empty()) {
net::HttpUtil::HeadersIterator it(headers_.begin(), headers_.end(), "\n");
while (it.GetNext()) {
web_request.addHTTPHeaderField(
WebString::fromUTF8(it.name()),
WebString::fromUTF8(it.values()));
}
}
if (!body_.empty()) {
WebHTTPBody http_body;
http_body.initialize();
for (size_t i = 0; i < body_.size(); ++i) {
if (body_[i].file_ref) {
http_body.appendFileRange(
webkit_glue::FilePathToWebString(
body_[i].file_ref->GetSystemPath()),
body_[i].start_offset,
body_[i].number_of_bytes,
body_[i].expected_last_modified_time);
} else {
DCHECK(!body_[i].data.empty());
http_body.appendData(WebData(body_[i].data));
}
}
web_request.setHTTPBody(http_body);
}
if (has_custom_referrer_url_) {
if (!custom_referrer_url_.empty())
frame->setReferrerForRequest(web_request, GURL(custom_referrer_url_));
} else {
frame->setReferrerForRequest(web_request, WebURL()); // Use default.
}
if (has_custom_content_transfer_encoding_) {
if (!custom_content_transfer_encoding_.empty()) {
web_request.addHTTPHeaderField(
WebString::fromUTF8("Content-Transfer-Encoding"),
WebString::fromUTF8(custom_content_transfer_encoding_));
}
}
return web_request;
}
bool PPB_URLRequestInfo_Impl::RequiresUniversalAccess() const {
return has_custom_referrer_url_ || has_custom_content_transfer_encoding_;
}
} // namespace ppapi
} // namespace webkit
|