Skip to content

Commit 1df8bd4

Browse files
Vincent Cheungfacebook-github-bot
Vincent Cheung
authored andcommitted
Fix multiple set-cookie not aggregated correctly in response headers (#27066)
Summary: Multiple `set-cookie` headers should be aggregated as one `set-cookie` with values in a comma separated list. It is working as expected on iOS but not on Android. On Android, only the last one is preserved The problem arises because `NetworkingModule.translateHeaders()` uses `WritableNativeMap` as the translated headers but uses both native and non-native methods. The mixup causes out of sync data that both sets of methods do no agree. A simple fix is to use `Bundle` as the storage and only convert it to `WritableMap` at the end in one go Related issues: #26280, #21795, #23185 ## Changelog [Android] [Fixed] - Fix multiple headers of the same name (e.g. `set-cookie`) not aggregated correctly Pull Request resolved: #27066 Test Plan: A mock api, https://demo6524373.mockable.io/, will return 2 `set-cookie` as follows: ``` set-cookie: cookie1=value1 set-cookie: cookie2=value2 ``` Verify the following will print the `set-cookie` with a value `cookie1=value1, cookie2=value2` ```javascript fetch('https://demo6524373.mockable.io/') .then(response => { console.log(response.headers); }); ``` On iOS, `set-cookie` will have `cookie1=value1, cookie2=value2` while on Android it will have `cookie2=value2` (preserving only the last one) Differential Revision: D18298933 Pulled By: cpojer fbshipit-source-id: ce53cd41d7c6de0469700617900f30a7d0914c26
1 parent a3b0804 commit 1df8bd4

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.facebook.react.modules.network;
99

1010
import android.net.Uri;
11+
import android.os.Bundle;
1112
import android.util.Base64;
1213
import androidx.annotation.Nullable;
1314
import com.facebook.common.logging.FLog;
@@ -629,18 +630,18 @@ private synchronized void cancelAllRequests() {
629630
}
630631

631632
private static WritableMap translateHeaders(Headers headers) {
632-
WritableMap responseHeaders = Arguments.createMap();
633+
Bundle responseHeaders = new Bundle();
633634
for (int i = 0; i < headers.size(); i++) {
634635
String headerName = headers.name(i);
635636
// multiple values for the same header
636-
if (responseHeaders.hasKey(headerName)) {
637+
if (responseHeaders.containsKey(headerName)) {
637638
responseHeaders.putString(
638639
headerName, responseHeaders.getString(headerName) + ", " + headers.value(i));
639640
} else {
640641
responseHeaders.putString(headerName, headers.value(i));
641642
}
642643
}
643-
return responseHeaders;
644+
return Arguments.fromBundle(responseHeaders);
644645
}
645646

646647
@ReactMethod

0 commit comments

Comments
 (0)