-
Notifications
You must be signed in to change notification settings - Fork 2
/
cookie.dart
82 lines (71 loc) · 1.97 KB
/
cookie.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
library cookie;
import 'dart:io';
/**
* This is largely lifted from dart:io code so we can use it in shelf
* todo: Replace this with wahatever cookie code comes in shelf.
* See [https://code.google.com/p/dart/issues/detail?id=18845]
*
*/
Map<String,Cookie> parseCookies(Map<String,String> headers) {
// Parse a Cookie header value according to the rules in RFC 6265.
var cookies = new Map<String,Cookie>();
void parseCookieString(String s) {
int index = 0;
bool done() => index == -1 || index == s.length;
void skipWS() {
while (!done()) {
if (s[index] != " " && s[index] != "\t") return;
index++;
}
}
String parseName() {
int start = index;
while (!done()) {
if (s[index] == " " || s[index] == "\t" || s[index] == "=") break;
index++;
}
return s.substring(start, index);
}
String parseValue() {
int start = index;
while (!done()) {
if (s[index] == " " || s[index] == "\t" || s[index] == ";") break;
index++;
}
return s.substring(start, index);
}
bool expect(String expected) {
if (done()) return false;
if (s[index] != expected) return false;
index++;
return true;
}
while (!done()) {
skipWS();
if (done()) return;
String name = parseName();
skipWS();
if (!expect("=")) {
index = s.indexOf(';', index);
continue;
}
skipWS();
String value = parseValue();
try {
cookies[name] = new Cookie(name, value);
} catch (_) {
// Skip it, invalid cookie data.
}
skipWS();
if (done()) return;
if (!expect(";")) {
index = s.indexOf(';', index);
continue;
}
}
}
var c = headers[HttpHeaders.COOKIE];
if( c != null )
parseCookieString(c);
return cookies;
}