1 // Copyright (c) 2012 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/spdy/spdy_credential_state.h" 6 7 #include "base/logging.h" 8 #include "base/strings/string_util.h" 9 #include "net/ssl/server_bound_cert_service.h" 10 11 namespace net { 12 13 namespace { 14 15 GURL GetCanonicalOrigin(const GURL& url) { 16 std::string domain = 17 ServerBoundCertService::GetDomainForHost(url.host()); 18 DCHECK(!domain.empty()); 19 if (domain == url.host()) 20 return url.GetOrigin(); 21 return GURL(url.scheme() + "://" + domain + ":" + url.port()); 22 } 23 24 } // namespace 25 26 const size_t SpdyCredentialState::kDefaultNumSlots = 8; 27 const size_t SpdyCredentialState::kNoEntry = 0; 28 29 SpdyCredentialState::SpdyCredentialState(size_t num_slots) 30 : slots_(num_slots), 31 last_added_(-1) {} 32 33 SpdyCredentialState::~SpdyCredentialState() {} 34 35 bool SpdyCredentialState::HasCredential(const GURL& origin) const { 36 return FindCredentialSlot(origin) != kNoEntry; 37 } 38 39 size_t SpdyCredentialState::SetHasCredential(const GURL& origin) { 40 size_t i = FindCredentialSlot(origin); 41 if (i != kNoEntry) 42 return i; 43 // Add the new entry at the next index following the index of the last 44 // entry added, or at index 0 if the last added index is the last index. 45 if (last_added_ + 1 == slots_.size()) { 46 last_added_ = 0; 47 } else { 48 last_added_++; 49 } 50 slots_[last_added_] = GetCanonicalOrigin(origin); 51 return last_added_ + 1; 52 } 53 54 size_t SpdyCredentialState::FindCredentialSlot(const GURL& origin) const { 55 GURL url = GetCanonicalOrigin(origin); 56 for (size_t i = 0; i < slots_.size(); i++) { 57 if (url == slots_[i]) 58 return i + 1; 59 } 60 return kNoEntry; 61 } 62 63 void SpdyCredentialState::Resize(size_t size) { 64 slots_.resize(size); 65 if (last_added_ >= slots_.size()) 66 last_added_ = slots_.size() - 1; 67 } 68 69 } // namespace net 70