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 "extensions/browser/api/web_request/web_request_api_helpers.h" | 5 #include "extensions/browser/api/web_request/web_request_api_helpers.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 } | 457 } |
458 | 458 |
459 // Helper function for MergeRedirectUrlOfResponses() that allows ignoring | 459 // Helper function for MergeRedirectUrlOfResponses() that allows ignoring |
460 // all redirects but those to data:// urls and about:blank. This is important | 460 // all redirects but those to data:// urls and about:blank. This is important |
461 // to treat these URLs as "cancel urls", i.e. URLs that extensions redirect | 461 // to treat these URLs as "cancel urls", i.e. URLs that extensions redirect |
462 // to if they want to express that they want to cancel a request. This reduces | 462 // to if they want to express that they want to cancel a request. This reduces |
463 // the number of conflicts that we need to flag, as canceling is considered | 463 // the number of conflicts that we need to flag, as canceling is considered |
464 // a higher precedence operation that redirects. | 464 // a higher precedence operation that redirects. |
465 // Returns whether a redirect occurred. | 465 // Returns whether a redirect occurred. |
466 static bool MergeRedirectUrlOfResponsesHelper( | 466 static bool MergeRedirectUrlOfResponsesHelper( |
| 467 const GURL& url, |
467 const EventResponseDeltas& deltas, | 468 const EventResponseDeltas& deltas, |
468 GURL* new_url, | 469 GURL* new_url, |
469 extensions::WarningSet* conflicting_extensions, | 470 extensions::WarningSet* conflicting_extensions, |
470 const net::NetLogWithSource* net_log, | 471 const net::NetLogWithSource* net_log, |
471 bool consider_only_cancel_scheme_urls) { | 472 bool consider_only_cancel_scheme_urls) { |
| 473 // Redirecting WebSocket handshake request is prohibited. |
| 474 if (url.SchemeIsWSOrWSS()) |
| 475 return false; |
| 476 |
472 bool redirected = false; | 477 bool redirected = false; |
473 | 478 |
474 // Extension that determines the |new_url|. | 479 // Extension that determines the |new_url|. |
475 std::string winning_extension_id; | 480 std::string winning_extension_id; |
476 EventResponseDeltas::const_iterator delta; | 481 EventResponseDeltas::const_iterator delta; |
477 for (delta = deltas.begin(); delta != deltas.end(); ++delta) { | 482 for (delta = deltas.begin(); delta != deltas.end(); ++delta) { |
478 if ((*delta)->new_url.is_empty()) | 483 if ((*delta)->new_url.is_empty()) |
479 continue; | 484 continue; |
480 if (consider_only_cancel_scheme_urls && | 485 if (consider_only_cancel_scheme_urls && |
481 !(*delta)->new_url.SchemeIs(url::kDataScheme) && | 486 !(*delta)->new_url.SchemeIs(url::kDataScheme) && |
(...skipping 16 matching lines...) Expand all Loading... |
498 (*delta)->new_url, | 503 (*delta)->new_url, |
499 *new_url)); | 504 *new_url)); |
500 net_log->AddEvent( | 505 net_log->AddEvent( |
501 net::NetLogEventType::CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT, | 506 net::NetLogEventType::CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT, |
502 CreateNetLogExtensionIdCallback(delta->get())); | 507 CreateNetLogExtensionIdCallback(delta->get())); |
503 } | 508 } |
504 } | 509 } |
505 return redirected; | 510 return redirected; |
506 } | 511 } |
507 | 512 |
508 void MergeRedirectUrlOfResponses(const EventResponseDeltas& deltas, | 513 void MergeRedirectUrlOfResponses(const GURL& url, |
| 514 const EventResponseDeltas& deltas, |
509 GURL* new_url, | 515 GURL* new_url, |
510 extensions::WarningSet* conflicting_extensions, | 516 extensions::WarningSet* conflicting_extensions, |
511 const net::NetLogWithSource* net_log) { | 517 const net::NetLogWithSource* net_log) { |
512 // First handle only redirects to data:// URLs and about:blank. These are a | 518 // First handle only redirects to data:// URLs and about:blank. These are a |
513 // special case as they represent a way of cancelling a request. | 519 // special case as they represent a way of cancelling a request. |
514 if (MergeRedirectUrlOfResponsesHelper( | 520 if (MergeRedirectUrlOfResponsesHelper( |
515 deltas, new_url, conflicting_extensions, net_log, true)) { | 521 url, deltas, new_url, conflicting_extensions, net_log, true)) { |
516 // If any extension cancelled a request by redirecting to a data:// URL or | 522 // If any extension cancelled a request by redirecting to a data:// URL or |
517 // about:blank, we don't consider the other redirects. | 523 // about:blank, we don't consider the other redirects. |
518 return; | 524 return; |
519 } | 525 } |
520 | 526 |
521 // Handle all other redirects. | 527 // Handle all other redirects. |
522 MergeRedirectUrlOfResponsesHelper( | 528 MergeRedirectUrlOfResponsesHelper(url, deltas, new_url, |
523 deltas, new_url, conflicting_extensions, net_log, false); | 529 conflicting_extensions, net_log, false); |
524 } | 530 } |
525 | 531 |
526 void MergeOnBeforeRequestResponses( | 532 void MergeOnBeforeRequestResponses( |
| 533 const GURL& url, |
527 const EventResponseDeltas& deltas, | 534 const EventResponseDeltas& deltas, |
528 GURL* new_url, | 535 GURL* new_url, |
529 extensions::WarningSet* conflicting_extensions, | 536 extensions::WarningSet* conflicting_extensions, |
530 const net::NetLogWithSource* net_log) { | 537 const net::NetLogWithSource* net_log) { |
531 MergeRedirectUrlOfResponses(deltas, new_url, conflicting_extensions, net_log); | 538 MergeRedirectUrlOfResponses(url, deltas, new_url, conflicting_extensions, |
| 539 net_log); |
532 } | 540 } |
533 | 541 |
534 static bool DoesRequestCookieMatchFilter( | 542 static bool DoesRequestCookieMatchFilter( |
535 const ParsedRequestCookie& cookie, | 543 const ParsedRequestCookie& cookie, |
536 RequestCookie* filter) { | 544 RequestCookie* filter) { |
537 if (!filter) return true; | 545 if (!filter) return true; |
538 if (filter->name.get() && cookie.first != *filter->name) return false; | 546 if (filter->name.get() && cookie.first != *filter->name) return false; |
539 if (filter->value.get() && cookie.second != *filter->value) return false; | 547 if (filter->value.get() && cookie.second != *filter->value) return false; |
540 return true; | 548 return true; |
541 } | 549 } |
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1077 for (const auto& delta : deltas) { | 1085 for (const auto& delta : deltas) { |
1078 for (const auto& deleted_hdr : delta->deleted_response_headers) { | 1086 for (const auto& deleted_hdr : delta->deleted_response_headers) { |
1079 if (base::ToLowerASCII(deleted_hdr.first) == lower_key) | 1087 if (base::ToLowerASCII(deleted_hdr.first) == lower_key) |
1080 return delta->extension_id; | 1088 return delta->extension_id; |
1081 } | 1089 } |
1082 } | 1090 } |
1083 return std::string(); | 1091 return std::string(); |
1084 } | 1092 } |
1085 | 1093 |
1086 void MergeOnHeadersReceivedResponses( | 1094 void MergeOnHeadersReceivedResponses( |
| 1095 const GURL& url, |
1087 const EventResponseDeltas& deltas, | 1096 const EventResponseDeltas& deltas, |
1088 const net::HttpResponseHeaders* original_response_headers, | 1097 const net::HttpResponseHeaders* original_response_headers, |
1089 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, | 1098 scoped_refptr<net::HttpResponseHeaders>* override_response_headers, |
1090 GURL* allowed_unsafe_redirect_url, | 1099 GURL* allowed_unsafe_redirect_url, |
1091 extensions::WarningSet* conflicting_extensions, | 1100 extensions::WarningSet* conflicting_extensions, |
1092 const net::NetLogWithSource* net_log) { | 1101 const net::NetLogWithSource* net_log) { |
1093 EventResponseDeltas::const_iterator delta; | 1102 EventResponseDeltas::const_iterator delta; |
1094 | 1103 |
1095 // Here we collect which headers we have removed or added so far due to | 1104 // Here we collect which headers we have removed or added so far due to |
1096 // extensions of higher precedence. Header keys are always stored as | 1105 // extensions of higher precedence. Header keys are always stored as |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1163 net_log->AddEvent( | 1172 net_log->AddEvent( |
1164 net::NetLogEventType::CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT, | 1173 net::NetLogEventType::CHROME_EXTENSION_IGNORED_DUE_TO_CONFLICT, |
1165 CreateNetLogExtensionIdCallback(delta->get())); | 1174 CreateNetLogExtensionIdCallback(delta->get())); |
1166 } | 1175 } |
1167 } | 1176 } |
1168 | 1177 |
1169 MergeCookiesInOnHeadersReceivedResponses(deltas, original_response_headers, | 1178 MergeCookiesInOnHeadersReceivedResponses(deltas, original_response_headers, |
1170 override_response_headers, conflicting_extensions, net_log); | 1179 override_response_headers, conflicting_extensions, net_log); |
1171 | 1180 |
1172 GURL new_url; | 1181 GURL new_url; |
1173 MergeRedirectUrlOfResponses( | 1182 MergeRedirectUrlOfResponses(url, deltas, &new_url, conflicting_extensions, |
1174 deltas, &new_url, conflicting_extensions, net_log); | 1183 net_log); |
1175 if (new_url.is_valid()) { | 1184 if (new_url.is_valid()) { |
1176 // Only create a copy if we really want to modify the response headers. | 1185 // Only create a copy if we really want to modify the response headers. |
1177 if (override_response_headers->get() == NULL) { | 1186 if (override_response_headers->get() == NULL) { |
1178 *override_response_headers = new net::HttpResponseHeaders( | 1187 *override_response_headers = new net::HttpResponseHeaders( |
1179 original_response_headers->raw_headers()); | 1188 original_response_headers->raw_headers()); |
1180 } | 1189 } |
1181 (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found"); | 1190 (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found"); |
1182 (*override_response_headers)->RemoveHeader("location"); | 1191 (*override_response_headers)->RemoveHeader("location"); |
1183 (*override_response_headers)->AddHeader("Location: " + new_url.spec()); | 1192 (*override_response_headers)->AddHeader("Location: " + new_url.spec()); |
1184 // Explicitly mark the URL as safe for redirection, to prevent the request | 1193 // Explicitly mark the URL as safe for redirection, to prevent the request |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1280 for (size_t i = 0; i < kResourceTypeStringsLength; ++i) { | 1289 for (size_t i = 0; i < kResourceTypeStringsLength; ++i) { |
1281 if (type_str == kResourceTypeStrings[i]) { | 1290 if (type_str == kResourceTypeStrings[i]) { |
1282 found = true; | 1291 found = true; |
1283 types->push_back(extensions::ResourceTypeExt(kResourceTypeValues[i])); | 1292 types->push_back(extensions::ResourceTypeExt(kResourceTypeValues[i])); |
1284 } | 1293 } |
1285 } | 1294 } |
1286 return found; | 1295 return found; |
1287 } | 1296 } |
1288 | 1297 |
1289 } // namespace extension_web_request_api_helpers | 1298 } // namespace extension_web_request_api_helpers |
OLD | NEW |