diff --git a/Sources/SwiftyRequest/RestRequest.swift b/Sources/SwiftyRequest/RestRequest.swift index 63b6951..919b490 100644 --- a/Sources/SwiftyRequest/RestRequest.swift +++ b/Sources/SwiftyRequest/RestRequest.swift @@ -321,6 +321,23 @@ public class RestRequest: NSObject { task.resume() } } + private func getCookies(from response: HTTPURLResponse?) -> [HTTPCookie]? { + guard let headers = response?.allHeaderFields else{ + return nil + } + var headerFields = [String : String]() + for (key, value) in headers{ + guard let key = key as? String, let value = value as? String else{ + continue } + headerFields[key] = value + } + guard headerFields["Set-Cookie"] != nil else { + return nil + } + let url = response?.url + let dummyUrl = URL(string:"http://example.com")! + return HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url ?? dummyUrl) + } /// Request response method with the expected result of a `Data` object. /// @@ -334,7 +351,7 @@ public class RestRequest: NSObject { if let error = performSubstitutions(params: templateParams) { let result = Result.failure(error) - let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result) + let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil) completionHandler(dataResponse) return } @@ -349,19 +366,22 @@ public class RestRequest: NSObject { if let error = error { let result = Result.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } guard let data = data else { let result = Result.failure(RestError.noData) - let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies) completionHandler(dataResponse) return } let result = Result.success(data) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) } } @@ -383,7 +403,7 @@ public class RestRequest: NSObject { if let error = performSubstitutions(params: templateParams) { let result = Result.failure(error) - let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result) + let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil) completionHandler(dataResponse) return } @@ -398,7 +418,8 @@ public class RestRequest: NSObject { if let error = error { let result = Result.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -406,7 +427,8 @@ public class RestRequest: NSObject { if let responseToError = responseToError, let error = responseToError(response, data) { let result = Result.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -414,7 +436,8 @@ public class RestRequest: NSObject { // ensure data is not nil guard let data = data else { let result = Result.failure(RestError.noData) - let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -443,7 +466,8 @@ public class RestRequest: NSObject { } // execute callback - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) } } @@ -463,7 +487,7 @@ public class RestRequest: NSObject { if let error = performSubstitutions(params: templateParams) { let result = Result.failure(error) - let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result) + let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil) completionHandler(dataResponse) return } @@ -478,7 +502,8 @@ public class RestRequest: NSObject { if let error = error ?? responseToError?(response, data) { let result = Result.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -486,7 +511,8 @@ public class RestRequest: NSObject { // ensure data is not nil guard let data = data else { let result = Result.failure(RestError.noData) - let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -501,7 +527,8 @@ public class RestRequest: NSObject { } // execute callback - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) } } @@ -523,7 +550,7 @@ public class RestRequest: NSObject { if let error = performSubstitutions(params: templateParams) { let result = Result<[T]>.failure(error) - let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result) + let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil) completionHandler(dataResponse) return } @@ -538,7 +565,8 @@ public class RestRequest: NSObject { if let error = error { let result = Result<[T]>.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -546,7 +574,8 @@ public class RestRequest: NSObject { if let responseToError = responseToError, let error = responseToError(response, data) { let result = Result<[T]>.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -554,7 +583,8 @@ public class RestRequest: NSObject { // ensure data is not nil guard let data = data else { let result = Result<[T]>.failure(RestError.noData) - let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -584,7 +614,8 @@ public class RestRequest: NSObject { } // execute callback - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) } } @@ -604,7 +635,7 @@ public class RestRequest: NSObject { if let error = performSubstitutions(params: templateParams) { let result = Result.failure(error) - let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result) + let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil) completionHandler(dataResponse) return } @@ -619,7 +650,8 @@ public class RestRequest: NSObject { if let error = error { let result = Result.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -627,7 +659,8 @@ public class RestRequest: NSObject { if let responseToError = responseToError, let error = responseToError(response, data) { let result = Result.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -635,7 +668,8 @@ public class RestRequest: NSObject { // ensure data is not nil guard let data = data else { let result = Result.failure(RestError.noData) - let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies) completionHandler(dataResponse) return } @@ -646,14 +680,16 @@ public class RestRequest: NSObject { // parse data as a string guard let string = String(data: data, encoding: encoding) else { let result = Result.failure(RestError.serializationError) - let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies) completionHandler(dataResponse) return } // execute callback let result = Result.success(string) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) } } @@ -673,7 +709,7 @@ public class RestRequest: NSObject { if let error = performSubstitutions(params: templateParams) { let result = Result.failure(error) - let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result) + let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil) completionHandler(dataResponse) return } @@ -688,21 +724,24 @@ public class RestRequest: NSObject { if let error = error { let result = Result.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } if let responseToError = responseToError, let error = responseToError(response, data) { let result = Result.failure(error) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) return } // execute callback let result = Result.success(()) - let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result) + let cookies = self.getCookies(from: response) + let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies) completionHandler(dataResponse) } } @@ -846,6 +885,9 @@ public struct RestResponse { /// The Reponse Result. public let result: Result + + // The cookies + public let cookies: [HTTPCookie]? } /// Enum to differentiate a success or failure. diff --git a/TestServer/Sources/TestServer/main.swift b/TestServer/Sources/TestServer/main.swift index a535c99..7d182fb 100644 --- a/TestServer/Sources/TestServer/main.swift +++ b/TestServer/Sources/TestServer/main.swift @@ -1,6 +1,23 @@ import Kitura import HeliumLogger import FileKit +import Foundation + +func cookieHandler(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws { + let number = request.parameters["number"].map { Int($0) ?? 0 } ?? 0 + for no in 0..