From 0c0d228815251cfaf66ba25a276852d32192f106 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Wed, 14 Oct 2020 16:54:42 +0100 Subject: [PATCH] Fixed networking --- .../android/app/src/main/AndroidManifest.xml | 1 + .../src/main/res/xml/network_security_config.xml | 9 +++++++++ ReactNativeClient/lib/WebDavApi.js | 13 ++++++++++--- ReactNativeClient/lib/shim-init-react.js | 7 +++++++ 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 ReactNativeClient/android/app/src/main/res/xml/network_security_config.xml diff --git a/ReactNativeClient/android/app/src/main/AndroidManifest.xml b/ReactNativeClient/android/app/src/main/AndroidManifest.xml index fccb57ef13..eb803a6d2e 100644 --- a/ReactNativeClient/android/app/src/main/AndroidManifest.xml +++ b/ReactNativeClient/android/app/src/main/AndroidManifest.xml @@ -17,6 +17,7 @@ android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" android:allowBackup="false" + android:networkSecurityConfig="@xml/network_security_config" android:theme="@style/AppTheme"> diff --git a/ReactNativeClient/android/app/src/main/res/xml/network_security_config.xml b/ReactNativeClient/android/app/src/main/res/xml/network_security_config.xml new file mode 100644 index 0000000000..fc0f96a715 --- /dev/null +++ b/ReactNativeClient/android/app/src/main/res/xml/network_security_config.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/ReactNativeClient/lib/WebDavApi.js b/ReactNativeClient/lib/WebDavApi.js index 18c7141c4d..6be99b9172 100644 --- a/ReactNativeClient/lib/WebDavApi.js +++ b/ReactNativeClient/lib/WebDavApi.js @@ -335,14 +335,22 @@ class WebDavApi { // ' async exec(method, path = '', body = null, headers = null, options = null) { - if (headers === null) headers = {}; - if (options === null) options = {}; + headers = Object.assign({}, headers); + options = Object.assign({}, options); + if (!options.responseFormat) options.responseFormat = 'json'; if (!options.target) options.target = 'string'; const authToken = this.authToken(); if (authToken) headers['Authorization'] = `Basic ${authToken}`; + + // That should not be needed, but it is required for React Native 0.63+ + // https://github.com/facebook/react-native/issues/30176 + if (!headers['Content-Type']) { + if (method === 'PROPFIND') headers['Content-Type'] = 'text/xml'; + if (method === 'PUT') headers['Content-Type'] = 'text/plain'; + } // On iOS, the network lib appends a If-None-Match header to PROPFIND calls, which is kind of correct because // the call is idempotent and thus could be cached. According to RFC-7232 though only GET and HEAD should have @@ -365,7 +373,6 @@ class WebDavApi { if (shim.httpAgent(url)) fetchOptions.agent = shim.httpAgent(url); - let response = null; // console.info('WebDAV Call', `${method} ${url}`, headers, options); diff --git a/ReactNativeClient/lib/shim-init-react.js b/ReactNativeClient/lib/shim-init-react.js index 1e6eed54ad..7fdb9806ed 100644 --- a/ReactNativeClient/lib/shim-init-react.js +++ b/ReactNativeClient/lib/shim-init-react.js @@ -44,6 +44,13 @@ function shimInit() { if (!validatedUrl) throw new Error(`Not a valid URL: ${url}`); return shim.fetchWithRetry(() => { + // If the request has a body and it's not a GET call, and it doesn't have a Content-Type header + // we display a warning, because it could trigger a "Network request failed" error. + // https://github.com/facebook/react-native/issues/30176 + if (options?.body && options?.method && options.method !== 'GET' && !options?.headers?.['Content-Type']) { + console.warn('Done a non-GET fetch call without a Content-Type header. It may make the request fail.', url, options); + } + return fetch(validatedUrl, options); }, options); };