From 19aeb27f469e989c8d71b48aa32139c8faa66cd2 Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Sat, 17 Sep 2022 10:25:18 +0200 Subject: [PATCH 1/2] feat: added `cookiesEnabled` property (`true` by default) --- src/request.android.ts | 56 ++++++++++++++++++++++++++++++------------ src/request.d.ts | 5 ++++ src/request.ios.ts | 2 +- 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/request.android.ts b/src/request.android.ts index 7c9cd63..f91f12e 100644 --- a/src/request.android.ts +++ b/src/request.android.ts @@ -39,7 +39,10 @@ export function clearCache() { } } +// TODO: rewrite this to not have to handle +// every single property let _timeout = 10; +let _cookiesEnabled = true; class HttpsResponseLegacy implements IHttpsResponseLegacy { private callback?: com.nativescript.https.OkHttpResponse.OkHttpResponseAsyncCallback; @@ -229,7 +232,7 @@ export function disableSSLPinning() { let Client: okhttp3.OkHttpClient; let cookieJar: com.nativescript.https.QuotePreservingCookieJar; let cookieManager: java.net.CookieManager; -function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHttpClient { +function getClient(reload: boolean = false, opts: Partial = {}): okhttp3.OkHttpClient { if (!Client) { // ssl error fix on KitKat. Only need to be done once. // client will be null only onced so will run only once @@ -245,24 +248,48 @@ function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHtt // Client.connectionPool().evictAll() // Client = null // } + const timeout = opts.timeout ?? 10; + const cookiesEnabled = opts.cookiesEnabled ?? true; if (Client && reload === false) { - if (timeout === _timeout) { + const needTimeoutChange = timeout === _timeout; + const needCookiesChange = cookiesEnabled === _cookiesEnabled; + if (!needTimeoutChange && !needCookiesChange) { return Client; } else { - return Client.newBuilder() - .connectTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS) - .writeTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS) - .readTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS) - .build(); + const builder = Client.newBuilder(); + if (needTimeoutChange) { + builder + .connectTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(timeout, java.util.concurrent.TimeUnit.SECONDS); + } + if (needCookiesChange) { + if (cookiesEnabled) { + if (!cookieJar) { + cookieManager = new java.net.CookieManager(); + cookieManager.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL); + cookieJar = new com.nativescript.https.QuotePreservingCookieJar(cookieManager); + } + builder.cookieJar(cookieJar); + } else { + builder.cookieJar(null); + } + } + return builder.build(); } } - if (!cookieJar) { - cookieManager = new java.net.CookieManager(); - cookieManager.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL); - cookieJar = new com.nativescript.https.QuotePreservingCookieJar(cookieManager); + const builder = new okhttp3.OkHttpClient.Builder(); + + _cookiesEnabled = cookiesEnabled; + if (cookiesEnabled) { + if (!cookieJar) { + cookieManager = new java.net.CookieManager(); + cookieManager.setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL); + cookieJar = new com.nativescript.https.QuotePreservingCookieJar(cookieManager); + } + builder.cookieJar(cookieJar); } - const builder = new okhttp3.OkHttpClient.Builder(); interceptors.forEach((interceptor) => builder.addInterceptor(interceptor)); networkInterceptors.forEach((interceptor) => builder.addNetworkInterceptor(interceptor)); if (peer.enabled === true) { @@ -333,9 +360,6 @@ function getClient(reload: boolean = false, timeout: number = 10): okhttp3.OkHtt builder.addInterceptor(com.nativescript.https.CacheInterceptor.INTERCEPTOR); } } - if (cookieJar) { - builder.cookieJar(cookieJar); - } Client = builder.build(); return Client; @@ -385,7 +409,7 @@ const runningClients: { [k: string]: okhttp3.OkHttpClient } = {}; let OkHttpResponse: typeof com.nativescript.https.OkHttpResponse; export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = true): HttpsRequest { - const client = getClient(false, opts.timeout); + const client = getClient(false, opts); const request = new okhttp3.Request.Builder(); request.url(opts.url); diff --git a/src/request.d.ts b/src/request.d.ts index 5af543b..a1ac570 100644 --- a/src/request.d.ts +++ b/src/request.d.ts @@ -57,6 +57,11 @@ export interface HttpsRequestOptions extends HttpRequestOptions { onProgress?: (current: number, total: number) => void; cachePolicy?: CachePolicy; + + /** + * default to true + */ + cookiesEnabled?: boolean; } export interface HttpsResponse { diff --git a/src/request.ios.ts b/src/request.ios.ts index 26b1f66..a23464c 100644 --- a/src/request.ios.ts +++ b/src/request.ios.ts @@ -319,7 +319,7 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr manager.responseSerializer = AFHTTPResponseSerializer.serializer(); } manager.requestSerializer.allowsCellularAccess = true; - manager.requestSerializer.HTTPShouldHandleCookies = true; + manager.requestSerializer.HTTPShouldHandleCookies = opts.cookiesEnabled !== false; manager.securityPolicy = policies.secured === true ? policies.secure : policies.def; if (opts.cachePolicy) { From dff1c3a2e89e5fcedb43f7b1166020722ce1d30e Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Sat, 17 Sep 2022 10:26:57 +0200 Subject: [PATCH 2/2] v4.0.7 --- CHANGELOG.md | 6 ++++++ lerna.json | 2 +- plugin/CHANGELOG.md | 4 ++++ plugin/package.json | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55edcdd..76735de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [4.0.7](https://github.com/nativescript-community/https/compare/v4.0.6...v4.0.7) (2022-09-17) + +### Features + +* added `cookiesEnabled` property (`true` by default) ([19aeb27](https://github.com/nativescript-community/https/commit/19aeb27f469e989c8d71b48aa32139c8faa66cd2)) + ## [4.0.6](https://github.com/nativescript-community/https/compare/v4.0.5...v4.0.6) (2022-05-18) ### Bug Fixes diff --git a/lerna.json b/lerna.json index 128df63..090ecd8 100644 --- a/lerna.json +++ b/lerna.json @@ -2,7 +2,7 @@ "packages": [ "plugin" ], - "version": "4.0.6", + "version": "4.0.7", "command": { "publish": { "conventionalCommits": true diff --git a/plugin/CHANGELOG.md b/plugin/CHANGELOG.md index 2d261f5..9ded29b 100644 --- a/plugin/CHANGELOG.md +++ b/plugin/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## [4.0.7](https://github.com/nativescript-community/https/compare/v4.0.6...v4.0.7) (2022-09-17) + +**Note:** Version bump only for package @nativescript-community/https + ## [4.0.6](https://github.com/nativescript-community/https/compare/v4.0.5...v4.0.6) (2022-05-18) **Note:** Version bump only for package @nativescript-community/https diff --git a/plugin/package.json b/plugin/package.json index 3c1073a..84d2983 100644 --- a/plugin/package.json +++ b/plugin/package.json @@ -1,6 +1,6 @@ { "name": "@nativescript-community/https", - "version": "4.0.6", + "version": "4.0.7", "description": "Nativescript plugin for gestures", "main": "index", "sideEffects": false,