Skip to content

Commit ea463e3

Browse files
Merge pull request #60 from kefahB/master
Malformed data due to data.classe() return undefined #57 and fix Content-Type issue with utf-8
2 parents 3978913 + bdffe11 commit ea463e3

File tree

7 files changed

+42
-13
lines changed

7 files changed

+42
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.vscode
22
.idea
33
.DS_Store
4+
*.history
45
*.esm.json
56
*.js
67
*.js.map

demo/app/assets/httpbin.org.cer

1 Byte
Binary file not shown.

demo/app/main-page.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ export function postHttpbin() {
4242
postRequest('https://httpbin.org/post', {"foo": "bar", "baz": undefined, "plaz": null});
4343
}
4444

45+
export function postHttpbinWithUTF8() {
46+
Https.request(
47+
{
48+
url: 'https://httpbin.org/post',
49+
method: 'POST',
50+
body: {"foo": "bar", "baz": undefined, "plaz": null},
51+
headers: {
52+
'Content-Type': "application/json; charset=utf-8"
53+
}
54+
})
55+
.then(response => console.log('Https.request response', response))
56+
.catch(error => {
57+
console.error('Https.request error', error);
58+
dialogs.alert(error);
59+
});
60+
}
61+
4562
export function getHttpbin() {
4663
getRequest('https://httpbin.org/get');
4764
}

demo/app/main-page.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<Button text="GET Httpbin" tap="getHttpbin" class="t-20 btn btn-primary btn-active"/>
1010
<Button text="GET Httpbin (large response)" tap="getHttpbinLargeResponse" class="t-20 btn btn-primary btn-active"/>
1111
<Button text="POST Httpbin " tap="postHttpbin" class="t-20 btn btn-primary btn-active"/>
12+
<Button text="POST Httpbin UTF-8" tap="postHttpbinWithUTF8" class="t-20 btn btn-primary btn-active"/>
1213
<Button text="Httpbin Pinning ON" tap="enableSSLPinning" class="t-20 btn btn-primary btn-active"/>
1314
<Button text="Httpbin Pinning ON, expired cert" tap="enableSSLPinningExpired" class="t-20 btn btn-primary btn-active"/>
1415
<Button text="Httpbin Pinning OFF" tap="disableSSLPinning" class="t-20 btn btn-primary btn-active"/>

src/https.android.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,18 @@ function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHtt
128128
verify: (hostname: string, session: javax.net.ssl.SSLSession): boolean => {
129129
let pp = session.getPeerPrincipal().getName();
130130
let hv = javax.net.ssl.HttpsURLConnection.getDefaultHostnameVerifier();
131-
return (
132-
hv.verify(peer.host, session) &&
133-
peer.host === hostname &&
134-
peer.host === session.getPeerHost() &&
135-
pp.indexOf(peer.commonName) !== -1
136-
);
131+
if (peer.commonName && peer.commonName[0] === "*") {
132+
return (hv.verify(peer.host, session) &&
133+
hostname.indexOf(peer.host) > -1 &&
134+
hostname.indexOf(session.getPeerHost()) > -1 &&
135+
pp.indexOf(peer.commonName) !== -1);
136+
}
137+
else {
138+
return (hv.verify(peer.host, session) &&
139+
peer.host === hostname &&
140+
peer.host === session.getPeerHost() &&
141+
pp.indexOf(peer.host) !== -1);
142+
}
137143
},
138144
}));
139145
} catch (error) {

src/https.ios.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
4444
let content: any;
4545
if (data && data.class) {
4646
// console.log('data.class().name', data.class().name)
47-
if (data.enumerateKeysAndObjectsUsingBlock || data.class().name === 'NSArray') {
47+
if (data.enumerateKeysAndObjectsUsingBlock || (<any>data) instanceof NSArray) {
4848
// content = {}
4949
// data.enumerateKeysAndObjectsUsingBlock(function(k, v) {
5050
// console.log('v.description', v.description)
@@ -53,7 +53,7 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
5353
let serial = NSJSONSerialization.dataWithJSONObjectOptionsError(data, NSJSONWritingOptions.PrettyPrinted);
5454
content = NSString.alloc().initWithDataEncoding(serial, NSUTF8StringEncoding).toString();
5555
// console.log('content', content)
56-
} else if (data.class().name === 'NSData') {
56+
} else if ((<any>data) instanceof NSData) {
5757
content = NSString.alloc().initWithDataEncoding(data, NSASCIIStringEncoding).toString();
5858
// } else if (data.class().name == 'NSArray') {
5959
// content = []
@@ -136,12 +136,10 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError)
136136
export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsResponse> {
137137
return new Promise((resolve, reject) => {
138138
try {
139-
140139
const manager = AFHTTPSessionManager.alloc().initWithBaseURL(NSURL.URLWithString(opts.url));
141-
142-
if (opts.headers && opts.headers['Content-Type'] === 'application/json') {
143-
manager.requestSerializer = AFJSONRequestSerializer.serializer();
144-
manager.responseSerializer = AFJSONResponseSerializer.serializerWithReadingOptions(NSJSONReadingOptions.AllowFragments);
140+
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);
145143
} else {
146144
manager.requestSerializer = AFHTTPRequestSerializer.serializer();
147145
// manager.responseSerializer = AFXMLParserResponseSerializer.serializer()

src/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@
4343
"name": "Robert Laverty",
4444
"email": "[email protected]",
4545
"url": "https://github.com/roblav96"
46+
},
47+
{
48+
"name": "Kefah BADER ALDIN",
49+
"email": "[email protected]",
50+
"url": "https://github.com/kefahB"
4651
}
52+
4753
],
4854
"repository": "github:gethuman/nativescript-https",
4955
"homepage": "https://github.com/gethuman/nativescript-https",

0 commit comments

Comments
 (0)