1- //
2-
3-
4- import * as application from 'application'
5- import { HttpRequestOptions , Headers , HttpResponse } from 'http'
6- import { isDefined , isNullOrUndefined } from 'utils/types'
1+ import * as application from 'tns-core-modules/application'
2+ import { HttpRequestOptions , Headers , HttpResponse } from 'tns-core-modules/http'
3+ import { isDefined , isNullOrUndefined } from 'tns-core-modules/utils/types'
74import * as Https from './https.common'
85
9-
10-
11- // declare var java: any
12- // declare var javax: any
13- // java.security.cert.Certificate as any
14- // declare module java {
15- // export module security {
16- // export module cert {
17- // export interface Certificate { }
18- // }
19- // }
20- // export module io {
21- // export interface FileInputStream { }
22- // }
23- // }
24-
25-
26-
276interface Ipeer {
287 enabled : boolean
298 allowInvalidCertificates : boolean
@@ -39,7 +18,6 @@ let peer: Ipeer = {
3918}
4019
4120export function enableSSLPinning ( options : Https . HttpsSSLPinningOptions ) {
42- // console.log('options', options)
4321 if ( ! peer . host && ! peer . certificate ) {
4422 let certificate : string
4523 let inputStream : java . io . FileInputStream
@@ -79,24 +57,14 @@ export function disableSSLPinning() {
7957}
8058console . info ( 'nativescript-https > Disabled SSL pinning by default' )
8159
82-
83-
8460let Client : okhttp3 . OkHttpClient
8561function getClient ( reload : boolean = false ) : okhttp3 . OkHttpClient {
86- // if (!Client) {
87- // Client = new okhttp3.OkHttpClient()
88- // }
89- // if (Client) {
90- // Client.connectionPool().evictAll()
91- // Client = null
92- // }
9362 if ( Client && reload == false ) {
9463 return Client
9564 }
9665
9766 let client = new okhttp3 . OkHttpClient . Builder ( )
9867 if ( peer . enabled == true ) {
99- // console.log('peer', peer)
10068 if ( peer . host || peer . certificate ) {
10169 let spec = okhttp3 . ConnectionSpec . MODERN_TLS
10270 client . connectionSpecs ( java . util . Collections . singletonList ( spec ) )
@@ -139,7 +107,7 @@ function getClient(reload: boolean = false): okhttp3.OkHttpClient {
139107 if ( peer . validatesDomainName == true ) {
140108 try {
141109 client . hostnameVerifier ( new javax . net . ssl . HostnameVerifier ( {
142- verify : function ( hostname : string , session : javax . net . ssl . ISSLSession ) : boolean {
110+ verify : function ( hostname : string , session : javax . net . ssl . ISSLSession ) : boolean {
143111 let pp = session . getPeerPrincipal ( ) . getName ( )
144112 let hv = javax . net . ssl . HttpsURLConnection . getDefaultHostnameVerifier ( )
145113 return (
@@ -169,15 +137,15 @@ function getClient(reload: boolean = false): okhttp3.OkHttpClient {
169137const strictModeThreadPolicyPermitAll = new android . os . StrictMode . ThreadPolicy . Builder ( ) . permitAll ( ) . build ( )
170138
171139export function request ( opts : Https . HttpsRequestOptions ) : Promise < Https . HttpsResponse > {
172- return new Promise ( function ( resolve , reject ) {
140+ return new Promise ( function ( resolve , reject ) {
173141 try {
174142 let client = getClient ( )
175143
176144 let request = new okhttp3 . Request . Builder ( )
177145 request . url ( opts . url )
178146
179147 if ( opts . headers ) {
180- Object . keys ( opts . headers ) . forEach ( function ( key ) {
148+ Object . keys ( opts . headers ) . forEach ( function ( key ) {
181149 request . addHeader ( key , opts . headers [ key ] as any )
182150 } )
183151 }
@@ -214,28 +182,7 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
214182 android . os . StrictMode . setThreadPolicy ( strictModeThreadPolicyPermitAll )
215183
216184 client . newCall ( request . build ( ) ) . enqueue ( new okhttp3 . Callback ( {
217- onResponse : function ( task , response ) {
218- // console.log('onResponse')
219- // console.keys('response', response)
220- // console.log('onResponse > response.isSuccessful()', response.isSuccessful())
221-
222- // let body = response.body()//.bytes()
223- // console.keys('body', body)
224- // console.log('body.contentType()', body.contentType())
225- // console.log('body.contentType().toString()', body.contentType().toString())
226- // console.log('body.bytes()', body.bytes())
227- // console.dump('wtf', wtf)
228- // console.log('opts.url', opts.url)
229- // console.log('body.string()', body.string())
230-
231- // let content: any = response.body().string()
232- // console.log('content', content)
233- // try {
234- // content = JSON.parse(response.body().string())
235- // } catch (error) {
236- // return reject(error)
237- // }
238-
185+ onResponse : function ( task , response ) {
239186 let content = response . body ( ) . string ( )
240187 try {
241188 content = JSON . parse ( content )
@@ -255,7 +202,7 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
255202 resolve ( { content, statusCode, headers } )
256203
257204 } ,
258- onFailure : function ( task , error ) {
205+ onFailure : function ( task , error ) {
259206 reject ( error )
260207 } ,
261208 } ) )
@@ -265,36 +212,6 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
265212 }
266213
267214 } )
268-
269- // {
270- // "content": {
271- // "code": "PreconditionFailed",
272- // "message": "!x-uuid"
273- // },
274- // "statusCode": 412,
275- // "headers": {
276- // "Content-Length": "49",
277- // "Server": "nginx/1.10.1",
278- // "Content-Type": "application/json",
279- // "Connection": "keep-alive",
280- // "Date": "Mon, 26 Dec 2016 03:31:42 GMT"
281- // }
282- // }
283-
284215}
285216
286-
287-
288217export * from './https.common'
289-
290-
291-
292-
293-
294-
295-
296-
297-
298-
299-
300-
0 commit comments