summaryrefslogtreecommitdiffstats
path: root/net/http/http_alternate_protocols.h
blob: a6ccab330cb7fb338aaf5a4c5bd4bf5b74b59449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// HttpAlternateProtocols is an in-memory data structure used for keeping track
// of which HTTP HostPortPairs have an alternate protocol that can be used
// instead of HTTP on a different port.

#ifndef NET_HTTP_HTTP_ALTERNATE_PROTOCOLS_H_
#define NET_HTTP_HTTP_ALTERNATE_PROTOCOLS_H_

#include <map>
#include <utility>
#include "base/basictypes.h"
#include "net/base/host_port_pair.h"

namespace net {

class HttpAlternateProtocols {
 public:
  enum Protocol {
    NPN_SPDY,
    NUM_ALTERNATE_PROTOCOLS,
    BROKEN,  // The alternate protocol is known to be broken.
  };

  struct PortProtocolPair {
    bool Equals(const PortProtocolPair& other) const {
      return port == other.port && protocol == other.protocol;
    }

    uint16 port;
    Protocol protocol;
  };

  static const char kHeader[];
  static const char* const kProtocolStrings[NUM_ALTERNATE_PROTOCOLS];

  HttpAlternateProtocols();
  ~HttpAlternateProtocols();

  // Reports whether or not we have received Alternate-Protocol for
  // |http_host_port_pair|.
  bool HasAlternateProtocolFor(const HostPortPair& http_host_port_pair) const;
  bool HasAlternateProtocolFor(const std::string& host, uint16 port) const;

  PortProtocolPair GetAlternateProtocolFor(
      const HostPortPair& http_host_port_pair) const;
  PortProtocolPair GetAlternateProtocolFor(
      const std::string& host, uint16 port) const;

  // SetAlternateProtocolFor() will ignore the request if the alternate protocol
  // has already been marked broken via MarkBrokenAlternateProtocolFor().
  void SetAlternateProtocolFor(const HostPortPair& http_host_port_pair,
                               uint16 alternate_port,
                               Protocol alternate_protocol);

  // Marks the alternate protocol as broken.  Once marked broken, any further
  // attempts to set the alternate protocol for |http_host_port_pair| will fail.
  void MarkBrokenAlternateProtocolFor(const HostPortPair& http_host_port_pair);

 private:
  typedef std::map<HostPortPair, PortProtocolPair> ProtocolMap;

  ProtocolMap protocol_map_;

  DISALLOW_COPY_AND_ASSIGN(HttpAlternateProtocols);
};

}  // namespace net

#endif  // NET_HTTP_HTTP_ALTERNATE_PROTOCOLS_H_