1 // Copyright (c) 2011 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 "base/memory/singleton.h"
6 #include "net/url_request/https_prober.h"
7
8 #include "net/url_request/url_request.h"
9 #include "net/url_request/url_request_context.h"
10
11 namespace net {
12
13 // static
GetInstance()14 HTTPSProber* HTTPSProber::GetInstance() {
15 return Singleton<HTTPSProber>::get();
16 }
17
HaveProbed(const std::string & host) const18 bool HTTPSProber::HaveProbed(const std::string& host) const {
19 return probed_.find(host) != probed_.end();
20 }
21
InFlight(const std::string & host) const22 bool HTTPSProber::InFlight(const std::string& host) const {
23 return inflight_probes_.find(host) != inflight_probes_.end();
24 }
25
ProbeHost(const std::string & host,URLRequestContext * ctx,HTTPSProberDelegate * delegate)26 bool HTTPSProber::ProbeHost(const std::string& host, URLRequestContext* ctx,
27 HTTPSProberDelegate* delegate) {
28 if (HaveProbed(host) || InFlight(host)) {
29 return false;
30 }
31
32 inflight_probes_[host] = delegate;
33
34 GURL url("https://" + host);
35 DCHECK_EQ(url.host(), host);
36
37 URLRequest* req = new URLRequest(url, this);
38 req->set_context(ctx);
39 req->Start();
40 return true;
41 }
42
OnAuthRequired(URLRequest * request,AuthChallengeInfo * auth_info)43 void HTTPSProber::OnAuthRequired(URLRequest* request,
44 AuthChallengeInfo* auth_info) {
45 Success(request);
46 }
47
OnSSLCertificateError(URLRequest * request,int cert_error,X509Certificate * cert)48 void HTTPSProber::OnSSLCertificateError(URLRequest* request,
49 int cert_error,
50 X509Certificate* cert) {
51 request->ContinueDespiteLastError();
52 }
53
OnResponseStarted(URLRequest * request)54 void HTTPSProber::OnResponseStarted(URLRequest* request) {
55 if (request->status().status() == URLRequestStatus::SUCCESS) {
56 Success(request);
57 } else {
58 Failure(request);
59 }
60 }
61
OnReadCompleted(URLRequest * request,int bytes_read)62 void HTTPSProber::OnReadCompleted(URLRequest* request, int bytes_read) {
63 NOTREACHED();
64 }
65
HTTPSProber()66 HTTPSProber::HTTPSProber() {
67 }
68
~HTTPSProber()69 HTTPSProber::~HTTPSProber() {
70 }
71
Success(URLRequest * request)72 void HTTPSProber::Success(URLRequest* request) {
73 DoCallback(request, true);
74 }
75
Failure(URLRequest * request)76 void HTTPSProber::Failure(URLRequest* request) {
77 DoCallback(request, false);
78 }
79
DoCallback(URLRequest * request,bool result)80 void HTTPSProber::DoCallback(URLRequest* request, bool result) {
81 std::map<std::string, HTTPSProberDelegate*>::iterator i =
82 inflight_probes_.find(request->original_url().host());
83 DCHECK(i != inflight_probes_.end());
84
85 HTTPSProberDelegate* delegate = i->second;
86 inflight_probes_.erase(i);
87 probed_.insert(request->original_url().host());
88 delete request;
89 delegate->ProbeComplete(result);
90 }
91
92 } // namespace net
93