11import { isDefined , isNullOrUndefined , isObject } from 'tns-core-modules/utils/types' ;
22import * as Https from './https.common' ;
33
4- interface Ipolicies {
5- def : AFSecurityPolicy ;
6- secured : boolean ;
7- secure ?: AFSecurityPolicy ;
4+ let useLegacy : boolean = false ;
5+
6+ let cache : NSURLCache ;
7+
8+ export function setCache ( options ?: Https . CacheOptions ) {
9+ if ( options ) {
10+ cache = NSURLCache . alloc ( ) . initWithMemoryCapacityDiskCapacityDirectoryURL (
11+ options . memorySize ,
12+ options . diskSize ,
13+ NSURL . URLWithString ( options . diskLocation )
14+ ) ;
15+ } else {
16+ cache = null ;
17+ }
18+ NSURLCache . sharedURLCache = cache ;
19+ }
20+ export function clearCache ( ) {
21+ if ( cache ) {
22+ cache . removeAllCachedResponses ( ) ;
23+ }
824}
925
10- let useLegacy : boolean = false ;
26+ interface Ipolicies {
27+ def : AFSecurityPolicy ;
28+ secured : boolean ;
29+ secure ?: AFSecurityPolicy ;
30+ }
1131
1232let policies : Ipolicies = {
13- def : AFSecurityPolicy . defaultPolicy ( ) ,
14- secured : false ,
33+ def : AFSecurityPolicy . defaultPolicy ( ) ,
34+ secured : false ,
1535} ;
1636
1737policies . def . allowInvalidCertificates = true ;
1838policies . def . validatesDomainName = false ;
1939
2040export function enableSSLPinning ( options : Https . HttpsSSLPinningOptions ) {
21- if ( ! policies . secure ) {
41+ if ( ! policies . secure ) {
2242 policies . secure = AFSecurityPolicy . policyWithPinningMode ( AFSSLPinningMode . PublicKey ) ;
2343 policies . secure . allowInvalidCertificates = ( isDefined ( options . allowInvalidCertificates ) ) ? options . allowInvalidCertificates : false ;
2444 policies . secure . validatesDomainName = ( isDefined ( options . validatesDomainName ) ) ? options . validatesDomainName : true ;
25- let data = NSData . dataWithContentsOfFile ( options . certificate ) ;
26- policies . secure . pinnedCertificates = NSSet . setWithObject ( data ) ;
27- }
45+ let data = NSData . dataWithContentsOfFile ( options . certificate ) ;
46+ policies . secure . pinnedCertificates = NSSet . setWithObject ( data ) ;
47+ }
2848 useLegacy = ( isDefined ( options . useLegacy ) ) ? options . useLegacy : false ;
29- policies . secured = true ;
49+ policies . secured = true ;
3050 console . log ( 'nativescript-https > Enabled SSL pinning' ) ;
3151}
3252
3353export function disableSSLPinning ( ) {
34- policies . secured = false ;
54+ policies . secured = false ;
3555 console . log ( 'nativescript-https > Disabled SSL pinning' ) ;
3656}
3757
3858console . info ( 'nativescript-https > Disabled SSL pinning by default' ) ;
3959
4060function AFSuccess ( resolve , task : NSURLSessionDataTask , data ?: NSDictionary < string , any > & NSData & NSArray < any > ) {
4161 let content = getData ( data ) ;
42- resolve ( { task, content} ) ;
62+ resolve ( { task, content } ) ;
4363}
4464
4565function AFFailure ( resolve , reject , task : NSURLSessionDataTask , error : NSError ) {
@@ -60,8 +80,8 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError)
6080 } else {
6181 let content : any = {
6282 body : parsedData ,
63- description : error . description ,
64- reason : error . localizedDescription ,
83+ description : error . description ,
84+ reason : error . localizedDescription ,
6585 url : error . userInfo . objectForKey ( 'NSErrorFailingURLKey' ) . description
6686 } ;
6787
@@ -70,53 +90,71 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError)
7090 }
7191
7292 let reason = error . localizedDescription ;
73- resolve ( { task, content, reason} ) ;
74- }
93+ resolve ( { task, content, reason } ) ;
94+ }
7595}
7696
7797export function request ( opts : Https . HttpsRequestOptions ) : Promise < Https . HttpsResponse > {
78- return new Promise ( ( resolve , reject ) => {
79- try {
98+ return new Promise ( ( resolve , reject ) => {
99+ try {
80100 const manager = AFHTTPSessionManager . alloc ( ) . initWithBaseURL ( NSURL . URLWithString ( opts . url ) ) ;
81101 if ( opts . headers && ( < any > opts . headers [ 'Content-Type' ] ) . substring ( 0 , 16 ) === 'application/json' ) {
82- manager . requestSerializer = AFJSONRequestSerializer . serializer ( ) ;
102+ manager . requestSerializer = AFJSONRequestSerializer . serializer ( ) ;
83103 manager . responseSerializer = AFJSONResponseSerializer . serializerWithReadingOptions ( NSJSONReadingOptions . AllowFragments ) ;
84- } else {
85- manager . requestSerializer = AFHTTPRequestSerializer . serializer ( ) ;
86- manager . responseSerializer = AFHTTPResponseSerializer . serializer ( ) ;
87- }
88- manager . requestSerializer . allowsCellularAccess = true ;
104+ } else {
105+ manager . requestSerializer = AFHTTPRequestSerializer . serializer ( ) ;
106+ manager . responseSerializer = AFHTTPResponseSerializer . serializer ( ) ;
107+ }
108+ manager . requestSerializer . allowsCellularAccess = true ;
89109 manager . securityPolicy = ( policies . secured === true ) ? policies . secure : policies . def ;
90110
91- let heads = opts . headers ;
92- if ( heads ) {
111+ if ( opts . cachePolicy ) {
112+ let cacheControlBuilder = new okhttp3 . CacheControl . Builder ( ) ;
113+ switch ( opts . cachePolicy ) {
114+ case "noCache" :
115+ manager . setDataTaskWillCacheResponseBlock ( ( session , task , cacheResponse ) => {
116+ return null ;
117+ } ) ;
118+ break ;
119+ case "onlyCache" :
120+ manager . requestSerializer . cachePolicy =
121+ NSURLRequestCachePolicy . ReturnCacheDataDontLoad ;
122+ break ;
123+ case "ignoreCache" :
124+ manager . requestSerializer . cachePolicy =
125+ NSURLRequestCachePolicy . ReloadIgnoringLocalCacheData ;
126+ break ;
127+ }
128+ }
129+ let heads = opts . headers ;
130+ if ( heads ) {
93131 Object . keys ( heads ) . forEach ( key => manager . requestSerializer . setValueForHTTPHeaderField ( heads [ key ] as any , key ) ) ;
94- }
95-
96- let dict = null ;
97- if ( opts . body ) {
98- let cont = opts . body ;
99- if ( Array . isArray ( cont ) ) {
100- dict = NSMutableArray . new ( ) ;
101- cont . forEach ( function ( item , idx ) {
102- dict . addObject ( item ) ;
103- } ) ;
104- } else if ( isObject ( cont ) ) {
105- dict = NSMutableDictionary . new < string , any > ( ) ;
132+ }
133+
134+ let dict = null ;
135+ if ( opts . body ) {
136+ let cont = opts . body ;
137+ if ( Array . isArray ( cont ) ) {
138+ dict = NSMutableArray . new ( ) ;
139+ cont . forEach ( function ( item , idx ) {
140+ dict . addObject ( item ) ;
141+ } ) ;
142+ } else if ( isObject ( cont ) ) {
143+ dict = NSMutableDictionary . new < string , any > ( ) ;
106144 Object . keys ( cont ) . forEach ( key => dict . setValueForKey ( cont [ key ] as any , key ) ) ;
107- }
108- }
145+ }
146+ }
109147
110148 manager . requestSerializer . timeoutInterval = opts . timeout ? opts . timeout : 10 ;
111149
112150 const headers = null ;
113151
114152 const success = ( task : NSURLSessionDataTask , data ?: any ) => {
115- AFSuccess ( resolve , task , data ) ;
153+ AFSuccess ( resolve , task , data ) ;
116154 } ;
117155
118156 const failure = ( task , error ) => {
119- AFFailure ( resolve , reject , task , error ) ;
157+ AFFailure ( resolve , reject , task , error ) ;
120158 } ;
121159
122160 const progress = ( progress : NSProgress ) => {
@@ -138,32 +176,32 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
138176 }
139177
140178
141- } catch ( error ) {
142- reject ( error ) ;
143- }
179+ } catch ( error ) {
180+ reject ( error ) ;
181+ }
144182
145183 } ) . then ( ( AFResponse : {
146184 task : NSURLSessionDataTask
147185 content : any
148186 reason ?: string
149- } ) => {
150- let sendi : Https . HttpsResponse = {
151- content : AFResponse . content ,
152- headers : { } ,
153- } ;
154-
155- let response = AFResponse . task . response as NSHTTPURLResponse ;
156- if ( ! isNullOrUndefined ( response ) ) {
157- sendi . statusCode = response . statusCode ;
158- let dict = response . allHeaderFields ;
187+ } ) => {
188+ let sendi : Https . HttpsResponse = {
189+ content : AFResponse . content ,
190+ headers : { } ,
191+ } ;
192+
193+ let response = AFResponse . task . response as NSHTTPURLResponse ;
194+ if ( ! isNullOrUndefined ( response ) ) {
195+ sendi . statusCode = response . statusCode ;
196+ let dict = response . allHeaderFields ;
159197 dict . enumerateKeysAndObjectsUsingBlock ( ( k , v ) => sendi . headers [ k ] = v ) ;
160- }
198+ }
161199
162- if ( AFResponse . reason ) {
163- sendi . reason = AFResponse . reason ;
164- }
200+ if ( AFResponse . reason ) {
201+ sendi . reason = AFResponse . reason ;
202+ }
165203
166- return Promise . resolve ( sendi ) ;
204+ return Promise . resolve ( sendi ) ;
167205 } ) ;
168206}
169207
0 commit comments