Chromium Code Reviews| 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/download/download_request_core.h" | 5 #include "content/browser/download/download_request_core.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 #include "content/public/browser/download_manager_delegate.h" | 30 #include "content/public/browser/download_manager_delegate.h" |
| 31 #include "content/public/browser/navigation_entry.h" | 31 #include "content/public/browser/navigation_entry.h" |
| 32 #include "content/public/browser/resource_context.h" | 32 #include "content/public/browser/resource_context.h" |
| 33 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 34 #include "device/power_save_blocker/power_save_blocker.h" | 34 #include "device/power_save_blocker/power_save_blocker.h" |
| 35 #include "net/base/elements_upload_data_stream.h" | 35 #include "net/base/elements_upload_data_stream.h" |
| 36 #include "net/base/io_buffer.h" | 36 #include "net/base/io_buffer.h" |
| 37 #include "net/base/load_flags.h" | 37 #include "net/base/load_flags.h" |
| 38 #include "net/base/net_errors.h" | 38 #include "net/base/net_errors.h" |
| 39 #include "net/base/upload_bytes_element_reader.h" | 39 #include "net/base/upload_bytes_element_reader.h" |
| 40 #include "net/http/http_request_headers.h" | |
| 40 #include "net/http/http_response_headers.h" | 41 #include "net/http/http_response_headers.h" |
| 41 #include "net/http/http_status_code.h" | 42 #include "net/http/http_status_code.h" |
| 42 #include "net/url_request/url_request_context.h" | 43 #include "net/url_request/url_request_context.h" |
| 43 | 44 |
| 44 namespace content { | 45 namespace content { |
| 45 | 46 |
| 46 namespace { | 47 namespace { |
| 47 | 48 |
| 48 // This is a UserData::Data that will be attached to a URLRequest as a | 49 // This is a UserData::Data that will be attached to a URLRequest as a |
| 49 // side-channel for passing download parameters. | 50 // side-channel for passing download parameters. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 else | 152 else |
| 152 load_flags |= net::LOAD_SKIP_CACHE_VALIDATION; | 153 load_flags |= net::LOAD_SKIP_CACHE_VALIDATION; |
| 153 } else { | 154 } else { |
| 154 load_flags |= net::LOAD_DISABLE_CACHE; | 155 load_flags |= net::LOAD_DISABLE_CACHE; |
| 155 } | 156 } |
| 156 request->SetLoadFlags(load_flags); | 157 request->SetLoadFlags(load_flags); |
| 157 | 158 |
| 158 bool has_last_modified = !params->last_modified().empty(); | 159 bool has_last_modified = !params->last_modified().empty(); |
| 159 bool has_etag = !params->etag().empty(); | 160 bool has_etag = !params->etag().empty(); |
| 160 | 161 |
| 161 // If we've asked for a range, we want to make sure that we only get that | 162 // Strong validator(i.e. etag or last modified) is required in range requests |
| 162 // range if our current copy of the information is good. We shouldn't be | 163 // for download resumption and parallel download. |
| 163 // asked to continue if we don't have a verifier. | 164 DCHECK((params->offset() == 0 && |
| 164 DCHECK(params->offset() == 0 || has_etag || has_last_modified); | 165 params->length() == DownloadSaveInfo::kLengthFullContent) || |
| 166 has_etag || has_last_modified); | |
| 165 | 167 |
| 166 // If we're not at the beginning of the file, retrieve only the remaining | 168 // Add "Range" and "If-Range" request header fields if the range request is to |
| 167 // portion. | 169 // retrieve bytes from {offset} to the end of the file. |
| 168 if (params->offset() > 0 && (has_etag || has_last_modified)) { | 170 // E.g. "Range:bytes=50-". |
| 171 if (params->offset() > 0 && | |
| 172 params->length() == DownloadSaveInfo::kLengthFullContent && | |
| 173 (has_etag || has_last_modified)) { | |
| 169 request->SetExtraRequestHeaderByName( | 174 request->SetExtraRequestHeaderByName( |
| 170 "Range", base::StringPrintf("bytes=%" PRId64 "-", params->offset()), | 175 net::HttpRequestHeaders::kRange, |
| 171 true); | 176 base::StringPrintf("bytes=%" PRId64 "-", params->offset()), true); |
| 172 | 177 |
| 173 // In accordance with RFC 2616 Section 14.27, use If-Range to specify that | 178 // In accordance with RFC 7233 Section 3.2, use If-Range to specify that |
| 174 // the server return the entire entity if the validator doesn't match. | 179 // the server return the entire entity if the validator doesn't match. |
| 175 // Last-Modified can be used in the absence of ETag as a validator if the | 180 // Last-Modified can be used in the absence of ETag as a validator if the |
| 176 // response headers satisfied the HttpUtil::HasStrongValidators() predicate. | 181 // response headers satisfied the HttpUtil::HasStrongValidators() predicate. |
| 177 // | 182 // |
| 178 // This function assumes that HasStrongValidators() was true and that the | 183 // This function assumes that HasStrongValidators() was true and that the |
| 179 // ETag and Last-Modified header values supplied are valid. | 184 // ETag and Last-Modified header values supplied are valid. |
| 180 request->SetExtraRequestHeaderByName( | 185 request->SetExtraRequestHeaderByName( |
| 181 "If-Range", has_etag ? params->etag() : params->last_modified(), true); | 186 "If-Range", has_etag ? params->etag() : params->last_modified(), true); |
| 182 } | 187 } |
| 183 | 188 |
| 189 // Add "Range", "If-Match", and "If-Unmodified-Since" for range requests with | |
| 190 // last byte position specified. e.g. "Range:bytes=50-59". If the file is | |
| 191 // updated on the server, we should get http 412 response code. | |
| 192 if (params->length() != DownloadSaveInfo::kLengthFullContent && | |
| 193 (has_etag || has_last_modified)) { | |
| 194 request->SetExtraRequestHeaderByName( | |
| 195 net::HttpRequestHeaders::kRange, | |
| 196 base::StringPrintf("bytes=%" PRId64 "-%" PRId64, params->offset(), | |
| 197 params->offset() + params->length() - 1), | |
| 198 true); | |
| 199 if (has_etag) | |
| 200 request->SetExtraRequestHeaderByName("If-Match", params->etag(), true); | |
|
asanka
2017/02/03 22:16:22
Could you add "If-Match"/"If-Unmodified-Since" to
xingliu
2017/02/06 19:31:39
Done.
| |
| 201 // According to RFC 7232 section 3.4, "If-Unmodified-Since" is mainly for | |
| 202 // old servers that didn't implement "If-Match" and must be ignored when | |
| 203 // "If-Match" presents. | |
| 204 if (has_last_modified) | |
| 205 request->SetExtraRequestHeaderByName("If-Unmodified-Since", | |
| 206 params->last_modified(), true); | |
| 207 } | |
| 208 | |
| 184 // Downloads are treated as top level navigations. Hence the first-party | 209 // Downloads are treated as top level navigations. Hence the first-party |
| 185 // origin for cookies is always based on the target URL and is updated on | 210 // origin for cookies is always based on the target URL and is updated on |
| 186 // redirects. | 211 // redirects. |
| 187 request->set_first_party_for_cookies(params->url()); | 212 request->set_first_party_for_cookies(params->url()); |
| 188 request->set_first_party_url_policy( | 213 request->set_first_party_url_policy( |
| 189 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); | 214 net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 190 request->set_initiator(params->initiator()); | 215 request->set_initiator(params->initiator()); |
| 191 | 216 |
| 192 for (const auto& header : params->request_headers()) | 217 for (const auto& header : params->request_headers()) |
| 193 request->SetExtraRequestHeaderByName(header.first, header.second, | 218 request->SetExtraRequestHeaderByName(header.first, header.second, |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 297 | 322 |
| 298 if (!override_mime_type.empty()) | 323 if (!override_mime_type.empty()) |
| 299 create_info->mime_type = override_mime_type; | 324 create_info->mime_type = override_mime_type; |
| 300 else | 325 else |
| 301 request()->GetMimeType(&create_info->mime_type); | 326 request()->GetMimeType(&create_info->mime_type); |
| 302 | 327 |
| 303 // Get the last modified time and etag. | 328 // Get the last modified time and etag. |
| 304 const net::HttpResponseHeaders* headers = request()->response_headers(); | 329 const net::HttpResponseHeaders* headers = request()->response_headers(); |
| 305 if (headers) { | 330 if (headers) { |
| 306 if (headers->HasStrongValidators()) { | 331 if (headers->HasStrongValidators()) { |
| 307 // If we don't have strong validators as per RFC 2616 section 13.3.3, then | 332 // If we don't have strong validators as per RFC 7232 section 2, then |
| 308 // we neither store nor use them for range requests. | 333 // we neither store nor use them for range requests. |
| 309 if (!headers->EnumerateHeader(nullptr, "Last-Modified", | 334 if (!headers->EnumerateHeader(nullptr, "Last-Modified", |
| 310 &create_info->last_modified)) | 335 &create_info->last_modified)) |
| 311 create_info->last_modified.clear(); | 336 create_info->last_modified.clear(); |
| 312 if (!headers->EnumerateHeader(nullptr, "ETag", &create_info->etag)) | 337 if (!headers->EnumerateHeader(nullptr, "ETag", &create_info->etag)) |
| 313 create_info->etag.clear(); | 338 create_info->etag.clear(); |
| 314 } | 339 } |
| 315 | 340 |
| 316 // Grab the first content-disposition header. There may be more than one, | 341 // Grab the first content-disposition header. There may be more than one, |
| 317 // though as of this writing, the network stack ensures if there are, they | 342 // though as of this writing, the network stack ensures if there are, they |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 529 DownloadInterruptReason reason = ConvertNetErrorToInterruptReason( | 554 DownloadInterruptReason reason = ConvertNetErrorToInterruptReason( |
| 530 error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK); | 555 error_code, DOWNLOAD_INTERRUPT_FROM_NETWORK); |
| 531 | 556 |
| 532 return reason; | 557 return reason; |
| 533 } | 558 } |
| 534 | 559 |
| 535 // static | 560 // static |
| 536 DownloadInterruptReason DownloadRequestCore::HandleSuccessfulServerResponse( | 561 DownloadInterruptReason DownloadRequestCore::HandleSuccessfulServerResponse( |
| 537 const net::HttpResponseHeaders& http_headers, | 562 const net::HttpResponseHeaders& http_headers, |
| 538 DownloadSaveInfo* save_info) { | 563 DownloadSaveInfo* save_info) { |
| 539 switch (http_headers.response_code()) { | 564 switch (http_headers.response_code()) { |
|
asanka
2017/02/03 22:16:23
We should now handle HTTP 412 here (net::HTTP_PREC
xingliu
2017/02/06 19:31:39
Can we do it later in a separate patch? Since it n
| |
| 540 case -1: // Non-HTTP request. | 565 case -1: // Non-HTTP request. |
| 541 case net::HTTP_OK: | 566 case net::HTTP_OK: |
| 542 case net::HTTP_NON_AUTHORITATIVE_INFORMATION: | 567 case net::HTTP_NON_AUTHORITATIVE_INFORMATION: |
| 543 case net::HTTP_PARTIAL_CONTENT: | 568 case net::HTTP_PARTIAL_CONTENT: |
| 544 // Expected successful codes. | 569 // Expected successful codes. |
| 545 break; | 570 break; |
| 546 | 571 |
| 547 case net::HTTP_CREATED: | 572 case net::HTTP_CREATED: |
| 548 case net::HTTP_ACCEPTED: | 573 case net::HTTP_ACCEPTED: |
| 549 // Per RFC 2616 the entity being transferred is metadata about the | 574 // Per RFC 7231 the entity being transferred is metadata about the |
| 550 // resource at the target URL and not the resource at that URL (or the | 575 // resource at the target URL and not the resource at that URL (or the |
| 551 // resource that would be at the URL once processing is completed in the | 576 // resource that would be at the URL once processing is completed in the |
| 552 // case of HTTP_ACCEPTED). However, we currently don't have special | 577 // case of HTTP_ACCEPTED). However, we currently don't have special |
| 553 // handling for these response and they are downloaded the same as a | 578 // handling for these response and they are downloaded the same as a |
| 554 // regular response. | 579 // regular response. |
| 555 break; | 580 break; |
| 556 | 581 |
| 557 case net::HTTP_NO_CONTENT: | 582 case net::HTTP_NO_CONTENT: |
| 558 case net::HTTP_RESET_CONTENT: | 583 case net::HTTP_RESET_CONTENT: |
| 559 // These two status codes don't have an entity (or rather RFC 2616 | 584 // These two status codes don't have an entity (or rather RFC 7231 |
| 560 // requires that there be no entity). They are treated the same as the | 585 // requires that there be no entity). They are treated the same as the |
| 561 // resource not being found since there is no entity to download. | 586 // resource not being found since there is no entity to download. |
| 562 | 587 |
| 563 case net::HTTP_NOT_FOUND: | 588 case net::HTTP_NOT_FOUND: |
| 564 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; | 589 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; |
| 565 break; | 590 break; |
| 566 | 591 |
| 567 case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE: | 592 case net::HTTP_REQUESTED_RANGE_NOT_SATISFIABLE: |
| 568 // Retry by downloading from the start automatically: | 593 // Retry by downloading from the start automatically: |
| 569 // If we haven't received data when we get this error, we won't. | 594 // If we haven't received data when we get this error, we won't. |
| 570 return DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE; | 595 return DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE; |
| 571 break; | 596 break; |
| 572 case net::HTTP_UNAUTHORIZED: | 597 case net::HTTP_UNAUTHORIZED: |
| 573 case net::HTTP_PROXY_AUTHENTICATION_REQUIRED: | 598 case net::HTTP_PROXY_AUTHENTICATION_REQUIRED: |
| 574 // Server didn't authorize this request. | 599 // Server didn't authorize this request. |
| 575 return DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED; | 600 return DOWNLOAD_INTERRUPT_REASON_SERVER_UNAUTHORIZED; |
| 576 break; | 601 break; |
| 577 case net::HTTP_FORBIDDEN: | 602 case net::HTTP_FORBIDDEN: |
| 578 // Server forbids access to this resource. | 603 // Server forbids access to this resource. |
| 579 return DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN; | 604 return DOWNLOAD_INTERRUPT_REASON_SERVER_FORBIDDEN; |
| 580 break; | 605 break; |
| 581 default: // All other errors. | 606 default: // All other errors. |
| 582 // Redirection and informational codes should have been handled earlier | 607 // Redirection and informational codes should have been handled earlier |
| 583 // in the stack. | 608 // in the stack. |
| 584 DCHECK_NE(3, http_headers.response_code() / 100); | 609 DCHECK_NE(3, http_headers.response_code() / 100); |
| 585 DCHECK_NE(1, http_headers.response_code() / 100); | 610 DCHECK_NE(1, http_headers.response_code() / 100); |
| 586 return DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; | 611 return DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED; |
| 587 } | 612 } |
| 588 | 613 |
| 589 if (save_info && save_info->offset > 0) { | 614 // The caller is expecting a partial response. |
| 590 // The caller is expecting a partial response. | 615 if (save_info && (save_info->offset > 0 || save_info->length > 0)) { |
| 616 if (http_headers.response_code() != net::HTTP_PARTIAL_CONTENT) { | |
| 617 // Server should send partial content when we ask for a range with last | |
| 618 // byte position specified. e.g. "Range:bytes=50-99". | |
|
asanka
2017/02/03 22:16:22
Nit: This is a result of the If-Match/If-Unmodifie
xingliu
2017/02/06 19:31:40
Done.
| |
| 619 if (save_info->length != DownloadSaveInfo::kLengthFullContent) | |
| 620 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; | |
| 591 | 621 |
| 592 if (http_headers.response_code() != net::HTTP_PARTIAL_CONTENT) { | 622 // Requested a partial range, but received the entire response, when |
| 593 // Requested a partial range, but received the entire response. | 623 // the range header is "Range:bytes={offset}-". |
| 594 save_info->offset = 0; | 624 save_info->offset = 0; |
| 595 save_info->hash_of_partial_file.clear(); | 625 save_info->hash_of_partial_file.clear(); |
| 596 save_info->hash_state.reset(); | 626 save_info->hash_state.reset(); |
| 597 return DOWNLOAD_INTERRUPT_REASON_NONE; | 627 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 598 } | 628 } |
| 599 | 629 |
| 600 int64_t first_byte = -1; | 630 int64_t first_byte = -1; |
| 601 int64_t last_byte = -1; | 631 int64_t last_byte = -1; |
| 602 int64_t length = -1; | 632 int64_t length = -1; |
| 603 if (!http_headers.GetContentRangeFor206(&first_byte, &last_byte, &length)) | 633 if (!http_headers.GetContentRangeFor206(&first_byte, &last_byte, &length)) |
| 604 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; | 634 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; |
| 605 DCHECK_GE(first_byte, 0); | 635 DCHECK_GE(first_byte, 0); |
| 606 | 636 |
| 607 if (first_byte != save_info->offset) { | 637 if (first_byte != save_info->offset || |
| 638 (save_info->length > 0 && | |
| 639 last_byte != save_info->offset + save_info->length - 1)) { | |
| 608 // The server returned a different range than the one we requested. Assume | 640 // The server returned a different range than the one we requested. Assume |
| 609 // the response is bad. | 641 // the response is bad. |
| 610 // | 642 // |
| 611 // In the future we should consider allowing offsets that are less than | 643 // In the future we should consider allowing offsets that are less than |
| 612 // the offset we've requested, since in theory we can truncate the partial | 644 // the offset we've requested, since in theory we can truncate the partial |
| 613 // file at the offset and continue. | 645 // file at the offset and continue. |
| 614 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; | 646 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; |
| 615 } | 647 } |
| 616 | 648 |
| 617 return DOWNLOAD_INTERRUPT_REASON_NONE; | 649 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 618 } | 650 } |
| 619 | 651 |
| 620 if (http_headers.response_code() == net::HTTP_PARTIAL_CONTENT) | 652 if (http_headers.response_code() == net::HTTP_PARTIAL_CONTENT) |
| 621 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; | 653 return DOWNLOAD_INTERRUPT_REASON_SERVER_BAD_CONTENT; |
| 622 | 654 |
| 623 return DOWNLOAD_INTERRUPT_REASON_NONE; | 655 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 624 } | 656 } |
| 625 | 657 |
| 626 } // namespace content | 658 } // namespace content |
| OLD | NEW |