-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathHTTPStatusCodes+Extensions.swift
More file actions
222 lines (184 loc) · 8.54 KB
/
Copy pathHTTPStatusCodes+Extensions.swift
File metadata and controls
222 lines (184 loc) · 8.54 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
//
// HTTPStatusCodes+Extensions.swift
// HTTPStatusCodes
//
// Created by Richard Hodgkins on 07/06/2016.
// Copyright © 2016 Rich H. All rights reserved.
//
import Foundation
public extension HTTPStatusCode {
/// Informational - Request received, continuing process.
var isInformational: Bool {
return isIn(range: 100...199)
}
/// Success - The action was successfully received, understood, and accepted.
var isSuccess: Bool {
return isIn(range: 200...299)
}
/// Redirection - Further action must be taken in order to complete the request.
var isRedirection: Bool {
return isIn(range: 300...399)
}
/// Client Error - The request contains bad syntax or cannot be fulfilled.
var isClientError: Bool {
return isIn(range: 400...499)
}
/// Server Error - The server failed to fulfill an apparently valid request.
var isServerError: Bool {
return isIn(range: 500...599)
}
/// Cloudflare Error - a dedicated Cloudflare status code was returned.
var isCloudflareError: Bool {
return isIn(range: 520...526)
}
/// - returns: `true` if the status code is in the provided range, false otherwise.
private func isIn(range: ClosedRange<HTTPStatusCode.RawValue>) -> Bool {
return range.contains(rawValue)
}
}
public extension HTTPStatusCode {
/// - returns: a localized string suitable for displaying to users that describes the specified status code.
var localizedReasonPhrase: String {
return HTTPURLResponse.localizedString(forStatusCode: rawValue)
}
}
// MARK: - Printing
extension HTTPStatusCode: CustomDebugStringConvertible, CustomStringConvertible {
public var description: String {
return "\(rawValue) - \(localizedReasonPhrase)"
}
public var debugDescription: String {
return "HTTPStatusCode:\(description)"
}
}
// MARK: - HTTP URL Response
public extension HTTPStatusCode {
/// Obtains a possible status code from an optional HTTP URL response.
init?(HTTPResponse: HTTPURLResponse?) {
guard let statusCodeValue = HTTPResponse?.statusCode else {
return nil
}
self.init(statusCodeValue)
}
/// This is declared as it's not automatically picked up by the complier for the above init
private init?(_ rawValue: Int) {
guard let value = HTTPStatusCode(rawValue: rawValue) else {
return nil
}
self = value
}
}
public extension HTTPURLResponse {
/**
* Marked internal to expose (as `statusCodeValue`) for Objective-C interoperability only.
*
* - returns: the receiver’s HTTP status code.
*/
@objc(statusCodeValue) var statusCodeEnum: HTTPStatusCode {
return HTTPStatusCode(HTTPResponse: self)!
}
/// - returns: the receiver’s HTTP status code.
var statusCodeValue: HTTPStatusCode? {
return HTTPStatusCode(HTTPResponse: self)
}
/**
* Initializer for NSHTTPURLResponse objects.
*
* - parameter url: the URL from which the response was generated.
* - parameter statusCode: an HTTP status code.
* - parameter HTTPVersion: the version of the HTTP response as represented by the server. This is typically represented as "HTTP/1.1".
* - parameter headerFields: a dictionary representing the header keys and values of the server response.
*
* - returns: the instance of the object, or `nil` if an error occurred during initialization.
*/
@available(iOS, introduced: 7.0)
@objc(initWithURL:statusCodeValue:HTTPVersion:headerFields:)
convenience init?(url: URL, statusCode: HTTPStatusCode, httpVersion: String?, headerFields: [String : String]?) {
self.init(url: url, statusCode: statusCode.rawValue, httpVersion: httpVersion, headerFields: headerFields)
}
}
// MARK: - Deprecated cases
public extension HTTPStatusCode {
/// - deprecated: Renamed to `payloadTooLarge`
@available(*, deprecated, renamed: "payloadTooLarge")
static let requestEntityTooLarge = payloadTooLarge
/// - deprecated: Renamed to `uriTooLong`
@available(*, deprecated, renamed: "uriTooLong")
static let requestURITooLong = uriTooLong
/// - deprecated: Renamed to `rangeNotSatisfiable`
@available(*, deprecated, renamed: "rangeNotSatisfiable")
static let requestedRangeNotSatisfiable = rangeNotSatisfiable
/// - deprecated: Renamed to `iisLoginTimeout`
@available(*, deprecated, renamed: "iisLoginTimeout")
static let loginTimeout = iisLoginTimeout
/// - deprecated: Renamed to `iisRetryWith`
@available(*, deprecated, renamed: "iisRetryWith")
static let retryWith = iisRetryWith
/// - deprecated: Renamed to `nginxNoResponse`
@available(*, deprecated, renamed: "nginxNoResponse")
static let noResponse = nginxNoResponse
/// - deprecated: Renamed to `nginxSSLCertificateError`
@available(*, deprecated, renamed: "nginxSSLCertificateError")
static let certError = nginxSSLCertificateError
/// - deprecated: Renamed to `nginxSSLCertificateRequired`
@available(*, deprecated, renamed: "nginxSSLCertificateRequired")
static let noCert = nginxSSLCertificateRequired
/// - deprecated: Renamed to `nginxHTTPToHTTPS`
@available(*, deprecated, renamed: "nginxHTTPToHTTPS")
static let httpToHTTPS = nginxHTTPToHTTPS
/// - deprecated: Renamed to `nginxClientClosedRequest`
@available(*, deprecated, renamed: "nginxClientClosedRequest")
static let clientClosedRequest = nginxClientClosedRequest
/// - deprecated: Renamed to `networkConnectTimeoutError`
@available(*, deprecated, renamed: "networkConnectTimeoutError")
static let networkTimeoutError = networkConnectTimeoutError
/// Returned by version 1 of the Twitter Search and Trends API when the client is being rate limited; versions 1.1 and later use the 429 Too Many Requests (`tooManyRequests`) response code instead.
///
/// - seealso: [Twitter Error Codes & Responses](https://dev.twitter.com/docs/error-codes-responses)
@available(*, deprecated, renamed: "tooManyRequests")
static let twitterEnhanceYourCalm = tooManyRequests
}
public extension HTTPURLResponse {
/// - deprecated: Renamed to `init(url:statusCode:httpVersion:headerFields)` to correct Swift 3 naming convention.
@available(*, deprecated, renamed: "init(url:statusCode:httpVersion:headerFields:)", message: "Renamed to correct Swift 3 naming convention")
@nonobjc
convenience init?(url: URL, statusCode: HTTPStatusCode, HTTPVersion: String?, headerFields: [String : String]?) {
self.init(url: url, statusCode: statusCode, httpVersion: HTTPVersion, headerFields: headerFields)
}
}
// MARK: - Remove cases
/// Declared here for a cleaner API with no `!` types.
private let __Unavailable: HTTPStatusCode! = nil
public extension HTTPStatusCode {
/// Checkpoint: 103
///
/// Used in the resumable requests proposal to resume aborted PUT or POST requests.
///
/// - seealso: [Original proposal](https://web.archive.org/web/20151013212135/http://code.google.com/p/gears/wiki/ResumableHttpRequestsProposal)
@available(*, unavailable, renamed: "earlyHints", message: "Replaced by RFC standard code with different meaning")
static let checkpoint = __Unavailable
/// Switch Proxy: 306
///
/// No longer used. Originally meant "Subsequent requests should use the specified proxy."
///
/// - seealso: [Original draft](https://tools.ietf.org/html/draft-cohen-http-305-306-responses-00)
@available(*, unavailable, message: "No longer used")
static let switchProxy = __Unavailable
/// Authentication Timeout: 419
///
/// Removed from Wikipedia page.
@available(*, unavailable, message: "No longer available")
static let authenticationTimeout = __Unavailable
/// Method Failure: 419
///
/// A deprecated response used by the Spring Framework when a method has failed.
///
/// - seealso: [Spring Framework: HttpStatus enum documentation - `METHOD_FAILURE`](https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/HttpStatus.html#METHOD_FAILURE)
@available(*, unavailable, message: "Deprecated")
static let springFrameworkMethodFailure = __Unavailable
/// Request Header Too Large: 494
///
/// Removed and replaced with `RequestHeaderFieldsTooLarge` - 431
@available(*, unavailable, renamed: "requestHeaderFieldsTooLarge", message: "Changed to a 431 status code")
static let requestHeaderTooLarge = __Unavailable
}