-
Notifications
You must be signed in to change notification settings - Fork 2
/
header_parser.swift
430 lines (394 loc) · 9.02 KB
/
header_parser.swift
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
public struct Header: CustomStringConvertible {
public var method = ""
public var uri = ""
public var httpVersion = ""
public var fields = [String: String]()
public subscript(key: String) -> String? {
get { return fields[key] ?? nil }
set { fields[key] = newValue }
}
public var description: String {
return "Header(method: \(inspect( method)), uri: \(inspect( uri)), " +
"httpVersion: \(inspect( httpVersion)), " +
"fields: \(inspect( fields)))"
}
}
enum HeaderParserEntry {
case Method
case MethodStarted
case Space
case Uri
case UriStarted
case HttpVersion
case HttpVersionStarted
case LineFeed
case CarriageReturn
case Key
case HeaderExit
case KeyStarted
case Colon
case Value
case ValueStarted
}
// Supports streaming.
public struct HeaderParser {
var stream = [UInt8]()
var entryParser: HeaderParserEntry = .Method
public var header = Header()
var index = 0
var length = 0
var linedUpParser: HeaderParserEntry = .Method
var tokenIndex = -1
var keyToken = ""
var tokenBuffer = [UInt8](repeating: 0, count: 1024)
var tokenBufferEnd = 0
var done = false
var _bodyIndex = -1
public init() { }
public var isDone: Bool { return done }
mutating func addToTokenBuffer(a: [UInt8], startIndex: Int, endIndex: Int) {
let tbe = tokenBufferEnd
let blen = tokenBuffer.count
let ne = tbe + (endIndex - startIndex)
if ne >= blen {
var c = [UInt8](repeating: 0, count: ne * 2)
for i in 0..<tbe {
c[i] = tokenBuffer[i]
}
tokenBuffer = c
}
var j = tbe
for i in startIndex..<endIndex {
tokenBuffer[j] = a[i]
j += 1
}
tokenBufferEnd = j
}
mutating func next() throws {
switch entryParser {
case .Method:
try inMethod()
case .MethodStarted:
try inMethodStarted()
case .Space:
try inSpace()
case .Uri:
try inUri()
case .UriStarted:
try inUriStarted()
case .HttpVersion:
try inHttpVersion()
case .HttpVersionStarted:
try inHttpVersionStarted()
case .LineFeed:
try inLineFeed()
case .CarriageReturn:
try inCarriageReturn()
case .Key:
try inKey()
case .HeaderExit:
try inHeaderExit()
case .KeyStarted:
try inKeyStarted()
case .Colon:
try inColon()
case .Value:
try inValue()
case .ValueStarted:
try inValueStarted()
}
}
mutating public func parse(bytes: [UInt8], maxBytes: Int = -1) throws {
stream = bytes
index = 0
length = maxBytes < 0 ? bytes.count : maxBytes
while index < length {
try next()
}
if tokenIndex >= 0 {
addToTokenBuffer(a: stream, startIndex: tokenIndex, endIndex: length)
tokenIndex = 0 // Set it at 0 to continue supporting addToTokenBuffer.
}
}
mutating func collectString(endIndex: Int) -> String? {
var s: String?
if tokenBufferEnd > 0 {
addToTokenBuffer(a: stream, startIndex: tokenIndex, endIndex: endIndex)
s = String.fromCharCodes(charCodes: tokenBuffer, start: 0,
end: tokenBufferEnd - 1)
tokenBufferEnd = 0
} else {
s = String.fromCharCodes(charCodes: stream, start: tokenIndex,
end: endIndex - 1)
}
index = endIndex + 1
tokenIndex = -1
return s
}
mutating func inMethod() throws {
let i = index
let c = stream[i]
if c >= 65 && c <= 90 { // A-Z
entryParser = .MethodStarted
tokenIndex = i
index = i + 1
} else {
throw HeaderParserError.Method
}
}
mutating func inMethodStarted() throws {
var i = index
let len = length
repeat {
let c = stream[i]
if c >= 65 && c <= 90 { // A-Z
// ignore
} else if c == 32 {
entryParser = .Space
linedUpParser = .Uri
if let m = collectString(endIndex: i) {
header.method = m
} else {
throw HeaderParserError.Method
}
break
} else {
throw HeaderParserError.Method
}
i += 1
} while i < len
if i >= len {
index = i
}
}
mutating func inSpace() throws {
var i = index
var c = stream[i]
let len = length
while c == 32 {
i += 1
if i >= len {
index = i
return
}
c = stream[i]
}
index = i
entryParser = linedUpParser
}
mutating func inUri() throws {
let i = index
if stream[i] > 32 {
tokenIndex = i
index = i + 1
entryParser = .UriStarted
} else {
throw HeaderParserError.URI
}
}
mutating func inUriStarted() throws {
var i = index
let len = length
repeat {
let c = stream[i]
if c > 32 {
// ignore
} else if c == 32 {
entryParser = .Space
linedUpParser = .HttpVersion
if let u = collectString(endIndex: i) {
header.uri = u
} else {
throw HeaderParserError.URI
}
break
} else {
throw HeaderParserError.URI
}
i += 1
} while i < len
if i >= len {
index = i
}
}
mutating func inHttpVersion() throws {
let i = index
if stream[i] == 72 { // H
tokenIndex = i
index = i + 1
entryParser = .HttpVersionStarted
} else {
throw HeaderParserError.Version
}
}
mutating func inHttpVersionStarted() throws {
var i = index
let len = length
func process() throws {
if let v = collectString(endIndex: i) {
header.httpVersion = v
} else {
throw HeaderParserError.Version
}
}
repeat {
let c = stream[i]
if c > 32 {
// ignore
} else if c == 32 {
entryParser = .Space
linedUpParser = .CarriageReturn
try process()
break
} else if c == 13 {
entryParser = .LineFeed
try process()
break
} else if c == 10 {
entryParser = .Key
try process()
break
} else {
throw HeaderParserError.Version
}
i += 1
} while i < len
if i >= len {
index = i
}
}
mutating func inLineFeed() throws {
if stream[index] == 10 { // \n
index += 1
entryParser = .Key
} else {
throw HeaderParserError.LineFeed
}
}
mutating func inCarriageReturn() throws {
let c = stream[index]
if c == 13 { // \r
index += 1
entryParser = .LineFeed
} else if c == 10 { // \n
index += 1
entryParser = .Key
} else {
throw HeaderParserError.CarriageReturn
}
}
mutating func inKey() throws {
let i = index
let c = stream[i]
if (c >= 65 && c <= 90) || (c >= 97 && c <= 122) { // A-Z a-z
tokenIndex = i
index = i + 1
entryParser = .KeyStarted
} else if c == 10 { // \n
done = true
_bodyIndex = index + 1
index = length // Header exit.
} else if c == 13 { // \r
index += 1
entryParser = .HeaderExit
} else {
throw HeaderParserError.Key
}
}
mutating func inHeaderExit() throws {
if stream[index] == 10 { // \n
done = true
_bodyIndex = index + 1
index = length
} else {
throw HeaderParserError.LineFeed
}
}
mutating func inKeyStarted() throws {
var i = index
let len = length
func process() throws {
if let k = collectString(endIndex: i) {
keyToken = k
} else {
throw HeaderParserError.Key
}
}
repeat {
let c = stream[i]
if c == 58 { // :
entryParser = .Space
linedUpParser = .Value
try process()
break
} else if c > 32 {
// ignore
} else if c == 32 {
entryParser = .Space
linedUpParser = .Colon
try process()
break
} else {
throw HeaderParserError.Key
}
i += 1
} while i < len
if i >= len {
index = i
}
}
mutating func inColon() throws {
if stream[index] == 58 { // :
index += 1
entryParser = .Space
linedUpParser = .Value
} else {
throw HeaderParserError.Colon
}
}
mutating func inValue() throws {
let i = index
if stream[i] > 32 {
tokenIndex = i
index = i + 1
entryParser = .ValueStarted
} else {
throw HeaderParserError.Value
}
}
mutating func inValueStarted() throws {
var i = index
let len = length
repeat {
let c = stream[i]
if c >= 32 {
// ignore
} else if c == 13 {
entryParser = .LineFeed
header[keyToken] = collectString(endIndex: i)
break
} else if c == 10 {
entryParser = .Key
header[keyToken] = collectString(endIndex: i)
break
} else {
throw HeaderParserError.Value
}
i += 1
} while i < len
if i >= len {
index = i
}
}
public var bodyIndex: Int { return _bodyIndex }
}
enum HeaderParserError: ErrorProtocol {
case Method
case URI
case Version
case LineFeed
case CarriageReturn
case Key
case Colon
case Value
}