1
1
import * as types from 'tns-core-modules/utils/types' ;
2
- import { Headers , HttpError , HttpRequestOptions } from './http-request-common' ;
3
- import * as domainDebugger from 'tns-core-modules/debugger' ;
2
+ import { Headers , HttpError , HttpRequestOptions , TNSHttpDebugging } from './http-request-common' ;
4
3
5
4
export type CancellablePromise = Promise < any > & { cancel : ( ) => void } ;
6
5
@@ -98,32 +97,26 @@ class NSURLSessionTaskDelegateImpl extends NSObject
98
97
dataTask : NSURLSessionDataTask ,
99
98
data : NSData
100
99
) {
101
- const method = this . _request . HTTPMethod . toLowerCase ( ) ;
102
- if ( method !== 'post' && method !== 'put' ) {
103
- if ( ! this . _loadingSent ) {
104
- const lengthComputable = this . _lastProgress . lengthComputable ;
105
- this . _onLoading ( {
106
- lengthComputable,
107
- loaded : this . _data . length ,
108
- total : this . _lastProgress . total
109
- } ) ;
110
- this . _loadingSent = true ;
111
- }
112
- if ( this . _data ) {
113
- this . _data . appendData ( data ) ;
114
- }
115
- if ( this . _onProgress ) {
116
- const lengthComputable = this . _lastProgress . lengthComputable ;
117
- this . _onProgress ( {
118
- lengthComputable,
119
- loaded : this . _data . length ,
120
- total : this . _lastProgress . total
121
- } ) ;
122
- }
123
- } else {
124
- if ( this . _data ) {
125
- this . _data . appendData ( data ) ;
126
- }
100
+ // const method = this._request.HTTPMethod.toLowerCase();
101
+ if ( this . _onLoading && ! this . _loadingSent ) {
102
+ const lengthComputable = this . _lastProgress . lengthComputable ;
103
+ this . _onLoading ( {
104
+ lengthComputable,
105
+ loaded : this . _data . length ,
106
+ total : this . _lastProgress . total
107
+ } ) ;
108
+ this . _loadingSent = true ;
109
+ }
110
+ if ( this . _onProgress ) {
111
+ const lengthComputable = this . _lastProgress . lengthComputable ;
112
+ this . _onProgress ( {
113
+ lengthComputable,
114
+ loaded : this . _data . length ,
115
+ total : this . _lastProgress . total
116
+ } ) ;
117
+ }
118
+ if ( this . _data ) {
119
+ this . _data . appendData ( data ) ;
127
120
}
128
121
}
129
122
@@ -134,30 +127,21 @@ class NSURLSessionTaskDelegateImpl extends NSObject
134
127
totalBytesSent ,
135
128
totalBytesExpectedToSend
136
129
) {
130
+ if ( this . _onLoading || this . _onProgress ) {
131
+ const lengthComputable = totalBytesExpectedToSend > - 1 ;
132
+ this . _lastProgress = {
133
+ lengthComputable,
134
+ loaded : totalBytesSent ,
135
+ total : lengthComputable ? totalBytesExpectedToSend : 0
136
+ } ;
137
+ if ( this . _onLoading && ! this . _loadingSent ) {
138
+ this . _onLoading ( this . _lastProgress ) ;
139
+ this . _loadingSent = true ;
140
+ }
137
141
if ( this . _onProgress ) {
138
- const method = this . _request . HTTPMethod . toLowerCase ( ) ;
139
- if ( method === 'post' || method === 'put' ) {
140
- const lengthComputable = totalBytesExpectedToSend > - 1 ;
141
- if ( ! this . _loadingSent ) {
142
- this . _onLoading ( {
143
- lengthComputable,
144
- loaded : totalBytesSent ,
145
- total : lengthComputable ? totalBytesExpectedToSend : 0
146
- } ) ;
147
- this . _loadingSent = true ;
148
- }
149
- this . _onProgress ( {
150
- lengthComputable,
151
- loaded : totalBytesSent ,
152
- total : lengthComputable ? totalBytesExpectedToSend : 0
153
- } ) ;
154
- this . _lastProgress = {
155
- lengthComputable,
156
- loaded : totalBytesSent ,
157
- total : lengthComputable ? totalBytesExpectedToSend : 0
158
- } ;
159
- }
142
+ this . _onProgress ( this . _lastProgress ) ;
160
143
}
144
+ }
161
145
}
162
146
163
147
public URLSessionDataTaskDidReceiveResponseCompletionHandler (
@@ -170,40 +154,33 @@ class NSURLSessionTaskDelegateImpl extends NSObject
170
154
this . _statusCode = ( response as any ) . statusCode ;
171
155
this . _url = response . URL . absoluteString ;
172
156
this . _response = response ;
173
- const method = this . _request . HTTPMethod . toLowerCase ( ) ;
174
- if ( method !== 'post' && method !== 'put' ) {
175
- if ( this . _onHeaders ) {
176
- const headers = { } ;
177
- if ( response && response . allHeaderFields ) {
178
- const headerFields = response . allHeaderFields ;
179
- headerFields . enumerateKeysAndObjectsUsingBlock (
180
- ( key , value , stop ) => {
181
- addHeader ( headers , key , value ) ;
182
- }
183
- ) ;
184
- }
185
- this . _onHeaders (
186
- {
187
- headers,
188
- status : this . _statusCode
157
+ if ( this . _onHeaders ) {
158
+ const headers = { } ;
159
+ if ( response && response . allHeaderFields ) {
160
+ const headerFields = response . allHeaderFields ;
161
+ headerFields . enumerateKeysAndObjectsUsingBlock (
162
+ ( key , value , stop ) => {
163
+ addHeader ( headers , key , value ) ;
189
164
}
190
165
) ;
191
166
}
192
- if ( this . _onProgress ) {
193
- const lengthComputable =
194
- response . expectedContentLength &&
195
- response . expectedContentLength > - 1 ;
196
- this . _onProgress ( {
197
- lengthComputable,
198
- loaded : 0 ,
199
- total : lengthComputable ? response . expectedContentLength : 0
200
- } ) ;
201
- this . _lastProgress = {
202
- lengthComputable,
203
- loaded : 0 ,
204
- total : lengthComputable ? response . expectedContentLength : 0
205
- } ;
206
- }
167
+ this . _onHeaders (
168
+ {
169
+ headers,
170
+ status : this . _statusCode
171
+ }
172
+ ) ;
173
+ }
174
+ if ( this . _onProgress ) {
175
+ const lengthComputable =
176
+ response . expectedContentLength &&
177
+ response . expectedContentLength > - 1 ;
178
+ this . _lastProgress = {
179
+ lengthComputable,
180
+ loaded : 0 ,
181
+ total : lengthComputable ? response . expectedContentLength : 0
182
+ } ;
183
+ this . _onProgress ( this . _lastProgress ) ;
207
184
}
208
185
}
209
186
@@ -273,12 +250,12 @@ class NSURLSessionTaskDelegateImpl extends NSObject
273
250
}
274
251
const request = this . _request as NSURLRequest ;
275
252
let contentType = request . allHTTPHeaderFields . objectForKey ( 'Content-Type' ) ;
276
- if ( contentType == null ) {
277
- contentType = request . allHTTPHeaderFields . objectForKey ( 'content-Type ' ) ;
253
+ if ( ! contentType ) {
254
+ contentType = request . allHTTPHeaderFields . objectForKey ( 'content-type ' ) ;
278
255
}
279
256
let acceptHeader ;
280
257
281
- if ( contentType == null ) {
258
+ if ( ! contentType ) {
282
259
acceptHeader = request . allHTTPHeaderFields . objectForKey ( 'Accept' ) ;
283
260
} else {
284
261
acceptHeader = contentType ;
@@ -390,8 +367,13 @@ export class Http {
390
367
391
368
try {
392
369
393
- const network = domainDebugger . getNetwork ( ) ;
394
- const debugRequest = network && network . create ( ) ;
370
+ let domainDebugger ;
371
+ let debugRequest ;
372
+ if ( TNSHttpDebugging . enabled ) {
373
+ domainDebugger = require ( 'tns-core-modules/debugger' ) ;
374
+ const network = domainDebugger . getNetwork ( ) ;
375
+ debugRequest = network && network . create ( ) ;
376
+ }
395
377
396
378
const urlRequest = NSMutableURLRequest . requestWithURL (
397
379
NSURL . URLWithString ( options . url )
0 commit comments