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 #include "net/cookies/cookie_util.h" | 5 #include "net/cookies/cookie_util.h" |
6 | 6 |
7 #include <cstdio> | 7 #include <cstdio> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 | 203 |
204 GURL CookieOriginToURL(const std::string& domain, bool is_https) { | 204 GURL CookieOriginToURL(const std::string& domain, bool is_https) { |
205 if (domain.empty()) | 205 if (domain.empty()) |
206 return GURL(); | 206 return GURL(); |
207 | 207 |
208 const std::string scheme = is_https ? "https" : "http"; | 208 const std::string scheme = is_https ? "https" : "http"; |
209 const std::string host = domain[0] == '.' ? domain.substr(1) : domain; | 209 const std::string host = domain[0] == '.' ? domain.substr(1) : domain; |
210 return GURL(scheme + "://" + host); | 210 return GURL(scheme + "://" + host); |
211 } | 211 } |
212 | 212 |
213 void ParseRequestCookieLine(const std::string& header_value, | |
214 ParsedRequestCookies* parsed_cookies) { | |
215 std::string::const_iterator i = header_value.begin(); | |
216 while (i != header_value.end()) { | |
217 // Here we are at the beginning of a cookie. | |
218 | |
219 // Eat whitespace. | |
220 while (i != header_value.end() && *i == ' ') ++i; | |
221 if (i == header_value.end()) return; | |
222 | |
223 // Find cookie name. | |
224 std::string::const_iterator cookie_name_beginning = i; | |
225 while (i != header_value.end() && *i != '=') ++i; | |
226 base::StringPiece cookie_name(cookie_name_beginning, i); | |
227 | |
228 // Find cookie value. | |
229 base::StringPiece cookie_value; | |
230 // Cookies may have no value, in this case '=' may or may not be there. | |
231 if (i != header_value.end() && i + 1 != header_value.end()) { | |
Marc Treib
2014/07/14 15:31:20
The previous version only checked for "i != header
| |
232 ++i; // Skip '='. | |
233 std::string::const_iterator cookie_value_beginning = i; | |
234 if (*i == '"') { | |
235 ++i; // Skip '"'. | |
236 while (i != header_value.end() && *i != '"') ++i; | |
237 if (i == header_value.end()) return; | |
238 ++i; // Skip '"'. | |
239 cookie_value = base::StringPiece(cookie_value_beginning, i); | |
240 // i points to character after '"', potentially a ';'. | |
241 } else { | |
242 while (i != header_value.end() && *i != ';') ++i; | |
243 cookie_value = base::StringPiece(cookie_value_beginning, i); | |
244 // i points to ';' or end of string. | |
245 } | |
246 } | |
247 parsed_cookies->push_back(std::make_pair(cookie_name, cookie_value)); | |
248 // Eat ';'. | |
249 if (i != header_value.end()) ++i; | |
250 } | |
251 } | |
252 | |
253 std::string SerializeRequestCookieLine( | |
254 const ParsedRequestCookies& parsed_cookies) { | |
255 std::string buffer; | |
256 for (ParsedRequestCookies::const_iterator i = parsed_cookies.begin(); | |
257 i != parsed_cookies.end(); ++i) { | |
258 if (!buffer.empty()) | |
259 buffer.append("; "); | |
260 buffer.append(i->first.begin(), i->first.end()); | |
261 buffer.push_back('='); | |
262 buffer.append(i->second.begin(), i->second.end()); | |
263 } | |
264 return buffer; | |
265 } | |
266 | |
213 } // namespace cookie_utils | 267 } // namespace cookie_utils |
214 } // namespace net | 268 } // namespace net |
215 | 269 |
OLD | NEW |