-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetCookieParser.ts
175 lines (175 loc) · 4.49 KB
/
setCookieParser.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
export class SetCookieParser {
current: number
start: number
source: string
state: ValidStates
cookies: Array<Record<string, string>> = []
currentCookie: Record<string, string> = {}
// deno-lint-ignore no-inferrable-types
currentProperty: string = ""
constructor(source: string) {
this.source = source
this.start = 0
this.current = 0
this.state = ValidStates.Name
}
parse() {
//console.log("parsing:", this.source)
while (!this.isAtEnd()) {
//console.log(this.state)
this.parseOne()
this.start = this.current
}
return this.cookies.reduce(
(acc, c) => acc.set(c.name, c),
new Map<string, Record<string, string>>(),
)
}
parseOne() {
switch (this.state) {
case ValidStates.Name:
this.parseName()
break
case ValidStates.Value:
this.parseValue()
break
case ValidStates.Debug:
//console.log("Debugger BreakPoint Reached", this.cookies)
this.current = this.source.length
break
case ValidStates.PropertyName:
this.parsePropName()
break
case ValidStates.PropertyValue:
this.parsePropValue()
break
default:
throw new Error("Unexpected state")
}
}
parseName() {
while (
!this.isAtEnd() &&
!(this.peek() == "=" || this.peek() == "," || this.peek() == ";")
) {
this.advance()
}
// console.log("Name:", this.source.substring(this.start, this.current).trim())
this.currentCookie.name = this.source.substring(this.start, this.current)
.trim()
this.current++
this.state = ValidStates.Value
}
parseValue() {
while (!this.isAtEnd() && !(this.peek() == ";" || this.peek() == ",")) {
this.advance()
}
this.currentCookie.value = this.source.substring(this.start, this.current)
this.afterNameAndValue()
}
afterNameAndValue() {
if (this.isAtEnd()) {
this.state = ValidStates.End
this.cookies.push(this.currentCookie)
this.currentCookie = {}
return
}
this.currentProperty = ""
const c = this.peek()
this.advance()
switch (c) {
case ";":
switch (this.state) {
case ValidStates.Value:
this.state = ValidStates.PropertyName
break
case ValidStates.PropertyValue:
this.state = ValidStates.PropertyName
break
case ValidStates.PropertyName:
this.state = ValidStates.PropertyName
break
default:
throw new Error("Unexpected state: " + this.state)
}
this.state = ValidStates.PropertyName
break
case ",":
this.state = ValidStates.Name
this.cookies.push(this.currentCookie)
this.currentCookie = {}
break
case "\0":
this.state = ValidStates.End
break
default:
throw new Error("Unexpected character")
}
}
parsePropName() {
while (
!this.isAtEnd() &&
!(this.peek() == "=" || this.peek() == ";" || this.peek() == ",")
) {
this.advance()
}
if ((this.peek() == ";" || this.peek() == ",")) {
this
.currentCookie[this.source.substring(this.start, this.current).trim()] =
""
this.afterNameAndValue()
return
}
this.currentProperty = this.source.substring(this.start, this.current)
.trim()
this.currentCookie[this.currentProperty] = ""
this.advance()
this.state = ValidStates.PropertyValue
}
parsePropValue() {
while (
!this.isAtEnd() &&
!(this.peek() == ";" ||
(this.currentProperty != "expires" && this.peek() == ","))
) {
this.advance()
}
this.currentCookie[this.currentProperty] = this.source.substring(
this.start,
this.current,
)
this.afterNameAndValue()
}
peek() {
if (this.isAtEnd()) return "\0"
return this.source[this.current]
}
peekNext() {
if (this.current + 1 >= this.source.length) return "\0"
return this.source[this.current + 1]
}
advance(): string {
return this.source[this.current++]
}
isAtEnd() {
return this.current >= this.source.length
}
match(expected: string) {
if (this.isAtEnd()) return false
if (this.source[this.current] != expected) return false
this.current++
return true
}
consume(expected: string) {
if (this.match(expected)) return true
throw new Error("Unexpected character")
}
}
enum ValidStates {
Name,
Value,
PropertyName,
PropertyValue,
Debug,
End,
}