1
1
import { Headers , HttpError , HttpRequestOptions } from './http-request-common' ;
2
2
import * as types from 'tns-core-modules/utils/types' ;
3
+ import { NetworkAgent } from 'tns-core-modules/debugger' ;
3
4
4
5
export type CancellablePromise = Promise < any > & { cancel : ( ) => void } ;
5
6
@@ -10,6 +11,49 @@ export enum HttpResponseEncoding {
10
11
GBK
11
12
}
12
13
14
+ const statuses = {
15
+ 100 : 'Continue' ,
16
+ 101 : 'Switching Protocols' ,
17
+ 200 : 'OK' ,
18
+ 201 : 'Created' ,
19
+ 202 : 'Accepted' ,
20
+ 203 : 'Non - Authoritative Information' ,
21
+ 204 : 'No Content' ,
22
+ 205 : 'Reset Content' ,
23
+ 206 : 'Partial Content' ,
24
+ 300 : 'Multiple Choices' ,
25
+ 301 : 'Moved Permanently' ,
26
+ 302 : 'Found' ,
27
+ 303 : 'See Other' ,
28
+ 304 : 'Not Modified' ,
29
+ 305 : 'Use Proxy' ,
30
+ 307 : 'Temporary Redirect' ,
31
+ 400 : 'Bad Request' ,
32
+ 401 : 'Unauthorized' ,
33
+ 402 : 'Payment Required' ,
34
+ 403 : 'Forbidden' ,
35
+ 404 : 'Not Found' ,
36
+ 405 : 'Method Not Allowed' ,
37
+ 406 : 'Not Acceptable' ,
38
+ 407 : 'Proxy Authentication Required' ,
39
+ 408 : 'Request Timeout' ,
40
+ 409 : 'Conflict' ,
41
+ 410 : 'Gone' ,
42
+ 411 : 'Length Required' ,
43
+ 412 : 'Precondition Failed' ,
44
+ 413 : 'Request Entity Too Large' ,
45
+ 414 : 'Request - URI Too Long' ,
46
+ 415 : 'Unsupported Media Type' ,
47
+ 416 : 'Requested Range Not Satisfiable' ,
48
+ 417 : 'Expectation Failed' ,
49
+ 500 : 'Internal Server Error' ,
50
+ 501 : 'Not Implemented' ,
51
+ 502 : 'Bad Gateway' ,
52
+ 503 : 'Service Unavailable' ,
53
+ 504 : 'Gateway Timeout' ,
54
+ 505 : 'HTTP Version Not Supported'
55
+ } ;
56
+
13
57
function parseJSON ( source : string ) : any {
14
58
const src = source . trim ( ) ;
15
59
if ( src . lastIndexOf ( ')' ) === src . length - 1 ) {
@@ -22,6 +66,7 @@ function parseJSON(source: string): any {
22
66
}
23
67
24
68
const requestCallbacks = new Map ( ) ;
69
+ let requestIdCounter = 0 ;
25
70
26
71
export class Http {
27
72
constructor ( ) {
@@ -79,10 +124,18 @@ export class Http {
79
124
const headers : Headers = { } ;
80
125
let statusCode = 0 ;
81
126
let id ;
127
+ const counter = requestIdCounter ;
82
128
const request = < CancellablePromise > new Promise < any > ( ( resolve , reject ) => {
83
129
try {
130
+
84
131
// initialize the options
85
132
const javaOptions = this . buildJavaOptions ( options ) ;
133
+
134
+ // @ts -ignore
135
+ if ( global . __inspector && global . __inspector . isConnected ) {
136
+ NetworkAgent . requestWillBeSent ( requestIdCounter , options ) ;
137
+ }
138
+
86
139
const callback = new com . github . triniwiz . async . Async . Http . Callback ( {
87
140
onCancel ( param : any ) : void {
88
141
reject ( {
@@ -93,8 +146,10 @@ export class Http {
93
146
} ,
94
147
onComplete ( result : any ) : void {
95
148
let content ;
149
+ let isString = false ;
96
150
if ( result . content instanceof org . json . JSONObject || result . content instanceof org . json . JSONArray ) {
97
151
content = deserialize ( result . content ) ;
152
+ isString = true ;
98
153
} else {
99
154
content = result . content ;
100
155
}
@@ -106,6 +161,57 @@ export class Http {
106
161
addHeader ( headers , pair . key , pair . value ) ;
107
162
}
108
163
}
164
+ // send response data (for requestId) to network debugger
165
+
166
+
167
+ let contentType = headers [ 'Content-Type' ] ;
168
+ if ( contentType == null ) {
169
+ contentType = headers [ 'content-Type' ] ;
170
+ }
171
+ let acceptHeader ;
172
+
173
+ if ( contentType == null ) {
174
+ acceptHeader = headers [ 'Accept' ] ;
175
+ } else {
176
+ acceptHeader = contentType ;
177
+ }
178
+
179
+ let returnType = 'text/plain' ;
180
+ if ( acceptHeader != null ) {
181
+ let acceptValues = acceptHeader . split ( ',' ) ;
182
+ let quality = [ ] ;
183
+ let defaultQuality = [ ] ;
184
+ let customQuality = [ ] ;
185
+ for ( let value of acceptValues ) {
186
+ if ( value . indexOf ( ';q=' ) > - 1 ) {
187
+ customQuality . push ( value ) ;
188
+ } else {
189
+ defaultQuality . push ( value ) ;
190
+ }
191
+ }
192
+ customQuality = customQuality . sort ( ( a , b ) => {
193
+ const a_quality = parseFloat ( a . substring ( a . indexOf ( ';q=' ) ) . replace ( ';q=' , '' ) ) ;
194
+ const b_quality = parseFloat ( b . substring ( b . indexOf ( ';q=' ) ) . replace ( ';q=' , '' ) ) ;
195
+ return ( b_quality - a_quality ) ;
196
+ } ) ;
197
+ quality . push ( ...defaultQuality ) ;
198
+ quality . push ( ...customQuality ) ;
199
+ returnType = quality [ 0 ] ;
200
+ }
201
+
202
+ result [ 'statusCode' ] = statusCode ;
203
+ // send response data (for requestId) to network debugger
204
+ // @ts -ignore
205
+ if ( global . __inspector && global . __inspector . isConnected ) {
206
+ NetworkAgent . responseReceived ( counter , {
207
+ url : result . url ,
208
+ statusCode,
209
+ headers,
210
+ responseAsString : isString ? result . content . toString ( ) : null ,
211
+ responseAsImage : null // TODO needs base64 Image
212
+ } , headers ) ;
213
+ }
214
+
109
215
resolve ( {
110
216
url : result . url ,
111
217
content,
@@ -155,6 +261,7 @@ export class Http {
155
261
} ) ;
156
262
id = com . github . triniwiz . async . Async . Http . makeRequest ( javaOptions , callback ) ;
157
263
requestCallbacks . set ( id , callback ) ;
264
+ requestIdCounter ++ ;
158
265
} catch ( ex ) {
159
266
reject ( {
160
267
type : HttpError . Error ,
0 commit comments