Don't force SPDY2 (hence we will use the default SPDY3).
[chromium-blink-merge.git] / net / cookies / parsed_cookie.h
blob694d7248b86a8c6014fb9e94a65c6b0921bb1cd6
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.
5 #ifndef NET_COOKIES_PARSED_COOKIE_H_
6 #define NET_COOKIES_PARSED_COOKIE_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "net/base/net_export.h"
14 namespace net {
16 class NET_EXPORT ParsedCookie {
17 public:
18 typedef std::pair<std::string, std::string> TokenValuePair;
19 typedef std::vector<TokenValuePair> PairList;
21 // The maximum length of a cookie string we will try to parse
22 static const size_t kMaxCookieSize = 4096;
23 // The maximum number of Token/Value pairs. Shouldn't have more than 8.
24 static const int kMaxPairs = 16;
26 // Construct from a cookie string like "BLAH=1; path=/; domain=.google.com"
27 ParsedCookie(const std::string& cookie_line);
28 ~ParsedCookie();
30 // You should not call any other methods except for SetName/SetValue on the
31 // class if !IsValid.
32 bool IsValid() const;
34 const std::string& Name() const { return pairs_[0].first; }
35 const std::string& Token() const { return Name(); }
36 const std::string& Value() const { return pairs_[0].second; }
38 bool HasPath() const { return path_index_ != 0; }
39 const std::string& Path() const { return pairs_[path_index_].second; }
40 bool HasDomain() const { return domain_index_ != 0; }
41 const std::string& Domain() const { return pairs_[domain_index_].second; }
42 bool HasMACKey() const { return mac_key_index_ != 0; }
43 const std::string& MACKey() const { return pairs_[mac_key_index_].second; }
44 bool HasMACAlgorithm() const { return mac_algorithm_index_ != 0; }
45 const std::string& MACAlgorithm() const {
46 return pairs_[mac_algorithm_index_].second;
48 bool HasExpires() const { return expires_index_ != 0; }
49 const std::string& Expires() const { return pairs_[expires_index_].second; }
50 bool HasMaxAge() const { return maxage_index_ != 0; }
51 const std::string& MaxAge() const { return pairs_[maxage_index_].second; }
52 bool IsSecure() const { return secure_index_ != 0; }
53 bool IsHttpOnly() const { return httponly_index_ != 0; }
55 // Returns the number of attributes, for example, returning 2 for:
56 // "BLAH=hah; path=/; domain=.google.com"
57 size_t NumberOfAttributes() const { return pairs_.size() - 1; }
59 // These functions set the respective properties of the cookie. If the
60 // parameters are empty, the respective properties are cleared.
61 // The functions return false in case an error occurred.
62 // The cookie needs to be assigned a name/value before setting the other
63 // attributes.
64 bool SetName(const std::string& name);
65 bool SetValue(const std::string& value);
66 bool SetPath(const std::string& path);
67 bool SetDomain(const std::string& domain);
68 bool SetMACKey(const std::string& mac_key);
69 bool SetMACAlgorithm(const std::string& mac_algorithm);
70 bool SetExpires(const std::string& expires);
71 bool SetMaxAge(const std::string& maxage);
72 bool SetIsSecure(bool is_secure);
73 bool SetIsHttpOnly(bool is_http_only);
75 // Returns the cookie description as it appears in a HTML response header.
76 std::string ToCookieLine() const;
78 // Returns an iterator pointing to the first terminator character found in
79 // the given string.
80 static std::string::const_iterator FindFirstTerminator(const std::string& s);
82 // Given iterators pointing to the beginning and end of a string segment,
83 // returns as output arguments token_start and token_end to the start and end
84 // positions of a cookie attribute token name parsed from the segment, and
85 // updates the segment iterator to point to the next segment to be parsed.
86 // If no token is found, the function returns false.
87 static bool ParseToken(std::string::const_iterator* it,
88 const std::string::const_iterator& end,
89 std::string::const_iterator* token_start,
90 std::string::const_iterator* token_end);
92 // Given iterators pointing to the beginning and end of a string segment,
93 // returns as output arguments value_start and value_end to the start and end
94 // positions of a cookie attribute value parsed from the segment, and updates
95 // the segment iterator to point to the next segment to be parsed.
96 static void ParseValue(std::string::const_iterator* it,
97 const std::string::const_iterator& end,
98 std::string::const_iterator* value_start,
99 std::string::const_iterator* value_end);
101 // Same as the above functions, except the input is assumed to contain the
102 // desired token/value and nothing else.
103 static std::string ParseTokenString(const std::string& token);
104 static std::string ParseValueString(const std::string& value);
106 private:
107 void ParseTokenValuePairs(const std::string& cookie_line);
108 void SetupAttributes();
110 // Sets a key/value pair for a cookie. |index| has to point to one of the
111 // |*_index_| fields in ParsedCookie and is updated to the position where
112 // the key/value pair is set in |pairs_|. Accordingly, |key| has to correspond
113 // to the token matching |index|. If |value| contains invalid characters, the
114 // cookie parameter is not changed and the function returns false.
115 // If |value| is empty/false the key/value pair is removed.
116 bool SetString(size_t* index,
117 const std::string& key,
118 const std::string& value);
119 bool SetBool(size_t* index,
120 const std::string& key,
121 bool value);
123 // Helper function for SetString and SetBool handling the case that the
124 // key/value pair shall not be removed.
125 bool SetAttributePair(size_t* index,
126 const std::string& key,
127 const std::string& value);
129 // Removes the key/value pair from a cookie that is identified by |index|.
130 // |index| refers to a position in |pairs_|.
131 void ClearAttributePair(size_t index);
133 PairList pairs_;
134 bool is_valid_;
135 // These will default to 0, but that should never be valid since the
136 // 0th index is the user supplied token/value, not an attribute.
137 // We're really never going to have more than like 8 attributes, so we
138 // could fit these into 3 bits each if we're worried about size...
139 size_t path_index_;
140 size_t domain_index_;
141 size_t mac_key_index_;
142 size_t mac_algorithm_index_;
143 size_t expires_index_;
144 size_t maxage_index_;
145 size_t secure_index_;
146 size_t httponly_index_;
148 DISALLOW_COPY_AND_ASSIGN(ParsedCookie);
151 } // namespace net
153 #endif // NET_COOKIES_COOKIE_MONSTER_H_