| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 5 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/process_util.h" | 13 #include "base/process_util.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/string_split.h" | 15 #include "base/string_split.h" |
| 16 #include "content/browser/browser_thread_impl.h" | 16 #include "content/browser/browser_thread_impl.h" |
| 17 #include "content/browser/child_process_security_policy_impl.h" | 17 #include "content/browser/child_process_security_policy_impl.h" |
| 18 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 18 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
| 19 #include "content/browser/renderer_host/resource_message_filter.h" | 19 #include "content/browser/renderer_host/resource_message_filter.h" |
| 20 #include "content/common/child_process_host_impl.h" | 20 #include "content/common/child_process_host_impl.h" |
| 21 #include "content/common/resource_messages.h" | 21 #include "content/common/resource_messages.h" |
| 22 #include "content/common/view_messages.h" | 22 #include "content/common/view_messages.h" |
| 23 #include "content/public/browser/global_request_id.h" | 23 #include "content/public/browser/global_request_id.h" |
| 24 #include "content/public/browser/resource_context.h" | 24 #include "content/public/browser/resource_context.h" |
| 25 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 25 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 26 #include "content/public/browser/resource_throttle.h" | 26 #include "content/public/browser/resource_throttle.h" |
| 27 #include "content/public/common/resource_response.h" | 27 #include "content/public/common/resource_response.h" |
| 28 #include "content/public/test/test_browser_context.h" | 28 #include "content/public/test/test_browser_context.h" |
| 29 #include "content/test/test_content_browser_client.h" |
| 29 #include "net/base/net_errors.h" | 30 #include "net/base/net_errors.h" |
| 30 #include "net/base/upload_data.h" | 31 #include "net/base/upload_data.h" |
| 31 #include "net/http/http_util.h" | 32 #include "net/http/http_util.h" |
| 32 #include "net/url_request/url_request.h" | 33 #include "net/url_request/url_request.h" |
| 33 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
| 34 #include "net/url_request/url_request_job.h" | 35 #include "net/url_request/url_request_job.h" |
| 35 #include "net/url_request/url_request_simple_job.h" | 36 #include "net/url_request/url_request_simple_job.h" |
| 36 #include "net/url_request/url_request_test_job.h" | 37 #include "net/url_request/url_request_test_job.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "webkit/appcache/appcache_interfaces.h" | 39 #include "webkit/appcache/appcache_interfaces.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 void GetClassifiedMessages(ClassifiedMessages* msgs); | 127 void GetClassifiedMessages(ClassifiedMessages* msgs); |
| 127 | 128 |
| 128 private: | 129 private: |
| 129 std::vector<IPC::Message> messages_; | 130 std::vector<IPC::Message> messages_; |
| 130 }; | 131 }; |
| 131 | 132 |
| 132 // This is very inefficient as a result of repeatedly extracting the ID, use | 133 // This is very inefficient as a result of repeatedly extracting the ID, use |
| 133 // only for tests! | 134 // only for tests! |
| 134 void ResourceIPCAccumulator::GetClassifiedMessages(ClassifiedMessages* msgs) { | 135 void ResourceIPCAccumulator::GetClassifiedMessages(ClassifiedMessages* msgs) { |
| 135 while (!messages_.empty()) { | 136 while (!messages_.empty()) { |
| 136 std::vector<IPC::Message> cur_requests; | 137 // Ignore unknown message types as it is valid for code to generated other |
| 137 cur_requests.push_back(messages_[0]); | 138 // IPCs as side-effects that we are not testing here. |
| 138 int cur_id = RequestIDForMessage(messages_[0]); | 139 int cur_id = RequestIDForMessage(messages_[0]); |
| 139 | 140 if (cur_id != -1) { |
| 140 // find all other messages with this ID | 141 std::vector<IPC::Message> cur_requests; |
| 141 for (int i = 1; i < static_cast<int>(messages_.size()); i++) { | 142 cur_requests.push_back(messages_[0]); |
| 142 int id = RequestIDForMessage(messages_[i]); | 143 // find all other messages with this ID |
| 143 if (id == cur_id) { | 144 for (int i = 1; i < static_cast<int>(messages_.size()); i++) { |
| 144 cur_requests.push_back(messages_[i]); | 145 int id = RequestIDForMessage(messages_[i]); |
| 145 messages_.erase(messages_.begin() + i); | 146 if (id == cur_id) { |
| 146 i--; | 147 cur_requests.push_back(messages_[i]); |
| 148 messages_.erase(messages_.begin() + i); |
| 149 i--; |
| 150 } |
| 147 } | 151 } |
| 152 msgs->push_back(cur_requests); |
| 148 } | 153 } |
| 149 messages_.erase(messages_.begin()); | 154 messages_.erase(messages_.begin()); |
| 150 msgs->push_back(cur_requests); | |
| 151 } | 155 } |
| 152 } | 156 } |
| 153 | 157 |
| 154 class MockURLRequestContextSelector | 158 class MockURLRequestContextSelector |
| 155 : public ResourceMessageFilter::URLRequestContextSelector { | 159 : public ResourceMessageFilter::URLRequestContextSelector { |
| 156 public: | 160 public: |
| 157 explicit MockURLRequestContextSelector( | 161 explicit MockURLRequestContextSelector( |
| 158 net::URLRequestContext* request_context) | 162 net::URLRequestContext* request_context) |
| 159 : request_context_(request_context) {} | 163 : request_context_(request_context) {} |
| 160 | 164 |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 } | 353 } |
| 350 | 354 |
| 351 ~TestUserData() { | 355 ~TestUserData() { |
| 352 *was_deleted_ = true; | 356 *was_deleted_ = true; |
| 353 } | 357 } |
| 354 | 358 |
| 355 private: | 359 private: |
| 356 bool* was_deleted_; | 360 bool* was_deleted_; |
| 357 }; | 361 }; |
| 358 | 362 |
| 363 class TransfersAllNavigationsContentBrowserClient |
| 364 : public TestContentBrowserClient { |
| 365 public: |
| 366 virtual bool ShouldSwapProcessesForRedirect(ResourceContext* resource_context, |
| 367 const GURL& current_url, |
| 368 const GURL& new_url) { |
| 369 return true; |
| 370 } |
| 371 }; |
| 372 |
| 359 enum { | 373 enum { |
| 360 DEFER_NONE = 0, | 374 DEFER_NONE = 0, |
| 361 DEFER_STARTING_REQUEST = 1 << 0, | 375 DEFER_STARTING_REQUEST = 1 << 0, |
| 362 DEFER_PROCESSING_RESPONSE = 1 << 1, | 376 DEFER_PROCESSING_RESPONSE = 1 << 1, |
| 363 }; | 377 }; |
| 364 | 378 |
| 365 class GenericResourceThrottle : public content::ResourceThrottle { | 379 class GenericResourceThrottle : public content::ResourceThrottle { |
| 366 public: | 380 public: |
| 367 GenericResourceThrottle(int defer_flags) : defer_flags_(defer_flags) { | 381 GenericResourceThrottle(int defer_flags) : defer_flags_(defer_flags) { |
| 368 } | 382 } |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 // Generates a request using the given filter. This will probably be a | 510 // Generates a request using the given filter. This will probably be a |
| 497 // ForwardingFilter. | 511 // ForwardingFilter. |
| 498 void MakeTestRequest(ResourceMessageFilter* filter, | 512 void MakeTestRequest(ResourceMessageFilter* filter, |
| 499 int render_view_id, | 513 int render_view_id, |
| 500 int request_id, | 514 int request_id, |
| 501 const GURL& url); | 515 const GURL& url); |
| 502 | 516 |
| 503 void CancelRequest(int request_id); | 517 void CancelRequest(int request_id); |
| 504 | 518 |
| 505 void CompleteStartRequest(int request_id); | 519 void CompleteStartRequest(int request_id); |
| 520 void CompleteStartRequest(ResourceMessageFilter* filter, int request_id); |
| 506 | 521 |
| 507 void EnsureSchemeIsAllowed(const std::string& scheme) { | 522 void EnsureSchemeIsAllowed(const std::string& scheme) { |
| 508 ChildProcessSecurityPolicyImpl* policy = | 523 ChildProcessSecurityPolicyImpl* policy = |
| 509 ChildProcessSecurityPolicyImpl::GetInstance(); | 524 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 510 if (!policy->IsWebSafeScheme(scheme)) | 525 if (!policy->IsWebSafeScheme(scheme)) |
| 511 policy->RegisterWebSafeScheme(scheme); | 526 policy->RegisterWebSafeScheme(scheme); |
| 512 } | 527 } |
| 513 | 528 |
| 514 void EnsureTestSchemeIsAllowed() { | 529 void EnsureTestSchemeIsAllowed() { |
| 515 EnsureSchemeIsAllowed("test"); | 530 EnsureSchemeIsAllowed("test"); |
| 516 } | 531 } |
| 517 | 532 |
| 518 // Sets a particular response for any request from now on. To switch back to | 533 // Sets a particular response for any request from now on. To switch back to |
| 519 // the default bahavior, pass an empty |headers|. |headers| should be raw- | 534 // the default bahavior, pass an empty |headers|. |headers| should be raw- |
| 520 // formatted (NULLs instead of EOLs). | 535 // formatted (NULLs instead of EOLs). |
| 521 void SetResponse(const std::string& headers, const std::string& data) { | 536 void SetResponse(const std::string& headers, const std::string& data) { |
| 522 response_headers_ = headers; | 537 response_headers_ = net::HttpUtil::AssembleRawHeaders(headers.data(), |
| 538 headers.size()); |
| 523 response_data_ = data; | 539 response_data_ = data; |
| 524 } | 540 } |
| 541 void SetResponse(const std::string& headers) { |
| 542 SetResponse(headers, std::string()); |
| 543 } |
| 525 | 544 |
| 526 // Sets a particular resource type for any request from now on. | 545 // Sets a particular resource type for any request from now on. |
| 527 void SetResourceType(ResourceType::Type type) { | 546 void SetResourceType(ResourceType::Type type) { |
| 528 resource_type_ = type; | 547 resource_type_ = type; |
| 529 } | 548 } |
| 530 | 549 |
| 531 void SendDataReceivedACKs(bool send_acks) { | 550 void SendDataReceivedACKs(bool send_acks) { |
| 532 send_data_received_acks_ = send_acks; | 551 send_data_received_acks_ = send_acks; |
| 533 } | 552 } |
| 534 | 553 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 bool msg_was_ok; | 653 bool msg_was_ok; |
| 635 host_.OnMessageReceived(msg, filter, &msg_was_ok); | 654 host_.OnMessageReceived(msg, filter, &msg_was_ok); |
| 636 KickOffRequest(); | 655 KickOffRequest(); |
| 637 } | 656 } |
| 638 | 657 |
| 639 void ResourceDispatcherHostTest::CancelRequest(int request_id) { | 658 void ResourceDispatcherHostTest::CancelRequest(int request_id) { |
| 640 host_.CancelRequest(filter_->child_id(), request_id, false); | 659 host_.CancelRequest(filter_->child_id(), request_id, false); |
| 641 } | 660 } |
| 642 | 661 |
| 643 void ResourceDispatcherHostTest::CompleteStartRequest(int request_id) { | 662 void ResourceDispatcherHostTest::CompleteStartRequest(int request_id) { |
| 644 GlobalRequestID gid(filter_->child_id(), request_id); | 663 CompleteStartRequest(filter_, request_id); |
| 664 } |
| 665 |
| 666 void ResourceDispatcherHostTest::CompleteStartRequest( |
| 667 ResourceMessageFilter* filter, |
| 668 int request_id) { |
| 669 GlobalRequestID gid(filter->child_id(), request_id); |
| 645 net::URLRequest* req = host_.GetURLRequest(gid); | 670 net::URLRequest* req = host_.GetURLRequest(gid); |
| 646 EXPECT_TRUE(req); | 671 EXPECT_TRUE(req); |
| 647 if (req) | 672 if (req) |
| 648 URLRequestTestDelayedStartJob::CompleteStart(req); | 673 URLRequestTestDelayedStartJob::CompleteStart(req); |
| 649 } | 674 } |
| 650 | 675 |
| 651 void CheckSuccessfulRequest(const std::vector<IPC::Message>& messages, | 676 void CheckSuccessfulRequest(const std::vector<IPC::Message>& messages, |
| 652 const std::string& reference_data) { | 677 const std::string& reference_data) { |
| 653 // A successful request will have received 4 messages: | 678 // A successful request will have received 4 messages: |
| 654 // ReceivedResponse (indicates headers received) | 679 // ReceivedResponse (indicates headers received) |
| (...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1191 CheckSuccessfulRequest(msgs[kMaxRequests + 2], | 1216 CheckSuccessfulRequest(msgs[kMaxRequests + 2], |
| 1192 net::URLRequestTestJob::test_data_2()); | 1217 net::URLRequestTestJob::test_data_2()); |
| 1193 CheckSuccessfulRequest(msgs[kMaxRequests + 3], | 1218 CheckSuccessfulRequest(msgs[kMaxRequests + 3], |
| 1194 net::URLRequestTestJob::test_data_2()); | 1219 net::URLRequestTestJob::test_data_2()); |
| 1195 } | 1220 } |
| 1196 | 1221 |
| 1197 // Tests that we sniff the mime type for a simple request. | 1222 // Tests that we sniff the mime type for a simple request. |
| 1198 TEST_F(ResourceDispatcherHostTest, MimeSniffed) { | 1223 TEST_F(ResourceDispatcherHostTest, MimeSniffed) { |
| 1199 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1224 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1200 | 1225 |
| 1201 std::string response("HTTP/1.1 200 OK\n\n"); | 1226 std::string raw_headers("HTTP/1.1 200 OK\n\n"); |
| 1202 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1203 response.size())); | |
| 1204 std::string response_data("<html><title>Test One</title></html>"); | 1227 std::string response_data("<html><title>Test One</title></html>"); |
| 1205 SetResponse(raw_headers, response_data); | 1228 SetResponse(raw_headers, response_data); |
| 1206 | 1229 |
| 1207 HandleScheme("http"); | 1230 HandleScheme("http"); |
| 1208 MakeTestRequest(0, 1, GURL("http:bla")); | 1231 MakeTestRequest(0, 1, GURL("http:bla")); |
| 1209 | 1232 |
| 1210 // Flush all pending requests. | 1233 // Flush all pending requests. |
| 1211 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1234 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1212 | 1235 |
| 1213 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1236 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1214 | 1237 |
| 1215 // Sorts out all the messages we saw by request. | 1238 // Sorts out all the messages we saw by request. |
| 1216 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1239 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 1217 accum_.GetClassifiedMessages(&msgs); | 1240 accum_.GetClassifiedMessages(&msgs); |
| 1218 ASSERT_EQ(1U, msgs.size()); | 1241 ASSERT_EQ(1U, msgs.size()); |
| 1219 | 1242 |
| 1220 content::ResourceResponseHead response_head; | 1243 content::ResourceResponseHead response_head; |
| 1221 GetResponseHead(msgs[0], &response_head); | 1244 GetResponseHead(msgs[0], &response_head); |
| 1222 ASSERT_EQ("text/html", response_head.mime_type); | 1245 ASSERT_EQ("text/html", response_head.mime_type); |
| 1223 } | 1246 } |
| 1224 | 1247 |
| 1225 // Tests that we don't sniff the mime type when the server provides one. | 1248 // Tests that we don't sniff the mime type when the server provides one. |
| 1226 TEST_F(ResourceDispatcherHostTest, MimeNotSniffed) { | 1249 TEST_F(ResourceDispatcherHostTest, MimeNotSniffed) { |
| 1227 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1250 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1228 | 1251 |
| 1229 std::string response("HTTP/1.1 200 OK\n" | 1252 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 1230 "Content-type: image/jpeg\n\n"); | 1253 "Content-type: image/jpeg\n\n"); |
| 1231 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1232 response.size())); | |
| 1233 std::string response_data("<html><title>Test One</title></html>"); | 1254 std::string response_data("<html><title>Test One</title></html>"); |
| 1234 SetResponse(raw_headers, response_data); | 1255 SetResponse(raw_headers, response_data); |
| 1235 | 1256 |
| 1236 HandleScheme("http"); | 1257 HandleScheme("http"); |
| 1237 MakeTestRequest(0, 1, GURL("http:bla")); | 1258 MakeTestRequest(0, 1, GURL("http:bla")); |
| 1238 | 1259 |
| 1239 // Flush all pending requests. | 1260 // Flush all pending requests. |
| 1240 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1261 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1241 | 1262 |
| 1242 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1263 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1243 | 1264 |
| 1244 // Sorts out all the messages we saw by request. | 1265 // Sorts out all the messages we saw by request. |
| 1245 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1266 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 1246 accum_.GetClassifiedMessages(&msgs); | 1267 accum_.GetClassifiedMessages(&msgs); |
| 1247 ASSERT_EQ(1U, msgs.size()); | 1268 ASSERT_EQ(1U, msgs.size()); |
| 1248 | 1269 |
| 1249 content::ResourceResponseHead response_head; | 1270 content::ResourceResponseHead response_head; |
| 1250 GetResponseHead(msgs[0], &response_head); | 1271 GetResponseHead(msgs[0], &response_head); |
| 1251 ASSERT_EQ("image/jpeg", response_head.mime_type); | 1272 ASSERT_EQ("image/jpeg", response_head.mime_type); |
| 1252 } | 1273 } |
| 1253 | 1274 |
| 1254 // Tests that we don't sniff the mime type when there is no message body. | 1275 // Tests that we don't sniff the mime type when there is no message body. |
| 1255 TEST_F(ResourceDispatcherHostTest, MimeNotSniffed2) { | 1276 TEST_F(ResourceDispatcherHostTest, MimeNotSniffed2) { |
| 1256 | 1277 |
| 1257 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1278 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1258 | 1279 |
| 1259 std::string response("HTTP/1.1 304 Not Modified\n\n"); | 1280 SetResponse("HTTP/1.1 304 Not Modified\n\n"); |
| 1260 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1261 response.size())); | |
| 1262 std::string response_data; | |
| 1263 SetResponse(raw_headers, response_data); | |
| 1264 | 1281 |
| 1265 HandleScheme("http"); | 1282 HandleScheme("http"); |
| 1266 MakeTestRequest(0, 1, GURL("http:bla")); | 1283 MakeTestRequest(0, 1, GURL("http:bla")); |
| 1267 | 1284 |
| 1268 // Flush all pending requests. | 1285 // Flush all pending requests. |
| 1269 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1286 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1270 | 1287 |
| 1271 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1288 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1272 | 1289 |
| 1273 // Sorts out all the messages we saw by request. | 1290 // Sorts out all the messages we saw by request. |
| 1274 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1291 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 1275 accum_.GetClassifiedMessages(&msgs); | 1292 accum_.GetClassifiedMessages(&msgs); |
| 1276 ASSERT_EQ(1U, msgs.size()); | 1293 ASSERT_EQ(1U, msgs.size()); |
| 1277 | 1294 |
| 1278 content::ResourceResponseHead response_head; | 1295 content::ResourceResponseHead response_head; |
| 1279 GetResponseHead(msgs[0], &response_head); | 1296 GetResponseHead(msgs[0], &response_head); |
| 1280 ASSERT_EQ("", response_head.mime_type); | 1297 ASSERT_EQ("", response_head.mime_type); |
| 1281 } | 1298 } |
| 1282 | 1299 |
| 1283 TEST_F(ResourceDispatcherHostTest, MimeSniff204) { | 1300 TEST_F(ResourceDispatcherHostTest, MimeSniff204) { |
| 1284 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1301 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1285 | 1302 |
| 1286 std::string response("HTTP/1.1 204 No Content\n\n"); | 1303 SetResponse("HTTP/1.1 204 No Content\n\n"); |
| 1287 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1288 response.size())); | |
| 1289 std::string response_data; | |
| 1290 SetResponse(raw_headers, response_data); | |
| 1291 | 1304 |
| 1292 HandleScheme("http"); | 1305 HandleScheme("http"); |
| 1293 MakeTestRequest(0, 1, GURL("http:bla")); | 1306 MakeTestRequest(0, 1, GURL("http:bla")); |
| 1294 | 1307 |
| 1295 // Flush all pending requests. | 1308 // Flush all pending requests. |
| 1296 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1309 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1297 | 1310 |
| 1298 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1311 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1299 | 1312 |
| 1300 // Sorts out all the messages we saw by request. | 1313 // Sorts out all the messages we saw by request. |
| 1301 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1314 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 1302 accum_.GetClassifiedMessages(&msgs); | 1315 accum_.GetClassifiedMessages(&msgs); |
| 1303 ASSERT_EQ(1U, msgs.size()); | 1316 ASSERT_EQ(1U, msgs.size()); |
| 1304 | 1317 |
| 1305 content::ResourceResponseHead response_head; | 1318 content::ResourceResponseHead response_head; |
| 1306 GetResponseHead(msgs[0], &response_head); | 1319 GetResponseHead(msgs[0], &response_head); |
| 1307 ASSERT_EQ("text/plain", response_head.mime_type); | 1320 ASSERT_EQ("text/plain", response_head.mime_type); |
| 1308 } | 1321 } |
| 1309 | 1322 |
| 1310 TEST_F(ResourceDispatcherHostTest, MimeSniffEmpty) { | 1323 TEST_F(ResourceDispatcherHostTest, MimeSniffEmpty) { |
| 1311 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1324 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1312 | 1325 |
| 1313 std::string response("HTTP/1.1 200 OK\n\n"); | 1326 SetResponse("HTTP/1.1 200 OK\n\n"); |
| 1314 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1315 response.size())); | |
| 1316 std::string response_data; | |
| 1317 SetResponse(raw_headers, response_data); | |
| 1318 | 1327 |
| 1319 HandleScheme("http"); | 1328 HandleScheme("http"); |
| 1320 MakeTestRequest(0, 1, GURL("http:bla")); | 1329 MakeTestRequest(0, 1, GURL("http:bla")); |
| 1321 | 1330 |
| 1322 // Flush all pending requests. | 1331 // Flush all pending requests. |
| 1323 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1332 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1324 | 1333 |
| 1325 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1334 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1326 | 1335 |
| 1327 // Sorts out all the messages we saw by request. | 1336 // Sorts out all the messages we saw by request. |
| 1328 ResourceIPCAccumulator::ClassifiedMessages msgs; | 1337 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 1329 accum_.GetClassifiedMessages(&msgs); | 1338 accum_.GetClassifiedMessages(&msgs); |
| 1330 ASSERT_EQ(1U, msgs.size()); | 1339 ASSERT_EQ(1U, msgs.size()); |
| 1331 | 1340 |
| 1332 content::ResourceResponseHead response_head; | 1341 content::ResourceResponseHead response_head; |
| 1333 GetResponseHead(msgs[0], &response_head); | 1342 GetResponseHead(msgs[0], &response_head); |
| 1334 ASSERT_EQ("text/plain", response_head.mime_type); | 1343 ASSERT_EQ("text/plain", response_head.mime_type); |
| 1335 } | 1344 } |
| 1336 | 1345 |
| 1337 // Tests for crbug.com/31266 (Non-2xx + application/octet-stream). | 1346 // Tests for crbug.com/31266 (Non-2xx + application/octet-stream). |
| 1338 TEST_F(ResourceDispatcherHostTest, ForbiddenDownload) { | 1347 TEST_F(ResourceDispatcherHostTest, ForbiddenDownload) { |
| 1339 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1348 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1340 | 1349 |
| 1341 std::string response("HTTP/1.1 403 Forbidden\n" | 1350 std::string raw_headers("HTTP/1.1 403 Forbidden\n" |
| 1342 "Content-disposition: attachment; filename=blah\n" | 1351 "Content-disposition: attachment; filename=blah\n" |
| 1343 "Content-type: application/octet-stream\n\n"); | 1352 "Content-type: application/octet-stream\n\n"); |
| 1344 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1345 response.size())); | |
| 1346 std::string response_data("<html><title>Test One</title></html>"); | 1353 std::string response_data("<html><title>Test One</title></html>"); |
| 1347 SetResponse(raw_headers, response_data); | 1354 SetResponse(raw_headers, response_data); |
| 1348 | 1355 |
| 1349 // Only MAIN_FRAMEs can trigger a download. | 1356 // Only MAIN_FRAMEs can trigger a download. |
| 1350 SetResourceType(ResourceType::MAIN_FRAME); | 1357 SetResourceType(ResourceType::MAIN_FRAME); |
| 1351 | 1358 |
| 1352 HandleScheme("http"); | 1359 HandleScheme("http"); |
| 1353 MakeTestRequest(0, 1, GURL("http:bla")); | 1360 MakeTestRequest(0, 1, GURL("http:bla")); |
| 1354 | 1361 |
| 1355 // Flush all pending requests. | 1362 // Flush all pending requests. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1381 | 1388 |
| 1382 // Test for http://crbug.com/76202 . We don't want to destroy a | 1389 // Test for http://crbug.com/76202 . We don't want to destroy a |
| 1383 // download request prematurely when processing a cancellation from | 1390 // download request prematurely when processing a cancellation from |
| 1384 // the renderer. | 1391 // the renderer. |
| 1385 TEST_F(ResourceDispatcherHostTest, IgnoreCancelForDownloads) { | 1392 TEST_F(ResourceDispatcherHostTest, IgnoreCancelForDownloads) { |
| 1386 EXPECT_EQ(0, host_.pending_requests()); | 1393 EXPECT_EQ(0, host_.pending_requests()); |
| 1387 | 1394 |
| 1388 int render_view_id = 0; | 1395 int render_view_id = 0; |
| 1389 int request_id = 1; | 1396 int request_id = 1; |
| 1390 | 1397 |
| 1391 std::string response("HTTP\n" | 1398 std::string raw_headers("HTTP\n" |
| 1392 "Content-disposition: attachment; filename=foo\n\n"); | 1399 "Content-disposition: attachment; filename=foo\n\n"); |
| 1393 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1394 response.size())); | |
| 1395 std::string response_data("01234567890123456789\x01foobar"); | 1400 std::string response_data("01234567890123456789\x01foobar"); |
| 1396 | 1401 |
| 1397 // Get past sniffing metrics in the BufferedResourceHandler. Note that | 1402 // Get past sniffing metrics in the BufferedResourceHandler. Note that |
| 1398 // if we don't get past the sniffing metrics, the result will be that | 1403 // if we don't get past the sniffing metrics, the result will be that |
| 1399 // the BufferedResourceHandler won't have figured out that it's a download, | 1404 // the BufferedResourceHandler won't have figured out that it's a download, |
| 1400 // won't have constructed a DownloadResourceHandler, and and the request | 1405 // won't have constructed a DownloadResourceHandler, and and the request |
| 1401 // will be successfully canceled below, failing the test. | 1406 // will be successfully canceled below, failing the test. |
| 1402 response_data.resize(1025, ' '); | 1407 response_data.resize(1025, ' '); |
| 1403 | 1408 |
| 1404 SetResponse(raw_headers, response_data); | 1409 SetResponse(raw_headers, response_data); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1424 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1429 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1425 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); | 1430 EXPECT_EQ(0, host_.GetOutstandingRequestsMemoryCost(0)); |
| 1426 } | 1431 } |
| 1427 | 1432 |
| 1428 TEST_F(ResourceDispatcherHostTest, CancelRequestsForContext) { | 1433 TEST_F(ResourceDispatcherHostTest, CancelRequestsForContext) { |
| 1429 EXPECT_EQ(0, host_.pending_requests()); | 1434 EXPECT_EQ(0, host_.pending_requests()); |
| 1430 | 1435 |
| 1431 int render_view_id = 0; | 1436 int render_view_id = 0; |
| 1432 int request_id = 1; | 1437 int request_id = 1; |
| 1433 | 1438 |
| 1434 std::string response("HTTP\n" | 1439 std::string raw_headers("HTTP\n" |
| 1435 "Content-disposition: attachment; filename=foo\n\n"); | 1440 "Content-disposition: attachment; filename=foo\n\n"); |
| 1436 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1437 response.size())); | |
| 1438 std::string response_data("01234567890123456789\x01foobar"); | 1441 std::string response_data("01234567890123456789\x01foobar"); |
| 1439 // Get past sniffing metrics. | 1442 // Get past sniffing metrics. |
| 1440 response_data.resize(1025, ' '); | 1443 response_data.resize(1025, ' '); |
| 1441 | 1444 |
| 1442 SetResponse(raw_headers, response_data); | 1445 SetResponse(raw_headers, response_data); |
| 1443 SetResourceType(ResourceType::MAIN_FRAME); | 1446 SetResourceType(ResourceType::MAIN_FRAME); |
| 1444 SetDelayedCompleteJobGeneration(true); | 1447 SetDelayedCompleteJobGeneration(true); |
| 1445 HandleScheme("http"); | 1448 HandleScheme("http"); |
| 1446 | 1449 |
| 1447 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah")); | 1450 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah")); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1469 } | 1472 } |
| 1470 | 1473 |
| 1471 // Test the cancelling of requests that are being transferred to a new renderer | 1474 // Test the cancelling of requests that are being transferred to a new renderer |
| 1472 // due to a redirection. | 1475 // due to a redirection. |
| 1473 TEST_F(ResourceDispatcherHostTest, CancelRequestsForContextTransferred) { | 1476 TEST_F(ResourceDispatcherHostTest, CancelRequestsForContextTransferred) { |
| 1474 EXPECT_EQ(0, host_.pending_requests()); | 1477 EXPECT_EQ(0, host_.pending_requests()); |
| 1475 | 1478 |
| 1476 int render_view_id = 0; | 1479 int render_view_id = 0; |
| 1477 int request_id = 1; | 1480 int request_id = 1; |
| 1478 | 1481 |
| 1479 std::string response("HTTP/1.1 200 OK\n" | 1482 std::string raw_headers("HTTP/1.1 200 OK\n" |
| 1480 "Content-Type: text/html; charset=utf-8\n\n"); | 1483 "Content-Type: text/html; charset=utf-8\n\n"); |
| 1481 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
| 1482 response.size())); | |
| 1483 std::string response_data("<html>foobar</html>"); | 1484 std::string response_data("<html>foobar</html>"); |
| 1484 | 1485 |
| 1485 SetResponse(raw_headers, response_data); | 1486 SetResponse(raw_headers, response_data); |
| 1486 SetResourceType(ResourceType::MAIN_FRAME); | 1487 SetResourceType(ResourceType::MAIN_FRAME); |
| 1487 HandleScheme("http"); | 1488 HandleScheme("http"); |
| 1488 | 1489 |
| 1489 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah")); | 1490 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah")); |
| 1490 | 1491 |
| 1491 GlobalRequestID global_request_id(filter_->child_id(), request_id); | 1492 GlobalRequestID global_request_id(filter_->child_id(), request_id); |
| 1492 host_.MarkAsTransferredNavigation(global_request_id); | 1493 host_.MarkAsTransferredNavigation(global_request_id); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1503 | 1504 |
| 1504 // Cancelling by other methods shouldn't work either. | 1505 // Cancelling by other methods shouldn't work either. |
| 1505 host_.CancelRequestsForProcess(render_view_id); | 1506 host_.CancelRequestsForProcess(render_view_id); |
| 1506 EXPECT_EQ(1, host_.pending_requests()); | 1507 EXPECT_EQ(1, host_.pending_requests()); |
| 1507 | 1508 |
| 1508 // Cancelling by context should work. | 1509 // Cancelling by context should work. |
| 1509 host_.CancelRequestsForContext(filter_->resource_context()); | 1510 host_.CancelRequestsForContext(filter_->resource_context()); |
| 1510 EXPECT_EQ(0, host_.pending_requests()); | 1511 EXPECT_EQ(0, host_.pending_requests()); |
| 1511 } | 1512 } |
| 1512 | 1513 |
| 1514 TEST_F(ResourceDispatcherHostTest, TransferNavigation) { |
| 1515 EXPECT_EQ(0, host_.pending_requests()); |
| 1516 |
| 1517 int render_view_id = 0; |
| 1518 int request_id = 1; |
| 1519 |
| 1520 // Configure initial request. |
| 1521 SetResponse("HTTP/1.1 302 Found\n" |
| 1522 "Location: http://other.com/blech\n\n"); |
| 1523 |
| 1524 SetResourceType(ResourceType::MAIN_FRAME); |
| 1525 HandleScheme("http"); |
| 1526 |
| 1527 // Temporarily replace ContentBrowserClient with one that will trigger the |
| 1528 // transfer navigation code paths. |
| 1529 ContentBrowserClient* old_client = GetContentClient()->browser(); |
| 1530 TransfersAllNavigationsContentBrowserClient new_client; |
| 1531 GetContentClient()->set_browser_for_testing(&new_client); |
| 1532 |
| 1533 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah")); |
| 1534 |
| 1535 // Restore. |
| 1536 GetContentClient()->set_browser_for_testing(old_client); |
| 1537 |
| 1538 // This second filter is used to emulate a second process. |
| 1539 scoped_refptr<ForwardingFilter> second_filter = new ForwardingFilter( |
| 1540 this, browser_context_->GetResourceContext()); |
| 1541 |
| 1542 int new_render_view_id = 1; |
| 1543 int new_request_id = 2; |
| 1544 |
| 1545 const std::string kResponseBody = "hello world"; |
| 1546 SetResponse("HTTP/1.1 200 OK\n" |
| 1547 "Content-Type: text/plain\n\n", |
| 1548 kResponseBody); |
| 1549 |
| 1550 ResourceHostMsg_Request request = |
| 1551 CreateResourceRequest("GET", ResourceType::MAIN_FRAME, |
| 1552 GURL("http://other.com/blech")); |
| 1553 request.transferred_request_child_id = filter_->child_id(); |
| 1554 request.transferred_request_request_id = request_id; |
| 1555 |
| 1556 ResourceHostMsg_RequestResource transfer_request_msg( |
| 1557 new_render_view_id, new_request_id, request); |
| 1558 bool msg_was_ok; |
| 1559 host_.OnMessageReceived(transfer_request_msg, second_filter, &msg_was_ok); |
| 1560 MessageLoop::current()->RunAllPending(); |
| 1561 |
| 1562 // Flush all the pending requests. |
| 1563 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1564 |
| 1565 // Check generated messages. |
| 1566 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 1567 accum_.GetClassifiedMessages(&msgs); |
| 1568 |
| 1569 ASSERT_EQ(1U, msgs.size()); |
| 1570 CheckSuccessfulRequest(msgs[0], kResponseBody); |
| 1571 } |
| 1572 |
| 1573 TEST_F(ResourceDispatcherHostTest, TransferNavigationAndThenRedirect) { |
| 1574 EXPECT_EQ(0, host_.pending_requests()); |
| 1575 |
| 1576 int render_view_id = 0; |
| 1577 int request_id = 1; |
| 1578 |
| 1579 // Configure initial request. |
| 1580 SetResponse("HTTP/1.1 302 Found\n" |
| 1581 "Location: http://other.com/blech\n\n"); |
| 1582 |
| 1583 SetResourceType(ResourceType::MAIN_FRAME); |
| 1584 HandleScheme("http"); |
| 1585 |
| 1586 // Temporarily replace ContentBrowserClient with one that will trigger the |
| 1587 // transfer navigation code paths. |
| 1588 ContentBrowserClient* old_client = GetContentClient()->browser(); |
| 1589 TransfersAllNavigationsContentBrowserClient new_client; |
| 1590 GetContentClient()->set_browser_for_testing(&new_client); |
| 1591 |
| 1592 MakeTestRequest(render_view_id, request_id, GURL("http://example.com/blah")); |
| 1593 |
| 1594 // Restore. |
| 1595 GetContentClient()->set_browser_for_testing(old_client); |
| 1596 |
| 1597 // This second filter is used to emulate a second process. |
| 1598 scoped_refptr<ForwardingFilter> second_filter = new ForwardingFilter( |
| 1599 this, browser_context_->GetResourceContext()); |
| 1600 |
| 1601 int new_render_view_id = 1; |
| 1602 int new_request_id = 2; |
| 1603 |
| 1604 // Delay the start of the next request so that we can setup the response for |
| 1605 // the next URL. |
| 1606 SetDelayedStartJobGeneration(true); |
| 1607 |
| 1608 SetResponse("HTTP/1.1 302 Found\n" |
| 1609 "Location: http://other.com/blerg\n\n"); |
| 1610 |
| 1611 ResourceHostMsg_Request request = |
| 1612 CreateResourceRequest("GET", ResourceType::MAIN_FRAME, |
| 1613 GURL("http://other.com/blech")); |
| 1614 request.transferred_request_child_id = filter_->child_id(); |
| 1615 request.transferred_request_request_id = request_id; |
| 1616 |
| 1617 ResourceHostMsg_RequestResource transfer_request_msg( |
| 1618 new_render_view_id, new_request_id, request); |
| 1619 bool msg_was_ok; |
| 1620 host_.OnMessageReceived(transfer_request_msg, second_filter, &msg_was_ok); |
| 1621 MessageLoop::current()->RunAllPending(); |
| 1622 |
| 1623 // Response data for "http://other.com/blerg": |
| 1624 const std::string kResponseBody = "hello world"; |
| 1625 SetResponse("HTTP/1.1 200 OK\n" |
| 1626 "Content-Type: text/plain\n\n", |
| 1627 kResponseBody); |
| 1628 |
| 1629 // OK, let the redirect happen. |
| 1630 SetDelayedStartJobGeneration(false); |
| 1631 CompleteStartRequest(second_filter, new_request_id); |
| 1632 MessageLoop::current()->RunAllPending(); |
| 1633 |
| 1634 // Flush all the pending requests. |
| 1635 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1636 |
| 1637 // Now, simulate the renderer choosing to follow the redirect. |
| 1638 ResourceHostMsg_FollowRedirect redirect_msg( |
| 1639 new_render_view_id, new_request_id, false, GURL()); |
| 1640 host_.OnMessageReceived(redirect_msg, second_filter, &msg_was_ok); |
| 1641 MessageLoop::current()->RunAllPending(); |
| 1642 |
| 1643 // Flush all the pending requests. |
| 1644 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| 1645 |
| 1646 // Check generated messages. |
| 1647 ResourceIPCAccumulator::ClassifiedMessages msgs; |
| 1648 accum_.GetClassifiedMessages(&msgs); |
| 1649 |
| 1650 ASSERT_EQ(1U, msgs.size()); |
| 1651 |
| 1652 // We should have received a redirect followed by a "normal" payload. |
| 1653 EXPECT_EQ(ResourceMsg_ReceivedRedirect::ID, msgs[0][0].type()); |
| 1654 msgs[0].erase(msgs[0].begin()); |
| 1655 CheckSuccessfulRequest(msgs[0], kResponseBody); |
| 1656 } |
| 1657 |
| 1513 TEST_F(ResourceDispatcherHostTest, UnknownURLScheme) { | 1658 TEST_F(ResourceDispatcherHostTest, UnknownURLScheme) { |
| 1514 EXPECT_EQ(0, host_.pending_requests()); | 1659 EXPECT_EQ(0, host_.pending_requests()); |
| 1515 | 1660 |
| 1516 SetResourceType(ResourceType::MAIN_FRAME); | 1661 SetResourceType(ResourceType::MAIN_FRAME); |
| 1517 HandleScheme("http"); | 1662 HandleScheme("http"); |
| 1518 | 1663 |
| 1519 MakeTestRequest(0, 1, GURL("foo://bar")); | 1664 MakeTestRequest(0, 1, GURL("foo://bar")); |
| 1520 | 1665 |
| 1521 // Flush all pending requests. | 1666 // Flush all pending requests. |
| 1522 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} | 1667 while (net::URLRequestTestJob::ProcessOnePendingMessage()) {} |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1601 } | 1746 } |
| 1602 | 1747 |
| 1603 MessageLoop::current()->RunAllPending(); | 1748 MessageLoop::current()->RunAllPending(); |
| 1604 | 1749 |
| 1605 msgs.clear(); | 1750 msgs.clear(); |
| 1606 accum_.GetClassifiedMessages(&msgs); | 1751 accum_.GetClassifiedMessages(&msgs); |
| 1607 } | 1752 } |
| 1608 } | 1753 } |
| 1609 | 1754 |
| 1610 } // namespace content | 1755 } // namespace content |
| OLD | NEW |