1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/child/npapi/plugin_url_fetcher.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "content/child/child_thread.h"
9 #include "content/child/npapi/plugin_host.h"
10 #include "content/child/npapi/plugin_instance.h"
11 #include "content/child/npapi/plugin_stream_url.h"
12 #include "content/child/npapi/webplugin.h"
13 #include "content/child/npapi/webplugin_resource_client.h"
14 #include "content/child/plugin_messages.h"
15 #include "content/child/request_extra_data.h"
16 #include "content/child/request_info.h"
17 #include "content/child/resource_dispatcher.h"
18 #include "content/child/web_url_loader_impl.h"
19 #include "content/common/resource_request_body.h"
20 #include "content/common/service_worker/service_worker_types.h"
21 #include "content/public/common/resource_response_info.h"
22 #include "net/base/load_flags.h"
23 #include "net/base/net_errors.h"
24 #include "net/http/http_response_headers.h"
25 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
26 #include "third_party/WebKit/public/platform/WebURLResponse.h"
27 #include "webkit/child/multipart_response_delegate.h"
28 #include "webkit/child/resource_loader_bridge.h"
33 // This class handles individual multipart responses. It is instantiated when
34 // we receive HTTP status code 206 in the HTTP response. This indicates
35 // that the response could have multiple parts each separated by a boundary
36 // specified in the response header.
37 // TODO(jam): this is similar to MultiPartResponseClient in webplugin_impl.cc,
38 // we should remove that other class once we switch to loading from the plugin
39 // process by default.
40 class MultiPartResponseClient
: public blink::WebURLLoaderClient
{
42 explicit MultiPartResponseClient(PluginStreamUrl
* plugin_stream
)
43 : byte_range_lower_bound_(0), plugin_stream_(plugin_stream
) {}
45 // blink::WebURLLoaderClient implementation:
46 virtual void didReceiveResponse(
47 blink::WebURLLoader
* loader
,
48 const blink::WebURLResponse
& response
) OVERRIDE
{
49 int64 byte_range_upper_bound
, instance_size
;
50 if (!webkit_glue::MultipartResponseDelegate::ReadContentRanges(
51 response
, &byte_range_lower_bound_
, &byte_range_upper_bound
,
56 virtual void didReceiveData(blink::WebURLLoader
* loader
,
59 int encoded_data_length
) OVERRIDE
{
61 // We should defer further loads on multipart resources on the same lines
62 // as regular resources requested by plugins to prevent reentrancy.
63 int64 data_offset
= byte_range_lower_bound_
;
64 byte_range_lower_bound_
+= data_length
;
65 plugin_stream_
->DidReceiveData(data
, data_length
, data_offset
);
66 // DANGER: this instance may be deleted at this point.
70 // The lower bound of the byte range.
71 int64 byte_range_lower_bound_
;
72 // The handler for the data.
73 PluginStreamUrl
* plugin_stream_
;
78 PluginURLFetcher::PluginURLFetcher(PluginStreamUrl
* plugin_stream
,
80 const GURL
& first_party_for_cookies
,
81 const std::string
& method
,
85 const std::string
& range
,
86 bool notify_redirects
,
87 bool is_plugin_src_load
,
91 unsigned long resource_id
,
92 bool copy_stream_data
)
93 : plugin_stream_(plugin_stream
),
95 first_party_for_cookies_(first_party_for_cookies
),
98 notify_redirects_(notify_redirects
),
99 is_plugin_src_load_(is_plugin_src_load
),
100 origin_pid_(origin_pid
),
101 render_frame_id_(render_frame_id
),
102 render_view_id_(render_view_id
),
103 resource_id_(resource_id
),
104 copy_stream_data_(copy_stream_data
),
106 pending_failure_notification_(false) {
107 RequestInfo request_info
;
108 request_info
.method
= method
;
109 request_info
.url
= url
;
110 request_info
.first_party_for_cookies
= first_party_for_cookies
;
111 request_info
.referrer
= referrer
;
112 request_info
.load_flags
= net::LOAD_NORMAL
;
113 request_info
.requestor_pid
= origin_pid
;
114 request_info
.request_type
= ResourceType::OBJECT
;
115 request_info
.routing_id
= render_view_id
;
117 RequestExtraData extra_data
;
118 extra_data
.set_render_frame_id(render_frame_id
);
119 extra_data
.set_is_main_frame(false);
120 request_info
.extra_data
= &extra_data
;
122 std::vector
<char> body
;
123 if (method
== "POST") {
124 bool content_type_found
= false;
125 std::vector
<std::string
> names
;
126 std::vector
<std::string
> values
;
127 PluginHost::SetPostData(buf
, len
, &names
, &values
, &body
);
128 for (size_t i
= 0; i
< names
.size(); ++i
) {
129 if (!request_info
.headers
.empty())
130 request_info
.headers
+= "\r\n";
131 request_info
.headers
+= names
[i
] + ": " + values
[i
];
132 if (LowerCaseEqualsASCII(names
[i
], "content-type"))
133 content_type_found
= true;
136 if (!content_type_found
) {
137 if (!request_info
.headers
.empty())
138 request_info
.headers
+= "\r\n";
139 request_info
.headers
+= "Content-Type: application/x-www-form-urlencoded";
143 request_info
.headers
= std::string("Range: ") + range
;
146 bridge_
.reset(ChildThread::current()->resource_dispatcher()->CreateBridge(
149 scoped_refptr
<ResourceRequestBody
> request_body
=
150 new ResourceRequestBody
;
151 request_body
->AppendBytes(&body
[0], body
.size());
152 bridge_
->SetRequestBody(request_body
.get());
155 bridge_
->Start(this);
157 // TODO(jam): range requests
160 PluginURLFetcher::~PluginURLFetcher() {
163 void PluginURLFetcher::Cancel() {
166 // Due to races and nested event loops, PluginURLFetcher may still receive
167 // events from the bridge before being destroyed. Do not forward additional
168 // events back to the plugin, via either |plugin_stream_| or
169 // |multipart_delegate_| which has its own pointer via
170 // MultiPartResponseClient.
171 if (multipart_delegate_
)
172 multipart_delegate_
->Cancel();
173 plugin_stream_
= NULL
;
176 void PluginURLFetcher::URLRedirectResponse(bool allow
) {
181 bridge_
->SetDefersLoading(false);
184 plugin_stream_
->DidFail(resource_id_
); // That will delete |this|.
188 void PluginURLFetcher::OnUploadProgress(uint64 position
, uint64 size
) {
191 bool PluginURLFetcher::OnReceivedRedirect(
193 const GURL
& new_first_party_for_cookies
,
194 const ResourceResponseInfo
& info
) {
198 // TODO(jam): THIS LOGIC IS COPIED FROM WebPluginImpl::willSendRequest until
199 // kDirectNPAPIRequests is the default and we can remove the old path there.
201 // Currently this check is just to catch an https -> http redirect when
202 // loading the main plugin src URL. Longer term, we could investigate
203 // firing mixed diplay or scripting issues for subresource loads
204 // initiated by plug-ins.
205 if (is_plugin_src_load_
&&
206 !plugin_stream_
->instance()->webplugin()->CheckIfRunInsecureContent(
208 plugin_stream_
->DidFail(resource_id_
); // That will delete |this|.
212 // It's unfortunate that this logic of when a redirect's method changes is
213 // in url_request.cc, but weburlloader_impl.cc and this file have to duplicate
214 // it instead of passing that information.
215 int response_code
= info
.headers
->response_code();
216 if (response_code
!= 307)
220 first_party_for_cookies_
= new_first_party_for_cookies
;
222 // If the plugin does not participate in url redirect notifications then just
223 // block cross origin 307 POST redirects.
224 if (!notify_redirects_
) {
225 if (response_code
== 307 && method_
== "POST" &&
226 old_url
.GetOrigin() != new_url
.GetOrigin()) {
227 plugin_stream_
->DidFail(resource_id_
); // That will delete |this|.
231 // Pause the request while we ask the plugin what to do about the redirect.
232 bridge_
->SetDefersLoading(true);
233 plugin_stream_
->WillSendRequest(url_
, response_code
);
239 void PluginURLFetcher::OnReceivedResponse(const ResourceResponseInfo
& info
) {
243 // TODO(jam): THIS LOGIC IS COPIED FROM WebPluginImpl::didReceiveResponse
244 // GetAllHeaders, and GetResponseInfo until kDirectNPAPIRequests is the
245 // default and we can remove the old path there.
247 bool request_is_seekable
= true;
248 DCHECK(!multipart_delegate_
.get());
249 if (plugin_stream_
->seekable()) {
250 int response_code
= info
.headers
->response_code();
251 if (response_code
== 206) {
252 blink::WebURLResponse response
;
253 response
.initialize();
254 WebURLLoaderImpl::PopulateURLResponse(url_
, info
, &response
);
256 std::string multipart_boundary
;
257 if (webkit_glue::MultipartResponseDelegate::ReadMultipartBoundary(
258 response
, &multipart_boundary
)) {
259 plugin_stream_
->instance()->webplugin()->DidStartLoading();
261 MultiPartResponseClient
* multi_part_response_client
=
262 new MultiPartResponseClient(plugin_stream_
);
264 multipart_delegate_
.reset(new webkit_glue::MultipartResponseDelegate(
265 multi_part_response_client
, NULL
, response
, multipart_boundary
));
267 // Multiple ranges requested, data will be delivered by
268 // MultipartResponseDelegate.
273 int64 upper_bound
= 0, instance_size
= 0;
274 // Single range requested - go through original processing for
275 // non-multipart requests, but update data offset.
276 webkit_glue::MultipartResponseDelegate::ReadContentRanges(
277 response
, &data_offset_
, &upper_bound
, &instance_size
);
278 } else if (response_code
== 200) {
279 // TODO: should we handle this case? We used to but it's not clear that we
280 // still need to. This was bug 5403, fixed in r7139.
284 // If the length comes in as -1, then it indicates that it was not
285 // read off the HTTP headers. We replicate Safari webkit behavior here,
286 // which is to set it to 0.
287 int expected_length
= std::max(static_cast<int>(info
.content_length
), 0);
290 uint32 last_modified
= 0;
292 if (info
.headers
) { // NULL for data: urls.
293 if (info
.headers
->GetLastModifiedValue(&temp
))
294 last_modified
= static_cast<uint32
>(temp
.ToDoubleT());
296 // TODO(darin): Shouldn't we also report HTTP version numbers?
297 int response_code
= info
.headers
->response_code();
298 headers
= base::StringPrintf("HTTP %d ", response_code
);
299 headers
+= info
.headers
->GetStatusText();
303 std::string name
, value
;
304 while (info
.headers
->EnumerateHeaderLines(&iter
, &name
, &value
)) {
305 // TODO(darin): Should we really exclude headers with an empty value?
306 if (!name
.empty() && !value
.empty())
307 headers
+= name
+ ": " + value
+ "\n";
310 // Bug http://b/issue?id=925559. The flash plugin would not handle the HTTP
311 // error codes in the stream header and as a result, was unaware of the fate
312 // of the HTTP requests issued via NPN_GetURLNotify. Webkit and FF destroy
313 // the stream and invoke the NPP_DestroyStream function on the plugin if the
314 // HTTPrequest fails.
315 if ((url_
.SchemeIs("http") || url_
.SchemeIs("https")) &&
316 (response_code
< 100 || response_code
>= 400)) {
317 pending_failure_notification_
= true;
321 plugin_stream_
->DidReceiveResponse(info
.mime_type
,
325 request_is_seekable
);
328 void PluginURLFetcher::OnDownloadedData(int len
,
329 int encoded_data_length
) {
332 void PluginURLFetcher::OnReceivedData(const char* data
,
334 int encoded_data_length
) {
338 if (multipart_delegate_
) {
339 multipart_delegate_
->OnReceivedData(data
, data_length
, encoded_data_length
);
341 int64 offset
= data_offset_
;
342 data_offset_
+= data_length
;
344 if (copy_stream_data_
) {
345 // QuickTime writes to this memory, and since we got it from
346 // ResourceDispatcher it's not mapped for write access in this process.
347 // http://crbug.com/308466.
348 scoped_ptr
<char[]> data_copy(new char[data_length
]);
349 memcpy(data_copy
.get(), data
, data_length
);
350 plugin_stream_
->DidReceiveData(data_copy
.get(), data_length
, offset
);
352 plugin_stream_
->DidReceiveData(data
, data_length
, offset
);
354 // DANGER: this instance may be deleted at this point.
358 void PluginURLFetcher::OnCompletedRequest(
360 bool was_ignored_by_handler
,
361 bool stale_copy_in_cache
,
362 const std::string
& security_info
,
363 const base::TimeTicks
& completion_time
,
364 int64 total_transfer_size
) {
368 if (multipart_delegate_
) {
369 multipart_delegate_
->OnCompletedRequest();
370 multipart_delegate_
.reset();
373 if (error_code
== net::OK
) {
374 plugin_stream_
->DidFinishLoading(resource_id_
);
376 plugin_stream_
->DidFail(resource_id_
);
380 } // namespace content