@@ -155,28 +155,30 @@ const NSURLSessionTaskDelegateImpl = (NSObject as any).extend(
155
155
error : NSError
156
156
) {
157
157
if ( error ) {
158
- switch ( error . code ) {
159
- case NSURLErrorTimedOut :
160
- this . _reject ( {
161
- type : HttpError . Timeout ,
162
- ios : error ,
163
- message : error . localizedDescription
164
- } ) ;
165
- break ;
166
- case NSURLErrorCancelled :
167
- this . _reject ( {
168
- type : HttpError . Cancelled ,
169
- ios : error ,
170
- message : error . localizedDescription
171
- } ) ;
172
- break ;
173
- default :
174
- this . _reject ( {
175
- type : HttpError . Error ,
176
- ios : error ,
177
- message : error . localizedDescription
178
- } ) ;
179
- break ;
158
+ if ( this . _reject ) {
159
+ switch ( error . code ) {
160
+ case NSURLErrorTimedOut :
161
+ this . _reject ( {
162
+ type : HttpError . Timeout ,
163
+ ios : error ,
164
+ message : error . localizedDescription
165
+ } ) ;
166
+ break ;
167
+ case NSURLErrorCancelled :
168
+ this . _reject ( {
169
+ type : HttpError . Cancelled ,
170
+ ios : error ,
171
+ message : error . localizedDescription
172
+ } ) ;
173
+ break ;
174
+ default :
175
+ this . _reject ( {
176
+ type : HttpError . Error ,
177
+ ios : error ,
178
+ message : error . localizedDescription
179
+ } ) ;
180
+ break ;
181
+ }
180
182
}
181
183
} else {
182
184
const textTypes : string [ ] = [
@@ -191,6 +193,7 @@ const NSURLSessionTaskDelegateImpl = (NSObject as any).extend(
191
193
let result = false ;
192
194
for ( let i = 0 ; i < textTypes . length ; i ++ ) {
193
195
if (
196
+ contentType &&
194
197
types . isString ( contentType ) &&
195
198
contentType . toLowerCase ( ) . indexOf ( textTypes [ i ] ) >= 0
196
199
) {
@@ -202,7 +205,7 @@ const NSURLSessionTaskDelegateImpl = (NSObject as any).extend(
202
205
} ;
203
206
204
207
const headers = { } ;
205
- const response = task . response as NSHTTPURLResponse ;
208
+ const response = task ? task . response as NSHTTPURLResponse : null ;
206
209
207
210
if ( response && response . allHeaderFields ) {
208
211
const headerFields = response . allHeaderFields ;
@@ -212,121 +215,123 @@ const NSURLSessionTaskDelegateImpl = (NSObject as any).extend(
212
215
} ) ;
213
216
}
214
217
const request = this . _request as NSURLRequest ;
215
- let contentType = request . allHTTPHeaderFields . objectForKey (
216
- "Content-Type"
217
- ) ;
218
- if ( ! contentType ) {
219
- contentType = request . allHTTPHeaderFields . objectForKey (
220
- "content-type"
218
+ if ( request ) {
219
+ let contentType = request . allHTTPHeaderFields . objectForKey (
220
+ "Content-Type"
221
221
) ;
222
- }
223
- let acceptHeader ;
224
-
225
- if ( ! contentType ) {
226
- acceptHeader = request . allHTTPHeaderFields . objectForKey ( "Accept" ) ;
227
- } else {
228
- acceptHeader = contentType ;
229
- }
230
-
231
- let returnType = "text/plain" ;
232
- if (
233
- ! types . isNullOrUndefined ( acceptHeader ) &&
234
- types . isString ( acceptHeader )
235
- ) {
236
- let acceptValues = acceptHeader . split ( "," ) ;
237
- let quality = [ ] ;
238
- let defaultQuality = [ ] ;
239
- let customQuality = [ ] ;
240
- for ( let value of acceptValues ) {
241
- if ( value . indexOf ( ";q=" ) > - 1 ) {
242
- customQuality . push ( value ) ;
243
- } else {
244
- defaultQuality . push ( value ) ;
222
+ if ( ! contentType ) {
223
+ contentType = request . allHTTPHeaderFields . objectForKey (
224
+ "content-type"
225
+ ) ;
226
+ }
227
+ let acceptHeader ;
228
+
229
+ if ( ! contentType ) {
230
+ acceptHeader = request . allHTTPHeaderFields . objectForKey ( "Accept" ) ;
231
+ } else {
232
+ acceptHeader = contentType ;
233
+ }
234
+
235
+ let returnType = "text/plain" ;
236
+ if (
237
+ ! types . isNullOrUndefined ( acceptHeader ) &&
238
+ types . isString ( acceptHeader )
239
+ ) {
240
+ let acceptValues = acceptHeader . split ( "," ) ;
241
+ let quality = [ ] ;
242
+ let defaultQuality = [ ] ;
243
+ let customQuality = [ ] ;
244
+ for ( let value of acceptValues ) {
245
+ if ( value . indexOf ( ";q=" ) > - 1 ) {
246
+ customQuality . push ( value ) ;
247
+ } else {
248
+ defaultQuality . push ( value ) ;
249
+ }
245
250
}
251
+ customQuality = customQuality . sort ( ( a , b ) => {
252
+ const a_quality = parseFloat (
253
+ a . substring ( a . indexOf ( ";q=" ) ) . replace ( ";q=" , "" )
254
+ ) ;
255
+ const b_quality = parseFloat (
256
+ b . substring ( b . indexOf ( ";q=" ) ) . replace ( ";q=" , "" )
257
+ ) ;
258
+ return b_quality - a_quality ;
259
+ } ) ;
260
+ quality . push ( ...defaultQuality ) ;
261
+ quality . push ( ...customQuality ) ;
262
+ returnType = quality [ 0 ] ;
246
263
}
247
- customQuality = customQuality . sort ( ( a , b ) => {
248
- const a_quality = parseFloat (
249
- a . substring ( a . indexOf ( ";q=" ) ) . replace ( ";q=" , "" )
250
- ) ;
251
- const b_quality = parseFloat (
252
- b . substring ( b . indexOf ( ";q=" ) ) . replace ( ";q=" , "" )
253
- ) ;
254
- return b_quality - a_quality ;
255
- } ) ;
256
- quality . push ( ...defaultQuality ) ;
257
- quality . push ( ...customQuality ) ;
258
- returnType = quality [ 0 ] ;
259
- }
260
-
261
- let content ;
262
- let responseText ;
263
- if ( this . _data && isTextContentType ( returnType ) ) {
264
- responseText = NSDataToString ( this . _data ) ;
265
- content = responseText ;
266
- } else if (
267
- this . _data &&
268
- types . isString ( returnType ) &&
269
- returnType . indexOf ( "application/json" ) > - 1
270
- ) {
271
- // @ts -ignore
272
- try {
264
+
265
+ let content ;
266
+ let responseText ;
267
+ if ( this . _data && isTextContentType ( returnType ) ) {
273
268
responseText = NSDataToString ( this . _data ) ;
274
- content = JSON . parse ( responseText ) ;
275
- // content = deserialize(NSJSONSerialization.JSONObjectWithDataOptionsError(this._data, NSJSONReadingOptions.AllowFragments, null));
276
- } catch ( err ) {
277
- this . _reject ( {
278
- type : HttpError . Error ,
279
- ios : null ,
280
- message : err
281
- } ) ;
282
- return ;
269
+ content = responseText ;
270
+ } else if (
271
+ this . _data &&
272
+ types . isString ( returnType ) &&
273
+ returnType . indexOf ( "application/json" ) > - 1
274
+ ) {
275
+ // @ts -ignore
276
+ try {
277
+ responseText = NSDataToString ( this . _data ) ;
278
+ content = JSON . parse ( responseText ) ;
279
+ // content = deserialize(NSJSONSerialization.JSONObjectWithDataOptionsError(this._data, NSJSONReadingOptions.AllowFragments, null));
280
+ } catch ( err ) {
281
+ this . _reject ( {
282
+ type : HttpError . Error ,
283
+ ios : null ,
284
+ message : err
285
+ } ) ;
286
+ return ;
287
+ }
288
+ } else {
289
+ content = this . _data ;
283
290
}
284
- } else {
285
- content = this . _data ;
286
- }
287
- if (
288
- TNSHttpSettings . saveImage &&
289
- TNSHttpSettings . currentlySavedImages &&
290
- TNSHttpSettings . currentlySavedImages [ this . _url ]
291
- ) {
292
- // ensure saved to disk
293
- if ( TNSHttpSettings . currentlySavedImages [ this . _url ] . localPath ) {
294
- FileManager . writeFile (
295
- content ,
296
- TNSHttpSettings . currentlySavedImages [ this . _url ] . localPath ,
297
- function ( error , result ) {
298
- if ( TNSHttpSettings . debug ) {
299
- console . log ( "http image save:" , error ? error : result ) ;
291
+ if (
292
+ TNSHttpSettings . saveImage &&
293
+ TNSHttpSettings . currentlySavedImages &&
294
+ TNSHttpSettings . currentlySavedImages [ this . _url ]
295
+ ) {
296
+ // ensure saved to disk
297
+ if ( TNSHttpSettings . currentlySavedImages [ this . _url ] . localPath ) {
298
+ FileManager . writeFile (
299
+ content ,
300
+ TNSHttpSettings . currentlySavedImages [ this . _url ] . localPath ,
301
+ function ( error , result ) {
302
+ if ( TNSHttpSettings . debug ) {
303
+ console . log ( "http image save:" , error ? error : result ) ;
304
+ }
300
305
}
301
- }
302
- ) ;
306
+ ) ;
307
+ }
303
308
}
304
- }
305
-
306
- if ( this . _debuggerRequest ) {
307
- this . _debuggerRequest . mimeType = this . _response . MIMEType ;
308
- this . _debuggerRequest . data = this . _data ;
309
- const debugResponse = {
309
+
310
+ if ( this . _debuggerRequest ) {
311
+ this . _debuggerRequest . mimeType = this . _response . MIMEType ;
312
+ this . _debuggerRequest . data = this . _data ;
313
+ const debugResponse = {
314
+ url : this . _url ,
315
+ status : this . _statusCode ,
316
+ statusText : NSHTTPURLResponse . localizedStringForStatusCode (
317
+ this . _statusCode
318
+ ) ,
319
+ headers : headers ,
320
+ mimeType : this . _response . MIMEType ,
321
+ fromDiskCache : false
322
+ } ;
323
+ this . _debuggerRequest . responseReceived ( debugResponse ) ;
324
+ this . _debuggerRequest . loadingFinished ( ) ;
325
+ }
326
+
327
+ this . _resolve ( {
310
328
url : this . _url ,
311
- status : this . _statusCode ,
312
- statusText : NSHTTPURLResponse . localizedStringForStatusCode (
313
- this . _statusCode
314
- ) ,
315
- headers : headers ,
316
- mimeType : this . _response . MIMEType ,
317
- fromDiskCache : false
318
- } ;
319
- this . _debuggerRequest . responseReceived ( debugResponse ) ;
320
- this . _debuggerRequest . loadingFinished ( ) ;
329
+ content,
330
+ responseText,
331
+ statusCode : this . _statusCode ,
332
+ headers : headers
333
+ } ) ;
321
334
}
322
-
323
- this . _resolve ( {
324
- url : this . _url ,
325
- content,
326
- responseText,
327
- statusCode : this . _statusCode ,
328
- headers : headers
329
- } ) ;
330
335
}
331
336
}
332
337
} ,
0 commit comments