@@ -16,16 +16,11 @@ policies.def.allowInvalidCertificates = true;
1616policies . def . validatesDomainName = false ;
1717
1818export function enableSSLPinning ( options : Https . HttpsSSLPinningOptions ) {
19- // console.log('options', options)
2019 if ( ! policies . secure ) {
2120 policies . secure = AFSecurityPolicy . policyWithPinningMode ( AFSSLPinningMode . PublicKey ) ;
2221 policies . secure . allowInvalidCertificates = ( isDefined ( options . allowInvalidCertificates ) ) ? options . allowInvalidCertificates : false ;
2322 policies . secure . validatesDomainName = ( isDefined ( options . validatesDomainName ) ) ? options . validatesDomainName : true ;
2423 let data = NSData . dataWithContentsOfFile ( options . certificate ) ;
25- // console.log('data.description', data.description)
26- // console.log('data.bytes', data.bytes)
27- // console.log('data.base64Encoding()', data.base64Encoding())
28- // console.log('data.length', data.length)
2924 policies . secure . pinnedCertificates = NSSet . setWithObject ( data ) ;
3025 }
3126 policies . secured = true ;
@@ -40,44 +35,20 @@ export function disableSSLPinning() {
4035console . info ( 'nativescript-https > Disabled SSL pinning by default' ) ;
4136
4237function AFSuccess ( resolve , task : NSURLSessionDataTask , data : NSDictionary < string , any > & NSData & NSArray < any > ) {
43- // console.log('AFSuccess')
4438 let content : any ;
4539 if ( data && data . class ) {
46- // console.log('data.class().name', data.class().name)
4740 if ( data . enumerateKeysAndObjectsUsingBlock || ( < any > data ) instanceof NSArray ) {
48- // content = {}
49- // data.enumerateKeysAndObjectsUsingBlock(function(k, v) {
50- // console.log('v.description', v.description)
51- // content[k] = v
52- // })
5341 let serial = NSJSONSerialization . dataWithJSONObjectOptionsError ( data , NSJSONWritingOptions . PrettyPrinted ) ;
5442 content = NSString . alloc ( ) . initWithDataEncoding ( serial , NSUTF8StringEncoding ) . toString ( ) ;
55- // console.log('content', content)
5643 } else if ( ( < any > data ) instanceof NSData ) {
5744 content = NSString . alloc ( ) . initWithDataEncoding ( data , NSASCIIStringEncoding ) . toString ( ) ;
58- // } else if (data.class().name == 'NSArray') {
59- // content = []
60- // let i: number, len: number = data.count
61- // for (i = 0; i < len; i++) {
62- // let item
63- // let result: NSDictionary<string, any> = data[i]
64- // if (result.enumerateKeysAndObjectsUsingBlock) {
65- // item = {}
66- // result.enumerateKeysAndObjectsUsingBlock(function(k, v) {
67- // item[k] = v
68- // })
69- // } else {
70- // item = data[i]
71- // }
72- // content.push(item)
73- // }
7445 } else {
7546 content = data ;
7647 }
7748
7849 try {
7950 content = JSON . parse ( content ) ;
80- } catch ( e ) {
51+ } catch ( ignore ) {
8152 }
8253
8354 } else {
@@ -88,47 +59,25 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
8859}
8960
9061function AFFailure ( resolve , reject , task : NSURLSessionDataTask , error : NSError ) {
91- // console.error('AFFailure')
92- // console.log('error.description', error.description)
93- // console.log('error.userInfo.description', error.userInfo.description)
94- // console.log('error.localizedDescription', error.localizedDescription)
9562 let data : NSData = error . userInfo . valueForKey ( AFNetworkingOperationFailingURLResponseDataErrorKey ) ;
9663 let body = NSString . alloc ( ) . initWithDataEncoding ( data , NSUTF8StringEncoding ) . toString ( ) ;
64+
9765 try {
9866 body = JSON . parse ( body ) ;
99- } catch ( e ) {
67+ } catch ( ignore ) {
10068 }
69+
10170 let content : any = {
10271 body,
10372 description : error . description ,
10473 reason : error . localizedDescription ,
10574 url : error . userInfo . objectForKey ( 'NSErrorFailingURLKey' ) . description
10675 } ;
107- // console.log('content.url', content.url)
108- // try {
109- // content.body = JSON.parse(body.description)
110- // } catch (e) {
76+
11177 if ( policies . secured === true ) {
112- // console.log('error.description', error.description)
113- // console.log('error.userInfo.description', error.userInfo.description)
11478 content . description = 'nativescript-https > Invalid SSL certificate! ' + content . description ;
115- // return reject(content)
11679 }
117- // }
118- // console.log('error.description', error.description)
119- // console.keys('error', error)
120- // console.keys('error.userInfo', error.userInfo)
121- // error.userInfo.enumerateKeysAndObjectsUsingBlock(function(k, v) {
122- // console.log('k', k)
123- // console.log('v.description', v.description)
124- // })
125- // let keys = error.userInfo.allKeysForObject(error.userInfo)
126- // console.log('keys.description', keys.description)
127- // let url = error.valueForKey('URL')
128- // console.error('url', url)
129- // if (!isNullOrUndefined(task.response)) {
130- // content.URL = task.response.URL
131- // }
80+
13281 let reason = error . localizedDescription ;
13382 resolve ( { task, content, reason} ) ;
13483}
@@ -138,14 +87,11 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
13887 try {
13988 const manager = AFHTTPSessionManager . alloc ( ) . initWithBaseURL ( NSURL . URLWithString ( opts . url ) ) ;
14089 if ( opts . headers && ( < any > opts . headers [ 'Content-Type' ] ) . substring ( 0 , 16 ) === 'application/json' ) {
141- manager . requestSerializer = AFJSONRequestSerializer . serializer ( ) ;
142- manager . responseSerializer = AFJSONResponseSerializer . serializerWithReadingOptions ( NSJSONReadingOptions . AllowFragments ) ;
90+ manager . requestSerializer = AFJSONRequestSerializer . serializer ( ) ;
91+ manager . responseSerializer = AFJSONResponseSerializer . serializerWithReadingOptions ( NSJSONReadingOptions . AllowFragments ) ;
14392 } else {
14493 manager . requestSerializer = AFHTTPRequestSerializer . serializer ( ) ;
145- // manager.responseSerializer = AFXMLParserResponseSerializer.serializer()
14694 manager . responseSerializer = AFHTTPResponseSerializer . serializer ( ) ;
147- // manager.responseSerializer.acceptableContentTypes = NSSet.setWithObject('text/html')
148- // manager.responseSerializer.acceptableContentTypes = NSSet.setWithObject('application/json')
14995 }
15096 manager . requestSerializer . allowsCellularAccess = true ;
15197 manager . securityPolicy = ( policies . secured === true ) ? policies . secure : policies . def ;
@@ -185,20 +131,6 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
185131 AFFailure ( resolve , reject , task , error ) ;
186132 } ) ;
187133
188- // if (opts.method == 'GET') {
189- // manager.GETParametersSuccessFailure(opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
190- // AFSuccess(resolve, task, data)
191- // }, function failure(task, error) {
192- // AFFailure(resolve, reject, task, error)
193- // })
194- // } else if (opts.method == 'POST') {
195- // manager.POSTParametersSuccessFailure(opts.url, dict, function success(task: NSURLSessionDataTask, data: any) {
196- // AFSuccess(resolve, task, data)
197- // }, function failure(task, error) {
198- // AFFailure(resolve, reject, task, error)
199- // })
200- // }
201-
202134 } catch ( error ) {
203135 reject ( error ) ;
204136 }
@@ -208,7 +140,6 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
208140 content : any
209141 reason ?: string
210142 } ) => {
211-
212143 let sendi : Https . HttpsResponse = {
213144 content : AFResponse . content ,
214145 headers : { } ,
@@ -224,23 +155,7 @@ export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsRes
224155 if ( AFResponse . reason ) {
225156 sendi . reason = AFResponse . reason ;
226157 }
227- return Promise . resolve ( sendi ) ;
228158
159+ return Promise . resolve ( sendi ) ;
229160 } ) ;
230-
231- // {
232- // "content": {
233- // "code": "PreconditionFailed",
234- // "message": "!x-uuid"
235- // },
236- // "statusCode": 412,
237- // "headers": {
238- // "Content-Length": "49",
239- // "Server": "nginx/1.10.1",
240- // "Content-Type": "application/json",
241- // "Connection": "keep-alive",
242- // "Date": "Mon, 26 Dec 2016 03:31:42 GMT"
243- // }
244- // }
245-
246161}
0 commit comments