[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef NET_COOKIES_PARSED_COOKIE_H_ |
| 6 | #define NET_COOKIES_PARSED_COOKIE_H_ |
| 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 14 | #include "net/base/net_export.h" |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 15 | #include "net/cookies/cookie_constants.h" |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 16 | |
| 17 | namespace net { |
| 18 | |
| 19 | class 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] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 26 | |
| 27 | // Construct from a cookie string like "BLAH=1; path=/; domain=.google.com" |
[email protected] | 8fbe41041 | 2014-07-28 17:17:41 | [diff] [blame] | 28 | // Format is according to RFC 6265. Cookies with both name and value empty |
| 29 | // will be considered invalid. |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 30 | ParsedCookie(const std::string& cookie_line); |
| 31 | ~ParsedCookie(); |
| 32 | |
[email protected] | 64527a5 | 2012-08-02 13:37:41 | [diff] [blame] | 33 | // You should not call any other methods except for SetName/SetValue on the |
| 34 | // class if !IsValid. |
| 35 | bool IsValid() const; |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 36 | |
| 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] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 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; } |
Lily Chen | 9a9c08a9 | 2019-09-18 00:18:16 | [diff] [blame] | 51 | // 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] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 55 | CookiePriority Priority() const; |
Lily Chen | ed6c24c | 2020-10-27 15:00:33 | [diff] [blame] | 56 | bool IsSameParty() const { return same_party_index_ != 0; } |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 57 | |
| 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] | 64527a5 | 2012-08-02 13:37:41 | [diff] [blame] | 62 | // 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 West | 3af2e34 | 2020-01-22 11:32:53 | [diff] [blame] | 67 | // |
Dylan Cutler | 80ed4729 | 2021-02-24 23:19:56 | [diff] [blame] | 68 | // TODO(crbug.com/1181788): Ideally, we can remove these mutators once we |
| 69 | // remove the single callsite. |
[email protected] | 64527a5 | 2012-08-02 13:37:41 | [diff] [blame] | 70 | 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] | 64527a5 | 2012-08-02 13:37:41 | [diff] [blame] | 74 | 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); |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 78 | bool SetSameSite(const std::string& same_site); |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 79 | bool SetPriority(const std::string& priority); |
Lily Chen | ed6c24c | 2020-10-27 15:00:33 | [diff] [blame] | 80 | bool SetIsSameParty(bool is_same_party); |
[email protected] | 64527a5 | 2012-08-02 13:37:41 | [diff] [blame] | 81 | |
| 82 | // Returns the cookie description as it appears in a HTML response header. |
| 83 | std::string ToCookieLine() const; |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 84 | |
| 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. |
jww | 03e6ff8c | 2016-08-17 19:19:58 | [diff] [blame] | 93 | // If no token is found, the function returns false and the segment iterator |
| 94 | // is set to end. |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 95 | 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 | |
rdsmith | 1e64af8e | 2017-06-17 00:25:28 | [diff] [blame] | 114 | // Is the string valid as the value of a cookie attribute? |
| 115 | static bool IsValidCookieAttributeValue(const std::string& value); |
| 116 | |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 117 | private: |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 118 | void ParseTokenValuePairs(const std::string& cookie_line); |
| 119 | void SetupAttributes(); |
| 120 | |
[email protected] | 64527a5 | 2012-08-02 13:37:41 | [diff] [blame] | 121 | // 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); |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 130 | bool SetBool(size_t* index, const std::string& key, bool value); |
[email protected] | 64527a5 | 2012-08-02 13:37:41 | [diff] [blame] | 131 | |
| 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] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 142 | PairList pairs_; |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 143 | // These will default to 0, but that should never be valid since the |
Lily Chen | ed6c24c | 2020-10-27 15:00:33 | [diff] [blame] | 144 | // 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] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 154 | |
| 155 | DISALLOW_COPY_AND_ASSIGN(ParsedCookie); |
| 156 | }; |
| 157 | |
| 158 | } // namespace net |
| 159 | |
bnc | 3698b0a0 | 2016-12-09 23:36:50 | [diff] [blame] | 160 | #endif // NET_COOKIES_PARSED_COOKIE_H_ |