OLD | NEW |
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 #include "content/browser/loader/mime_sniffing_resource_handler.h" | 5 #include "content/browser/loader/mime_sniffing_resource_handler.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 is_main_frame, // is_main_frame | 335 is_main_frame, // is_main_frame |
336 false, // parent_is_main_frame | 336 false, // parent_is_main_frame |
337 false, // allow_download | 337 false, // allow_download |
338 true, // is_async | 338 true, // is_async |
339 false); // is_using_lofi | 339 false); // is_using_lofi |
340 | 340 |
341 std::unique_ptr<ResourceHandler> mime_sniffing_handler( | 341 std::unique_ptr<ResourceHandler> mime_sniffing_handler( |
342 new MimeSniffingResourceHandler( | 342 new MimeSniffingResourceHandler( |
343 std::unique_ptr<ResourceHandler>( | 343 std::unique_ptr<ResourceHandler>( |
344 new TestResourceHandler(false, false, false, false, false)), | 344 new TestResourceHandler(false, false, false, false, false)), |
345 nullptr, nullptr, nullptr, request)); | 345 nullptr, nullptr, nullptr, request, |
| 346 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
346 | 347 |
347 bool defer = false; | 348 bool defer = false; |
348 mime_sniffing_handler->OnWillStart(request->url(), &defer); | 349 mime_sniffing_handler->OnWillStart(request->url(), &defer); |
349 content::RunAllPendingInMessageLoop(); | 350 content::RunAllPendingInMessageLoop(); |
350 | 351 |
351 std::string accept_header; | 352 std::string accept_header; |
352 request->extra_request_headers().GetHeader("Accept", &accept_header); | 353 request->extra_request_headers().GetHeader("Accept", &accept_header); |
353 return accept_header; | 354 return accept_header; |
354 } | 355 } |
355 | 356 |
(...skipping 20 matching lines...) Expand all Loading... |
376 TestResourceDispatcherHostDelegate host_delegate(must_download); | 377 TestResourceDispatcherHostDelegate host_delegate(must_download); |
377 host.SetDelegate(&host_delegate); | 378 host.SetDelegate(&host_delegate); |
378 | 379 |
379 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 380 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
380 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 381 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
381 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), | 382 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
382 nullptr)); | 383 nullptr)); |
383 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | 384 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( |
384 std::unique_ptr<ResourceHandler>( | 385 std::unique_ptr<ResourceHandler>( |
385 new TestResourceHandler(false, false, false, false, false)), | 386 new TestResourceHandler(false, false, false, false, false)), |
386 &host, &plugin_service, intercepting_handler.get(), request.get())); | 387 &host, &plugin_service, intercepting_handler.get(), request.get(), |
| 388 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
387 | 389 |
388 TestResourceController resource_controller; | 390 TestResourceController resource_controller; |
389 mime_handler->SetController(&resource_controller); | 391 mime_handler->SetController(&resource_controller); |
390 | 392 |
391 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 393 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
392 // The MIME type isn't important but it shouldn't be empty. | 394 // The MIME type isn't important but it shouldn't be empty. |
393 response->head.mime_type = "application/pdf"; | 395 response->head.mime_type = "application/pdf"; |
394 | 396 |
395 bool defer = false; | 397 bool defer = false; |
396 mime_handler->OnResponseStarted(response.get(), &defer); | 398 mime_handler->OnResponseStarted(response.get(), &defer); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); | 432 TestFakePluginService plugin_service(plugin_available_, plugin_stale_); |
431 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 433 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
432 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), | 434 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
433 nullptr)); | 435 nullptr)); |
434 std::unique_ptr<TestResourceHandler> scoped_test_handler = | 436 std::unique_ptr<TestResourceHandler> scoped_test_handler = |
435 std::unique_ptr<TestResourceHandler>(new TestResourceHandler( | 437 std::unique_ptr<TestResourceHandler>(new TestResourceHandler( |
436 response_started, defer_response_started, will_read, read_completed, | 438 response_started, defer_response_started, will_read, read_completed, |
437 defer_read_completed)); | 439 defer_read_completed)); |
438 TestResourceHandler* test_handler = scoped_test_handler.get(); | 440 TestResourceHandler* test_handler = scoped_test_handler.get(); |
439 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( | 441 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( |
440 new MimeSniffingResourceHandler( | 442 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, |
441 std::move(scoped_test_handler), &host, &plugin_service, | 443 &plugin_service, |
442 intercepting_handler.get(), request.get())); | 444 intercepting_handler.get(), request.get(), |
| 445 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
443 | 446 |
444 TestResourceController resource_controller; | 447 TestResourceController resource_controller; |
445 mime_sniffing_handler->SetController(&resource_controller); | 448 mime_sniffing_handler->SetController(&resource_controller); |
446 | 449 |
447 bool defer = false; | 450 bool defer = false; |
448 mime_sniffing_handler->OnWillStart(GURL(), &defer); | 451 mime_sniffing_handler->OnWillStart(GURL(), &defer); |
449 | 452 |
450 // The response should be sniffed. | 453 // The response should be sniffed. |
451 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 454 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
452 response->head.mime_type.assign("text/plain"); | 455 response->head.mime_type.assign("text/plain"); |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( | 594 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
592 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), | 595 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
593 nullptr)); | 596 nullptr)); |
594 | 597 |
595 std::unique_ptr<TestResourceHandler> scoped_test_handler = | 598 std::unique_ptr<TestResourceHandler> scoped_test_handler = |
596 std::unique_ptr<TestResourceHandler>(new TestResourceHandler( | 599 std::unique_ptr<TestResourceHandler>(new TestResourceHandler( |
597 response_started, defer_response_started, will_read, read_completed, | 600 response_started, defer_response_started, will_read, read_completed, |
598 defer_read_completed)); | 601 defer_read_completed)); |
599 TestResourceHandler* test_handler = scoped_test_handler.get(); | 602 TestResourceHandler* test_handler = scoped_test_handler.get(); |
600 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( | 603 std::unique_ptr<MimeSniffingResourceHandler> mime_sniffing_handler( |
601 new MimeSniffingResourceHandler( | 604 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, |
602 std::move(scoped_test_handler), &host, &plugin_service, | 605 &plugin_service, |
603 intercepting_handler.get(), request.get())); | 606 intercepting_handler.get(), request.get(), |
| 607 REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
604 | 608 |
605 TestResourceController resource_controller; | 609 TestResourceController resource_controller; |
606 mime_sniffing_handler->SetController(&resource_controller); | 610 mime_sniffing_handler->SetController(&resource_controller); |
607 | 611 |
608 int expected_resume_calls = 0; | 612 int expected_resume_calls = 0; |
609 | 613 |
610 bool defer = false; | 614 bool defer = false; |
611 mime_sniffing_handler->OnWillStart(GURL(), &defer); | 615 mime_sniffing_handler->OnWillStart(GURL(), &defer); |
612 | 616 |
613 // The response should not be sniffed. | 617 // The response should not be sniffed. |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
982 | 986 |
983 TestFakePluginService plugin_service(false, false); | 987 TestFakePluginService plugin_service(false, false); |
984 std::unique_ptr<ResourceHandler> intercepting_handler( | 988 std::unique_ptr<ResourceHandler> intercepting_handler( |
985 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), | 989 new InterceptingResourceHandler(std::unique_ptr<ResourceHandler>(), |
986 nullptr)); | 990 nullptr)); |
987 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( | 991 std::unique_ptr<ResourceHandler> mime_handler(new MimeSniffingResourceHandler( |
988 std::unique_ptr<ResourceHandler>( | 992 std::unique_ptr<ResourceHandler>( |
989 new TestResourceHandler(true, false, true, true, false)), | 993 new TestResourceHandler(true, false, true, true, false)), |
990 &host, &plugin_service, | 994 &host, &plugin_service, |
991 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), | 995 static_cast<InterceptingResourceHandler*>(intercepting_handler.get()), |
992 request.get())); | 996 request.get(), REQUEST_CONTEXT_TYPE_UNSPECIFIED)); |
993 | 997 |
994 TestResourceController resource_controller; | 998 TestResourceController resource_controller; |
995 mime_handler->SetController(&resource_controller); | 999 mime_handler->SetController(&resource_controller); |
996 | 1000 |
997 // Simulate a 304 response. | 1001 // Simulate a 304 response. |
998 scoped_refptr<ResourceResponse> response(new ResourceResponse); | 1002 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
999 // The MIME type isn't important but it shouldn't be empty. | 1003 // The MIME type isn't important but it shouldn't be empty. |
1000 response->head.mime_type = "application/pdf"; | 1004 response->head.mime_type = "application/pdf"; |
1001 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); | 1005 response->head.headers = new net::HttpResponseHeaders("HTTP/1.x 304 OK"); |
1002 | 1006 |
1003 // The response is received. No new ResourceHandler should be created to | 1007 // The response is received. No new ResourceHandler should be created to |
1004 // handle the download. | 1008 // handle the download. |
1005 bool defer = false; | 1009 bool defer = false; |
1006 mime_handler->OnResponseStarted(response.get(), &defer); | 1010 mime_handler->OnResponseStarted(response.get(), &defer); |
1007 EXPECT_FALSE(defer); | 1011 EXPECT_FALSE(defer); |
1008 EXPECT_FALSE(host.new_resource_handler()); | 1012 EXPECT_FALSE(host.new_resource_handler()); |
1009 | 1013 |
1010 content::RunAllPendingInMessageLoop(); | 1014 content::RunAllPendingInMessageLoop(); |
1011 } | 1015 } |
1012 | 1016 |
| 1017 TEST_F(MimeSniffingResourceHandlerTest, FetchShouldDisableMimeSniffing) { |
| 1018 net::URLRequestContext context; |
| 1019 std::unique_ptr<net::URLRequest> request(context.CreateRequest( |
| 1020 GURL("http://www.google.com"), net::DEFAULT_PRIORITY, nullptr)); |
| 1021 ResourceRequestInfo::AllocateForTesting(request.get(), |
| 1022 RESOURCE_TYPE_MAIN_FRAME, |
| 1023 nullptr, // context |
| 1024 0, // render_process_id |
| 1025 0, // render_view_id |
| 1026 0, // render_frame_id |
| 1027 true, // is_main_frame |
| 1028 false, // parent_is_main_frame |
| 1029 false, // allow_download |
| 1030 true, // is_async |
| 1031 false); // is_using_lofi |
| 1032 |
| 1033 TestResourceDispatcherHost host(false); |
| 1034 |
| 1035 TestFakePluginService plugin_service(false, false); |
| 1036 std::unique_ptr<InterceptingResourceHandler> intercepting_handler( |
| 1037 new InterceptingResourceHandler(nullptr, nullptr)); |
| 1038 |
| 1039 std::unique_ptr<TestResourceHandler> scoped_test_handler( |
| 1040 new TestResourceHandler(false, // response_started |
| 1041 false, // defer_response_started |
| 1042 true, // will_read, |
| 1043 true, // read_completed, |
| 1044 false)); // defer_read_completed |
| 1045 std::unique_ptr<ResourceHandler> mime_sniffing_handler( |
| 1046 new MimeSniffingResourceHandler(std::move(scoped_test_handler), &host, |
| 1047 &plugin_service, |
| 1048 intercepting_handler.get(), request.get(), |
| 1049 REQUEST_CONTEXT_TYPE_FETCH)); |
| 1050 |
| 1051 TestResourceController resource_controller; |
| 1052 mime_sniffing_handler->SetController(&resource_controller); |
| 1053 |
| 1054 bool defer = false; |
| 1055 mime_sniffing_handler->OnWillStart(GURL(), &defer); |
| 1056 ASSERT_FALSE(defer); |
| 1057 |
| 1058 scoped_refptr<ResourceResponse> response(new ResourceResponse); |
| 1059 response->head.mime_type = "text/plain"; |
| 1060 |
| 1061 // |mime_sniffing_handler->OnResponseStarted| should return false because |
| 1062 // mime sniffing is disabled and the wrapped resource handler returns false |
| 1063 // on OnResponseStarted. |
| 1064 EXPECT_FALSE( |
| 1065 mime_sniffing_handler->OnResponseStarted(response.get(), &defer)); |
| 1066 |
| 1067 // Process all messages to ensure proper test teardown. |
| 1068 content::RunAllPendingInMessageLoop(); |
| 1069 } |
| 1070 |
1013 } // namespace content | 1071 } // namespace content |
OLD | NEW |