Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(315)

Side by Side Diff: content/child/web_url_loader_impl.cc

Issue 294193002: Set response headers for data URL. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | net/base/data_url.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // An implementation of WebURLLoader in terms of ResourceLoaderBridge. 5 // An implementation of WebURLLoader in terms of ResourceLoaderBridge.
6 6
7 #include "content/child/web_url_loader_impl.h" 7 #include "content/child/web_url_loader_impl.h"
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 13 matching lines...) Expand all
24 #include "content/common/resource_request_body.h" 24 #include "content/common/resource_request_body.h"
25 #include "content/public/child/request_peer.h" 25 #include "content/public/child/request_peer.h"
26 #include "net/base/data_url.h" 26 #include "net/base/data_url.h"
27 #include "net/base/filename_util.h" 27 #include "net/base/filename_util.h"
28 #include "net/base/load_flags.h" 28 #include "net/base/load_flags.h"
29 #include "net/base/mime_util.h" 29 #include "net/base/mime_util.h"
30 #include "net/base/net_errors.h" 30 #include "net/base/net_errors.h"
31 #include "net/http/http_response_headers.h" 31 #include "net/http/http_response_headers.h"
32 #include "net/http/http_util.h" 32 #include "net/http/http_util.h"
33 #include "net/url_request/redirect_info.h" 33 #include "net/url_request/redirect_info.h"
34 #include "net/url_request/url_request_data_job.h"
34 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" 35 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
35 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h" 36 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h"
36 #include "third_party/WebKit/public/platform/WebURL.h" 37 #include "third_party/WebKit/public/platform/WebURL.h"
37 #include "third_party/WebKit/public/platform/WebURLError.h" 38 #include "third_party/WebKit/public/platform/WebURLError.h"
38 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h" 39 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
39 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" 40 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
40 #include "third_party/WebKit/public/platform/WebURLRequest.h" 41 #include "third_party/WebKit/public/platform/WebURLRequest.h"
41 #include "third_party/WebKit/public/platform/WebURLResponse.h" 42 #include "third_party/WebKit/public/platform/WebURLResponse.h"
42 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" 43 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
43 44
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 has_accept_header_ = true; 101 has_accept_header_ = true;
101 } 102 }
102 return buffer_; 103 return buffer_;
103 } 104 }
104 105
105 private: 106 private:
106 std::string buffer_; 107 std::string buffer_;
107 bool has_accept_header_; 108 bool has_accept_header_;
108 }; 109 };
109 110
110 // Extracts the information from a data: url.
111 bool GetInfoFromDataURL(const GURL& url,
112 ResourceResponseInfo* info,
113 std::string* data,
114 int* error_code) {
115 std::string mime_type;
116 std::string charset;
117 if (net::DataURL::Parse(url, &mime_type, &charset, data)) {
118 *error_code = net::OK;
119 // Assure same time for all time fields of data: URLs.
120 Time now = Time::Now();
121 info->load_timing.request_start = TimeTicks::Now();
122 info->load_timing.request_start_time = now;
123 info->request_time = now;
124 info->response_time = now;
125 info->headers = NULL;
126 info->mime_type.swap(mime_type);
127 info->charset.swap(charset);
128 info->security_info.clear();
129 info->content_length = data->length();
130 info->encoded_data_length = 0;
131
132 return true;
133 }
134
135 *error_code = net::ERR_INVALID_URL;
136 return false;
137 }
138
139 typedef ResourceDevToolsInfo::HeadersVector HeadersVector; 111 typedef ResourceDevToolsInfo::HeadersVector HeadersVector;
140 112
141 // Converts timing data from |load_timing| to the format used by WebKit. 113 // Converts timing data from |load_timing| to the format used by WebKit.
142 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing, 114 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing,
143 WebURLLoadTiming* url_timing) { 115 WebURLLoadTiming* url_timing) {
144 DCHECK(!load_timing.request_start.is_null()); 116 DCHECK(!load_timing.request_start.is_null());
145 117
146 const TimeTicks kNullTicks; 118 const TimeTicks kNullTicks;
147 url_timing->initialize(); 119 url_timing->initialize();
148 url_timing->setRequestTime( 120 url_timing->setRequestTime(
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 case WebURLRequest::PriorityVeryLow: 161 case WebURLRequest::PriorityVeryLow:
190 return net::IDLE; 162 return net::IDLE;
191 163
192 case WebURLRequest::PriorityUnresolved: 164 case WebURLRequest::PriorityUnresolved:
193 default: 165 default:
194 NOTREACHED(); 166 NOTREACHED();
195 return net::LOW; 167 return net::LOW;
196 } 168 }
197 } 169 }
198 170
171 // Extracts info from a data scheme URL into |info| and |data|. Returns net::OK
172 // if successful. Returns a net error code otherwise. Exported only for testing.
173 int GetInfoFromDataURL(const GURL& url,
174 ResourceResponseInfo* info,
175 std::string* data) {
176 // Assure same time for all time fields of data: URLs.
177 Time now = Time::Now();
178 info->load_timing.request_start = TimeTicks::Now();
179 info->load_timing.request_start_time = now;
180 info->request_time = now;
181 info->response_time = now;
182
183 std::string mime_type;
184 std::string charset;
185 scoped_refptr<net::HttpResponseHeaders> headers(
186 new net::HttpResponseHeaders(std::string()));
187 int result = net::URLRequestDataJob::BuildResponse(
188 url, &mime_type, &charset, data, headers.get());
189 if (result != net::OK)
190 return result;
191
192 info->headers = headers;
193 info->mime_type.swap(mime_type);
194 info->charset.swap(charset);
195 info->security_info.clear();
196 info->content_length = data->length();
197 info->encoded_data_length = 0;
198
199 return net::OK;
200 }
201
199 } // namespace 202 } // namespace
200 203
201 // WebURLLoaderImpl::Context -------------------------------------------------- 204 // WebURLLoaderImpl::Context --------------------------------------------------
202 205
203 // This inner class exists since the WebURLLoader may be deleted while inside a 206 // This inner class exists since the WebURLLoader may be deleted while inside a
204 // call to WebURLLoaderClient. Refcounting is to keep the context from being 207 // call to WebURLLoaderClient. Refcounting is to keep the context from being
205 // deleted if it may have work to do after calling into the client. 208 // deleted if it may have work to do after calling into the client.
206 class WebURLLoaderImpl::Context : public base::RefCounted<Context>, 209 class WebURLLoaderImpl::Context : public base::RefCounted<Context>,
207 public RequestPeer { 210 public RequestPeer {
208 public: 211 public:
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 SyncLoadResponse* sync_load_response) { 311 SyncLoadResponse* sync_load_response) {
309 DCHECK(!bridge_.get()); 312 DCHECK(!bridge_.get());
310 313
311 request_ = request; // Save the request. 314 request_ = request; // Save the request.
312 315
313 GURL url = request.url(); 316 GURL url = request.url();
314 if (CanHandleDataURLRequestLocally()) { 317 if (CanHandleDataURLRequestLocally()) {
315 if (sync_load_response) { 318 if (sync_load_response) {
316 // This is a sync load. Do the work now. 319 // This is a sync load. Do the work now.
317 sync_load_response->url = url; 320 sync_load_response->url = url;
318 std::string data; 321 sync_load_response->error_code =
319 GetInfoFromDataURL(sync_load_response->url, sync_load_response, 322 GetInfoFromDataURL(sync_load_response->url, sync_load_response,
320 &sync_load_response->data, 323 &sync_load_response->data);
321 &sync_load_response->error_code);
322 } else { 324 } else {
323 base::MessageLoop::current()->PostTask( 325 base::MessageLoop::current()->PostTask(
324 FROM_HERE, base::Bind(&Context::HandleDataURL, this)); 326 FROM_HERE, base::Bind(&Context::HandleDataURL, this));
325 } 327 }
326 return; 328 return;
327 } 329 }
328 330
329 GURL referrer_url( 331 GURL referrer_url(
330 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1()); 332 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1());
331 const std::string& method = request.httpMethod().latin1(); 333 const std::string& method = request.httpMethod().latin1();
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 std::string mime_type, unused_charset; 689 std::string mime_type, unused_charset;
688 if (net::DataURL::Parse(request_.url(), &mime_type, &unused_charset, NULL) && 690 if (net::DataURL::Parse(request_.url(), &mime_type, &unused_charset, NULL) &&
689 net::IsSupportedMimeType(mime_type)) 691 net::IsSupportedMimeType(mime_type))
690 return true; 692 return true;
691 693
692 return false; 694 return false;
693 } 695 }
694 696
695 void WebURLLoaderImpl::Context::HandleDataURL() { 697 void WebURLLoaderImpl::Context::HandleDataURL() {
696 ResourceResponseInfo info; 698 ResourceResponseInfo info;
697 int error_code;
698 std::string data; 699 std::string data;
699 700
700 if (GetInfoFromDataURL(request_.url(), &info, &data, &error_code)) { 701 int error_code = GetInfoFromDataURL(request_.url(), &info, &data);
702
703 if (error_code == net::OK) {
701 OnReceivedResponse(info); 704 OnReceivedResponse(info);
702 if (!data.empty()) 705 if (!data.empty())
703 OnReceivedData(data.data(), data.size(), 0); 706 OnReceivedData(data.data(), data.size(), 0);
704 } 707 }
705 708
706 OnCompletedRequest(error_code, false, false, info.security_info, 709 OnCompletedRequest(error_code, false, false, info.security_info,
707 base::TimeTicks::Now(), 0); 710 base::TimeTicks::Now(), 0);
708 } 711 }
709 712
710 // WebURLLoaderImpl ----------------------------------------------------------- 713 // WebURLLoaderImpl -----------------------------------------------------------
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 int intra_priority_value) { 895 int intra_priority_value) {
893 context_->DidChangePriority(new_priority, intra_priority_value); 896 context_->DidChangePriority(new_priority, intra_priority_value);
894 } 897 }
895 898
896 bool WebURLLoaderImpl::attachThreadedDataReceiver( 899 bool WebURLLoaderImpl::attachThreadedDataReceiver(
897 blink::WebThreadedDataReceiver* threaded_data_receiver) { 900 blink::WebThreadedDataReceiver* threaded_data_receiver) {
898 return context_->AttachThreadedDataReceiver(threaded_data_receiver); 901 return context_->AttachThreadedDataReceiver(threaded_data_receiver);
899 } 902 }
900 903
901 } // namespace content 904 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | net/base/data_url.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698