@@ -117,6 +117,7 @@ public final class HTTPResult : NSObject {
117117 }
118118 return nil
119119 }
120+
120121 public var statusCode: Int? {
121122 if let theResponse = self . response as? NSHTTPURLResponse {
122123 return theResponse. statusCode
@@ -133,7 +134,7 @@ public final class HTTPResult : NSObject {
133134
134135 public lazy var headers: CaseInsensitiveDictionary< String, String> = {
135136 return CaseInsensitiveDictionary < String , String > ( dictionary: ( self . response as? NSHTTPURLResponse ) ? . allHeaderFields as? [ String : String ] ?? [ : ] )
136- } ( )
137+ } ( )
137138
138139 public lazy var cookies : [ String : NSHTTPCookie ] = {
139140 let foundCookies : [ NSHTTPCookie ]
@@ -156,6 +157,35 @@ public final class HTTPResult : NSObject {
156157 public var url : NSURL ? {
157158 return response? . URL
158159 }
160+ public lazy var links : [ String : [ String : String ] ] = {
161+ var result = [ String: [ String: String] ] ( )
162+ if let content = self . headers [ " link " ] {
163+ content. componentsSeparatedByString ( " , " ) . forEach { s in
164+ let linkComponents = s. componentsSeparatedByString ( " ; " ) . map { ( $0 as NSString ) . stringByTrimmingCharactersInSet ( NSCharacterSet . whitespaceCharacterSet ( ) ) }
165+ if linkComponents. count > 1 { // although a link without a rel is valid, there's no way to reference it
166+ let urlComponent = linkComponents. first!
167+ let urlRange = urlComponent. startIndex. advancedBy ( 1 ) ..< urlComponent. endIndex. advancedBy ( - 1 )
168+ var link : [ String : String ] = [ " url " : String ( urlComponent. characters [ urlRange] ) ]
169+ linkComponents. dropFirst ( ) . forEach { s in
170+ if let equalIndex = s. characters. indexOf ( " = " ) {
171+ let componentKey = String ( s. characters [ s. startIndex..< equalIndex] )
172+ let componentValueCharacters = s. characters [ equalIndex. advancedBy ( 1 ) ..< s. endIndex]
173+ if componentValueCharacters. first == " \" " && componentValueCharacters. last == " \" " {
174+ let unquotedValueRange = componentValueCharacters. startIndex. advancedBy ( 1 ) ..< componentValueCharacters. endIndex. advancedBy ( - 1 )
175+ link [ componentKey] = String ( componentValueCharacters [ unquotedValueRange] )
176+ } else {
177+ link [ componentKey] = String ( componentValueCharacters)
178+ }
179+ }
180+ }
181+ if let rel = link [ " rel " ] {
182+ result [ rel] = link
183+ }
184+ }
185+ }
186+ }
187+ return result
188+ } ( )
159189}
160190
161191
0 commit comments