22
33import * as application from 'application'
44import { HttpRequestOptions , Headers , HttpResponse } from 'http'
5- import { isDefined , isNullOrUndefined } from 'utils/types'
5+ import { isDefined , isNullOrUndefined , isObject } from 'utils/types'
66import * as Https from './https.common'
77
88
@@ -45,60 +45,136 @@ console.info('nativescript-https > Disabled SSL pinning by default')
4545
4646
4747
48- function AFSuccess ( resolve , task : NSURLSessionDataTask , dict : NSDictionary < string , any > ) {
49- let content = { }
50- dict . enumerateKeysAndObjectsUsingBlock ( function ( k , v ) {
51- content [ k ] = v
52- } )
48+ function AFSuccess ( resolve , task : NSURLSessionDataTask , data : NSDictionary < string , any > & NSData & NSArray < any > ) {
49+ // console.log('AFSuccess')
50+ let content : any
51+ if ( data && data . class ) {
52+ // console.log('data.class().name', data.class().name)
53+ if ( data . enumerateKeysAndObjectsUsingBlock || data . class ( ) . name == 'NSArray' ) {
54+ // content = {}
55+ // data.enumerateKeysAndObjectsUsingBlock(function(k, v) {
56+ // console.log('v.description', v.description)
57+ // content[k] = v
58+ // })
59+ let serial = NSJSONSerialization . dataWithJSONObjectOptionsError ( data , NSJSONWritingOptions . PrettyPrinted )
60+ let results = NSString . alloc ( ) . initWithDataEncoding ( serial , NSUTF8StringEncoding )
61+ content = JSON . parse ( results . toString ( ) )
62+ } else if ( data . class ( ) . name == 'NSData' ) {
63+ content = NSString . alloc ( ) . initWithDataEncoding ( data , NSASCIIStringEncoding ) . toString ( )
64+ // } else if (data.class().name == 'NSArray') {
65+ // content = []
66+ // let i: number, len: number = data.count
67+ // for (i = 0; i < len; i++) {
68+ // let item
69+ // let result: NSDictionary<string, any> = data[i]
70+ // if (result.enumerateKeysAndObjectsUsingBlock) {
71+ // item = {}
72+ // result.enumerateKeysAndObjectsUsingBlock(function(k, v) {
73+ // item[k] = v
74+ // })
75+ // } else {
76+ // item = data[i]
77+ // }
78+ // content.push(item)
79+ // }
80+ } else {
81+ content = data
82+ }
83+ } else {
84+ content = data
85+ }
5386 resolve ( { task, content } )
5487}
5588
5689function AFFailure ( resolve , reject , task : NSURLSessionDataTask , error : NSError ) {
90+ // console.error('AFFailure')
91+ // console.log('error.description', error.description)
92+ // console.log('error.userInfo.description', error.userInfo.description)
93+ // console.log('error.localizedDescription', error.localizedDescription)
5794 let data : NSData = error . userInfo . valueForKey ( AFNetworkingOperationFailingURLResponseDataErrorKey )
5895 let body = NSString . alloc ( ) . initWithDataEncoding ( data , NSUTF8StringEncoding )
59- let content = { }
60- try {
61- content = JSON . parse ( body . description )
62- } catch ( e ) {
63- content = error . description
64- if ( policies . secured == true ) {
65- // console.log('error.description', error.description)
66- // console.log('error.userInfo.description', error.userInfo.description)
67- content = 'nativescript-https > Invalid SSL certificate! ' + content
68- return reject ( content )
69- }
96+ let content : any = {
97+ body : body . description ,
98+ description : error . description ,
99+ reason : error . localizedDescription ,
100+ url : error . userInfo . objectForKey ( 'NSErrorFailingURLKey' ) . description
70101 }
71- resolve ( { task, content } )
102+ // console.log('content.url', content.url)
103+ // try {
104+ // content.body = JSON.parse(body.description)
105+ // } catch (e) {
106+ if ( policies . secured == true ) {
107+ // console.log('error.description', error.description)
108+ // console.log('error.userInfo.description', error.userInfo.description)
109+ content . description = 'nativescript-https > Invalid SSL certificate! ' + content . description
110+ // return reject(content)
111+ }
112+ // }
113+ // console.log('error.description', error.description)
114+ // console.keys('error', error)
115+ // console.keys('error.userInfo', error.userInfo)
116+ // error.userInfo.enumerateKeysAndObjectsUsingBlock(function(k, v) {
117+ // console.log('k', k)
118+ // console.log('v.description', v.description)
119+ // })
120+ // let keys = error.userInfo.allKeysForObject(error.userInfo)
121+ // console.log('keys.description', keys.description)
122+ // let url = error.valueForKey('URL')
123+ // console.error('url', url)
124+ // if (!isNullOrUndefined(task.response)) {
125+ // content.URL = task.response.URL
126+ // }
127+ let reason = error . localizedDescription
128+ resolve ( { task, content, reason } )
72129}
73130
74- export function request ( opts : Https . HttpsRequestOptions ) : Promise < HttpResponse > {
131+ export function request ( opts : Https . HttpsRequestOptions ) : Promise < Https . HttpsResponse > {
75132 return new Promise ( function ( resolve , reject ) {
76133 try {
77134
78135 let manager = AFHTTPSessionManager . manager ( )
79- manager . requestSerializer = AFJSONRequestSerializer . serializer ( )
136+
137+ if ( opts . headers [ 'Content-Type' ] == 'application/json' ) {
138+ manager . requestSerializer = AFJSONRequestSerializer . serializer ( )
139+ manager . responseSerializer = AFJSONResponseSerializer . serializerWithReadingOptions ( NSJSONReadingOptions . AllowFragments )
140+ } else {
141+ manager . requestSerializer = AFHTTPRequestSerializer . serializer ( )
142+ // manager.responseSerializer = AFXMLParserResponseSerializer.serializer()
143+ manager . responseSerializer = AFHTTPResponseSerializer . serializer ( )
144+ // manager.responseSerializer.acceptableContentTypes = NSSet.setWithObject('text/html')
145+ // manager.responseSerializer.acceptableContentTypes = NSSet.setWithObject('application/json')
146+ }
80147 manager . requestSerializer . allowsCellularAccess = true
81148 manager . securityPolicy = ( policies . secured == true ) ? policies . secure : policies . def
149+ manager . requestSerializer . timeoutInterval = 10
82150
83151 let heads = opts . headers
84- Object . keys ( heads ) . forEach ( function ( key ) {
85- manager . requestSerializer . setValueForHTTPHeaderField ( heads [ key ] as any , key )
86- } )
152+ if ( heads ) {
153+ Object . keys ( heads ) . forEach ( function ( key ) {
154+ manager . requestSerializer . setValueForHTTPHeaderField ( heads [ key ] as any , key )
155+ } )
156+ }
157+
158+ let dict : NSMutableDictionary < string , any > = null
159+ if ( opts . content ) {
160+ let cont = JSON . parse ( opts . content as any )
161+ if ( isObject ( cont ) ) {
162+ dict = NSMutableDictionary . new < string , any > ( )
163+ Object . keys ( cont ) . forEach ( function ( key ) {
164+ dict . setValueForKey ( cont [ key ] as any , key )
165+ } )
166+ }
167+ }
87168
88169 if ( opts . method == 'GET' ) {
89- manager . GETParametersSuccessFailure ( opts . url , null , function success ( task : NSURLSessionDataTask , dict : NSDictionary < string , any > ) {
90- AFSuccess ( resolve , task , dict )
170+ manager . GETParametersSuccessFailure ( opts . url , dict , function success ( task : NSURLSessionDataTask , data : any ) {
171+ AFSuccess ( resolve , task , data )
91172 } , function failure ( task , error ) {
92173 AFFailure ( resolve , reject , task , error )
93174 } )
94175 } else if ( opts . method == 'POST' ) {
95- let cont = JSON . parse ( opts . content as any )
96- let dict = NSMutableDictionary . new ( )
97- Object . keys ( cont ) . forEach ( function ( key ) {
98- dict . setValueForKey ( cont [ key ] as any , key )
99- } )
100- manager . POSTParametersSuccessFailure ( opts . url , dict , function success ( task : NSURLSessionDataTask , dict : NSDictionary < string , any > ) {
101- AFSuccess ( resolve , task , dict )
176+ manager . POSTParametersSuccessFailure ( opts . url , dict , function success ( task : NSURLSessionDataTask , data : any ) {
177+ AFSuccess ( resolve , task , data )
102178 } , function failure ( task , error ) {
103179 AFFailure ( resolve , reject , task , error )
104180 } )
@@ -111,21 +187,27 @@ export function request(opts: Https.HttpsRequestOptions): Promise<HttpResponse>
111187 } ) . then ( function ( AFResponse : {
112188 task : NSURLSessionDataTask
113189 content : any
190+ reason ?: string
114191 } ) {
115192
193+ let sendi : Https . HttpsResponse = {
194+ content : AFResponse . content ,
195+ headers : { } ,
196+ }
197+
116198 let response = AFResponse . task . response as NSHTTPURLResponse
117- if ( isNullOrUndefined ( response ) ) {
118- return Promise . reject ( AFResponse . content )
199+ if ( ! isNullOrUndefined ( response ) ) {
200+ sendi . statusCode = response . statusCode
201+ let dict = response . allHeaderFields
202+ dict . enumerateKeysAndObjectsUsingBlock ( function ( k , v ) {
203+ sendi . headers [ k ] = v
204+ } )
205+ }
206+
207+ if ( AFResponse . reason ) {
208+ sendi . reason = AFResponse . reason
119209 }
120- let content = AFResponse . content
121- let statusCode = response . statusCode
122- let headers = { }
123- let dict = response . allHeaderFields
124- dict . enumerateKeysAndObjectsUsingBlock ( function ( k , v ) {
125- headers [ k ] = v
126- } )
127-
128- return Promise . resolve ( { content, statusCode, headers } )
210+ return Promise . resolve ( sendi )
129211
130212 } )
131213
0 commit comments