blob: 598a04d6234163bb3eec69662f28e1652cc52d4d [file] [log] [blame]
[email protected]ebfe3172012-07-12 12:21:411// 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#ifndef NET_COOKIES_PARSED_COOKIE_H_
6#define NET_COOKIES_PARSED_COOKIE_H_
7
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9
[email protected]ebfe3172012-07-12 12:21:4110#include <string>
11#include <vector>
12
Avi Drissman13fc8932015-12-20 04:40:4613#include "base/macros.h"
[email protected]ebfe3172012-07-12 12:21:4114#include "net/base/net_export.h"
[email protected]ab2d75c82013-04-19 18:39:0415#include "net/cookies/cookie_constants.h"
[email protected]ebfe3172012-07-12 12:21:4116
17namespace net {
18
19class NET_EXPORT ParsedCookie {
20 public:
21 typedef std::pair<std::string, std::string> TokenValuePair;
22 typedef std::vector<TokenValuePair> PairList;
23
24 // The maximum length of a cookie string we will try to parse
25 static const size_t kMaxCookieSize = 4096;
[email protected]ebfe3172012-07-12 12:21:4126
27 // Construct from a cookie string like "BLAH=1; path=/; domain=.google.com"
[email protected]8fbe410412014-07-28 17:17:4128 // Format is according to RFC 6265. Cookies with both name and value empty
29 // will be considered invalid.
[email protected]ebfe3172012-07-12 12:21:4130 ParsedCookie(const std::string& cookie_line);
31 ~ParsedCookie();
32
[email protected]64527a52012-08-02 13:37:4133 // You should not call any other methods except for SetName/SetValue on the
34 // class if !IsValid.
35 bool IsValid() const;
[email protected]ebfe3172012-07-12 12:21:4136
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; }
40
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; }
[email protected]ebfe3172012-07-12 12:21:4145 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; }
Lily Chen9a9c08a92019-09-18 00:18:1651 // Also spits out an enum value representing the string given as the SameSite
52 // attribute value, if |samesite_string| is non-null.
53 CookieSameSite SameSite(
54 CookieSameSiteString* samesite_string = nullptr) const;
[email protected]ab2d75c82013-04-19 18:39:0455 CookiePriority Priority() const;
Lily Chened6c24c2020-10-27 15:00:3356 bool IsSameParty() const { return same_party_index_ != 0; }
[email protected]ebfe3172012-07-12 12:21:4157
58 // Returns the number of attributes, for example, returning 2 for:
59 // "BLAH=hah; path=/; domain=.google.com"
60 size_t NumberOfAttributes() const { return pairs_.size() - 1; }
61
[email protected]64527a52012-08-02 13:37:4162 // These functions set the respective properties of the cookie. If the
63 // parameters are empty, the respective properties are cleared.
64 // The functions return false in case an error occurred.
65 // The cookie needs to be assigned a name/value before setting the other
66 // attributes.
Mike West3af2e342020-01-22 11:32:5367 //
Dylan Cutler80ed47292021-02-24 23:19:5668 // TODO(crbug.com/1181788): Ideally, we can remove these mutators once we
69 // remove the single callsite.
[email protected]64527a52012-08-02 13:37:4170 bool SetName(const std::string& name);
71 bool SetValue(const std::string& value);
72 bool SetPath(const std::string& path);
73 bool SetDomain(const std::string& domain);
[email protected]64527a52012-08-02 13:37:4174 bool SetExpires(const std::string& expires);
75 bool SetMaxAge(const std::string& maxage);
76 bool SetIsSecure(bool is_secure);
77 bool SetIsHttpOnly(bool is_http_only);
mkwste1a29582016-03-15 10:07:5278 bool SetSameSite(const std::string& same_site);
[email protected]ab2d75c82013-04-19 18:39:0479 bool SetPriority(const std::string& priority);
Lily Chened6c24c2020-10-27 15:00:3380 bool SetIsSameParty(bool is_same_party);
[email protected]64527a52012-08-02 13:37:4181
82 // Returns the cookie description as it appears in a HTML response header.
83 std::string ToCookieLine() const;
[email protected]ebfe3172012-07-12 12:21:4184
85 // Returns an iterator pointing to the first terminator character found in
86 // the given string.
87 static std::string::const_iterator FindFirstTerminator(const std::string& s);
88
89 // Given iterators pointing to the beginning and end of a string segment,
90 // returns as output arguments token_start and token_end to the start and end
91 // positions of a cookie attribute token name parsed from the segment, and
92 // updates the segment iterator to point to the next segment to be parsed.
jww03e6ff8c2016-08-17 19:19:5893 // If no token is found, the function returns false and the segment iterator
94 // is set to end.
[email protected]ebfe3172012-07-12 12:21:4195 static bool ParseToken(std::string::const_iterator* it,
96 const std::string::const_iterator& end,
97 std::string::const_iterator* token_start,
98 std::string::const_iterator* token_end);
99
100 // Given iterators pointing to the beginning and end of a string segment,
101 // returns as output arguments value_start and value_end to the start and end
102 // positions of a cookie attribute value parsed from the segment, and updates
103 // the segment iterator to point to the next segment to be parsed.
104 static void ParseValue(std::string::const_iterator* it,
105 const std::string::const_iterator& end,
106 std::string::const_iterator* value_start,
107 std::string::const_iterator* value_end);
108
109 // Same as the above functions, except the input is assumed to contain the
110 // desired token/value and nothing else.
111 static std::string ParseTokenString(const std::string& token);
112 static std::string ParseValueString(const std::string& value);
113
rdsmith1e64af8e2017-06-17 00:25:28114 // Is the string valid as the value of a cookie attribute?
115 static bool IsValidCookieAttributeValue(const std::string& value);
116
[email protected]ebfe3172012-07-12 12:21:41117 private:
[email protected]ebfe3172012-07-12 12:21:41118 void ParseTokenValuePairs(const std::string& cookie_line);
119 void SetupAttributes();
120
[email protected]64527a52012-08-02 13:37:41121 // Sets a key/value pair for a cookie. |index| has to point to one of the
122 // |*_index_| fields in ParsedCookie and is updated to the position where
123 // the key/value pair is set in |pairs_|. Accordingly, |key| has to correspond
124 // to the token matching |index|. If |value| contains invalid characters, the
125 // cookie parameter is not changed and the function returns false.
126 // If |value| is empty/false the key/value pair is removed.
127 bool SetString(size_t* index,
128 const std::string& key,
129 const std::string& value);
mkwstbe84af312015-02-20 08:52:45130 bool SetBool(size_t* index, const std::string& key, bool value);
[email protected]64527a52012-08-02 13:37:41131
132 // Helper function for SetString and SetBool handling the case that the
133 // key/value pair shall not be removed.
134 bool SetAttributePair(size_t* index,
135 const std::string& key,
136 const std::string& value);
137
138 // Removes the key/value pair from a cookie that is identified by |index|.
139 // |index| refers to a position in |pairs_|.
140 void ClearAttributePair(size_t index);
141
[email protected]ebfe3172012-07-12 12:21:41142 PairList pairs_;
[email protected]ebfe3172012-07-12 12:21:41143 // These will default to 0, but that should never be valid since the
Lily Chened6c24c2020-10-27 15:00:33144 // 0th index is the user supplied cookie name/value, not an attribute.
145 size_t path_index_ = 0;
146 size_t domain_index_ = 0;
147 size_t expires_index_ = 0;
148 size_t maxage_index_ = 0;
149 size_t secure_index_ = 0;
150 size_t httponly_index_ = 0;
151 size_t same_site_index_ = 0;
152 size_t priority_index_ = 0;
153 size_t same_party_index_ = 0;
[email protected]ebfe3172012-07-12 12:21:41154
155 DISALLOW_COPY_AND_ASSIGN(ParsedCookie);
156};
157
158} // namespace net
159
bnc3698b0a02016-12-09 23:36:50160#endif // NET_COOKIES_PARSED_COOKIE_H_