forked from pointfreeco/swift-parsing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTP.swift
152 lines (135 loc) · 3.58 KB
/
HTTP.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
import Benchmark
import Parsing
/// This benchmark reproduces an HTTP parser from [a Rust parser benchmark suite][rust-parser].
///
/// [rust-parser]: https://github.com/rust-bakery/parser_benchmarks/tree/master/http
///
/// In particular, it benchmarks the same HTTP header as that defined in `one_test`.
let httpSuite = BenchmarkSuite(name: "HTTP") { suite in
let method = ParsePrint(.substring) { Prefix { $0.isToken } }
let uri = ParsePrint(.substring) { Prefix { $0 != .init(ascii: " ") } }
let httpVersion = ParsePrint(.substring) {
"HTTP/".utf8
Prefix { $0.isVersion }
}
let newline = OneOf {
"\r\n".utf8
"\n".utf8
}
let requestLine = ParsePrint(.memberwise(Request.init(method:uri:version:))) {
method
" ".utf8
uri
" ".utf8
httpVersion
newline
}
let headerValue = ParsePrint(.substring) {
Skip {
Prefix(1...) { $0.isHorizontalSpace }
}
.printing(" ".utf8)
Prefix { !$0.isNewline }
newline
}
let header = ParsePrint(.memberwise(Header.init(name:value:))) {
Prefix { $0.isToken }.map(.substring)
":".utf8
Many {
headerValue
}
}
let request = ParsePrint {
requestLine
Many {
header
}
}
let input = """
GET / HTTP/1.1
Host: www.reddit.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
"""
let expected = (
Request(
method: "GET",
uri: "/",
version: "1.1"
),
headers: [
Header(name: "Host", value: ["www.reddit.com"]),
Header(
name: "User-Agent",
value: [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:15.0) Gecko/20100101 Firefox/15.0.1"
]
),
Header(
name: "Accept",
value: ["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"]
),
Header(name: "Accept-Language", value: ["en-us,en;q=0.5"]),
Header(name: "Accept-Encoding", value: ["gzip, deflate"]),
Header(name: "Connection", value: ["keep-alive"]),
]
)
var output: (Request, [Header])!
suite.benchmark("HTTP") {
var input = input[...].utf8
output = try request.parse(&input)
} tearDown: {
precondition(output == expected)
precondition(Substring(try! request.print(output)) == input)
}
}
private struct Request: Equatable {
let method: Substring
let uri: Substring
let version: Substring
}
private struct Header: Equatable {
let name: Substring
let value: [Substring]
}
extension UTF8.CodeUnit {
fileprivate var isHorizontalSpace: Bool {
self == .init(ascii: " ") || self == .init(ascii: "\t")
}
fileprivate var isNewline: Bool {
self == .init(ascii: "\n") || self == .init(ascii: "\r")
}
fileprivate var isToken: Bool {
switch self {
case 128...,
...31,
.init(ascii: "("),
.init(ascii: ")"),
.init(ascii: "<"),
.init(ascii: ">"),
.init(ascii: "@"),
.init(ascii: ","),
.init(ascii: ";"),
.init(ascii: ":"),
.init(ascii: "\\"),
.init(ascii: "'"),
.init(ascii: "/"),
.init(ascii: "["),
.init(ascii: "]"),
.init(ascii: "?"),
.init(ascii: "="),
.init(ascii: "{"),
.init(ascii: "}"),
.init(ascii: " "):
return false
default:
return true
}
}
fileprivate var isVersion: Bool {
(.init(ascii: "0") ... .init(ascii: "9")).contains(self) || self == .init(ascii: ".")
}
}