[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/http/failing_http_transaction_factory.h" |
| 6 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/compiler_specific.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 11 | #include "base/location.h" |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 12 | #include "base/logging.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
| 14 | #include "base/thread_task_runner_handle.h" |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 15 | #include "net/base/load_timing_info.h" |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 16 | #include "net/base/net_error_details.h" |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 17 | #include "net/base/upload_progress.h" |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 18 | #include "net/http/http_response_info.h" |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 19 | #include "net/socket/connection_attempts.h" |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | |
| 23 | class AuthCredentials; |
| 24 | class BoundNetLog; |
| 25 | class HttpRequestHeaders; |
| 26 | class IOBuffer; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 27 | class SSLPrivateKey; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 28 | class X509Certificate; |
| 29 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // A simple class to interpose between the cache and network http layers. |
| 33 | // These transactions can be generated by the FailingHttpTransactionFactory |
| 34 | // to test interactions between cache and network. |
| 35 | class FailingHttpTransaction : public HttpTransaction { |
| 36 | public: |
| 37 | explicit FailingHttpTransaction(Error error); |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 38 | ~FailingHttpTransaction() override; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 39 | |
| 40 | // HttpTransaction |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 41 | int Start(const HttpRequestInfo* request_info, |
| 42 | const CompletionCallback& callback, |
| 43 | const BoundNetLog& net_log) override; |
| 44 | int RestartIgnoringLastError(const CompletionCallback& callback) override; |
| 45 | int RestartWithCertificate(X509Certificate* client_cert, |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 46 | SSLPrivateKey* client_private_key, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 47 | const CompletionCallback& callback) override; |
| 48 | int RestartWithAuth(const AuthCredentials& credentials, |
| 49 | const CompletionCallback& callback) override; |
| 50 | bool IsReadyToRestartForAuth() override; |
| 51 | int Read(IOBuffer* buf, |
| 52 | int buf_len, |
| 53 | const CompletionCallback& callback) override; |
| 54 | void StopCaching() override; |
| 55 | bool GetFullRequestHeaders(HttpRequestHeaders* headers) const override; |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 56 | int64_t GetTotalReceivedBytes() const override; |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 57 | int64_t GetTotalSentBytes() const override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 58 | void DoneReading() override; |
| 59 | const HttpResponseInfo* GetResponseInfo() const override; |
| 60 | LoadState GetLoadState() const override; |
| 61 | UploadProgress GetUploadProgress() const override; |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 62 | void SetQuicServerInfo(QuicServerInfo* quic_server_info) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 63 | bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 64 | bool GetRemoteEndpoint(IPEndPoint* endpoint) const override; |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 65 | void PopulateNetErrorDetails(NetErrorDetails* details) const override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 66 | void SetPriority(RequestPriority priority) override; |
| 67 | void SetWebSocketHandshakeStreamCreateHelper( |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 68 | WebSocketHandshakeStreamBase::CreateHelper* create_helper) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 69 | void SetBeforeNetworkStartCallback( |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 70 | const BeforeNetworkStartCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 71 | void SetBeforeProxyHeadersSentCallback( |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 72 | const BeforeProxyHeadersSentCallback& callback) override; |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 73 | int ResumeNetworkStart() override; |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 74 | void GetConnectionAttempts(ConnectionAttempts* out) const override; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | Error error_; |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 78 | HttpResponseInfo response_; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | FailingHttpTransaction::FailingHttpTransaction(Error error) : error_(error) { |
| 82 | DCHECK_LT(error, OK); |
| 83 | } |
| 84 | |
| 85 | FailingHttpTransaction::~FailingHttpTransaction() {} |
| 86 | |
| 87 | int FailingHttpTransaction::Start(const HttpRequestInfo* request_info, |
| 88 | const CompletionCallback& callback, |
| 89 | const BoundNetLog& net_log) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 90 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 91 | base::Bind(callback, error_)); |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 92 | return ERR_IO_PENDING; |
| 93 | } |
| 94 | |
| 95 | int FailingHttpTransaction::RestartIgnoringLastError( |
| 96 | const CompletionCallback& callback) { |
| 97 | return ERR_FAILED; |
| 98 | } |
| 99 | |
| 100 | int FailingHttpTransaction::RestartWithCertificate( |
| 101 | X509Certificate* client_cert, |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 102 | SSLPrivateKey* client_private_key, |
| 103 | const CompletionCallback& callback) { |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 104 | return ERR_FAILED; |
| 105 | } |
| 106 | |
| 107 | int FailingHttpTransaction::RestartWithAuth( |
| 108 | const AuthCredentials& credentials, |
| 109 | const CompletionCallback& callback) { |
| 110 | return ERR_FAILED; |
| 111 | } |
| 112 | |
| 113 | bool FailingHttpTransaction::IsReadyToRestartForAuth() { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | int FailingHttpTransaction::Read(IOBuffer* buf, int buf_len, |
| 118 | const CompletionCallback& callback) { |
| 119 | NOTREACHED(); |
| 120 | return ERR_FAILED; |
| 121 | } |
| 122 | |
| 123 | void FailingHttpTransaction::StopCaching() {} |
| 124 | |
| 125 | bool FailingHttpTransaction::GetFullRequestHeaders( |
| 126 | HttpRequestHeaders* headers) const { |
| 127 | return false; |
| 128 | } |
| 129 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 130 | int64_t FailingHttpTransaction::GetTotalReceivedBytes() const { |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 134 | int64_t FailingHttpTransaction::GetTotalSentBytes() const { |
| 135 | return 0; |
| 136 | } |
| 137 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 138 | void FailingHttpTransaction::DoneReading() { |
| 139 | NOTREACHED(); |
| 140 | } |
| 141 | |
| 142 | const HttpResponseInfo* FailingHttpTransaction::GetResponseInfo() const { |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 143 | return &response_; |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | LoadState FailingHttpTransaction::GetLoadState() const { |
| 147 | return LOAD_STATE_IDLE; |
| 148 | } |
| 149 | |
| 150 | UploadProgress FailingHttpTransaction::GetUploadProgress() const { |
| 151 | return UploadProgress(); |
| 152 | } |
| 153 | |
| 154 | void FailingHttpTransaction::SetQuicServerInfo( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 155 | QuicServerInfo* quic_server_info) { |
| 156 | } |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 157 | |
| 158 | bool FailingHttpTransaction::GetLoadTimingInfo( |
| 159 | LoadTimingInfo* load_timing_info) const { |
| 160 | return false; |
| 161 | } |
| 162 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 163 | bool FailingHttpTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const { |
| 164 | return false; |
| 165 | } |
| 166 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 167 | void FailingHttpTransaction::PopulateNetErrorDetails( |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 168 | NetErrorDetails* /*details*/) const { |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 172 | void FailingHttpTransaction::SetPriority(RequestPriority priority) {} |
| 173 | |
| 174 | void FailingHttpTransaction::SetWebSocketHandshakeStreamCreateHelper( |
| 175 | WebSocketHandshakeStreamBase::CreateHelper* create_helper) { |
| 176 | NOTREACHED(); |
| 177 | } |
| 178 | |
| 179 | void FailingHttpTransaction::SetBeforeNetworkStartCallback( |
| 180 | const BeforeNetworkStartCallback& callback) { |
| 181 | } |
| 182 | |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 183 | void FailingHttpTransaction::SetBeforeProxyHeadersSentCallback( |
| 184 | const BeforeProxyHeadersSentCallback& callback) { |
| 185 | } |
| 186 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 187 | int FailingHttpTransaction::ResumeNetworkStart() { |
| 188 | NOTREACHED(); |
| 189 | return ERR_FAILED; |
| 190 | } |
| 191 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 192 | void FailingHttpTransaction::GetConnectionAttempts( |
| 193 | ConnectionAttempts* out) const { |
| 194 | NOTIMPLEMENTED(); |
| 195 | } |
| 196 | |
[email protected] | dcbe3df | 2014-02-06 23:08:37 | [diff] [blame] | 197 | } // namespace |
| 198 | |
| 199 | FailingHttpTransactionFactory::FailingHttpTransactionFactory( |
| 200 | HttpNetworkSession* session, |
| 201 | Error error) : session_(session), error_(error) { |
| 202 | DCHECK_LT(error, OK); |
| 203 | } |
| 204 | |
| 205 | FailingHttpTransactionFactory::~FailingHttpTransactionFactory() {} |
| 206 | |
| 207 | // HttpTransactionFactory: |
| 208 | int FailingHttpTransactionFactory::CreateTransaction( |
| 209 | RequestPriority priority, |
| 210 | scoped_ptr<HttpTransaction>* trans) { |
| 211 | trans->reset(new FailingHttpTransaction(error_)); |
| 212 | return OK; |
| 213 | } |
| 214 | |
| 215 | HttpCache* FailingHttpTransactionFactory::GetCache() { |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | HttpNetworkSession* FailingHttpTransactionFactory::GetSession() { |
| 220 | return session_; |
| 221 | } |
| 222 | |
| 223 | } // namespace net |