| // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| // This code was pulled from dart:io. | |
| // https://github.com/dart-lang/sdk/blob/main/sdk/lib/_http/http_headers.dart#L965 | |
| // TODO(mossman): Is there a way to use dart:io directly? | |
| // ignore_for_file: prefer_contains | |
| // ignore_for_file: prefer_final_locals | |
| // ignore_for_file: prefer_is_empty | |
| // ignore_for_file: prefer_single_quotes | |
| // ignore_for_file: sort_constructors_first | |
| // ignore_for_file: unnecessary_const | |
| // ignore_for_file: unnecessary_new | |
| // ignore_for_file: unused_element | |
| part of 'http.dart'; | |
| class Cookie { | |
| String? _name; | |
| String? _value; | |
| DateTime? expires; | |
| int? maxAge; | |
| String? domain; | |
| String? path; | |
| bool httpOnly = false; | |
| bool secure = false; | |
| String? get name => _name; | |
| String? get value => _value; | |
| set name(String? newName) { | |
| _validateName(newName); | |
| _name = newName; | |
| } | |
| set value(String? newValue) { | |
| _validateValue(newValue); | |
| _value = newValue; | |
| } | |
| Cookie.fromSetCookieValue(String value) { | |
| // Parse the 'set-cookie' header value. | |
| _parseSetCookieValue(value); | |
| } | |
| // Parse a 'set-cookie' header value according to the rules in RFC 6265. | |
| void _parseSetCookieValue(String s) { | |
| int index = 0; | |
| bool done() => index == s.length; | |
| String parseName() { | |
| int start = index; | |
| while (!done()) { | |
| if (s[index] == "=") break; | |
| index++; | |
| } | |
| return s.substring(start, index).trim(); | |
| } | |
| String parseValue() { | |
| int start = index; | |
| while (!done()) { | |
| if (s[index] == ";") break; | |
| index++; | |
| } | |
| return s.substring(start, index).trim(); | |
| } | |
| void expect(String expected) { | |
| if (done()) throw new HttpException("Failed to parse header value [$s]"); | |
| if (s[index] != expected) { | |
| throw new HttpException("Failed to parse header value [$s]"); | |
| } | |
| index++; | |
| } | |
| void parseAttributes() { | |
| String parseAttributeName() { | |
| int start = index; | |
| while (!done()) { | |
| if (s[index] == "=" || s[index] == ";") break; | |
| index++; | |
| } | |
| return s.substring(start, index).trim().toLowerCase(); | |
| } | |
| String parseAttributeValue() { | |
| int start = index; | |
| while (!done()) { | |
| if (s[index] == ";") break; | |
| index++; | |
| } | |
| return s.substring(start, index).trim().toLowerCase(); | |
| } | |
| while (!done()) { | |
| String name = parseAttributeName(); | |
| String value = ""; | |
| if (!done() && s[index] == "=") { | |
| index++; // Skip the = character. | |
| value = parseAttributeValue(); | |
| } | |
| if (name == "expires") { | |
| expires = HttpDate._parseCookieDate(value); | |
| } else if (name == "max-age") { | |
| maxAge = int.parse(value); | |
| } else if (name == "domain") { | |
| domain = value; | |
| } else if (name == "path") { | |
| path = value; | |
| } else if (name == "httponly") { | |
| httpOnly = true; | |
| } else if (name == "secure") { | |
| secure = true; | |
| } | |
| if (!done()) index++; // Skip the ; character | |
| } | |
| } | |
| _name = _validateName(parseName()); | |
| if (done() || _name!.length == 0) { | |
| throw new HttpException("Failed to parse header value [$s]"); | |
| } | |
| index++; // Skip the = character. | |
| _value = _validateValue(parseValue()); | |
| if (done()) return; | |
| index++; // Skip the ; character. | |
| parseAttributes(); | |
| } | |
| static String _validateName(String? newName) { | |
| const separators = const [ | |
| "(", | |
| ")", | |
| "<", | |
| ">", | |
| "@", | |
| ",", | |
| ";", | |
| ":", | |
| "\\", | |
| '"', | |
| "/", | |
| "[", | |
| "]", | |
| "?", | |
| "=", | |
| "{", | |
| "}", | |
| ]; | |
| if (newName == null) throw new ArgumentError.notNull("name"); | |
| for (int i = 0; i < newName.length; i++) { | |
| int codeUnit = newName.codeUnits[i]; | |
| if (codeUnit <= 32 || | |
| codeUnit >= 127 || | |
| separators.indexOf(newName[i]) >= 0) { | |
| throw new FormatException( | |
| "Invalid character in cookie name, code unit: '$codeUnit'", | |
| newName, | |
| i, | |
| ); | |
| } | |
| } | |
| return newName; | |
| } | |
| static String _validateValue(String? newValue) { | |
| if (newValue == null) throw new ArgumentError.notNull("value"); | |
| // Per RFC 6265, consider surrounding "" as part of the value, but otherwise | |
| // double quotes are not allowed. | |
| int start = 0; | |
| int end = newValue.length; | |
| if (2 <= newValue.length && | |
| newValue.codeUnits[start] == 0x22 && | |
| newValue.codeUnits[end - 1] == 0x22) { | |
| start++; | |
| end--; | |
| } | |
| for (int i = start; i < end; i++) { | |
| int codeUnit = newValue.codeUnits[i]; | |
| if (!(codeUnit == 0x21 || | |
| (codeUnit >= 0x23 && codeUnit <= 0x2B) || | |
| (codeUnit >= 0x2D && codeUnit <= 0x3A) || | |
| (codeUnit >= 0x3C && codeUnit <= 0x5B) || | |
| (codeUnit >= 0x5D && codeUnit <= 0x7E))) { | |
| throw new FormatException( | |
| "Invalid character in cookie value, code unit: '$codeUnit'", | |
| newValue, | |
| i, | |
| ); | |
| } | |
| } | |
| return newValue; | |
| } | |
| } |