|
| 1 | +// |
| 2 | +// HTTPStatusCode.swift |
| 3 | +// QuickHatch |
| 4 | +// |
| 5 | +// Created by Daniel Koster on 3/30/17. |
| 6 | +// Copyright © 2019 DaVinci Labs. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +/** |
| 11 | + HTTP status codes as per http://en.wikipedia.org/wiki/List_of_HTTP_status_codes |
| 12 | + |
| 13 | + The RF2616 standard is completely covered (http://www.ietf.org/rfc/rfc2616.txt) |
| 14 | + */ |
| 15 | +public enum HTTPStatusCode: Int, Sendable { |
| 16 | + // Informational |
| 17 | + case httpContinue = 100 |
| 18 | + case switchingProtocols = 101 |
| 19 | + case processing = 102 |
| 20 | + |
| 21 | + // Success |
| 22 | + case oK = 200 |
| 23 | + case created = 201 |
| 24 | + case accepted = 202 |
| 25 | + case nonAuthoritativeInformation = 203 |
| 26 | + case noContent = 204 |
| 27 | + case resetContent = 205 |
| 28 | + case partialContent = 206 |
| 29 | + case multiStatus = 207 |
| 30 | + case alreadyReported = 208 |
| 31 | + case iMUsed = 226 |
| 32 | + |
| 33 | + // Redirections |
| 34 | + case multipleChoices = 300 |
| 35 | + case movedPermanently = 301 |
| 36 | + case found = 302 |
| 37 | + case seeOther = 303 |
| 38 | + case notModified = 304 |
| 39 | + case useProxy = 305 |
| 40 | + case switchProxy = 306 |
| 41 | + case temporaryRedirect = 307 |
| 42 | + case permanentRedirect = 308 |
| 43 | + |
| 44 | + // Client Errors |
| 45 | + case badRequest = 400 |
| 46 | + case unauthorized = 401 |
| 47 | + case paymentRequired = 402 |
| 48 | + case forbidden = 403 |
| 49 | + case notFound = 404 |
| 50 | + case methodNotAllowed = 405 |
| 51 | + case notAcceptable = 406 |
| 52 | + case proxyAuthenticationRequired = 407 |
| 53 | + case requestTimeout = 408 |
| 54 | + case conflict = 409 |
| 55 | + case gone = 410 |
| 56 | + case lengthRequired = 411 |
| 57 | + case preconditionFailed = 412 |
| 58 | + case requestEntityTooLarge = 413 |
| 59 | + case requestURITooLong = 414 |
| 60 | + case unsupportedMediaType = 415 |
| 61 | + case requestedRangeNotSatisfiable = 416 |
| 62 | + case expectationFailed = 417 |
| 63 | + case imATeapot = 418 |
| 64 | + case authenticationTimeout = 419 |
| 65 | + case unprocessableEntity = 422 |
| 66 | + case locked = 423 |
| 67 | + case failedDependency = 424 |
| 68 | + case upgradeRequired = 426 |
| 69 | + case preconditionRequired = 428 |
| 70 | + case tooManyRequests = 429 |
| 71 | + case requestHeaderFieldsTooLarge = 431 |
| 72 | + case loginTimeout = 440 |
| 73 | + case noResponse = 444 |
| 74 | + case retryWith = 449 |
| 75 | + case unavailableForLegalReasons = 451 |
| 76 | + case requestHeaderTooLarge = 494 |
| 77 | + case certError = 495 |
| 78 | + case noCert = 496 |
| 79 | + case hTTPToHTTPS = 497 |
| 80 | + case tokenExpired = 498 |
| 81 | + case clientClosedRequest = 499 |
| 82 | + |
| 83 | + // Server Errors |
| 84 | + case internalServerError = 500 |
| 85 | + case notImplemented = 501 |
| 86 | + case badGateway = 502 |
| 87 | + case serviceUnavailable = 503 |
| 88 | + case gatewayTimeout = 504 |
| 89 | + case hTTPVersionNotSupported = 505 |
| 90 | + case variantAlsoNegotiates = 506 |
| 91 | + case insufficientStorage = 507 |
| 92 | + case loopDetected = 508 |
| 93 | + case bandwidthLimitExceeded = 509 |
| 94 | + case notExtended = 510 |
| 95 | + case networkAuthenticationRequired = 511 |
| 96 | + case networkTimeoutError = 599 |
| 97 | +} |
| 98 | + |
| 99 | +extension HTTPStatusCode { |
| 100 | + /// Informational - Request received, continuing process. |
| 101 | + public var isInformational: Bool { |
| 102 | + return self.rawValue >= 100 && self.rawValue <= 199 |
| 103 | + } |
| 104 | + /// Success - The action was successfully received, understood, and accepted. |
| 105 | + public var isSuccess: Bool { |
| 106 | + return self.rawValue >= 200 && self.rawValue <= 299 |
| 107 | + } |
| 108 | + /// Redirection - Further action must be taken in order to complete the request. |
| 109 | + public var isRedirection: Bool { |
| 110 | + return self.rawValue >= 300 && self.rawValue <= 399 |
| 111 | + } |
| 112 | + /// Client Error - The request contains bad syntax or cannot be fulfilled. |
| 113 | + public var isClientError: Bool { |
| 114 | + return self.rawValue >= 400 && self.rawValue <= 499 |
| 115 | + } |
| 116 | + /// Server Error - The server failed to fulfill an apparently valid request. |
| 117 | + public var isServerError: Bool { |
| 118 | + return self.rawValue >= 500 && self.rawValue <= 599 |
| 119 | + } |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | +extension HTTPStatusCode { |
| 124 | + /// - returns: a localized string suitable for displaying to users that describes the specified status code. |
| 125 | + public var localizedReasonPhrase: String { |
| 126 | + return HTTPURLResponse.localizedString(forStatusCode: rawValue) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// MARK: - Printing |
| 131 | + |
| 132 | +extension HTTPStatusCode: CustomDebugStringConvertible, CustomStringConvertible { |
| 133 | + public var description: String { |
| 134 | + return "\(rawValue) - \(localizedReasonPhrase)" |
| 135 | + } |
| 136 | + public var debugDescription: String { |
| 137 | + return "HTTPStatusCode:\(description)" |
| 138 | + } |
| 139 | +} |
0 commit comments