| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "net/http/http_cache_transaction.h" | 5 #include "net/http/http_cache_transaction.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 | 8 |
| 9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value)) | 90 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value)) |
| 91 return true; | 91 return true; |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 return false; | 94 return false; |
| 95 } | 95 } |
| 96 | 96 |
| 97 //----------------------------------------------------------------------------- | 97 //----------------------------------------------------------------------------- |
| 98 | 98 |
| 99 HttpCache::Transaction::Transaction(HttpCache* cache, bool enable_range_support) | 99 HttpCache::Transaction::Transaction(HttpCache* cache, bool enable_range_support) |
| 100 : request_(NULL), | 100 : next_state_(STATE_NONE), |
| 101 request_(NULL), |
| 101 cache_(cache->AsWeakPtr()), | 102 cache_(cache->AsWeakPtr()), |
| 102 entry_(NULL), | 103 entry_(NULL), |
| 103 network_trans_(NULL), | 104 network_trans_(NULL), |
| 104 callback_(NULL), | 105 callback_(NULL), |
| 105 mode_(NONE), | 106 mode_(NONE), |
| 106 reading_(false), | 107 reading_(false), |
| 107 invalid_range_(false), | 108 invalid_range_(false), |
| 108 enable_range_support_(enable_range_support), | 109 enable_range_support_(enable_range_support), |
| 109 truncated_(false), | 110 truncated_(false), |
| 110 read_offset_(0), | 111 read_offset_(0), |
| 111 effective_load_flags_(0), | 112 effective_load_flags_(0), |
| 112 final_upload_progress_(0), | 113 final_upload_progress_(0), |
| 113 ALLOW_THIS_IN_INITIALIZER_LIST( | 114 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 114 network_info_callback_(this, &Transaction::OnNetworkInfoAvailable)), | 115 network_callback_(this, &Transaction::OnIOComplete)), |
| 115 ALLOW_THIS_IN_INITIALIZER_LIST( | 116 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 116 network_read_callback_(this, &Transaction::OnNetworkReadCompleted)), | 117 cache_callback_(new CancelableCompletionCallback<Transaction>( |
| 117 ALLOW_THIS_IN_INITIALIZER_LIST( | 118 this, &Transaction::OnIOComplete))) { |
| 118 cache_read_callback_(new CancelableCompletionCallback<Transaction>( | |
| 119 this, &Transaction::OnCacheReadCompleted))), | |
| 120 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 121 cache_write_callback_(new CancelableCompletionCallback<Transaction>( | |
| 122 this, &Transaction::OnCacheWriteCompleted))), | |
| 123 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 124 entry_ready_callback_(new CancelableCompletionCallback<Transaction>( | |
| 125 this, &Transaction::OnCacheEntryReady))) { | |
| 126 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders == | 119 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders == |
| 127 ARRAYSIZE_UNSAFE(kValidationHeaders), | 120 ARRAYSIZE_UNSAFE(kValidationHeaders), |
| 128 Invalid_number_of_validation_headers); | 121 Invalid_number_of_validation_headers); |
| 129 } | 122 } |
| 130 | 123 |
| 131 HttpCache::Transaction::~Transaction() { | 124 HttpCache::Transaction::~Transaction() { |
| 132 if (cache_) { | 125 if (cache_) { |
| 133 if (entry_) { | 126 if (entry_) { |
| 134 bool cancel_request = reading_ && enable_range_support_; | 127 bool cancel_request = reading_ && enable_range_support_; |
| 135 if (cancel_request) { | 128 if (cancel_request) { |
| 136 if (partial_.get()) { | 129 if (partial_.get()) { |
| 137 entry_->disk_entry->CancelSparseIO(); | 130 entry_->disk_entry->CancelSparseIO(); |
| 138 } else { | 131 } else { |
| 139 cancel_request &= (response_.headers->response_code() == 200); | 132 cancel_request &= (response_.headers->response_code() == 200); |
| 140 } | 133 } |
| 141 } | 134 } |
| 142 | 135 |
| 143 cache_->DoneWithEntry(entry_, this, cancel_request); | 136 cache_->DoneWithEntry(entry_, this, cancel_request); |
| 144 } else { | 137 } else { |
| 145 cache_->RemovePendingTransaction(this); | 138 cache_->RemovePendingTransaction(this); |
| 146 } | 139 } |
| 147 } | 140 } |
| 148 | 141 |
| 149 // If there is an outstanding callback, mark it as cancelled so running it | 142 // If there is an outstanding callback, mark it as cancelled so running it |
| 150 // does nothing. | 143 // does nothing. |
| 151 cache_read_callback_->Cancel(); | 144 cache_callback_->Cancel(); |
| 152 cache_write_callback_->Cancel(); | |
| 153 entry_ready_callback_->Cancel(); | |
| 154 | 145 |
| 155 // We could still have a cache read or write in progress, so we just null the | 146 // We could still have a cache read or write in progress, so we just null the |
| 156 // cache_ pointer to signal that we are dead. See DoCacheReadCompleted. | 147 // cache_ pointer to signal that we are dead. See DoCacheReadCompleted. |
| 157 cache_.reset(); | 148 cache_.reset(); |
| 158 } | 149 } |
| 159 | 150 |
| 160 int HttpCache::Transaction::Start(const HttpRequestInfo* request, | 151 int HttpCache::Transaction::Start(const HttpRequestInfo* request, |
| 161 CompletionCallback* callback, | 152 CompletionCallback* callback, |
| 162 LoadLog* load_log) { | 153 LoadLog* load_log) { |
| 163 DCHECK(request); | 154 DCHECK(request); |
| 164 DCHECK(callback); | 155 DCHECK(callback); |
| 165 | 156 |
| 166 // ensure that we only have one asynchronous call at a time. | 157 // Ensure that we only have one asynchronous call at a time. |
| 167 DCHECK(!callback_); | 158 DCHECK(!callback_); |
| 168 | 159 |
| 169 if (!cache_) | 160 if (!cache_) |
| 170 return ERR_UNEXPECTED; | 161 return ERR_UNEXPECTED; |
| 171 | 162 |
| 172 SetRequest(load_log, request); | 163 SetRequest(load_log, request); |
| 173 | 164 |
| 174 int rv; | 165 int rv; |
| 175 | 166 |
| 176 if (!ShouldPassThrough()) { | 167 if (!ShouldPassThrough()) { |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 c->Run(rv); | 471 c->Run(rv); |
| 481 } | 472 } |
| 482 | 473 |
| 483 int HttpCache::Transaction::HandleResult(int rv) { | 474 int HttpCache::Transaction::HandleResult(int rv) { |
| 484 DCHECK(rv != ERR_IO_PENDING); | 475 DCHECK(rv != ERR_IO_PENDING); |
| 485 if (callback_) | 476 if (callback_) |
| 486 DoCallback(rv); | 477 DoCallback(rv); |
| 487 return rv; | 478 return rv; |
| 488 } | 479 } |
| 489 | 480 |
| 481 int HttpCache::Transaction::DoLoop(int result) { |
| 482 DCHECK(next_state_ != STATE_NONE); |
| 483 |
| 484 int rv = result; |
| 485 do { |
| 486 State state = next_state_; |
| 487 next_state_ = STATE_NONE; |
| 488 switch (state) { |
| 489 case STATE_SEND_REQUEST: |
| 490 DCHECK_EQ(OK, rv); |
| 491 rv = DoSendRequest(); |
| 492 break; |
| 493 case STATE_SEND_REQUEST_COMPLETE: |
| 494 rv = DoSendRequestComplete(rv); |
| 495 break; |
| 496 case STATE_NETWORK_READ: |
| 497 DCHECK_EQ(OK, rv); |
| 498 rv = DoNetworkRead(); |
| 499 break; |
| 500 case STATE_NETWORK_READ_COMPLETE: |
| 501 rv = DoNetworkReadComplete(rv); |
| 502 break; |
| 503 case STATE_CACHE_QUERY_DATA: |
| 504 DCHECK_EQ(OK, rv); |
| 505 rv = DoCacheQueryData(); |
| 506 break; |
| 507 case STATE_CACHE_QUERY_DATA_COMPLETE: |
| 508 rv = DoCacheQueryDataComplete(rv); |
| 509 break; |
| 510 case STATE_CACHE_READ_DATA: |
| 511 DCHECK_EQ(OK, rv); |
| 512 rv = DoCacheReadData(); |
| 513 break; |
| 514 case STATE_CACHE_READ_DATA_COMPLETE: |
| 515 rv = DoCacheReadDataComplete(rv); |
| 516 break; |
| 517 case STATE_CACHE_WRITE_DATA: |
| 518 rv = DoCacheWriteData(rv); |
| 519 break; |
| 520 case STATE_CACHE_WRITE_DATA_COMPLETE: |
| 521 rv = DoCacheWriteDataComplete(rv); |
| 522 break; |
| 523 default: |
| 524 NOTREACHED() << "bad state"; |
| 525 rv = ERR_FAILED; |
| 526 break; |
| 527 } |
| 528 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 529 |
| 530 if (rv != ERR_IO_PENDING) |
| 531 HandleResult(rv); |
| 532 |
| 533 return rv; |
| 534 } |
| 535 |
| 490 void HttpCache::Transaction::SetRequest(LoadLog* load_log, | 536 void HttpCache::Transaction::SetRequest(LoadLog* load_log, |
| 491 const HttpRequestInfo* request) { | 537 const HttpRequestInfo* request) { |
| 492 load_log_ = load_log; | 538 load_log_ = load_log; |
| 493 request_ = request; | 539 request_ = request; |
| 494 effective_load_flags_ = request_->load_flags; | 540 effective_load_flags_ = request_->load_flags; |
| 495 | 541 |
| 496 switch (cache_->mode()) { | 542 switch (cache_->mode()) { |
| 497 case NORMAL: | 543 case NORMAL: |
| 498 break; | 544 break; |
| 499 case RECORD: | 545 case RECORD: |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 | 726 |
| 681 if (response_.headers->response_code() != 206 && !partial_.get() && | 727 if (response_.headers->response_code() != 206 && !partial_.get() && |
| 682 !truncated_) | 728 !truncated_) |
| 683 return BeginCacheValidation(); | 729 return BeginCacheValidation(); |
| 684 | 730 |
| 685 if (!enable_range_support_) | 731 if (!enable_range_support_) |
| 686 return BeginCacheValidation(); | 732 return BeginCacheValidation(); |
| 687 | 733 |
| 688 bool byte_range_requested = partial_.get() != NULL; | 734 bool byte_range_requested = partial_.get() != NULL; |
| 689 if (byte_range_requested) { | 735 if (byte_range_requested) { |
| 690 // Balanced in ValidateEntryHeadersAndContinue. | 736 next_state_ = STATE_CACHE_QUERY_DATA; |
| 691 entry_ready_callback_->AddRef(); | 737 return DoLoop(OK); |
| 692 if (OK != entry_->disk_entry->ReadyForSparseIO(entry_ready_callback_)) | 738 } |
| 693 return ERR_IO_PENDING; | 739 // The request is not for a range, but we have stored just ranges. |
| 694 } else { | 740 partial_.reset(new PartialData()); |
| 695 // The request is not for a range, but we have stored just ranges. | 741 partial_->SetHeaders(request_->extra_headers); |
| 696 partial_.reset(new PartialData()); | 742 if (!custom_request_.get()) { |
| 697 partial_->SetHeaders(request_->extra_headers); | 743 custom_request_.reset(new HttpRequestInfo(*request_)); |
| 698 if (!custom_request_.get()) { | 744 request_ = custom_request_.get(); |
| 699 custom_request_.reset(new HttpRequestInfo(*request_)); | |
| 700 request_ = custom_request_.get(); | |
| 701 } | |
| 702 } | 745 } |
| 703 | 746 |
| 704 return ValidateEntryHeadersAndContinue(byte_range_requested); | 747 return ValidateEntryHeadersAndContinue(false); |
| 748 } |
| 749 |
| 750 int HttpCache::Transaction::DoCacheQueryData() { |
| 751 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE; |
| 752 |
| 753 // Balanced in ValidateEntryHeadersAndContinue. |
| 754 cache_callback_->AddRef(); |
| 755 return entry_->disk_entry->ReadyForSparseIO(cache_callback_); |
| 756 } |
| 757 |
| 758 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { |
| 759 DCHECK_EQ(OK, result); |
| 760 // Balance the AddRef from BeginPartialCacheValidation. |
| 761 cache_callback_->Release(); |
| 762 if (!cache_) |
| 763 return ERR_UNEXPECTED; |
| 764 |
| 765 return ValidateEntryHeadersAndContinue(true); |
| 705 } | 766 } |
| 706 | 767 |
| 707 int HttpCache::Transaction::ValidateEntryHeadersAndContinue( | 768 int HttpCache::Transaction::ValidateEntryHeadersAndContinue( |
| 708 bool byte_range_requested) { | 769 bool byte_range_requested) { |
| 709 DCHECK(mode_ == READ_WRITE); | 770 DCHECK(mode_ == READ_WRITE); |
| 710 | 771 |
| 711 if (byte_range_requested) { | |
| 712 // Balance the AddRef from BeginPartialCacheValidation. | |
| 713 entry_ready_callback_->Release(); | |
| 714 } | |
| 715 | |
| 716 if (!cache_) | |
| 717 return HandleResult(ERR_UNEXPECTED); | |
| 718 | |
| 719 if (!partial_->UpdateFromStoredHeaders(response_.headers, entry_->disk_entry, | 772 if (!partial_->UpdateFromStoredHeaders(response_.headers, entry_->disk_entry, |
| 720 truncated_)) { | 773 truncated_)) { |
| 721 // The stored data cannot be used. Get rid of it and restart this request. | 774 // The stored data cannot be used. Get rid of it and restart this request. |
| 722 // We need to also reset the |truncated_| flag as a new entry is created. | 775 // We need to also reset the |truncated_| flag as a new entry is created. |
| 723 DoomPartialEntry(!byte_range_requested); | 776 DoomPartialEntry(!byte_range_requested); |
| 724 mode_ = WRITE; | 777 mode_ = WRITE; |
| 725 truncated_ = false; | 778 truncated_ = false; |
| 726 return AddToEntry(); | 779 return AddToEntry(); |
| 727 } | 780 } |
| 728 | 781 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 // The externally conditionalized request is not a validation request | 840 // The externally conditionalized request is not a validation request |
| 788 // for our existing cache entry. Proceed with caching disabled. | 841 // for our existing cache entry. Proceed with caching disabled. |
| 789 DoneWritingToEntry(true); | 842 DoneWritingToEntry(true); |
| 790 } | 843 } |
| 791 } | 844 } |
| 792 | 845 |
| 793 return BeginNetworkRequest(); | 846 return BeginNetworkRequest(); |
| 794 } | 847 } |
| 795 | 848 |
| 796 int HttpCache::Transaction::BeginNetworkRequest() { | 849 int HttpCache::Transaction::BeginNetworkRequest() { |
| 850 next_state_ = STATE_SEND_REQUEST; |
| 851 return DoLoop(OK); |
| 852 } |
| 853 |
| 854 int HttpCache::Transaction::DoSendRequest() { |
| 797 DCHECK(mode_ & WRITE || mode_ == NONE); | 855 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 798 DCHECK(!network_trans_.get()); | 856 DCHECK(!network_trans_.get()); |
| 799 | 857 |
| 800 // Create a network transaction. | 858 // Create a network transaction. |
| 801 int rv = cache_->network_layer_->CreateTransaction(&network_trans_); | 859 int rv = cache_->network_layer_->CreateTransaction(&network_trans_); |
| 802 if (rv != OK) | 860 if (rv != OK) |
| 803 return rv; | 861 return rv; |
| 804 | 862 |
| 805 rv = network_trans_->Start(request_, &network_info_callback_, load_log_); | 863 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 806 if (rv != ERR_IO_PENDING) | 864 rv = network_trans_->Start(request_, &network_callback_, load_log_); |
| 807 OnNetworkInfoAvailable(rv); | |
| 808 return rv; | 865 return rv; |
| 809 } | 866 } |
| 810 | 867 |
| 811 int HttpCache::Transaction::RestartNetworkRequest() { | 868 int HttpCache::Transaction::RestartNetworkRequest() { |
| 812 DCHECK(mode_ & WRITE || mode_ == NONE); | 869 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 813 DCHECK(network_trans_.get()); | 870 DCHECK(network_trans_.get()); |
| 871 DCHECK_EQ(STATE_NONE, next_state_); |
| 814 | 872 |
| 815 int rv = network_trans_->RestartIgnoringLastError(&network_info_callback_); | 873 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 874 int rv = network_trans_->RestartIgnoringLastError(&network_callback_); |
| 816 if (rv != ERR_IO_PENDING) | 875 if (rv != ERR_IO_PENDING) |
| 817 OnNetworkInfoAvailable(rv); | 876 return DoLoop(rv); |
| 818 return rv; | 877 return rv; |
| 819 } | 878 } |
| 820 | 879 |
| 821 int HttpCache::Transaction::RestartNetworkRequestWithCertificate( | 880 int HttpCache::Transaction::RestartNetworkRequestWithCertificate( |
| 822 X509Certificate* client_cert) { | 881 X509Certificate* client_cert) { |
| 823 DCHECK(mode_ & WRITE || mode_ == NONE); | 882 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 824 DCHECK(network_trans_.get()); | 883 DCHECK(network_trans_.get()); |
| 884 DCHECK_EQ(STATE_NONE, next_state_); |
| 825 | 885 |
| 886 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 826 int rv = network_trans_->RestartWithCertificate(client_cert, | 887 int rv = network_trans_->RestartWithCertificate(client_cert, |
| 827 &network_info_callback_); | 888 &network_callback_); |
| 828 if (rv != ERR_IO_PENDING) | 889 if (rv != ERR_IO_PENDING) |
| 829 OnNetworkInfoAvailable(rv); | 890 return DoLoop(rv); |
| 830 return rv; | 891 return rv; |
| 831 } | 892 } |
| 832 | 893 |
| 833 int HttpCache::Transaction::RestartNetworkRequestWithAuth( | 894 int HttpCache::Transaction::RestartNetworkRequestWithAuth( |
| 834 const std::wstring& username, | 895 const std::wstring& username, |
| 835 const std::wstring& password) { | 896 const std::wstring& password) { |
| 836 DCHECK(mode_ & WRITE || mode_ == NONE); | 897 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 837 DCHECK(network_trans_.get()); | 898 DCHECK(network_trans_.get()); |
| 899 DCHECK_EQ(STATE_NONE, next_state_); |
| 838 | 900 |
| 901 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 839 int rv = network_trans_->RestartWithAuth(username, password, | 902 int rv = network_trans_->RestartWithAuth(username, password, |
| 840 &network_info_callback_); | 903 &network_callback_); |
| 841 if (rv != ERR_IO_PENDING) | 904 if (rv != ERR_IO_PENDING) |
| 842 OnNetworkInfoAvailable(rv); | 905 return DoLoop(rv); |
| 843 return rv; | 906 return rv; |
| 844 } | 907 } |
| 845 | 908 |
| 846 bool HttpCache::Transaction::RequiresValidation() { | 909 bool HttpCache::Transaction::RequiresValidation() { |
| 847 // TODO(darin): need to do more work here: | 910 // TODO(darin): need to do more work here: |
| 848 // - make sure we have a matching request method | 911 // - make sure we have a matching request method |
| 849 // - watch out for cached responses that depend on authentication | 912 // - watch out for cached responses that depend on authentication |
| 850 // In playback mode, nothing requires validation. | 913 // In playback mode, nothing requires validation. |
| 851 if (cache_->mode() == net::HttpCache::PLAYBACK) | 914 if (cache_->mode() == net::HttpCache::PLAYBACK) |
| 852 return false; | 915 return false; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1028 } else if (mode_ & READ && entry_) { | 1091 } else if (mode_ & READ && entry_) { |
| 1029 cache_->DoneReadingFromEntry(entry_, this); | 1092 cache_->DoneReadingFromEntry(entry_, this); |
| 1030 } | 1093 } |
| 1031 | 1094 |
| 1032 partial_.reset(NULL); | 1095 partial_.reset(NULL); |
| 1033 entry_ = NULL; | 1096 entry_ = NULL; |
| 1034 mode_ = NONE; | 1097 mode_ = NONE; |
| 1035 } | 1098 } |
| 1036 | 1099 |
| 1037 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) { | 1100 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) { |
| 1038 int rv = network_trans_->Read(data, data_len, &network_read_callback_); | |
| 1039 read_buf_ = data; | 1101 read_buf_ = data; |
| 1040 read_buf_len_ = data_len; | 1102 read_buf_len_ = data_len; |
| 1041 if (rv >= 0) | 1103 next_state_ = STATE_NETWORK_READ; |
| 1042 rv = DoNetworkReadCompleted(rv); | 1104 return DoLoop(OK); |
| 1043 return rv; | 1105 } |
| 1106 |
| 1107 int HttpCache::Transaction::DoNetworkRead() { |
| 1108 next_state_ = STATE_NETWORK_READ_COMPLETE; |
| 1109 return network_trans_->Read(read_buf_, read_buf_len_, &network_callback_); |
| 1044 } | 1110 } |
| 1045 | 1111 |
| 1046 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) { | 1112 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) { |
| 1047 DCHECK(entry_); | |
| 1048 int rv; | |
| 1049 cache_read_callback_->AddRef(); // Balanced in OnCacheReadCompleted. | |
| 1050 if (partial_.get()) { | |
| 1051 rv = partial_->CacheRead(entry_->disk_entry, data, data_len, | |
| 1052 cache_read_callback_); | |
| 1053 } else { | |
| 1054 rv = entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_, | |
| 1055 data, data_len, cache_read_callback_); | |
| 1056 } | |
| 1057 read_buf_ = data; | 1113 read_buf_ = data; |
| 1058 read_buf_len_ = data_len; | 1114 read_buf_len_ = data_len; |
| 1059 if (rv != ERR_IO_PENDING) | 1115 next_state_ = STATE_CACHE_READ_DATA; |
| 1060 cache_read_callback_->Release(); | 1116 return DoLoop(OK); |
| 1117 } |
| 1061 | 1118 |
| 1062 if (rv >= 0) | 1119 int HttpCache::Transaction::DoCacheReadData() { |
| 1063 rv = DoCacheReadCompleted(rv); | 1120 DCHECK(entry_); |
| 1121 next_state_ = STATE_CACHE_READ_DATA_COMPLETE; |
| 1122 cache_callback_->AddRef(); // Balanced in DoCacheReadDataComplete. |
| 1123 if (partial_.get()) { |
| 1124 return partial_->CacheRead(entry_->disk_entry, read_buf_, read_buf_len_, |
| 1125 cache_callback_); |
| 1126 } |
| 1064 | 1127 |
| 1065 return rv; | 1128 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_, |
| 1129 read_buf_, read_buf_len_, |
| 1130 cache_callback_); |
| 1066 } | 1131 } |
| 1067 | 1132 |
| 1068 int HttpCache::Transaction::ReadResponseInfoFromEntry() { | 1133 int HttpCache::Transaction::ReadResponseInfoFromEntry() { |
| 1069 DCHECK(entry_); | 1134 DCHECK(entry_); |
| 1070 | 1135 |
| 1071 LoadLog::BeginEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO); | 1136 LoadLog::BeginEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO); |
| 1072 bool read_ok = | 1137 bool read_ok = |
| 1073 HttpCache::ReadResponseInfo(entry_->disk_entry, &response_, &truncated_); | 1138 HttpCache::ReadResponseInfo(entry_->disk_entry, &response_, &truncated_); |
| 1074 LoadLog::EndEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO); | 1139 LoadLog::EndEvent(load_log_, LoadLog::TYPE_HTTP_CACHE_READ_INFO); |
| 1075 | 1140 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1169 } | 1234 } |
| 1170 | 1235 |
| 1171 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) { | 1236 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) { |
| 1172 cache_->DoneWithEntry(entry_, this, false); | 1237 cache_->DoneWithEntry(entry_, this, false); |
| 1173 cache_->DoomEntry(cache_key_); | 1238 cache_->DoomEntry(cache_key_); |
| 1174 entry_ = NULL; | 1239 entry_ = NULL; |
| 1175 if (delete_object) | 1240 if (delete_object) |
| 1176 partial_.reset(NULL); | 1241 partial_.reset(NULL); |
| 1177 } | 1242 } |
| 1178 | 1243 |
| 1179 int HttpCache::Transaction::DoNetworkReadCompleted(int result) { | 1244 int HttpCache::Transaction::DoNetworkReadComplete(int result) { |
| 1180 DCHECK(mode_ & WRITE || mode_ == NONE); | 1245 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 1181 | 1246 |
| 1182 if (!cache_) | 1247 if (!cache_) |
| 1183 return HandleResult(ERR_UNEXPECTED); | 1248 return ERR_UNEXPECTED; |
| 1184 | 1249 |
| 1185 cache_write_callback_->AddRef(); // Balanced in DoCacheWriteCompleted. | 1250 next_state_ = STATE_CACHE_WRITE_DATA; |
| 1251 return result; |
| 1252 } |
| 1186 | 1253 |
| 1187 result = AppendResponseDataToEntry(read_buf_, result, cache_write_callback_); | 1254 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) { |
| 1188 if (result == ERR_IO_PENDING) | 1255 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE; |
| 1189 return result; | 1256 cache_callback_->AddRef(); // Balanced in DoCacheWriteDataComplete. |
| 1190 | 1257 |
| 1191 return DoCacheWriteCompleted(result); | 1258 return AppendResponseDataToEntry(read_buf_, num_bytes, cache_callback_); |
| 1192 } | 1259 } |
| 1193 | 1260 |
| 1194 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) { | 1261 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) { |
| 1195 partial_->OnNetworkReadCompleted(result); | 1262 partial_->OnNetworkReadCompleted(result); |
| 1196 | 1263 |
| 1197 if (result == 0) { // End of file. | 1264 if (result == 0) { // End of file. |
| 1198 if (mode_ == READ_WRITE) { | 1265 if (mode_ == READ_WRITE) { |
| 1199 // We need to move on to the next range. | 1266 // We need to move on to the next range. |
| 1200 network_trans_.reset(); | 1267 network_trans_.reset(); |
| 1201 result = ContinuePartialCacheValidation(); | 1268 result = ContinuePartialCacheValidation(); |
| 1202 if (result != OK) | 1269 if (result != OK) |
| 1203 // Any error was already handled. | 1270 // Any error was already handled. |
| 1204 return result; | 1271 return result; |
| 1205 } | 1272 } |
| 1206 DoneWritingToEntry(true); | 1273 DoneWritingToEntry(true); |
| 1207 } | 1274 } |
| 1208 return HandleResult(result); | 1275 return result; |
| 1209 } | 1276 } |
| 1210 | 1277 |
| 1211 int HttpCache::Transaction::DoCacheReadCompleted(int result) { | 1278 int HttpCache::Transaction::DoCacheReadDataComplete(int result) { |
| 1212 DCHECK(cache_); | 1279 cache_callback_->Release(); // Balance the AddRef from DoCacheReadData. |
| 1213 | 1280 |
| 1214 if (!cache_) | 1281 if (!cache_) |
| 1215 return HandleResult(ERR_UNEXPECTED); | 1282 return ERR_UNEXPECTED; |
| 1216 | 1283 |
| 1217 if (partial_.get()) | 1284 if (partial_.get()) |
| 1218 return DoPartialCacheReadCompleted(result); | 1285 return DoPartialCacheReadCompleted(result); |
| 1219 | 1286 |
| 1220 if (result > 0) { | 1287 if (result > 0) { |
| 1221 read_offset_ += result; | 1288 read_offset_ += result; |
| 1222 } else if (result == 0) { // End of file. | 1289 } else if (result == 0) { // End of file. |
| 1223 cache_->DoneReadingFromEntry(entry_, this); | 1290 cache_->DoneReadingFromEntry(entry_, this); |
| 1224 entry_ = NULL; | 1291 entry_ = NULL; |
| 1225 } | 1292 } |
| 1226 return HandleResult(result); | 1293 return result; |
| 1227 } | 1294 } |
| 1228 | 1295 |
| 1229 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) { | 1296 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) { |
| 1230 partial_->OnCacheReadCompleted(result); | 1297 partial_->OnCacheReadCompleted(result); |
| 1231 | 1298 |
| 1232 if (result == 0) { // End of file. | 1299 if (result == 0) { // End of file. |
| 1233 if (partial_.get() && mode_ == READ_WRITE) { | 1300 if (partial_.get() && mode_ == READ_WRITE) { |
| 1234 // We need to move on to the next range. | 1301 // We need to move on to the next range. |
| 1235 result = ContinuePartialCacheValidation(); | 1302 result = ContinuePartialCacheValidation(); |
| 1236 if (result != OK || !entry_) { | 1303 if (result != OK || !entry_) { |
| 1237 // Any error was already handled. | 1304 // Any error was already handled. |
| 1238 return result; | 1305 return result; |
| 1239 } | 1306 } |
| 1240 cache_->ConvertWriterToReader(entry_); | 1307 cache_->ConvertWriterToReader(entry_); |
| 1241 } | 1308 } |
| 1242 cache_->DoneReadingFromEntry(entry_, this); | 1309 cache_->DoneReadingFromEntry(entry_, this); |
| 1243 entry_ = NULL; | 1310 entry_ = NULL; |
| 1244 } | 1311 } |
| 1245 return HandleResult(result); | 1312 return result; |
| 1246 } | 1313 } |
| 1247 | 1314 |
| 1248 int HttpCache::Transaction::DoCacheWriteCompleted(int result) { | 1315 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) { |
| 1249 DCHECK(cache_); | 1316 // Balance the AddRef from DoCacheWriteData. |
| 1250 // Balance the AddRef from DoNetworkReadCompleted. | 1317 cache_callback_->Release(); |
| 1251 cache_write_callback_->Release(); | |
| 1252 if (!cache_) | 1318 if (!cache_) |
| 1253 return HandleResult(ERR_UNEXPECTED); | 1319 return ERR_UNEXPECTED; |
| 1254 | 1320 |
| 1255 if (result < 0) | 1321 if (result < 0) |
| 1256 return HandleResult(result); | 1322 return result; |
| 1257 | 1323 |
| 1258 if (partial_.get()) | 1324 if (partial_.get()) |
| 1259 return DoPartialNetworkReadCompleted(result); | 1325 return DoPartialNetworkReadCompleted(result); |
| 1260 | 1326 |
| 1261 if (result == 0) // End of file. | 1327 if (result == 0) // End of file. |
| 1262 DoneWritingToEntry(true); | 1328 DoneWritingToEntry(true); |
| 1263 | 1329 |
| 1264 return HandleResult(result); | 1330 return result; |
| 1265 } | 1331 } |
| 1266 | 1332 |
| 1267 void HttpCache::Transaction::OnNetworkInfoAvailable(int result) { | 1333 int HttpCache::Transaction::DoSendRequestComplete(int result) { |
| 1268 DCHECK(result != ERR_IO_PENDING); | 1334 if (!cache_) |
| 1269 | 1335 return ERR_UNEXPECTED; |
| 1270 if (!cache_) { | |
| 1271 HandleResult(ERR_UNEXPECTED); | |
| 1272 return; | |
| 1273 } | |
| 1274 | 1336 |
| 1275 if (result == OK) { | 1337 if (result == OK) { |
| 1276 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); | 1338 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); |
| 1277 if (new_response->headers->response_code() == 401 || | 1339 if (new_response->headers->response_code() == 401 || |
| 1278 new_response->headers->response_code() == 407) { | 1340 new_response->headers->response_code() == 407) { |
| 1279 auth_response_ = *new_response; | 1341 auth_response_ = *new_response; |
| 1280 } else { | 1342 } else { |
| 1281 bool partial_content; | 1343 bool partial_content; |
| 1282 if (!ValidatePartialResponse(new_response->headers, &partial_content) && | 1344 if (!ValidatePartialResponse(new_response->headers, &partial_content) && |
| 1283 !auth_response_.headers && callback_) { | 1345 !auth_response_.headers) { |
| 1284 // Something went wrong with this request and we have to restart it. | 1346 // Something went wrong with this request and we have to restart it. |
| 1285 // If there is no callback we'll return OK to the caller so we cannot | 1347 // If we have an authentication response, we are exposed to weird things |
| 1286 // restart the request. If we have an authentication response, we are | 1348 // hapenning if the user cancels the authentication before we receive |
| 1287 // exposed to weird things hapenning if the user cancels the | 1349 // the new response. |
| 1288 // authentication before we receive the new response. | 1350 response_ = HttpResponseInfo(); |
| 1289 network_trans_.reset(); | 1351 network_trans_.reset(); |
| 1290 response_ = HttpResponseInfo(); | 1352 next_state_ = STATE_SEND_REQUEST; |
| 1291 BeginNetworkRequest(); | 1353 return OK; |
| 1292 return; | |
| 1293 } | 1354 } |
| 1294 if (partial_content && mode_ == READ_WRITE && !truncated_ && | 1355 if (partial_content && mode_ == READ_WRITE && !truncated_ && |
| 1295 response_.headers->response_code() == 200) { | 1356 response_.headers->response_code() == 200) { |
| 1296 // We have stored the full entry, but it changed and the server is | 1357 // We have stored the full entry, but it changed and the server is |
| 1297 // sending a range. We have to delete the old entry. | 1358 // sending a range. We have to delete the old entry. |
| 1298 DoneWritingToEntry(false); | 1359 DoneWritingToEntry(false); |
| 1299 } | 1360 } |
| 1300 | 1361 |
| 1301 // Are we expecting a response to a conditional query? | 1362 // Are we expecting a response to a conditional query? |
| 1302 if (mode_ == READ_WRITE || mode_ == UPDATE) { | 1363 if (mode_ == READ_WRITE || mode_ == UPDATE) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1352 // Truncate response data. | 1413 // Truncate response data. |
| 1353 TruncateResponseData(); | 1414 TruncateResponseData(); |
| 1354 | 1415 |
| 1355 // If this response is a redirect, then we can stop writing now. (We | 1416 // If this response is a redirect, then we can stop writing now. (We |
| 1356 // don't need to cache the response body of a redirect.) | 1417 // don't need to cache the response body of a redirect.) |
| 1357 if (response_.headers->IsRedirect(NULL)) | 1418 if (response_.headers->IsRedirect(NULL)) |
| 1358 DoneWritingToEntry(true); | 1419 DoneWritingToEntry(true); |
| 1359 } | 1420 } |
| 1360 if (reading_ && partial_.get()) { | 1421 if (reading_ && partial_.get()) { |
| 1361 if (network_trans_.get()) { | 1422 if (network_trans_.get()) { |
| 1423 next_state_ = STATE_NETWORK_READ_COMPLETE; |
| 1362 result = ReadFromNetwork(read_buf_, read_buf_len_); | 1424 result = ReadFromNetwork(read_buf_, read_buf_len_); |
| 1363 } else { | 1425 } else { |
| 1426 next_state_ = STATE_CACHE_READ_DATA_COMPLETE; |
| 1364 result = ReadFromEntry(read_buf_, read_buf_len_); | 1427 result = ReadFromEntry(read_buf_, read_buf_len_); |
| 1365 } | 1428 } |
| 1366 if (result >= 0 || result == net::ERR_IO_PENDING) | 1429 if (result >= 0 || result == ERR_IO_PENDING) { |
| 1367 return; | 1430 // Keep looping. |
| 1431 return result; |
| 1432 } else { |
| 1433 // Don't keep looping when we return. |
| 1434 next_state_ = STATE_NONE; |
| 1435 } |
| 1368 } else if (mode_ != NONE && partial_.get()) { | 1436 } else if (mode_ != NONE && partial_.get()) { |
| 1369 // We are about to return the headers for a byte-range request to the | 1437 // We are about to return the headers for a byte-range request to the |
| 1370 // user, so let's fix them. | 1438 // user, so let's fix them. |
| 1371 partial_->FixResponseHeaders(response_.headers); | 1439 partial_->FixResponseHeaders(response_.headers); |
| 1372 } | 1440 } |
| 1373 } | 1441 } |
| 1374 } else if (IsCertificateError(result)) { | 1442 } else if (IsCertificateError(result)) { |
| 1375 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); | 1443 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); |
| 1376 // If we get a certificate error, then there is a certificate in ssl_info, | 1444 // If we get a certificate error, then there is a certificate in ssl_info, |
| 1377 // so GetResponseInfo() should never returns NULL here. | 1445 // so GetResponseInfo() should never returns NULL here. |
| 1378 DCHECK(response); | 1446 DCHECK(response); |
| 1379 response_.ssl_info = response->ssl_info; | 1447 response_.ssl_info = response->ssl_info; |
| 1380 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { | 1448 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 1381 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); | 1449 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); |
| 1382 DCHECK(response); | 1450 DCHECK(response); |
| 1383 response_.cert_request_info = response->cert_request_info; | 1451 response_.cert_request_info = response->cert_request_info; |
| 1384 } | 1452 } |
| 1385 HandleResult(result); | 1453 return result; |
| 1386 } | 1454 } |
| 1387 | 1455 |
| 1388 void HttpCache::Transaction::OnNetworkReadCompleted(int result) { | 1456 void HttpCache::Transaction::OnIOComplete(int result) { |
| 1389 DoNetworkReadCompleted(result); | 1457 DoLoop(result); |
| 1390 } | |
| 1391 | |
| 1392 void HttpCache::Transaction::OnCacheReadCompleted(int result) { | |
| 1393 cache_read_callback_->Release(); // Balance the AddRef from ReadFromEntry. | |
| 1394 DoCacheReadCompleted(result); | |
| 1395 } | |
| 1396 | |
| 1397 void HttpCache::Transaction::OnCacheWriteCompleted(int result) { | |
| 1398 DoCacheWriteCompleted(result); | |
| 1399 } | |
| 1400 | |
| 1401 void HttpCache::Transaction::OnCacheEntryReady(int result) { | |
| 1402 DCHECK_EQ(OK, result); | |
| 1403 ValidateEntryHeadersAndContinue(true); | |
| 1404 } | 1458 } |
| 1405 | 1459 |
| 1406 } // namespace net | 1460 } // namespace net |
| OLD | NEW |