Repository navigation
Expand file tree
/
Copy pathHTTPStatus.swift
More file actions
253 lines (230 loc) · 10.1 KB
/
Copy pathHTTPStatus.swift
File metadata and controls
253 lines (230 loc) · 10.1 KB
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
public struct HTTPStatus {
public let code: Int
public var customMessage: String?
public var message: String {
return HTTPStatus.messages[code] ?? customMessage ?? "Unknown"
}
public init(safeCode code: Int, message: String? = nil) {
precondition(HTTPStatus.codeIsValid(code), "Status Code must be between 100 and 599")
self.code = code
self.customMessage = message
}
public init?(code: Int, message: String? = nil) {
guard HTTPStatus.codeIsValid(code) else { return nil }
self.code = code
self.customMessage = message
}
static func codeIsValid(code: Int) -> Bool {
return 100...599 ~= code
}
}
public extension HTTPStatus {
enum Family: String {
case Informational
case Successful
case Redirection
case ClientError
case ServerError
var range: Range<Int> {
switch self {
case Informational: return 100...199
case Successful: return 200...299
case Redirection: return 300...399
case ClientError: return 400...499
case ServerError: return 500...599
}
}
}
var family: Family {
switch code {
case Family.Informational.range:
return .Informational
case Family.Successful.range:
return .Successful
case Family.Redirection.range:
return .Redirection
case Family.ClientError.range:
return .ClientError
case Family.ServerError.range:
return .ServerError
default:
return .ServerError
}
}
var isInformational: Bool { return family == .Informational }
var isSuccessful: Bool { return family == .Successful }
var isRedirection: Bool { return family == .Redirection }
var isClientError: Bool { return family == .ClientError }
var isServerError: Bool { return family == .ServerError }
}
extension HTTPStatus: IntegerLiteralConvertible {
public init(integerLiteral: Int) {
self.init(safeCode: integerLiteral)
}
}
extension HTTPStatus: CustomStringConvertible {
public var description: String {
return "\(code) \(message)"
}
}
extension HTTPStatus: Equatable {}
public func == (left: HTTPStatus, right: HTTPStatus) -> Bool {
return left.code == right.code
}
extension HTTPStatus: Hashable {
public var hashValue: Int {
return code.hashValue
}
}
extension HTTPStatus {
static var messages = [
100: "Continue",
101: "Switching Protocols",
102: "Processing",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-Authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
207: "Multi-Status",
208: "Already Reported",
226: "IM Used",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
306: "Switch Proxy",
307: "Temporary Redirect",
308: "Permanent Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Payload Too Large",
414: "URI Too Long",
415: "Unsupported Media Type",
416: "Range Not Satisfiable",
417: "Expectation Failed",
418: "Im A Teapot",
419: "Authentication Timeout",
421: "Misdirected Request",
422: "Unprocessable Entity",
423: "Locked",
424: "Failed Dependency",
426: "Upgrade Required",
428: "Precondition Required",
429: "Too Many Requests",
431: "Request Header Fields Too Large",
440: "Login Timeout",
444: "No Response",
449: "Retry With",
451: "Unavailable For Legal Reasons",
494: "Request Header Too Large",
495: "Cert Error",
496: "No Cert",
497: "HTTP To HTTPS",
498: "Token Expired",
499: "Client Closed Request",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
505: "HTTP Version Not Supported",
506: "Variant Also Negotiates",
507: "Insufficient Storage",
508: "Loop Detected",
509: "Bandwidth Limit Exceeded",
510: "Not Extended",
511: "Network Authentication Required",
599: "Network Timeout Error"
]
}
public extension HTTPStatus {
static let Continue = HTTPStatus(safeCode: 100)
static let SwitchingProtocols = HTTPStatus(safeCode: 101)
static let Processing = HTTPStatus(safeCode: 102)
static let OK = HTTPStatus(safeCode: 200)
static let Created = HTTPStatus(safeCode: 201)
static let Accepted = HTTPStatus(safeCode: 202)
static let NonAuthoritativeInformation = HTTPStatus(safeCode: 203)
static let NoContent = HTTPStatus(safeCode: 204)
static let ResetContent = HTTPStatus(safeCode: 205)
static let PartialContent = HTTPStatus(safeCode: 206)
static let MultiStatus = HTTPStatus(safeCode: 207)
static let AlreadyReported = HTTPStatus(safeCode: 208)
static let IMUsed = HTTPStatus(safeCode: 226)
static let MultipleChoices = HTTPStatus(safeCode: 300)
static let MovedPermanently = HTTPStatus(safeCode: 301)
static let Found = HTTPStatus(safeCode: 302)
static let SeeOther = HTTPStatus(safeCode: 303)
static let NotModified = HTTPStatus(safeCode: 304)
static let UseProxy = HTTPStatus(safeCode: 305)
static let SwitchProxy = HTTPStatus(safeCode: 306)
static let TemporaryRedirect = HTTPStatus(safeCode: 307)
static let PermanentRedirect = HTTPStatus(safeCode: 308)
static let BadRequest = HTTPStatus(safeCode: 400)
static let Unauthorized = HTTPStatus(safeCode: 401)
static let PaymentRequired = HTTPStatus(safeCode: 402)
static let Forbidden = HTTPStatus(safeCode: 403)
static let NotFound = HTTPStatus(safeCode: 404)
static let MethodNotAllowed = HTTPStatus(safeCode: 405)
static let NotAcceptable = HTTPStatus(safeCode: 406)
static let ProxyAuthenticationRequired = HTTPStatus(safeCode: 407)
static let RequestTimeout = HTTPStatus(safeCode: 408)
static let Conflict = HTTPStatus(safeCode: 409)
static let Gone = HTTPStatus(safeCode: 410)
static let LengthRequired = HTTPStatus(safeCode: 411)
static let PreconditionFailed = HTTPStatus(safeCode: 412)
static let PayloadTooLarge = HTTPStatus(safeCode: 413)
static let URITooLong = HTTPStatus(safeCode: 414)
static let UnsupportedMediaType = HTTPStatus(safeCode: 415)
static let RangeNotSatisfiable = HTTPStatus(safeCode: 416)
static let ExpectationFailed = HTTPStatus(safeCode: 417)
static let ImATeapot = HTTPStatus(safeCode: 418)
static let AuthenticationTimeout = HTTPStatus(safeCode: 419)
static let MisdirectedRequest = HTTPStatus(safeCode: 421)
static let UnprocessableEntity = HTTPStatus(safeCode: 422)
static let Locked = HTTPStatus(safeCode: 423)
static let FailedDependency = HTTPStatus(safeCode: 424)
static let UpgradeRequired = HTTPStatus(safeCode: 426)
static let PreconditionRequired = HTTPStatus(safeCode: 428)
static let TooManyRequests = HTTPStatus(safeCode: 429)
static let RequestHeaderFieldsTooLarge = HTTPStatus(safeCode: 431)
static let LoginTimeout = HTTPStatus(safeCode: 440)
static let NoResponse = HTTPStatus(safeCode: 444)
static let RetryWith = HTTPStatus(safeCode: 449)
static let UnavailableForLegalReasons = HTTPStatus(safeCode: 451)
static let RequestHeaderTooLarge = HTTPStatus(safeCode: 494)
static let CertError = HTTPStatus(safeCode: 495)
static let NoCert = HTTPStatus(safeCode: 496)
static let HTTPToHTTPS = HTTPStatus(safeCode: 497)
static let TokenExpired = HTTPStatus(safeCode: 498)
static let ClientClosedRequest = HTTPStatus(safeCode: 499)
static let InternalServerError = HTTPStatus(safeCode: 500)
static let NotImplemented = HTTPStatus(safeCode: 501)
static let BadGateway = HTTPStatus(safeCode: 502)
static let ServiceUnavailable = HTTPStatus(safeCode: 503)
static let GatewayTimeout = HTTPStatus(safeCode: 504)
static let HTTPVersionNotSupported = HTTPStatus(safeCode: 505)
static let VariantAlsoNegotiates = HTTPStatus(safeCode: 506)
static let InsufficientStorage = HTTPStatus(safeCode: 507)
static let LoopDetected = HTTPStatus(safeCode: 508)
static let BandwidthLimitExceeded = HTTPStatus(safeCode: 509)
static let NotExtended = HTTPStatus(safeCode: 510)
static let NetworkAuthenticationRequired = HTTPStatus(safeCode: 511)
static let NetworkTimeoutError = HTTPStatus(safeCode: 599)
}