forked from daltoniam/SwiftHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPRequestSerializer.swift
More file actions
286 lines (253 loc) · 11.5 KB
/
HTTPRequestSerializer.swift
File metadata and controls
286 lines (253 loc) · 11.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// HTTPRequestSerializer.swift
//
// Created by Dalton Cherry on 6/3/14.
// Copyright (c) 2014 Vluxe. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
import Foundation
extension String {
/**
A simple extension to the String object to encode it for web request.
:returns: Encoded version of of string it was called as.
*/
var escaped: String {
return CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,self,"[].",":/?&=;+!@#$()',*",CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) as! String
}
}
/// Default Serializer for serializing an object to an HTTP request. This applies to form serialization, parameter encoding, etc.
public class HTTPRequestSerializer: NSObject {
let contentTypeKey = "Content-Type"
/// headers for the request.
public var headers = Dictionary<String,String>()
/// encoding for the request.
public var stringEncoding: UInt = NSUTF8StringEncoding
/// Send request if using cellular network or not. Defaults to true.
public var allowsCellularAccess = true
/// If the request should handle cookies of not. Defaults to true.
public var HTTPShouldHandleCookies = true
/// If the request should use piplining or not. Defaults to false.
public var HTTPShouldUsePipelining = false
/// How long the timeout interval is. Defaults to 60 seconds.
public var timeoutInterval: NSTimeInterval = 60
/// Set the request cache policy. Defaults to UseProtocolCachePolicy.
public var cachePolicy: NSURLRequestCachePolicy = NSURLRequestCachePolicy.UseProtocolCachePolicy
/// Set the network service. Defaults to NetworkServiceTypeDefault.
public var networkServiceType = NSURLRequestNetworkServiceType.NetworkServiceTypeDefault
/// Initializes a new HTTPRequestSerializer Object.
public override init() {
super.init()
}
/**
Creates a new NSMutableURLRequest object with configured options.
:param: url The url you would like to make a request to.
:param: method The HTTP method/verb for the request.
:returns: A new NSMutableURLRequest with said options.
*/
public func newRequest(url: NSURL, method: HTTPMethod) -> NSMutableURLRequest {
var request = NSMutableURLRequest(URL: url, cachePolicy: cachePolicy, timeoutInterval: timeoutInterval)
request.HTTPMethod = method.rawValue
request.cachePolicy = self.cachePolicy
request.timeoutInterval = self.timeoutInterval
request.allowsCellularAccess = self.allowsCellularAccess
request.HTTPShouldHandleCookies = self.HTTPShouldHandleCookies
request.HTTPShouldUsePipelining = self.HTTPShouldUsePipelining
request.networkServiceType = self.networkServiceType
for (key,val) in self.headers {
request.addValue(val, forHTTPHeaderField: key)
}
return request
}
/**
Creates a new NSMutableURLRequest object with configured options.
:param: url The url you would like to make a request to.
:param: method The HTTP method/verb for the request.
:param: parameters The parameters are HTTP parameters you would like to send.
:returns: A new NSMutableURLRequest with said options or an error.
*/
public func createRequest(url: NSURL, method: HTTPMethod, parameters: Dictionary<String,AnyObject>?) -> (request: NSURLRequest, error: NSError?) {
var request = newRequest(url, method: method)
var isMulti = false
//do a check for upload objects to see if we are multi form
if let params = parameters {
isMulti = isMultiForm(params)
}
if isMulti {
if(method != .POST && method != .PUT && method != .PATCH) {
request.HTTPMethod = HTTPMethod.POST.rawValue // you probably wanted a post
}
var boundary = "Boundary+\(arc4random())\(arc4random())"
if parameters != nil {
request.HTTPBody = dataFromParameters(parameters!,boundary: boundary)
}
if request.valueForHTTPHeaderField(contentTypeKey) == nil {
request.setValue("multipart/form-data; boundary=\(boundary)",
forHTTPHeaderField:contentTypeKey)
}
return (request,nil)
}
var queryString = ""
if parameters != nil {
queryString = self.stringFromParameters(parameters!)
}
if isURIParam(method) {
var para = (request.URL!.query != nil) ? "&" : "?"
var newUrl = "\(request.URL!.absoluteString!)"
if count(queryString) > 0 {
newUrl += "\(para)\(queryString)"
}
request.URL = NSURL(string: newUrl)
} else {
var charset = CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(self.stringEncoding));
if request.valueForHTTPHeaderField(contentTypeKey) == nil {
request.setValue("application/x-www-form-urlencoded; charset=\(charset)",
forHTTPHeaderField:contentTypeKey)
}
request.HTTPBody = queryString.dataUsingEncoding(self.stringEncoding)
}
return (request,nil)
}
///check for multi form objects
public func isMultiForm(params: Dictionary<String,AnyObject>) -> Bool {
for (name,object: AnyObject) in params {
if object is HTTPUpload {
return true
} else if let subParams = object as? Dictionary<String,AnyObject> {
if isMultiForm(subParams) {
return true
}
}
}
return false
}
///check if enum is a HTTPMethod that requires the params in the URL
public func isURIParam(method: HTTPMethod) -> Bool {
if(method == .GET || method == .HEAD || method == .DELETE) {
return true
}
return false
}
///convert the parameter dict to its HTTP string representation
public func stringFromParameters(parameters: Dictionary<String,AnyObject>) -> String {
return join("&", map(serializeObject(parameters, key: nil), {(pair) in
return pair.stringValue()
}))
}
///the method to serialized all the objects
func serializeObject(object: AnyObject,key: String?) -> Array<HTTPPair> {
var collect = Array<HTTPPair>()
if let array = object as? Array<AnyObject> {
for nestedValue : AnyObject in array {
collect.extend(self.serializeObject(nestedValue,key: "\(key!)[]"))
}
} else if let dict = object as? Dictionary<String,AnyObject> {
for (nestedKey, nestedObject: AnyObject) in dict {
var newKey = key != nil ? "\(key!)[\(nestedKey)]" : nestedKey
collect.extend(self.serializeObject(nestedObject,key: newKey))
}
} else {
collect.append(HTTPPair(value: object, key: key))
}
return collect
}
//create a multi form data object of the parameters
func dataFromParameters(parameters: Dictionary<String,AnyObject>,boundary: String) -> NSData {
var mutData = NSMutableData()
var multiCRLF = "\r\n"
var boundSplit = "\(multiCRLF)--\(boundary)\(multiCRLF)".dataUsingEncoding(self.stringEncoding)!
var lastBound = "\(multiCRLF)--\(boundary)--\(multiCRLF)".dataUsingEncoding(self.stringEncoding)!
mutData.appendData("--\(boundary)\(multiCRLF)".dataUsingEncoding(self.stringEncoding)!)
let pairs = serializeObject(parameters, key: nil)
let count = pairs.count-1
var i = 0
for pair in pairs {
var append = true
if let upload = pair.getUpload() {
if let data = upload.data {
mutData.appendData(multiFormHeader(pair.k, fileName: upload.fileName,
type: upload.mimeType, multiCRLF: multiCRLF).dataUsingEncoding(self.stringEncoding)!)
mutData.appendData(data)
} else {
append = false
}
} else {
let str = "\(multiFormHeader(pair.k, fileName: nil, type: nil, multiCRLF: multiCRLF))\(pair.getValue())"
mutData.appendData(str.dataUsingEncoding(self.stringEncoding)!)
}
if append {
if i == count {
mutData.appendData(lastBound)
} else {
mutData.appendData(boundSplit)
}
}
i++
}
return mutData
}
///helper method to create the multi form headers
func multiFormHeader(name: String, fileName: String?, type: String?, multiCRLF: String) -> String {
var str = "Content-Disposition: form-data; name=\"\(name.escaped)\""
if fileName != nil {
str += "; filename=\"\(fileName!)\""
}
str += multiCRLF
if type != nil {
str += "Content-Type: \(type!)\(multiCRLF)"
}
str += multiCRLF
return str
}
/// Creates key/pair of the parameters.
class HTTPPair: NSObject {
var val: AnyObject
var k: String!
init(value: AnyObject, key: String?) {
self.val = value
self.k = key
}
func getUpload() -> HTTPUpload? {
return self.val as? HTTPUpload
}
func getValue() -> String {
var val = ""
if let str = self.val as? String {
val = str
} else if self.val.description != nil {
val = self.val.description
}
return val
}
func stringValue() -> String {
var v = getValue()
if self.k == nil {
return v.escaped
}
return "\(self.k.escaped)=\(v.escaped)"
}
}
}
/// JSON Serializer for serializing an object to an HTTP request. Same as HTTPRequestSerializer, expect instead of HTTP form encoding it does JSON.
public class JSONRequestSerializer: HTTPRequestSerializer {
/**
Creates a new NSMutableURLRequest object with configured options.
:param: url The url you would like to make a request to.
:param: method The HTTP method/verb for the request.
:param: parameters The parameters are HTTP parameters you would like to send.
:returns: A new NSMutableURLRequest with said options or an error.
*/
public override func createRequest(url: NSURL, method: HTTPMethod, parameters: Dictionary<String,AnyObject>?) -> (request: NSURLRequest, error: NSError?) {
if self.isURIParam(method) {
return super.createRequest(url, method: method, parameters: parameters)
}
var request = newRequest(url, method: method)
var error: NSError?
if parameters != nil {
var charset = CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
request.setValue("application/json; charset=\(charset)", forHTTPHeaderField: self.contentTypeKey)
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters!, options: NSJSONWritingOptions(), error:&error)
}
return (request, error)
}
}