Re-land: C++ readability review
[chromium-blink-merge.git] / net / cookies / parsed_cookie.h
blob029728df1490a6fd37a84d7fc871abe1b0384e65
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"
13 #include "net/cookies/cookie_constants.h"
15 namespace net {
17 class NET_EXPORT ParsedCookie {
18 public:
19 typedef std::pair<std::string, std::string> TokenValuePair;
20 typedef std::vector<TokenValuePair> PairList;
22 // The maximum length of a cookie string we will try to parse
23 static const size_t kMaxCookieSize = 4096;
24 // The maximum number of Token/Value pairs. Shouldn't have more than 8.
25 static const int kMaxPairs = 16;
27 // Construct from a cookie string like "BLAH=1; path=/; domain=.google.com"
28 // Format is according to RFC 6265. Cookies with both name and value empty
29 // will be considered invalid.
30 ParsedCookie(const std::string& cookie_line);
31 ~ParsedCookie();
33 // You should not call any other methods except for SetName/SetValue on the
34 // class if !IsValid.
35 bool IsValid() const;
37 const std::string& Name() const { return pairs_[0].first; }
38 const std::string& Token() const { return Name(); }
39 const std::string& Value() const { return pairs_[0].second; }
41 bool HasPath() const { return path_index_ != 0; }
42 const std::string& Path() const { return pairs_[path_index_].second; }
43 bool HasDomain() const { return domain_index_ != 0; }
44 const std::string& Domain() const { return pairs_[domain_index_].second; }
45 bool HasExpires() const { return expires_index_ != 0; }
46 const std::string& Expires() const { return pairs_[expires_index_].second; }
47 bool HasMaxAge() const { return maxage_index_ != 0; }
48 const std::string& MaxAge() const { return pairs_[maxage_index_].second; }
49 bool IsSecure() const { return secure_index_ != 0; }
50 bool IsHttpOnly() const { return httponly_index_ != 0; }
51 bool IsFirstPartyOnly() const { return firstpartyonly_index_ != 0; }
52 CookiePriority Priority() const;
54 // Returns the number of attributes, for example, returning 2 for:
55 // "BLAH=hah; path=/; domain=.google.com"
56 size_t NumberOfAttributes() const { return pairs_.size() - 1; }
58 // These functions set the respective properties of the cookie. If the
59 // parameters are empty, the respective properties are cleared.
60 // The functions return false in case an error occurred.
61 // The cookie needs to be assigned a name/value before setting the other
62 // attributes.
63 bool SetName(const std::string& name);
64 bool SetValue(const std::string& value);
65 bool SetPath(const std::string& path);
66 bool SetDomain(const std::string& domain);
67 bool SetExpires(const std::string& expires);
68 bool SetMaxAge(const std::string& maxage);
69 bool SetIsSecure(bool is_secure);
70 bool SetIsHttpOnly(bool is_http_only);
71 bool SetIsFirstPartyOnly(bool is_first_party_only);
72 bool SetPriority(const std::string& priority);
74 // Returns the cookie description as it appears in a HTML response header.
75 std::string ToCookieLine() const;
77 // Returns an iterator pointing to the first terminator character found in
78 // the given string.
79 static std::string::const_iterator FindFirstTerminator(const std::string& s);
81 // Given iterators pointing to the beginning and end of a string segment,
82 // returns as output arguments token_start and token_end to the start and end
83 // positions of a cookie attribute token name parsed from the segment, and
84 // updates the segment iterator to point to the next segment to be parsed.
85 // If no token is found, the function returns false.
86 static bool ParseToken(std::string::const_iterator* it,
87 const std::string::const_iterator& end,
88 std::string::const_iterator* token_start,
89 std::string::const_iterator* token_end);
91 // Given iterators pointing to the beginning and end of a string segment,
92 // returns as output arguments value_start and value_end to the start and end
93 // positions of a cookie attribute value parsed from the segment, and updates
94 // the segment iterator to point to the next segment to be parsed.
95 static void ParseValue(std::string::const_iterator* it,
96 const std::string::const_iterator& end,
97 std::string::const_iterator* value_start,
98 std::string::const_iterator* value_end);
100 // Same as the above functions, except the input is assumed to contain the
101 // desired token/value and nothing else.
102 static std::string ParseTokenString(const std::string& token);
103 static std::string ParseValueString(const std::string& value);
105 private:
106 void ParseTokenValuePairs(const std::string& cookie_line);
107 void SetupAttributes();
109 // Sets a key/value pair for a cookie. |index| has to point to one of the
110 // |*_index_| fields in ParsedCookie and is updated to the position where
111 // the key/value pair is set in |pairs_|. Accordingly, |key| has to correspond
112 // to the token matching |index|. If |value| contains invalid characters, the
113 // cookie parameter is not changed and the function returns false.
114 // If |value| is empty/false the key/value pair is removed.
115 bool SetString(size_t* index,
116 const std::string& key,
117 const std::string& value);
118 bool SetBool(size_t* index, const std::string& key, bool value);
120 // Helper function for SetString and SetBool handling the case that the
121 // key/value pair shall not be removed.
122 bool SetAttributePair(size_t* index,
123 const std::string& key,
124 const std::string& value);
126 // Removes the key/value pair from a cookie that is identified by |index|.
127 // |index| refers to a position in |pairs_|.
128 void ClearAttributePair(size_t index);
130 PairList pairs_;
131 // These will default to 0, but that should never be valid since the
132 // 0th index is the user supplied token/value, not an attribute.
133 // We're really never going to have more than like 8 attributes, so we
134 // could fit these into 3 bits each if we're worried about size...
135 size_t path_index_;
136 size_t domain_index_;
137 size_t expires_index_;
138 size_t maxage_index_;
139 size_t secure_index_;
140 size_t httponly_index_;
141 size_t firstpartyonly_index_;
142 size_t priority_index_;
144 DISALLOW_COPY_AND_ASSIGN(ParsedCookie);
147 } // namespace net
149 #endif // NET_COOKIES_COOKIE_MONSTER_H_