Validation.swift 5.6 KB
Newer Older
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
// Alamofire.swift
//
// Copyright (c) 2014–2015 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

extension Request {
26

27 28 29
    /**
        A closure used to validate a request that takes a URL request and URL response, and returns whether the request was valid.
    */
30
    public typealias Validation = (NSURLRequest, NSHTTPURLResponse) -> Bool
31

32 33
    /**
        Validates the request, using the specified closure.
34

35
        If validation fails, subsequent calls to response handlers will have an associated error.
36

37
        :param: validation A closure to validate the request.
38

39 40 41
        :returns: The request.
    */
    public func validate(validation: Validation) -> Self {
42
        self.delegate.queue.addOperationWithBlock {
43 44
            if let response = self.response where self.delegate.error == nil && !validation(self.request, response) {
                self.delegate.error = NSError(domain: AlamofireErrorDomain, code: -1, userInfo: nil)
45 46
            }
        }
47

48 49
        return self
    }
50

51
    // MARK: - Status Code
52

53 54
    /**
        Validates that the response has a status code in the specified range.
55

56
        If validation fails, subsequent calls to response handlers will have an associated error.
57

58
        :param: range The range of acceptable status codes.
59

60 61
        :returns: The request.
    */
62
    public func validate<S: SequenceType where S.Generator.Element == Int>(statusCode acceptableStatusCode: S) -> Self {
63
        return validate { _, response in
64 65 66
            return contains(acceptableStatusCode, response.statusCode)
        }
    }
67

68
    // MARK: - Content-Type
69

70 71 72
    private struct MIMEType {
        let type: String
        let subtype: String
73

74
        init?(_ string: String) {
75 76 77
            let components = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
                                   .substringToIndex(string.rangeOfString(";")?.endIndex ?? string.endIndex)
                                   .componentsSeparatedByString("/")
78

79 80
            if let
                type = components.first,
81 82 83 84 85 86 87 88
                subtype = components.last
            {
                self.type = type
                self.subtype = subtype
            } else {
                return nil
            }
        }
89

90
        func matches(MIME: MIMEType) -> Bool {
91
            switch (self.type, self.subtype) {
92 93 94 95 96 97 98
            case (MIME.type, MIME.subtype), (MIME.type, "*"), ("*", MIME.subtype), ("*", "*"):
                return true
            default:
                return false
            }
        }
    }
99

100 101
    /**
        Validates that the response has a content type in the specified array.
102

103
        If validation fails, subsequent calls to response handlers will have an associated error.
104

105
        :param: contentType The acceptable content types, which may specify wildcard types and/or subtypes.
106

107 108 109
        :returns: The request.
    */
    public func validate<S : SequenceType where S.Generator.Element == String>(contentType acceptableContentTypes: S) -> Self {
110
        return validate { _, response in
111 112
            if let
                responseContentType = response.MIMEType,
113 114 115
                responseMIMEType = MIMEType(responseContentType)
            {
                for contentType in acceptableContentTypes {
116
                    if let acceptableMIMEType = MIMEType(contentType) where acceptableMIMEType.matches(responseMIMEType) {
117 118 119
                        return true
                    }
                }
120 121 122 123 124 125
            } else {
                for contentType in acceptableContentTypes {
                    if let MIMEType = MIMEType(contentType) where MIMEType.type == "*" && MIMEType.subtype == "*" {
                        return true
                    }
                }
126
            }
127

128 129 130
            return false
        }
    }
131

132
    // MARK: - Automatic
133

134 135
    /**
        Validates that the response has a status code in the default acceptable range of 200...299, and that the content type matches any specified in the Accept HTTP header field.
136

137
        If validation fails, subsequent calls to response handlers will have an associated error.
138

139 140 141 142 143 144 145 146
        :returns: The request.
    */
    public func validate() -> Self {
        let acceptableStatusCodes: Range<Int> = 200..<300
        let acceptableContentTypes: [String] = {
            if let accept = self.request.valueForHTTPHeaderField("Accept") {
                return accept.componentsSeparatedByString(",")
            }
147

148 149
            return ["*/*"]
        }()
150

151 152 153
        return validate(statusCode: acceptableStatusCodes).validate(contentType: acceptableContentTypes)
    }
}