-
Notifications
You must be signed in to change notification settings - Fork 34
/
HTTPHeaders.swift
382 lines (369 loc) · 13.2 KB
/
HTTPHeaders.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
//
// HTTPHeaders.swift
// PerfectLib
//
// Created by Kyle Jessup on 2016-06-17.
// Copyright (C) 2016 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
/// An HTTP request header.
public enum HTTPRequestHeader {
/// A header name type. Each has a corresponding value type.
public enum Name: Hashable {
case accept, acceptCharset, acceptEncoding, acceptLanguage, acceptDatetime, authorization
case cacheControl, connection, cookie, contentLength, contentMD5, contentType
case date, expect, forwarded, from, host
case ifMatch, ifModifiedSince, ifNoneMatch, ifRange, ifUnmodifiedSince
case maxForwards, origin, pragma, proxyAuthorization, range, referer
case te, userAgent, upgrade, via, warning, xRequestedWith, xRequestedBy, dnt
case xAuthorization, xForwardedFor, xForwardedHost, xForwardedProto
case frontEndHttps, xHttpMethodOverride, xATTDeviceId, xWapProfile
case proxyConnection, xUIDH, xCsrfToken, accessControlRequestMethod, accessControlRequestHeaders
case xB3TraceId, xB3SpanId, xB3ParentSpanId
case custom(name: String)
#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(standardName.lowercased())
}
#else
public var hashValue: Int {
return standardName.lowercased().hashValue
}
#endif
public var standardName: String {
switch self {
case .accept: return "Accept"
case .acceptCharset: return "Accept-Charset"
case .acceptEncoding: return "Accept-Encoding"
case .acceptLanguage: return "Accept-Language"
case .acceptDatetime: return "Accept-Datetime"
case .accessControlRequestMethod: return "Access-Control-Request-Method"
case .accessControlRequestHeaders: return "Access-Control-Request-Headers"
case .authorization: return "Authorization"
case .cacheControl: return "Cache-Control"
case .connection: return "Connection"
case .cookie: return "Cookie"
case .contentLength: return "Content-Length"
case .contentMD5: return "Content-MD5"
case .contentType: return "Content-Type"
case .date: return "Date"
case .expect: return "Expect"
case .forwarded: return "Forwarded"
case .from: return "From"
case .host: return "Host"
case .ifMatch: return "If-Match"
case .ifModifiedSince: return "If-Modified-Since"
case .ifNoneMatch: return "If-None-Match"
case .ifRange: return "If-Range"
case .ifUnmodifiedSince: return "If-Unmodified-Since"
case .maxForwards: return "Max-Forwards"
case .origin: return "Origin"
case .pragma: return "Pragma"
case .proxyAuthorization: return "Proxy-Authorization"
case .range: return "Range"
case .referer: return "Referer"
case .te: return "TE"
case .userAgent: return "User-Agent"
case .upgrade: return "Upgrade"
case .via: return "Via"
case .warning: return "Warning"
case .xAuthorization: return "X-Authorization"
case .xRequestedWith: return "X-Requested-with"
case .xRequestedBy: return "X-Requested-by"
case .dnt: return "DNT"
case .xForwardedFor: return "X-Forwarded-For"
case .xForwardedHost: return "X-Forwarded-Host"
case .xForwardedProto: return "X-Forwarded-Proto"
case .frontEndHttps: return "Front-End-Https"
case .xHttpMethodOverride: return "X-HTTP-Method-Override"
case .xATTDeviceId: return "X-Att-Deviceid"
case .xWapProfile: return "X-WAP-Profile"
case .proxyConnection: return "Proxy-Connection"
case .xUIDH: return "X-UIDH"
case .xCsrfToken: return "X-CSRF-Token"
case .xB3TraceId: return "X-B3-TraceId"
case .xB3SpanId: return "X-B3-SpanId"
case .xB3ParentSpanId: return "X-B3-ParentSpanId"
case .custom(let str): return str
}
}
static let lookupTable: [String:HTTPRequestHeader.Name] = [
"accept":.accept,
"accept-charset":.acceptCharset,
"accept-encoding":.acceptEncoding,
"accept-language":.acceptLanguage,
"accept-datetime":.acceptDatetime,
"access-control-request-method":.accessControlRequestMethod,
"access-control-request-headers":.accessControlRequestHeaders,
"authorization":.authorization,
"cache-control":.cacheControl,
"connection":.connection,
"cookie":.cookie,
"content-length":.contentLength,
"content-md5":.contentMD5,
"content-type":.contentType,
"date":.date,
"expect":.expect,
"forwarded":.forwarded,
"from":.from,
"host":.host,
"if-match":.ifMatch,
"if-modified-since":.ifModifiedSince,
"if-none-match":.ifNoneMatch,
"if-range":.ifRange,
"if-unmodified-since":.ifUnmodifiedSince,
"max-forwards":.maxForwards,
"origin":.origin,
"pragma":.pragma,
"proxy-authorization":.proxyAuthorization,
"range":.range,
"referer":.referer,
"te":.te,
"user-agent":.userAgent,
"upgrade":.upgrade,
"via":.via,
"warning":.warning,
"x-requested-with":.xRequestedWith,
"x-requested-by":.xRequestedBy,
"dnt":.dnt,
"x-authorization":.xAuthorization,
"x-forwarded-for":.xForwardedFor,
"x-forwarded-host":.xForwardedHost,
"x-forwarded-proto":.xForwardedProto,
"front-end-https":.frontEndHttps,
"x-http-method-override":.xHttpMethodOverride,
"x-att-deviceid":.xATTDeviceId,
"x-wap-profile":.xWapProfile,
"proxy-connection":.proxyConnection,
"x-uidh":.xUIDH,
"x-csrf-token":.xCsrfToken,
"x-b3-traceid":.xB3TraceId,
"x-b3-spanid":.xB3SpanId,
"x-b3-parentspanid":.xB3ParentSpanId
]
public static func fromStandard(name: String) -> HTTPRequestHeader.Name {
if let found = HTTPRequestHeader.Name.lookupTable[name.lowercased()] {
return found
}
return .custom(name: name)
}
}
}
public func ==(lhs: HTTPRequestHeader.Name, rhs: HTTPRequestHeader.Name) -> Bool {
return lhs.standardName.lowercased() == rhs.standardName.lowercased()
}
/// A HTTP response header.
public enum HTTPResponseHeader {
public enum Name: Hashable {
case accessControlAllowOrigin
case accessControlAllowMethods
case accessControlAllowCredentials
case accessControlAllowHeaders
case accessControlMaxAge
case acceptPatch
case acceptRanges
case age
case allow
case altSvc
case cacheControl
case connection
case contentDisposition
case contentEncoding
case contentLanguage
case contentLength
case contentLocation
case contentMD5
case contentRange
case contentType
case date
case eTag
case expires
case lastModified
case link
case location
case p3p
case pragma
case proxyAuthenticate
case publicKeyPins
case refresh
case retryAfter
case server
case setCookie
case status
case strictTransportSecurity
case trailer
case transferEncoding
case tsv
case upgrade
case vary
case via
case warning
case wwwAuthenticate
case xFrameOptions
case xxsSProtection
case contentSecurityPolicy
case xContentSecurityPolicy
case xWebKitCSP
case xContentTypeOptions
case xPoweredBy
case xUACompatible
case xContentDuration
case upgradeInsecureRequests
case xRequestID
case xCorrelationID
case xB3TraceId
case xB3SpanId
case xB3ParentSpanId
case custom(name: String)
#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(standardName.lowercased())
}
#else
public var hashValue: Int {
return standardName.lowercased().hashValue
}
#endif
public var standardName: String {
switch self {
case .accessControlAllowOrigin: return "Access-Control-Allow-Origin"
case .accessControlAllowMethods: return "Access-Control-Allow-Methods"
case .accessControlAllowCredentials: return "Access-Control-Allow-Credentials"
case .accessControlAllowHeaders: return "Access-Control-Allow-Headers"
case .accessControlMaxAge: return "Access-Control-Max-Age"
case .acceptPatch: return "Accept-Patch"
case .acceptRanges: return "Accept-Ranges"
case .age: return "Age"
case .allow: return "Allow"
case .altSvc: return "Alt-Svc"
case .cacheControl: return "Cache-Control"
case .connection: return "Connection"
case .contentDisposition: return "Content-Disposition"
case .contentEncoding: return "Content-Encoding"
case .contentLanguage: return "Content-Language"
case .contentLength: return "Content-Length"
case .contentLocation: return "Content-Location"
case .contentMD5: return "Content-MD5"
case .contentRange: return "Content-Range"
case .contentType: return "Content-Type"
case .date: return "Date"
case .eTag: return "ETag"
case .expires: return "Expires"
case .lastModified: return "Last-Modified"
case .link: return "Link"
case .location: return "Location"
case .p3p: return "P3P"
case .pragma: return "Pragma"
case .proxyAuthenticate: return "Proxy-Authenticate"
case .publicKeyPins: return "Public-Key-Pins"
case .refresh: return "Refresh"
case .retryAfter: return "Retry-After"
case .server: return "Server"
case .setCookie: return "Set-Cookie"
case .status: return "Status"
case .strictTransportSecurity: return "Strict-Transport-Security"
case .trailer: return "Trailer"
case .transferEncoding: return "Transfer-Encoding"
case .tsv: return "TSV"
case .upgrade: return "Upgrade"
case .vary: return "Vary"
case .via: return "Via"
case .warning: return "Warning"
case .wwwAuthenticate: return "WWW-Authenticate"
case .xFrameOptions: return "X-Frame-Options"
case .xxsSProtection: return "X-XSS-Protection"
case .contentSecurityPolicy: return "Content-Security-Policy"
case .xContentSecurityPolicy: return "X-Content-Security-Policy"
case .xWebKitCSP: return "X-WebKit-CSP"
case .xContentTypeOptions: return "X-Content-Type-Options"
case .xPoweredBy: return "X-Powered-By"
case .xUACompatible: return "X-UA-Compatible"
case .xContentDuration: return "X-Content-Duration"
case .upgradeInsecureRequests: return "Upgrade-Insecure-Requests"
case .xRequestID: return "X-Request-ID"
case .xCorrelationID: return "X-Correlation-ID"
case .xB3TraceId: return "X-B3-TraceId"
case .xB3SpanId: return "X-B3-SpanId"
case .xB3ParentSpanId: return "X-B3-ParentSpanId"
case .custom(let str): return str
}
}
public static func fromStandard(name: String) -> HTTPResponseHeader.Name {
switch name.lowercased() {
case "access-control-Allow-Origin": return .accessControlAllowOrigin
case "access-control-Allow-Methods": return .accessControlAllowMethods
case "access-control-Allow-Credentials": return .accessControlAllowCredentials
case "access-control-Allow-Headers": return .accessControlAllowHeaders
case "access-control-Max-Age": return .accessControlMaxAge
case "accept-patch": return .acceptPatch
case "accept-ranges": return .acceptRanges
case "age": return .age
case "allow": return .allow
case "alt-svc": return .altSvc
case "cache-control": return .cacheControl
case "connection": return .connection
case "content-disposition": return .contentDisposition
case "content-encoding": return .contentEncoding
case "content-language": return .contentLanguage
case "content-length": return .contentLength
case "content-location": return .contentLocation
case "content-mD5": return .contentMD5
case "content-range": return .contentRange
case "content-type": return .contentType
case "date": return .date
case "etag": return .eTag
case "expires": return .expires
case "last-modified": return .lastModified
case "link": return .link
case "location": return .location
case "p3p": return .p3p
case "pragma": return .pragma
case "proxy-authenticate": return .proxyAuthenticate
case "public-key-pins": return .publicKeyPins
case "refresh": return .refresh
case "retry-after": return .retryAfter
case "server": return .server
case "set-cookie": return .setCookie
case "status": return .status
case "strict-transport-security": return .strictTransportSecurity
case "srailer": return .trailer
case "sransfer-encoding": return .transferEncoding
case "ssv": return .tsv
case "upgrade": return .upgrade
case "vary": return .vary
case "via": return .via
case "warning": return .warning
case "www-authenticate": return .wwwAuthenticate
case "x-frame-options": return .xFrameOptions
case "x-xss-protection": return .xxsSProtection
case "content-security-policy": return .contentSecurityPolicy
case "x-content-security-policy": return .xContentSecurityPolicy
case "x-webkit-csp": return .xWebKitCSP
case "x-content-type-options": return .xContentTypeOptions
case "x-powered-by": return .xPoweredBy
case "x-ua-compatible": return .xUACompatible
case "x-content-duration": return .xContentDuration
case "upgrade-insecure-requests": return .upgradeInsecureRequests
case "x-request-id": return .xRequestID
case "x-correlation-id": return .xCorrelationID
case "x-b3-traceid": return .xB3TraceId
case "x-b3-spanid": return .xB3SpanId
case "x-b3-parentspanid": return .xB3ParentSpanId
default: return .custom(name: name)
}
}
}
}
public func ==(lhs: HTTPResponseHeader.Name, rhs: HTTPResponseHeader.Name) -> Bool {
return lhs.standardName.lowercased() == rhs.standardName.lowercased()
}