blob: 4549b3727f3f056732d6e28b17dec5d5bcf1fe23 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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"
Torne (Richard Coles)5e3f23d2013-06-11 16:24:11 +01008#include "base/strings/string_util.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00009#include "net/ssl/server_bound_cert_service.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010
11namespace net {
12
13namespace {
14
15GURL 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
26const size_t SpdyCredentialState::kDefaultNumSlots = 8;
27const size_t SpdyCredentialState::kNoEntry = 0;
28
29SpdyCredentialState::SpdyCredentialState(size_t num_slots)
30 : slots_(num_slots),
31 last_added_(-1) {}
32
33SpdyCredentialState::~SpdyCredentialState() {}
34
35bool SpdyCredentialState::HasCredential(const GURL& origin) const {
36 return FindCredentialSlot(origin) != kNoEntry;
37}
38
39size_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
54size_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
63void 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