@@ -7,6 +7,8 @@ interface Ipolicies {
77 secure ?: AFSecurityPolicy ;
88}
99
10+ let useLegacy : boolean = false ;
11+
1012let policies : Ipolicies = {
1113 def : AFSecurityPolicy . defaultPolicy ( ) ,
1214 secured : false ,
@@ -23,6 +25,7 @@ export function enableSSLPinning(options: Https.HttpsSSLPinningOptions) {
2325 let data = NSData . dataWithContentsOfFile ( options . certificate ) ;
2426 policies . secure . pinnedCertificates = NSSet . setWithObject ( data ) ;
2527 }
28+ useLegacy = ( isDefined ( options . useLegacy ) ) ? options . useLegacy : false ;
2629 policies . secured = true ;
2730 console . log ( 'nativescript-https > Enabled SSL pinning' ) ;
2831}
@@ -35,72 +38,41 @@ export function disableSSLPinning() {
3538console . info ( 'nativescript-https > Disabled SSL pinning by default' ) ;
3639
3740function AFSuccess ( resolve , task : NSURLSessionDataTask , data : NSDictionary < string , any > & NSData & NSArray < any > ) {
38- let content : any ;
39- if ( data && data . class ) {
40- if ( data . enumerateKeysAndObjectsUsingBlock || ( < any > data ) instanceof NSArray ) {
41- let serial = NSJSONSerialization . dataWithJSONObjectOptionsError ( data , NSJSONWritingOptions . PrettyPrinted ) ;
42- content = NSString . alloc ( ) . initWithDataEncoding ( serial , NSUTF8StringEncoding ) . toString ( ) ;
43- } else if ( ( < any > data ) instanceof NSData ) {
44- content = NSString . alloc ( ) . initWithDataEncoding ( data , NSASCIIStringEncoding ) . toString ( ) ;
45- } else {
46- content = data ;
47- }
48-
49- try {
50- content = JSON . parse ( content ) ;
51- } catch ( ignore ) {
52- }
53-
54- } else {
55- content = data ;
56- }
57-
41+ let content = getData ( data ) ;
5842 resolve ( { task, content} ) ;
5943}
6044
6145function AFFailure ( resolve , reject , task : NSURLSessionDataTask , error : NSError ) {
6246 let data : NSDictionary < string , any > & NSData & NSArray < any > = error . userInfo . valueForKey ( AFNetworkingOperationFailingURLResponseDataErrorKey ) ;
63- let content : any ;
64- if ( data && data . class ) {
65- if ( data . enumerateKeysAndObjectsUsingBlock || ( < any > data ) instanceof NSArray ) {
66- let serial = NSJSONSerialization . dataWithJSONObjectOptionsError ( data , NSJSONWritingOptions . PrettyPrinted ) ;
67- content = NSString . alloc ( ) . initWithDataEncoding ( serial , NSUTF8StringEncoding ) . toString ( ) ;
68- } else if ( ( < any > data ) instanceof NSData ) {
69- content = NSString . alloc ( ) . initWithDataEncoding ( data , NSASCIIStringEncoding ) . toString ( ) ;
70- } else {
71- content = data ;
47+ let parsedData = getData ( data ) ;
48+ if ( useLegacy ) {
49+ let failure : any = {
50+ body : parsedData ,
51+ description : error . description ,
52+ reason : error . localizedDescription ,
53+ url : error . userInfo . objectForKey ( 'NSErrorFailingURLKey' ) . description
54+ } ;
55+ if ( policies . secured === true ) {
56+ failure . description = 'nativescript-https > Invalid SSL certificate! ' + error . description ;
57+ }
58+ let reason = error . localizedDescription ;
59+ let content = parsedData ;
60+ resolve ( { task : task , content : content , reason : reason , failure : failure } ) ;
61+ } else {
62+ let content : any = {
63+ body : parsedData ,
64+ description : error . description ,
65+ reason : error . localizedDescription ,
66+ url : error . userInfo . objectForKey ( 'NSErrorFailingURLKey' ) . description
67+ } ;
68+
69+ if ( policies . secured === true ) {
70+ content . description = 'nativescript-https > Invalid SSL certificate! ' + content . description ;
7271 }
7372
74- try {
75- content = JSON . parse ( content ) ;
76- } catch ( e ) {
77- }
78- } else {
79- content = data ;
80- }
81- /*let body = NSString.alloc().initWithDataEncoding(data, NSUTF8StringEncoding).toString();
82- try {
83- body = JSON.parse(body);
84- } catch (e) {
85- }
86- let content: any = {
87- body,
88- description: error.description,
89- reason: error.localizedDescription,
90- url: error.userInfo.objectForKey('NSErrorFailingURLKey').description
91- };
92- */
93- let failure : any = {
94- body : content ,
95- description : error . description ,
96- reason : error . localizedDescription ,
97- url : error . userInfo . objectForKey ( 'NSErrorFailingURLKey' ) . description
98- } ;
99- if ( policies . secured === true ) {
100- failure . description = 'nativescript-https > Invalid SSL certificate! ' + error . description ;
73+ let reason = error . localizedDescription ;
74+ resolve ( { task, content, reason} ) ;
10175 }
102- let reason = error . localizedDescription ;
103- resolve ( { task, content, reason, failure : failure } ) ;
10476}
10577
10678export function request ( opts : Https . HttpsRequestOptions ) : Promise < Https . HttpsResponse > {
@@ -180,3 +152,25 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
180152 return Promise . resolve ( sendi ) ;
181153 } ) ;
182154}
155+
156+ function getData ( data ) {
157+ let content : any ;
158+ if ( data && data . class ) {
159+ if ( data . enumerateKeysAndObjectsUsingBlock || ( < any > data ) instanceof NSArray ) {
160+ let serial = NSJSONSerialization . dataWithJSONObjectOptionsError ( data , NSJSONWritingOptions . PrettyPrinted ) ;
161+ content = NSString . alloc ( ) . initWithDataEncoding ( serial , NSUTF8StringEncoding ) . toString ( ) ;
162+ } else if ( ( < any > data ) instanceof NSData ) {
163+ content = NSString . alloc ( ) . initWithDataEncoding ( data , NSASCIIStringEncoding ) . toString ( ) ;
164+ } else {
165+ content = data ;
166+ }
167+
168+ try {
169+ content = JSON . parse ( content ) ;
170+ } catch ( e ) {
171+ }
172+ } else {
173+ content = data ;
174+ }
175+ return content ;
176+ }
0 commit comments