Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.vscode
.idea
.DS_Store
*.history
*.esm.json
*.js
*.js.map
Expand Down
Binary file modified demo/app/assets/httpbin.org.cer
Binary file not shown.
17 changes: 17 additions & 0 deletions demo/app/main-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ export function postHttpbin() {
postRequest('https://httpbin.org/post', {"foo": "bar", "baz": undefined, "plaz": null});
}

export function postHttpbinWithUTF8() {
Https.request(
{
url: 'https://httpbin.org/post',
method: 'POST',
body: {"foo": "bar", "baz": undefined, "plaz": null},
headers: {
'Content-Type': "application/json; charset=utf-8"
}
})
.then(response => console.log('Https.request response', response))
.catch(error => {
console.error('Https.request error', error);
dialogs.alert(error);
});
}

export function getHttpbin() {
getRequest('https://httpbin.org/get');
}
Expand Down
1 change: 1 addition & 0 deletions demo/app/main-page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<Button text="GET Httpbin" tap="getHttpbin" class="t-20 btn btn-primary btn-active"/>
<Button text="GET Httpbin (large response)" tap="getHttpbinLargeResponse" class="t-20 btn btn-primary btn-active"/>
<Button text="POST Httpbin " tap="postHttpbin" class="t-20 btn btn-primary btn-active"/>
<Button text="POST Httpbin UTF-8" tap="postHttpbinWithUTF8" class="t-20 btn btn-primary btn-active"/>
<Button text="Httpbin Pinning ON" tap="enableSSLPinning" class="t-20 btn btn-primary btn-active"/>
<Button text="Httpbin Pinning ON, expired cert" tap="enableSSLPinningExpired" class="t-20 btn btn-primary btn-active"/>
<Button text="Httpbin Pinning OFF" tap="disableSSLPinning" class="t-20 btn btn-primary btn-active"/>
Expand Down
18 changes: 12 additions & 6 deletions src/https.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,18 @@ function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHtt
verify: (hostname: string, session: javax.net.ssl.SSLSession): boolean => {
let pp = session.getPeerPrincipal().getName();
let hv = javax.net.ssl.HttpsURLConnection.getDefaultHostnameVerifier();
return (
hv.verify(peer.host, session) &&
peer.host === hostname &&
peer.host === session.getPeerHost() &&
pp.indexOf(peer.commonName) !== -1
);
if (peer.commonName && peer.commonName[0] === "*") {
return (hv.verify(peer.host, session) &&
hostname.indexOf(peer.host) > -1 &&
hostname.indexOf(session.getPeerHost()) > -1 &&
pp.indexOf(peer.commonName) !== -1);
}
else {
return (hv.verify(peer.host, session) &&
peer.host === hostname &&
peer.host === session.getPeerHost() &&
pp.indexOf(peer.host) !== -1);
}
},
}));
} catch (error) {
Expand Down
12 changes: 5 additions & 7 deletions src/https.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
let content: any;
if (data && data.class) {
// console.log('data.class().name', data.class().name)
if (data.enumerateKeysAndObjectsUsingBlock || data.class().name === 'NSArray') {
if (data.enumerateKeysAndObjectsUsingBlock || (<any>data) instanceof NSArray) {
// content = {}
// data.enumerateKeysAndObjectsUsingBlock(function(k, v) {
// console.log('v.description', v.description)
Expand All @@ -53,7 +53,7 @@ function AFSuccess(resolve, task: NSURLSessionDataTask, data: NSDictionary<strin
let serial = NSJSONSerialization.dataWithJSONObjectOptionsError(data, NSJSONWritingOptions.PrettyPrinted);
content = NSString.alloc().initWithDataEncoding(serial, NSUTF8StringEncoding).toString();
// console.log('content', content)
} else if (data.class().name === 'NSData') {
} else if ((<any>data) instanceof NSData) {
content = NSString.alloc().initWithDataEncoding(data, NSASCIIStringEncoding).toString();
// } else if (data.class().name == 'NSArray') {
// content = []
Expand Down Expand Up @@ -136,12 +136,10 @@ function AFFailure(resolve, reject, task: NSURLSessionDataTask, error: NSError)
export function request(opts: Https.HttpsRequestOptions): Promise<Https.HttpsResponse> {
return new Promise((resolve, reject) => {
try {

const manager = AFHTTPSessionManager.alloc().initWithBaseURL(NSURL.URLWithString(opts.url));

if (opts.headers && opts.headers['Content-Type'] === 'application/json') {
manager.requestSerializer = AFJSONRequestSerializer.serializer();
manager.responseSerializer = AFJSONResponseSerializer.serializerWithReadingOptions(NSJSONReadingOptions.AllowFragments);
if (opts.headers && (<any>opts.headers['Content-Type']).substring(0, 16) === 'application/json') {
manager.requestSerializer = AFJSONRequestSerializer.serializer();
manager.responseSerializer = AFJSONResponseSerializer.serializerWithReadingOptions(NSJSONReadingOptions.AllowFragments);
} else {
manager.requestSerializer = AFHTTPRequestSerializer.serializer();
// manager.responseSerializer = AFXMLParserResponseSerializer.serializer()
Expand Down
6 changes: 6 additions & 0 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@
"name": "Robert Laverty",
"email": "roblav96@gmail.com",
"url": "https://github.com/roblav96"
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
"name": "Kefah BADER ALDIN",
"email": "kefah.bader@gmail.com",
"url": "https://github.com/kefahB"
}

],
"repository": "github:gethuman/nativescript-https",
"homepage": "https://github.com/gethuman/nativescript-https",
Expand Down