| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_COOKIES_PARSED_COOKIE_H_ | 5 #ifndef NET_COOKIES_PARSED_COOKIE_H_ |
| 6 #define NET_COOKIES_PARSED_COOKIE_H_ | 6 #define NET_COOKIES_PARSED_COOKIE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 class NET_EXPORT ParsedCookie { | 16 class NET_EXPORT ParsedCookie { |
| 17 public: | 17 public: |
| 18 typedef std::pair<std::string, std::string> TokenValuePair; | 18 typedef std::pair<std::string, std::string> TokenValuePair; |
| 19 typedef std::vector<TokenValuePair> PairList; | 19 typedef std::vector<TokenValuePair> PairList; |
| 20 | 20 |
| 21 // The maximum length of a cookie string we will try to parse | 21 // The maximum length of a cookie string we will try to parse |
| 22 static const size_t kMaxCookieSize = 4096; | 22 static const size_t kMaxCookieSize = 4096; |
| 23 // The maximum number of Token/Value pairs. Shouldn't have more than 8. | 23 // The maximum number of Token/Value pairs. Shouldn't have more than 8. |
| 24 static const int kMaxPairs = 16; | 24 static const int kMaxPairs = 16; |
| 25 | 25 |
| 26 // Construct from a cookie string like "BLAH=1; path=/; domain=.google.com" | 26 // Construct from a cookie string like "BLAH=1; path=/; domain=.google.com" |
| 27 ParsedCookie(const std::string& cookie_line); | 27 ParsedCookie(const std::string& cookie_line); |
| 28 ~ParsedCookie(); | 28 ~ParsedCookie(); |
| 29 | 29 |
| 30 // You should not call any other methods on the class if !IsValid | 30 // You should not call any other methods except for SetName/SetValue on the |
| 31 bool IsValid() const { return is_valid_; } | 31 // class if !IsValid. |
| 32 bool IsValid() const; |
| 32 | 33 |
| 33 const std::string& Name() const { return pairs_[0].first; } | 34 const std::string& Name() const { return pairs_[0].first; } |
| 34 const std::string& Token() const { return Name(); } | 35 const std::string& Token() const { return Name(); } |
| 35 const std::string& Value() const { return pairs_[0].second; } | 36 const std::string& Value() const { return pairs_[0].second; } |
| 36 | 37 |
| 37 bool HasPath() const { return path_index_ != 0; } | 38 bool HasPath() const { return path_index_ != 0; } |
| 38 const std::string& Path() const { return pairs_[path_index_].second; } | 39 const std::string& Path() const { return pairs_[path_index_].second; } |
| 39 bool HasDomain() const { return domain_index_ != 0; } | 40 bool HasDomain() const { return domain_index_ != 0; } |
| 40 const std::string& Domain() const { return pairs_[domain_index_].second; } | 41 const std::string& Domain() const { return pairs_[domain_index_].second; } |
| 41 bool HasMACKey() const { return mac_key_index_ != 0; } | 42 bool HasMACKey() const { return mac_key_index_ != 0; } |
| 42 const std::string& MACKey() const { return pairs_[mac_key_index_].second; } | 43 const std::string& MACKey() const { return pairs_[mac_key_index_].second; } |
| 43 bool HasMACAlgorithm() const { return mac_algorithm_index_ != 0; } | 44 bool HasMACAlgorithm() const { return mac_algorithm_index_ != 0; } |
| 44 const std::string& MACAlgorithm() const { | 45 const std::string& MACAlgorithm() const { |
| 45 return pairs_[mac_algorithm_index_].second; | 46 return pairs_[mac_algorithm_index_].second; |
| 46 } | 47 } |
| 47 bool HasExpires() const { return expires_index_ != 0; } | 48 bool HasExpires() const { return expires_index_ != 0; } |
| 48 const std::string& Expires() const { return pairs_[expires_index_].second; } | 49 const std::string& Expires() const { return pairs_[expires_index_].second; } |
| 49 bool HasMaxAge() const { return maxage_index_ != 0; } | 50 bool HasMaxAge() const { return maxage_index_ != 0; } |
| 50 const std::string& MaxAge() const { return pairs_[maxage_index_].second; } | 51 const std::string& MaxAge() const { return pairs_[maxage_index_].second; } |
| 51 bool IsSecure() const { return secure_index_ != 0; } | 52 bool IsSecure() const { return secure_index_ != 0; } |
| 52 bool IsHttpOnly() const { return httponly_index_ != 0; } | 53 bool IsHttpOnly() const { return httponly_index_ != 0; } |
| 53 | 54 |
| 54 // Returns the number of attributes, for example, returning 2 for: | 55 // Returns the number of attributes, for example, returning 2 for: |
| 55 // "BLAH=hah; path=/; domain=.google.com" | 56 // "BLAH=hah; path=/; domain=.google.com" |
| 56 size_t NumberOfAttributes() const { return pairs_.size() - 1; } | 57 size_t NumberOfAttributes() const { return pairs_.size() - 1; } |
| 57 | 58 |
| 58 // For debugging only! | 59 // These functions set the respective properties of the cookie. If the |
| 59 std::string DebugString() const; | 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); |
| 74 |
| 75 // Returns the cookie description as it appears in a HTML response header. |
| 76 std::string ToCookieLine() const; |
| 60 | 77 |
| 61 // Returns an iterator pointing to the first terminator character found in | 78 // Returns an iterator pointing to the first terminator character found in |
| 62 // the given string. | 79 // the given string. |
| 63 static std::string::const_iterator FindFirstTerminator(const std::string& s); | 80 static std::string::const_iterator FindFirstTerminator(const std::string& s); |
| 64 | 81 |
| 65 // Given iterators pointing to the beginning and end of a string segment, | 82 // Given iterators pointing to the beginning and end of a string segment, |
| 66 // returns as output arguments token_start and token_end to the start and end | 83 // returns as output arguments token_start and token_end to the start and end |
| 67 // positions of a cookie attribute token name parsed from the segment, and | 84 // positions of a cookie attribute token name parsed from the segment, and |
| 68 // updates the segment iterator to point to the next segment to be parsed. | 85 // updates the segment iterator to point to the next segment to be parsed. |
| 69 // If no token is found, the function returns false. | 86 // If no token is found, the function returns false. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 80 const std::string::const_iterator& end, | 97 const std::string::const_iterator& end, |
| 81 std::string::const_iterator* value_start, | 98 std::string::const_iterator* value_start, |
| 82 std::string::const_iterator* value_end); | 99 std::string::const_iterator* value_end); |
| 83 | 100 |
| 84 // Same as the above functions, except the input is assumed to contain the | 101 // Same as the above functions, except the input is assumed to contain the |
| 85 // desired token/value and nothing else. | 102 // desired token/value and nothing else. |
| 86 static std::string ParseTokenString(const std::string& token); | 103 static std::string ParseTokenString(const std::string& token); |
| 87 static std::string ParseValueString(const std::string& value); | 104 static std::string ParseValueString(const std::string& value); |
| 88 | 105 |
| 89 private: | 106 private: |
| 90 static const char kTerminator[]; | |
| 91 static const int kTerminatorLen; | |
| 92 static const char kWhitespace[]; | |
| 93 static const char kValueSeparator[]; | |
| 94 static const char kTokenSeparator[]; | |
| 95 | |
| 96 void ParseTokenValuePairs(const std::string& cookie_line); | 107 void ParseTokenValuePairs(const std::string& cookie_line); |
| 97 void SetupAttributes(); | 108 void SetupAttributes(); |
| 98 | 109 |
| 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); |
| 122 |
| 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); |
| 128 |
| 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); |
| 132 |
| 99 PairList pairs_; | 133 PairList pairs_; |
| 100 bool is_valid_; | 134 bool is_valid_; |
| 101 // These will default to 0, but that should never be valid since the | 135 // These will default to 0, but that should never be valid since the |
| 102 // 0th index is the user supplied token/value, not an attribute. | 136 // 0th index is the user supplied token/value, not an attribute. |
| 103 // We're really never going to have more than like 8 attributes, so we | 137 // We're really never going to have more than like 8 attributes, so we |
| 104 // could fit these into 3 bits each if we're worried about size... | 138 // could fit these into 3 bits each if we're worried about size... |
| 105 size_t path_index_; | 139 size_t path_index_; |
| 106 size_t domain_index_; | 140 size_t domain_index_; |
| 107 size_t mac_key_index_; | 141 size_t mac_key_index_; |
| 108 size_t mac_algorithm_index_; | 142 size_t mac_algorithm_index_; |
| 109 size_t expires_index_; | 143 size_t expires_index_; |
| 110 size_t maxage_index_; | 144 size_t maxage_index_; |
| 111 size_t secure_index_; | 145 size_t secure_index_; |
| 112 size_t httponly_index_; | 146 size_t httponly_index_; |
| 113 | 147 |
| 114 DISALLOW_COPY_AND_ASSIGN(ParsedCookie); | 148 DISALLOW_COPY_AND_ASSIGN(ParsedCookie); |
| 115 }; | 149 }; |
| 116 | 150 |
| 117 } // namespace net | 151 } // namespace net |
| 118 | 152 |
| 119 #endif // NET_COOKIES_COOKIE_MONSTER_H_ | 153 #endif // NET_COOKIES_COOKIE_MONSTER_H_ |
| OLD | NEW |