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,22 @@ 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 ( data ) {
102
+ this . _data . appendData ( data ) ;
103
+
104
+ const lastProgress : any = this . _lastProgress || {
105
+ lengthComputable : false ,
106
+ total : 0
107
+ } ;
108
+ lastProgress . loaded = this . _data . length ;
109
+ if ( this . _onLoading && ! this . _loadingSent ) {
110
+ this . _onLoading ( lastProgress ) ;
111
+ this . _loadingSent = true ;
112
+ }
113
+ if ( this . _onProgress ) {
114
+ this . _onProgress ( lastProgress ) ;
115
+ }
127
116
}
128
117
}
129
118
@@ -134,30 +123,20 @@ class NSURLSessionTaskDelegateImpl extends NSObject
134
123
totalBytesSent ,
135
124
totalBytesExpectedToSend
136
125
) {
126
+ if ( this . _onLoading || this . _onProgress ) {
127
+ this . _lastProgress = {
128
+ lengthComputable : totalBytesExpectedToSend > - 1 ,
129
+ loaded : totalBytesSent ,
130
+ total : totalBytesExpectedToSend > - 1 ? totalBytesExpectedToSend : 0
131
+ } ;
132
+ if ( this . _onLoading && ! this . _loadingSent ) {
133
+ this . _onLoading ( this . _lastProgress ) ;
134
+ this . _loadingSent = true ;
135
+ }
137
136
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
- }
137
+ this . _onProgress ( this . _lastProgress ) ;
160
138
}
139
+ }
161
140
}
162
141
163
142
public URLSessionDataTaskDidReceiveResponseCompletionHandler (
@@ -170,40 +149,33 @@ class NSURLSessionTaskDelegateImpl extends NSObject
170
149
this . _statusCode = ( response as any ) . statusCode ;
171
150
this . _url = response . URL . absoluteString ;
172
151
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
152
+ if ( this . _onHeaders ) {
153
+ const headers = { } ;
154
+ if ( response && response . allHeaderFields ) {
155
+ const headerFields = response . allHeaderFields ;
156
+ headerFields . enumerateKeysAndObjectsUsingBlock (
157
+ ( key , value , stop ) => {
158
+ addHeader ( headers , key , value ) ;
189
159
}
190
160
) ;
191
161
}
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
- }
162
+ this . _onHeaders (
163
+ {
164
+ headers,
165
+ status : this . _statusCode
166
+ }
167
+ ) ;
168
+ }
169
+ if ( this . _onProgress ) {
170
+ const lengthComputable =
171
+ response . expectedContentLength &&
172
+ response . expectedContentLength > - 1 ;
173
+ this . _lastProgress = {
174
+ lengthComputable,
175
+ loaded : 0 ,
176
+ total : lengthComputable ? response . expectedContentLength : 0
177
+ } ;
178
+ this . _onProgress ( this . _lastProgress ) ;
207
179
}
208
180
}
209
181
@@ -273,12 +245,12 @@ class NSURLSessionTaskDelegateImpl extends NSObject
273
245
}
274
246
const request = this . _request as NSURLRequest ;
275
247
let contentType = request . allHTTPHeaderFields . objectForKey ( 'Content-Type' ) ;
276
- if ( contentType == null ) {
277
- contentType = request . allHTTPHeaderFields . objectForKey ( 'content-Type ' ) ;
248
+ if ( ! contentType ) {
249
+ contentType = request . allHTTPHeaderFields . objectForKey ( 'content-type ' ) ;
278
250
}
279
251
let acceptHeader ;
280
252
281
- if ( contentType == null ) {
253
+ if ( ! contentType ) {
282
254
acceptHeader = request . allHTTPHeaderFields . objectForKey ( 'Accept' ) ;
283
255
} else {
284
256
acceptHeader = contentType ;
@@ -390,8 +362,13 @@ export class Http {
390
362
391
363
try {
392
364
393
- const network = domainDebugger . getNetwork ( ) ;
394
- const debugRequest = network && network . create ( ) ;
365
+ let domainDebugger ;
366
+ let debugRequest ;
367
+ if ( TNSHttpDebugging . enabled ) {
368
+ domainDebugger = require ( 'tns-core-modules/debugger' ) ;
369
+ const network = domainDebugger . getNetwork ( ) ;
370
+ debugRequest = network && network . create ( ) ;
371
+ }
395
372
396
373
const urlRequest = NSMutableURLRequest . requestWithURL (
397
374
NSURL . URLWithString ( options . url )
0 commit comments