OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/certificate_transparency/log_proof_fetcher.h" | 5 #include "components/certificate_transparency/log_proof_fetcher.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/format_macros.h" | |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "base/strings/stringprintf.h" | |
12 #include "base/values.h" | 14 #include "base/values.h" |
13 #include "components/safe_json/safe_json_parser.h" | 15 #include "components/safe_json/safe_json_parser.h" |
14 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
15 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
16 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
17 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
18 #include "net/cert/ct_log_response_parser.h" | 20 #include "net/cert/ct_log_response_parser.h" |
19 #include "net/cert/signed_tree_head.h" | 21 #include "net/cert/signed_tree_head.h" |
20 #include "net/http/http_status_code.h" | 22 #include "net/http/http_status_code.h" |
21 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
35 case net::URLRequestStatus::FAILED: | 37 case net::URLRequestStatus::FAILED: |
36 return status.error(); | 38 return status.error(); |
37 default: | 39 default: |
38 NOTREACHED(); | 40 NOTREACHED(); |
39 return net::ERR_FAILED; | 41 return net::ERR_FAILED; |
40 } | 42 } |
41 } | 43 } |
42 | 44 |
43 } // namespace | 45 } // namespace |
44 | 46 |
45 struct LogProofFetcher::FetchState { | 47 // Interface for handling the response from a CT log for particular |
46 FetchState(const std::string& log_id, | 48 // requests. |
47 const SignedTreeHeadFetchedCallback& fetched_callback, | 49 // All log responses are JSON and should be parsed; however the response |
48 const FetchFailedCallback& failed_callback); | 50 // to each request should be parsed and validated diffenetly. |
49 ~FetchState(); | 51 class LogProofFetcher::LogResponseHandler { |
50 | 52 public: |
51 std::string log_id; | 53 // |log_id| will be passed to the callback to indicate which log |
52 SignedTreeHeadFetchedCallback fetched_callback; | 54 // this failure pretains to. |
53 FetchFailedCallback failed_callback; | 55 // All requests could fail so the |failure_callback| is shared to all |
54 scoped_refptr<net::IOBufferWithSize> response_buffer; | 56 // request types. |
55 std::string assembled_response; | 57 LogResponseHandler( |
58 const std::string& log_id, | |
59 const LogProofFetcher::FetchFailedCallback& failure_callback); | |
60 virtual ~LogResponseHandler(); | |
61 | |
62 // Handle respones JSON that parsed successfully, usually by | |
63 // invoking the success callback. | |
64 virtual void HandleParsedJson(const base::Value& parsed_json) = 0; | |
65 | |
66 // Handle failure to parse response JSON, usually by invoking the failure | |
67 // callback with a request-specific net error code. | |
68 virtual void HandleJsonParseFailure(const std::string& json_error) = 0; | |
69 | |
70 // Handle network failure to complete the request to the log. | |
71 virtual void HandleNetFailure(int net_error, int http_response_code); | |
Ryan Sleevi
2015/12/08 01:13:52
nit: s/Net/Network/
mmenke
2015/12/08 17:10:12
net may actually be more accurate - net::kErrorDom
Eran Messeri
2015/12/14 13:01:42
Leaving as mmenke suggested.
| |
72 | |
73 protected: | |
74 const std::string log_id_; | |
75 const LogProofFetcher::FetchFailedCallback failure_callback_; | |
56 }; | 76 }; |
57 | 77 |
58 LogProofFetcher::FetchState::FetchState( | 78 LogProofFetcher::LogResponseHandler::LogResponseHandler( |
59 const std::string& log_id, | 79 const std::string& log_id, |
60 const SignedTreeHeadFetchedCallback& fetched_callback, | 80 const LogProofFetcher::FetchFailedCallback& failure_callback) |
61 const FetchFailedCallback& failed_callback) | 81 : log_id_(log_id), failure_callback_(failure_callback) { |
62 : log_id(log_id), | 82 DCHECK(!failure_callback_.is_null()); |
63 fetched_callback(fetched_callback), | 83 } |
64 failed_callback(failed_callback), | 84 |
65 response_buffer(new net::IOBufferWithSize(kMaxLogResponseSizeInBytes)) {} | 85 LogProofFetcher::LogResponseHandler::~LogResponseHandler() {} |
66 | 86 |
67 LogProofFetcher::FetchState::~FetchState() {} | 87 void LogProofFetcher::LogResponseHandler::HandleNetFailure( |
88 int net_error, | |
89 int http_response_code) { | |
90 failure_callback_.Run(log_id_, net_error, http_response_code); | |
91 } | |
92 | |
93 class LogProofFetcher::GetSTHLogResponseHandler | |
94 : public LogProofFetcher::LogResponseHandler { | |
95 public: | |
96 GetSTHLogResponseHandler( | |
97 const std::string& log_id, | |
98 const LogProofFetcher::SignedTreeHeadFetchedCallback& sth_fetch_callback, | |
99 const LogProofFetcher::FetchFailedCallback& failure_callback) | |
100 : LogResponseHandler(log_id, failure_callback), | |
101 sth_fetched_(sth_fetch_callback) {} | |
102 | |
103 // Fill a net::ct::SignedTreeHead instance from the parsed JSON and, if | |
104 // successful, invoke the success callback with this STH. | |
105 // Otherwise, invoke the failure callback. | |
106 void HandleParsedJson(const base::Value& parsed_json) override { | |
107 net::ct::SignedTreeHead signed_tree_head; | |
108 if (net::ct::FillSignedTreeHead(parsed_json, &signed_tree_head)) { | |
Ryan Sleevi
2015/12/08 01:13:52
nit: restructure this so errors first?
if (!net::
Eran Messeri
2015/12/14 13:01:42
Done - switched to using base::ResetAndReturn and
| |
109 sth_fetched_.Run(log_id_, signed_tree_head); | |
110 } else { | |
111 failure_callback_.Run(log_id_, net::ERR_CT_STH_INCOMPLETE, net::HTTP_OK); | |
112 } | |
113 } | |
114 | |
115 // Invoke the error callback indicating that STH parsing failed. | |
116 void HandleJsonParseFailure(const std::string& json_error) override { | |
117 failure_callback_.Run(log_id_, net::ERR_CT_STH_PARSING_FAILED, | |
118 net::HTTP_OK); | |
119 } | |
120 | |
121 private: | |
122 const LogProofFetcher::SignedTreeHeadFetchedCallback sth_fetched_; | |
123 }; | |
124 | |
125 class LogProofFetcher::GetConsistencyProofLogResponseHandler | |
126 : public LogProofFetcher::LogResponseHandler { | |
127 public: | |
128 GetConsistencyProofLogResponseHandler( | |
129 const std::string& log_id, | |
130 const LogProofFetcher::ConsistencyProofFetchedCallback& | |
131 proof_fetch_callback, | |
132 const LogProofFetcher::FetchFailedCallback& failure_callback) | |
133 : LogResponseHandler(log_id, failure_callback), | |
134 proof_fetched_(proof_fetch_callback) {} | |
135 | |
136 // Fill a vector of strings with nodes from the received consistency proof | |
137 // and, if successful, invoke the success callback with this vector. | |
138 // Otherwise, invoke the failure callback indicating proof parsing has failed. | |
139 void HandleParsedJson(const base::Value& parsed_json) override { | |
140 std::vector<std::string> consistency_proof; | |
141 if (net::ct::FillConsistencyProof(parsed_json, &consistency_proof)) { | |
Ryan Sleevi
2015/12/08 01:13:52
same comments re: error handling
Eran Messeri
2015/12/14 13:01:42
Done.
| |
142 proof_fetched_.Run(log_id_, consistency_proof); | |
143 } else { | |
144 failure_callback_.Run( | |
145 log_id_, net::ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED, net::HTTP_OK); | |
146 } | |
147 } | |
148 | |
149 // Invoke the error callback indicating proof fetching failed. | |
150 void HandleJsonParseFailure(const std::string& json_error) override { | |
151 failure_callback_.Run(log_id_, net::ERR_CT_CONSISTENCY_PROOF_PARSING_FAILED, | |
152 net::HTTP_OK); | |
153 } | |
154 | |
155 private: | |
156 const LogProofFetcher::ConsistencyProofFetchedCallback proof_fetched_; | |
157 }; | |
158 | |
159 // Class for issuing a particular request from a CT log and assembling the | |
160 // response. | |
161 // Creates the URLRequest instance for fetching the URL from the log | |
162 // (supplied as |request_url| in the c'tor) and implements the | |
163 // URLRequest::Delegate interface for assembling the response. | |
164 class LogProofFetcher::LogFetcher : public net::URLRequest::Delegate { | |
165 public: | |
166 using SuccessCallback = base::Callback<void(LogFetcher*)>; | |
167 using FailureCallback = base::Callback<void(LogFetcher*, int, int)>; | |
168 LogFetcher(net::URLRequestContext* request_context, | |
169 const GURL& request_url, | |
170 const SuccessCallback& success_callback, | |
171 const FailureCallback& failure_callback); | |
172 // Nothing explicit; The URLRequest instance held will be deleted | |
173 // automatically. | |
Ryan Sleevi
2015/12/08 01:13:52
I'm having trouble trying to figure out what you'r
Eran Messeri
2015/12/14 13:01:42
Removed the comment ; I wanted to say there's no n
| |
174 ~LogFetcher() override {} | |
175 | |
176 // net::URLRequest::Delegate | |
177 void OnResponseStarted(net::URLRequest* request) override; | |
178 void OnReadCompleted(net::URLRequest* request, int bytes_read) override; | |
179 | |
180 const std::string& assembled_response() { return assembled_response_; } | |
181 | |
182 private: | |
183 // Handles the final result of a URLRequest::Read call on the request. | |
184 // Returns true if another read should be started, false if the read | |
185 // failed completely or we have to wait for OnResponseStarted to | |
186 // be called. | |
187 bool HandleReadResult(int bytes_read); | |
188 | |
189 // Calls URLRequest::Read on |request| repeatedly, until HandleReadResult | |
190 // indicates it should no longer be called. Usually this would be when there | |
191 // is pending IO that requires waiting for OnResponseStarted to be called. | |
192 void StartNextRead(); | |
193 | |
194 // Invokes the success callback. | |
195 void RequestComplete(); | |
196 // Invokes the failure callback with the supplied error information. | |
Ryan Sleevi
2015/12/08 01:13:52
newline between 195 and 196
Eran Messeri
2015/12/14 13:01:42
N/A anymore.
| |
197 // After this method the LogFetcher instance may be deleted and |this| | |
198 // no longer valid. | |
199 void InvokeFailureCallback(int net_error, int http_response_code); | |
200 | |
201 scoped_ptr<net::URLRequest> url_request_; | |
202 const GURL request_url_; | |
203 SuccessCallback success_callback_; | |
204 FailureCallback failure_callback_; | |
205 scoped_refptr<net::IOBufferWithSize> response_buffer_; | |
206 std::string assembled_response_; | |
207 | |
208 DISALLOW_COPY_AND_ASSIGN(LogFetcher); | |
209 }; | |
210 | |
211 LogProofFetcher::LogFetcher::LogFetcher(net::URLRequestContext* request_context, | |
212 const GURL& request_url, | |
213 const SuccessCallback& success_callback, | |
214 const FailureCallback& failure_callback) | |
215 : request_url_(request_url), | |
216 success_callback_(success_callback), | |
217 failure_callback_(failure_callback), | |
218 response_buffer_(new net::IOBufferWithSize( | |
219 LogProofFetcher::kMaxLogResponseSizeInBytes)) { | |
Ryan Sleevi
2015/12/08 01:13:52
Is this really sufficient? I didn't see any commen
Eran Messeri
2015/12/14 13:01:42
Good point - added documentation to the constant w
| |
220 DCHECK(request_url_.SchemeIsHTTPOrHTTPS()); | |
221 url_request_ = | |
222 request_context->CreateRequest(request_url_, net::DEFAULT_PRIORITY, this); | |
223 // This request should not send any cookies or otherwise identifying data | |
224 // as CT logs are expected to be publicly-accessible and connections to them | |
225 // stateless. | |
226 url_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
227 net::LOAD_DO_NOT_SAVE_COOKIES | | |
228 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
229 | |
230 url_request_->Start(); | |
231 } | |
232 | |
233 void LogProofFetcher::LogFetcher::OnResponseStarted(net::URLRequest* request) { | |
234 DCHECK_EQ(url_request_.get(), request); | |
235 net::URLRequestStatus status(request->status()); | |
236 | |
237 if (!status.is_success() || request->GetResponseCode() != net::HTTP_OK) { | |
Ryan Sleevi
2015/12/08 01:13:52
mmenke: Is this right? What about situations of ca
mmenke
2015/12/08 17:10:12
Redirects don't result in calling this method - th
Eran Messeri
2015/12/14 13:01:42
Cached responses are fine for all replies from the
| |
238 int net_error = net::OK; | |
239 int http_response_code = request->GetResponseCode(); | |
240 | |
241 DVLOG(1) << "Fetching STH from " << request_url_ | |
242 << " failed. status:" << status.status() | |
243 << " error:" << status.error() | |
244 << " http response code: " << http_response_code; | |
Ryan Sleevi
2015/12/08 01:13:52
Seems unnecessary; the NetLog will already have th
Eran Messeri
2015/12/14 13:01:42
Done.
| |
245 if (!status.is_success()) | |
246 net_error = GetNetErrorFromURLRequestStatus(status); | |
247 | |
248 InvokeFailureCallback(net_error, http_response_code); | |
249 return; | |
250 } | |
251 | |
252 StartNextRead(); | |
253 } | |
254 | |
255 void LogProofFetcher::LogFetcher::OnReadCompleted(net::URLRequest* request, | |
256 int bytes_read) { | |
257 DCHECK_EQ(url_request_.get(), request); | |
258 | |
259 if (HandleReadResult(bytes_read)) | |
260 StartNextRead(); | |
261 } | |
262 | |
263 bool LogProofFetcher::LogFetcher::HandleReadResult(int bytes_read) { | |
264 // Start by checking for an error condition. | |
265 // If there are errors, invoke the failure callback and clean up the | |
266 // request. | |
267 if (bytes_read == -1 || !url_request_->status().is_success()) { | |
Ryan Sleevi
2015/12/08 01:13:52
Shouldn't this be bytes_read < 0 ?
Eran Messeri
2015/12/14 13:01:42
Done.
| |
268 net::URLRequestStatus status(url_request_->status()); | |
269 DVLOG(1) << "Read error: " << status.status() << " " << status.error(); | |
270 InvokeFailureCallback(GetNetErrorFromURLRequestStatus(status), net::OK); | |
271 | |
272 return false; | |
273 } | |
274 | |
275 // Not an error, but no data available, so wait for OnReadCompleted | |
276 // callback. | |
277 if (url_request_->status().is_io_pending()) | |
278 return false; | |
279 | |
280 // Nothing more to read from the stream - finish handling the response. | |
281 if (bytes_read == 0) { | |
282 RequestComplete(); | |
283 return false; | |
284 } | |
285 | |
286 // We have data, collect it and indicate another read is needed. | |
287 DVLOG(1) << "Have " << bytes_read << " bytes to assemble."; | |
Ryan Sleevi
2015/12/08 01:13:52
Unnecessary?
Eran Messeri
2015/12/14 13:01:42
Removed, although I originally added it to identif
| |
288 DCHECK_GE(bytes_read, 0); | |
289 assembled_response_.append(response_buffer_->data(), bytes_read); | |
Ryan Sleevi
2015/12/08 01:13:52
Suggestion: Alternatively write this as
if (byte
Eran Messeri
2015/12/14 13:01:42
Good point, done.
| |
290 if (assembled_response_.size() > | |
291 LogProofFetcher::kMaxLogResponseSizeInBytes) { | |
292 // Log response is too big, invoke the failure callback. | |
293 InvokeFailureCallback(net::ERR_FILE_TOO_BIG, net::HTTP_OK); | |
294 return false; | |
295 } | |
296 | |
297 return true; | |
298 } | |
299 | |
300 void LogProofFetcher::LogFetcher::StartNextRead() { | |
301 bool continue_reading = true; | |
302 while (continue_reading) { | |
303 int read_bytes = 0; | |
304 url_request_->Read(response_buffer_.get(), response_buffer_->size(), | |
Ryan Sleevi
2015/12/08 01:13:52
1) Why aren't you checking this return code?
2) Wh
mmenke
2015/12/08 17:10:12
url_request_->Read response codes are really weird
Eran Messeri
2015/12/14 13:01:42
There is a test case covering a response that's to
mmenke
2015/12/16 15:45:28
Ah, right...we append to a string after each read,
| |
305 &read_bytes); | |
306 continue_reading = HandleReadResult(read_bytes); | |
307 } | |
308 } | |
309 | |
310 void LogProofFetcher::LogFetcher::RequestComplete() { | |
311 // Get rid of the buffer as it really isn't necessary. | |
312 response_buffer_ = nullptr; | |
313 success_callback_.Run(this); | |
314 } | |
315 | |
316 void LogProofFetcher::LogFetcher::InvokeFailureCallback( | |
317 int net_error, | |
318 int http_response_code) { | |
319 failure_callback_.Run(this, net_error, http_response_code); | |
320 // NOTE: |this| not valid after this callback as the LogFetcher instance | |
321 // invoking the callback will be deleted by the callback. | |
322 } | |
68 | 323 |
69 LogProofFetcher::LogProofFetcher(net::URLRequestContext* request_context) | 324 LogProofFetcher::LogProofFetcher(net::URLRequestContext* request_context) |
70 : request_context_(request_context), weak_factory_(this) { | 325 : request_context_(request_context), weak_factory_(this) { |
71 DCHECK(request_context); | 326 DCHECK(request_context); |
72 } | 327 } |
73 | 328 |
74 LogProofFetcher::~LogProofFetcher() { | 329 LogProofFetcher::~LogProofFetcher() { |
75 STLDeleteContainerPairPointers(inflight_requests_.begin(), | 330 STLDeleteContainerPairPointers(inflight_fetches_.begin(), |
76 inflight_requests_.end()); | 331 inflight_fetches_.end()); |
77 } | 332 } |
78 | 333 |
79 void LogProofFetcher::FetchSignedTreeHead( | 334 void LogProofFetcher::FetchSignedTreeHead( |
80 const GURL& base_log_url, | 335 const GURL& base_log_url, |
81 const std::string& log_id, | 336 const std::string& log_id, |
82 const SignedTreeHeadFetchedCallback& fetched_callback, | 337 const SignedTreeHeadFetchedCallback& fetched_callback, |
83 const FetchFailedCallback& failed_callback) { | 338 const FetchFailedCallback& failed_callback) { |
84 DCHECK(base_log_url.SchemeIsHTTPOrHTTPS()); | 339 const GURL request_url = base_log_url.Resolve("ct/v1/get-sth"); |
85 GURL fetch_url(base_log_url.Resolve("ct/v1/get-sth")); | 340 StartFetch(request_url, new GetSTHLogResponseHandler(log_id, fetched_callback, |
86 scoped_ptr<net::URLRequest> request = | 341 failed_callback)); |
87 request_context_->CreateRequest(fetch_url, net::DEFAULT_PRIORITY, this); | 342 } |
88 request->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 343 |
89 net::LOAD_DO_NOT_SAVE_COOKIES | | 344 void LogProofFetcher::FetchConsistencyProof( |
90 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 345 const GURL& base_log_url, |
91 | 346 const std::string& log_id, |
92 FetchState* fetch_state = | 347 uint64_t old_tree_size, |
93 new FetchState(log_id, fetched_callback, failed_callback); | 348 uint64_t new_tree_size, |
94 request->Start(); | 349 const ConsistencyProofFetchedCallback& fetched_callback, |
95 inflight_requests_.insert(std::make_pair(request.release(), fetch_state)); | 350 const FetchFailedCallback& failed_callback) { |
96 } | 351 const GURL request_url = base_log_url.Resolve(base::StringPrintf( |
97 | 352 "ct/v1/get-sth-consistency?first=%" PRIu64 "&second=%" PRIu64, |
98 void LogProofFetcher::OnResponseStarted(net::URLRequest* request) { | 353 old_tree_size, new_tree_size)); |
99 net::URLRequestStatus status(request->status()); | 354 StartFetch(request_url, new GetConsistencyProofLogResponseHandler( |
100 DCHECK(inflight_requests_.count(request)); | 355 log_id, fetched_callback, failed_callback)); |
101 FetchState* fetch_state = inflight_requests_.find(request)->second; | 356 } |
102 | 357 |
103 if (!status.is_success() || request->GetResponseCode() != net::HTTP_OK) { | 358 void LogProofFetcher::StartFetch(const GURL& request_url, |
104 int net_error = net::OK; | 359 LogResponseHandler* log_request) { |
105 int http_response_code = request->GetResponseCode(); | 360 LogFetcher::SuccessCallback success_callback = base::Bind( |
106 | 361 &LogProofFetcher::OnFetchCompleted, weak_factory_.GetWeakPtr()); |
107 DVLOG(1) << "Fetching STH from " << request->original_url() | 362 LogFetcher::FailureCallback failure_callback = |
108 << " failed. status:" << status.status() | 363 base::Bind(&LogProofFetcher::OnFetchFailed, weak_factory_.GetWeakPtr()); |
109 << " error:" << status.error() | 364 |
110 << " http response code: " << http_response_code; | 365 LogFetcher* fetch = new LogFetcher(request_context_, request_url, |
111 if (!status.is_success()) | 366 success_callback, failure_callback); |
112 net_error = GetNetErrorFromURLRequestStatus(status); | 367 inflight_fetches_.insert(std::make_pair(fetch, log_request)); |
113 | 368 } |
114 InvokeFailureCallback(request, net_error, http_response_code); | 369 |
115 return; | 370 void LogProofFetcher::OnFetchCompleted(LogFetcher* fetcher) { |
116 } | 371 DCHECK(inflight_fetches_.find(fetcher) != inflight_fetches_.end()); |
117 | 372 // The |fetcher| will be deleted once JSON parsing will end. |
118 StartNextRead(request, fetch_state); | 373 // It must exist until then as it holds the assembled response buffer, |
119 } | 374 // whose reference is taken here. |
120 | |
121 void LogProofFetcher::OnReadCompleted(net::URLRequest* request, | |
122 int bytes_read) { | |
123 DCHECK(inflight_requests_.count(request)); | |
124 FetchState* fetch_state = inflight_requests_.find(request)->second; | |
125 | |
126 if (HandleReadResult(request, fetch_state, bytes_read)) | |
127 StartNextRead(request, fetch_state); | |
128 } | |
129 | |
130 bool LogProofFetcher::HandleReadResult(net::URLRequest* request, | |
131 FetchState* fetch_state, | |
132 int bytes_read) { | |
133 // Start by checking for an error condition. | |
134 // If there are errors, invoke the failure callback and clean up the | |
135 // request. | |
136 if (bytes_read == -1 || !request->status().is_success()) { | |
137 net::URLRequestStatus status(request->status()); | |
138 DVLOG(1) << "Read error: " << status.status() << " " << status.error(); | |
139 InvokeFailureCallback(request, GetNetErrorFromURLRequestStatus(status), | |
140 net::OK); | |
141 | |
142 return false; | |
143 } | |
144 | |
145 // Not an error, but no data available, so wait for OnReadCompleted | |
146 // callback. | |
147 if (request->status().is_io_pending()) | |
148 return false; | |
149 | |
150 // Nothing more to read from the stream - finish handling the response. | |
151 if (bytes_read == 0) { | |
152 RequestComplete(request); | |
153 return false; | |
154 } | |
155 | |
156 // We have data, collect it and indicate another read is needed. | |
157 DVLOG(1) << "Have " << bytes_read << " bytes to assemble."; | |
158 DCHECK_GE(bytes_read, 0); | |
159 fetch_state->assembled_response.append(fetch_state->response_buffer->data(), | |
160 bytes_read); | |
161 if (fetch_state->assembled_response.size() > kMaxLogResponseSizeInBytes) { | |
162 // Log response is too big, invoke the failure callback. | |
163 InvokeFailureCallback(request, net::ERR_FILE_TOO_BIG, net::HTTP_OK); | |
164 return false; | |
165 } | |
166 | |
167 return true; | |
168 } | |
169 | |
170 void LogProofFetcher::StartNextRead(net::URLRequest* request, | |
171 FetchState* fetch_state) { | |
172 bool continue_reading = true; | |
173 while (continue_reading) { | |
174 int read_bytes = 0; | |
175 request->Read(fetch_state->response_buffer.get(), | |
176 fetch_state->response_buffer->size(), &read_bytes); | |
177 continue_reading = HandleReadResult(request, fetch_state, read_bytes); | |
178 } | |
179 } | |
180 | |
181 void LogProofFetcher::RequestComplete(net::URLRequest* request) { | |
182 DCHECK(inflight_requests_.count(request)); | |
183 | |
184 FetchState* fetch_state = inflight_requests_.find(request)->second; | |
185 | |
186 // Get rid of the buffer as it really isn't necessary. | |
187 fetch_state->response_buffer = nullptr; | |
188 safe_json::SafeJsonParser::Parse( | 375 safe_json::SafeJsonParser::Parse( |
189 fetch_state->assembled_response, | 376 fetcher->assembled_response(), |
190 base::Bind(&LogProofFetcher::OnSTHJsonParseSuccess, | 377 base::Bind(&LogProofFetcher::OnJsonParseSuccess, |
191 weak_factory_.GetWeakPtr(), request), | 378 weak_factory_.GetWeakPtr(), fetcher), |
192 base::Bind(&LogProofFetcher::OnSTHJsonParseError, | 379 base::Bind(&LogProofFetcher::OnJsonParseError, weak_factory_.GetWeakPtr(), |
193 weak_factory_.GetWeakPtr(), request)); | 380 fetcher)); |
194 } | 381 } |
195 | 382 |
196 void LogProofFetcher::CleanupRequest(net::URLRequest* request) { | 383 void LogProofFetcher::OnFetchFailed(LogFetcher* fetcher, |
197 DVLOG(1) << "Cleaning up request to " << request->original_url(); | 384 int net_error, |
198 auto it = inflight_requests_.find(request); | 385 int http_response_code) { |
199 DCHECK(it != inflight_requests_.end()); | 386 DCHECK(inflight_fetches_.find(fetcher) != inflight_fetches_.end()); |
200 auto next_it = it; | 387 LogResponseHandler* handler = inflight_fetches_.find(fetcher)->second; |
201 std::advance(next_it, 1); | 388 handler->HandleNetFailure(net_error, http_response_code); |
202 | 389 CleanupRequest(fetcher); |
203 // Delete FetchState and URLRequest, then the entry from inflight_requests_. | 390 } |
204 STLDeleteContainerPairPointers(it, next_it); | 391 |
205 inflight_requests_.erase(it); | 392 void LogProofFetcher::OnJsonParseSuccess(LogFetcher* fetcher, |
206 } | 393 scoped_ptr<base::Value> parsed_json) { |
207 | 394 DCHECK(inflight_fetches_.find(fetcher) != inflight_fetches_.end()); |
208 void LogProofFetcher::InvokeFailureCallback(net::URLRequest* request, | 395 LogResponseHandler* handler = inflight_fetches_.find(fetcher)->second; |
209 int net_error, | 396 handler->HandleParsedJson(*parsed_json); |
210 int http_response_code) { | 397 CleanupRequest(fetcher); |
211 DCHECK(inflight_requests_.count(request)); | 398 } |
212 auto it = inflight_requests_.find(request); | 399 |
213 FetchState* fetch_state = it->second; | 400 void LogProofFetcher::OnJsonParseError(LogFetcher* fetcher, |
214 | 401 const std::string& error) { |
215 fetch_state->failed_callback.Run(fetch_state->log_id, net_error, | 402 DCHECK(inflight_fetches_.find(fetcher) != inflight_fetches_.end()); |
216 http_response_code); | 403 LogResponseHandler* handler = inflight_fetches_.find(fetcher)->second; |
217 CleanupRequest(request); | 404 handler->HandleJsonParseFailure(error); |
218 } | 405 CleanupRequest(fetcher); |
219 | 406 } |
220 void LogProofFetcher::OnSTHJsonParseSuccess( | 407 |
221 net::URLRequest* request, | 408 void LogProofFetcher::CleanupRequest(LogFetcher* fetcher) { |
222 scoped_ptr<base::Value> parsed_json) { | 409 auto it = inflight_fetches_.find(fetcher); |
223 DCHECK(inflight_requests_.count(request)); | 410 DCHECK(it != inflight_fetches_.end()); |
224 | 411 |
225 FetchState* fetch_state = inflight_requests_.find(request)->second; | 412 auto next = it; |
226 net::ct::SignedTreeHead signed_tree_head; | 413 std::advance(next, 1); |
227 if (net::ct::FillSignedTreeHead(*parsed_json.get(), &signed_tree_head)) { | 414 |
228 fetch_state->fetched_callback.Run(fetch_state->log_id, signed_tree_head); | 415 STLDeleteContainerPairPointers(it, next); |
229 } else { | 416 inflight_fetches_.erase(it); |
230 fetch_state->failed_callback.Run(fetch_state->log_id, | |
231 net::ERR_CT_STH_INCOMPLETE, net::HTTP_OK); | |
232 } | |
233 | |
234 CleanupRequest(request); | |
235 } | |
236 | |
237 void LogProofFetcher::OnSTHJsonParseError(net::URLRequest* request, | |
238 const std::string& error) { | |
239 InvokeFailureCallback(request, net::ERR_CT_STH_PARSING_FAILED, net::HTTP_OK); | |
240 } | 417 } |
241 | 418 |
242 } // namespace certificate_transparency | 419 } // namespace certificate_transparency |
OLD | NEW |