summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/REST/HTTP.swift
blob: 595809ee0e060bd6d2a26d1c184657f45ab52c82 (plain)
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
//
//  HTTP.swift
//  HTTP
//
//  Created by pronebird on 06/09/2021.
//  Copyright © 2021 Mullvad VPN AB. All rights reserved.
//

import Foundation

/// HTTP method
struct HTTPMethod: RawRepresentable {
    static let get = HTTPMethod(rawValue: "GET")
    static let post = HTTPMethod(rawValue: "POST")
    static let delete = HTTPMethod(rawValue: "DELETE")

    let rawValue: String
    init(rawValue: String) {
        self.rawValue = rawValue.uppercased()
    }
}

enum HTTPStatus {
    static let notModified = 304

    static func isSuccess(_ code: Int) -> Bool {
        return (200..<300).contains(code)
    }
}

/// HTTP headers
enum HTTPHeader {
    static let host = "Host"
    static let authorization = "Authorization"
    static let contentType = "Content-Type"
    static let etag = "ETag"
    static let ifNoneMatch = "If-None-Match"
}

extension HTTPURLResponse {
    func value(forCaseInsensitiveHTTPHeaderField headerField: String) -> String? {
        if #available(iOS 13.0, *) {
            return self.value(forHTTPHeaderField: headerField)
        } else {
            for case let key as String in self.allHeaderFields.keys {
                if case .orderedSame = key.caseInsensitiveCompare(headerField) {
                    return self.allHeaderFields[key] as? String
                }
            }
            return nil
        }
    }
}