Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: components/certificate_transparency/log_proof_fetcher.cc

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

Powered by Google App Engine
This is Rietveld 408576698