Skip to content

Commit f876e47

Browse files
committed
NSMutableURLRequest initialization and getters
1 parent bf53cd8 commit f876e47

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

Foundation/NSURLRequest.swift

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
252252
@abstract Returns the HTTP request method of the receiver.
253253
@result the HTTP request method of the receiver.
254254
*/
255-
public var HTTPMethod: String? { get { return "GET" }}
255+
public var HTTPMethod: String? { get { return "GET" } }
256256

257257
/*!
258258
@method allHTTPHeaderFields
@@ -308,15 +308,40 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
308308
*/
309309
public class NSMutableURLRequest : NSURLRequest {
310310

311+
private var _HTTPMethod: String? = "GET"
312+
311313
public required init?(coder aDecoder: NSCoder) {
312314
NSUnimplemented()
313315
}
314-
/*!
316+
317+
/*!
318+
@method initWithURL:
319+
@abstract Initializes an NSMutableURLRequest with the given URL.
320+
@discussion Default values are used for cache policy
321+
(NSURLRequestUseProtocolCachePolicy) and timeout interval (60
322+
seconds).
323+
@param URL The URL for the request.
324+
@result An initialized NSMutableURLRequest.
325+
*/
326+
public init(URL: NSURL) {
327+
super.init()
328+
_URL = URL
329+
}
330+
331+
332+
/*!
315333
@method URL
316334
@abstract Sets the URL of the receiver.
317335
@param URL The new URL for the receiver.
318336
*/
319-
/*@NSCopying */ public override var URL: NSURL? { get { NSUnimplemented() } set { NSUnimplemented() } }
337+
/*@NSCopying */ public override var URL: NSURL? {
338+
get {
339+
return _URL
340+
}
341+
set(newURL) {
342+
_URL = newURL
343+
}
344+
}
320345

321346
/*!
322347
@method setMainDocumentURL:
@@ -329,14 +354,27 @@ public class NSMutableURLRequest : NSURLRequest {
329354
"only from same domain as main document" policy, and possibly
330355
other things in the future.
331356
*/
332-
/*@NSCopying*/ public override var mainDocumentURL: NSURL? { get { NSUnimplemented() } set { NSUnimplemented() } }
357+
/*@NSCopying*/ public override var mainDocumentURL: NSURL? {
358+
get {
359+
return _mainDocumentURL
360+
} set(newMainDocumentURL) {
361+
_mainDocumentURL = newMainDocumentURL
362+
}
363+
}
364+
333365

334366
/*!
335367
@method HTTPMethod
336368
@abstract Sets the HTTP request method of the receiver.
337369
@result the HTTP request method of the receiver.
338370
*/
339-
public override var HTTPMethod: String? { get { NSUnimplemented() } set { NSUnimplemented() } }
371+
public override var HTTPMethod: String? {
372+
get {
373+
return _HTTPMethod
374+
} set(newHTTPMethod) {
375+
_HTTPMethod = newHTTPMethod
376+
}
377+
}
340378

341379
/*!
342380
@method setValue:forHTTPHeaderField:
@@ -348,7 +386,12 @@ public class NSMutableURLRequest : NSURLRequest {
348386
@param value the header field value.
349387
@param field the header field name (case-insensitive).
350388
*/
351-
public func setValue(value: String?, forHTTPHeaderField field: String) { NSUnimplemented() }
389+
public func setValue(value: String?, forHTTPHeaderField field: String) {
390+
if _httpHeaderFields == nil {
391+
_httpHeaderFields = [:]
392+
}
393+
_httpHeaderFields?[field.lowercaseString] = value
394+
}
352395

353396
/*!
354397
@method addValue:forHTTPHeaderField:
@@ -364,7 +407,16 @@ public class NSMutableURLRequest : NSURLRequest {
364407
@param value the header field value.
365408
@param field the header field name (case-insensitive).
366409
*/
367-
public func addValue(value: String, forHTTPHeaderField field: String) { NSUnimplemented() }
410+
public func addValue(value: String, forHTTPHeaderField field: String) {
411+
if _httpHeaderFields == nil {
412+
_httpHeaderFields = [:]
413+
}
414+
if let oldValue = _httpHeaderFields?[field.lowercaseString] {
415+
_httpHeaderFields?[field.lowercaseString] = "\(oldValue),\(value)"
416+
} else {
417+
_httpHeaderFields?[field.lowercaseString] = value
418+
}
419+
}
368420
}
369421

370422

0 commit comments

Comments
 (0)