36
36
import androidx .media3 .common .util .Assertions ;
37
37
import androidx .media3 .common .util .Clock ;
38
38
import androidx .media3 .common .util .ConditionVariable ;
39
- import androidx .media3 .common .util .Log ;
40
39
import androidx .media3 .common .util .UnstableApi ;
41
40
import androidx .media3 .common .util .Util ;
42
41
import com .google .common .base .Ascii ;
43
42
import com .google .common .base .Predicate ;
44
- import com .google .common .collect .ImmutableMap ;
45
43
import com .google .common .net .HttpHeaders ;
46
44
import com .google .common .primitives .Longs ;
47
45
import com .google .errorprone .annotations .CanIgnoreReturnValue ;
48
46
import java .io .IOException ;
49
47
import java .io .InterruptedIOException ;
50
- import java .net .CookieHandler ;
51
- import java .net .CookieManager ;
52
48
import java .net .SocketTimeoutException ;
53
- import java .net .URI ;
54
49
import java .net .UnknownHostException ;
55
50
import java .nio .ByteBuffer ;
56
51
import java .util .Arrays ;
@@ -937,6 +932,14 @@ private static boolean isCompressed(UrlResponseInfo info) {
937
932
return false ;
938
933
}
939
934
935
+ @ Nullable
936
+ private static String parseCookies (@ Nullable List <String > setCookieHeaders ) {
937
+ if (setCookieHeaders == null || setCookieHeaders .isEmpty ()) {
938
+ return null ;
939
+ }
940
+ return TextUtils .join (";" , setCookieHeaders );
941
+ }
942
+
940
943
@ Nullable
941
944
private static String getFirstHeader (Map <String , List <String >> allHeaders , String headerName ) {
942
945
@ Nullable List <String > headers = allHeaders .get (headerName );
@@ -958,8 +961,7 @@ private static int copyByteBuffer(ByteBuffer src, ByteBuffer dst) {
958
961
* A wrapper class that manages a {@link UrlRequest} and the {@link UrlRequestCallback} associated
959
962
* with that request.
960
963
*/
961
- @ VisibleForTesting
962
- /* private */ static final class UrlRequestWrapper {
964
+ private static final class UrlRequestWrapper {
963
965
964
966
private final UrlRequest urlRequest ;
965
967
private final UrlRequestCallback urlRequestCallback ;
@@ -1002,21 +1004,14 @@ public void onStatus(int status) {
1002
1004
}
1003
1005
}
1004
1006
1005
- @ VisibleForTesting
1006
- /* private */ final class UrlRequestCallback implements UrlRequest .Callback {
1007
+ private final class UrlRequestCallback implements UrlRequest .Callback {
1007
1008
1008
- private static final String TAG = "HttpEngineDataSource" ;
1009
1009
private volatile boolean isClosed = false ;
1010
1010
1011
1011
public void close () {
1012
1012
this .isClosed = true ;
1013
1013
}
1014
1014
1015
- @ SuppressWarnings ("argument.type.incompatible" )
1016
- private void resetDefaultCookieHandler () {
1017
- CookieHandler .setDefault (null );
1018
- }
1019
-
1020
1015
@ Override
1021
1016
public synchronized void onRedirectReceived (
1022
1017
UrlRequest request , UrlResponseInfo info , String newLocationUrl ) {
@@ -1045,34 +1040,24 @@ public synchronized void onRedirectReceived(
1045
1040
resetConnectTimeout ();
1046
1041
}
1047
1042
1048
- boolean hasDefaultCookieHandler = CookieHandler .getDefault () != null ;
1049
- if (!hasDefaultCookieHandler && handleSetCookieRequests ) {
1050
- // a temporary CookieManager is created for the duration of this request - this guarantees
1051
- // redirects preserve the cookies correctly.
1052
- CookieManager cm = new CookieManager ();
1053
- CookieHandler .setDefault (cm );
1054
- }
1055
-
1056
- storeCookiesFromHeaders (info );
1057
- String cookieHeaders = getCookieHeader (info .getUrl (), info .getHeaders ().getAsMap ());
1058
-
1059
- if (!hasDefaultCookieHandler && handleSetCookieRequests ) {
1060
- resetDefaultCookieHandler ();
1061
- }
1062
-
1063
1043
boolean shouldKeepPost =
1064
1044
keepPostFor302Redirects
1065
1045
&& dataSpec .httpMethod == DataSpec .HTTP_METHOD_POST
1066
1046
&& responseCode == 302 ;
1067
1047
1068
1048
// request.followRedirect() transforms a POST request into a GET request, so if we want to
1069
1049
// keep it as a POST we need to fall through to the manual redirect logic below.
1070
- if (!shouldKeepPost ) {
1071
- // No cookies, or we're not handling them - so just follow the redirect.
1072
- if (!handleSetCookieRequests || TextUtils .isEmpty (cookieHeaders )) {
1073
- request .followRedirect ();
1074
- return ;
1075
- }
1050
+ if (!shouldKeepPost && !handleSetCookieRequests ) {
1051
+ request .followRedirect ();
1052
+ return ;
1053
+ }
1054
+
1055
+ @ Nullable
1056
+ String cookieHeadersValue =
1057
+ parseCookies (info .getHeaders ().getAsMap ().get (HttpHeaders .SET_COOKIE ));
1058
+ if (!shouldKeepPost && TextUtils .isEmpty (cookieHeadersValue )) {
1059
+ request .followRedirect ();
1060
+ return ;
1076
1061
}
1077
1062
1078
1063
request .cancel ();
@@ -1090,15 +1075,13 @@ public synchronized void onRedirectReceived(
1090
1075
} else {
1091
1076
redirectUrlDataSpec = dataSpec .withUri (Uri .parse (newLocationUrl ));
1092
1077
}
1093
-
1094
- if (!TextUtils .isEmpty (cookieHeaders )) {
1078
+ if (!TextUtils .isEmpty (cookieHeadersValue )) {
1095
1079
Map <String , String > requestHeaders = new HashMap <>();
1096
1080
requestHeaders .putAll (dataSpec .httpRequestHeaders );
1097
- requestHeaders .put (HttpHeaders .COOKIE , cookieHeaders );
1081
+ requestHeaders .put (HttpHeaders .COOKIE , cookieHeadersValue );
1098
1082
redirectUrlDataSpec =
1099
1083
redirectUrlDataSpec .buildUpon ().setHttpRequestHeaders (requestHeaders ).build ();
1100
1084
}
1101
-
1102
1085
UrlRequestWrapper redirectUrlRequestWrapper ;
1103
1086
try {
1104
1087
redirectUrlRequestWrapper = buildRequestWrapper (redirectUrlDataSpec );
@@ -1113,56 +1096,11 @@ public synchronized void onRedirectReceived(
1113
1096
currentUrlRequestWrapper .start ();
1114
1097
}
1115
1098
1116
- /**
1117
- * Stores the cookie headers from the response in the default {@link CookieHandler}.
1118
- *
1119
- * This is a no-op if the {@link CookieHandler} is not set .
1120
- */
1121
- static void storeCookiesFromHeaders (UrlResponseInfo info ) {
1122
- CookieHandler cookieHandler = CookieHandler .getDefault ();
1123
- if (cookieHandler != null ) {
1124
- try {
1125
- cookieHandler .put (new URI (info .getUrl ()), info .getHeaders ().getAsMap ());
1126
- } catch (Exception e ) {
1127
- Log .w (TAG , "Failed to store cookies in CookieHandler" , e );
1128
- }
1129
- }
1130
- }
1131
-
1132
- static String getCookieHeader (String url ) {
1133
- return getCookieHeader (url , ImmutableMap .of ());
1134
- }
1135
-
1136
- // getCookieHeader maps Set-Cookie2 (RFC 2965) to Cookie just like CookieManager does.
1137
- static String getCookieHeader (String url , Map <String , List <String >> headers ) {
1138
- CookieHandler cookieHandler = CookieHandler .getDefault ();
1139
- if (cookieHandler == null ) {
1140
- return "" ;
1141
- }
1142
-
1143
- Map <String , List <String >> cookieHeaders = ImmutableMap .of ();
1144
- try {
1145
- cookieHeaders = cookieHandler .get (new URI (url ), headers );
1146
- } catch (Exception e ) {
1147
- Log .w (TAG , "Failed to read cookies from CookieHandler" , e );
1148
- }
1149
-
1150
- StringBuilder cookies = new StringBuilder ();
1151
- if (cookieHeaders .containsKey (HttpHeaders .COOKIE )) {
1152
- List <String > cookiesList = cookieHeaders .get (HttpHeaders .COOKIE );
1153
- for (String cookie : cookiesList ) {
1154
- cookies .append (cookie ).append ("; " );
1155
- }
1156
- }
1157
- return cookies .toString ().stripTrailing ();
1158
- }
1159
-
1160
1099
@ Override
1161
1100
public synchronized void onResponseStarted (UrlRequest request , UrlResponseInfo info ) {
1162
1101
if (isClosed ) {
1163
1102
return ;
1164
1103
}
1165
- storeCookiesFromHeaders (info );
1166
1104
responseInfo = info ;
1167
1105
operation .open ();
1168
1106
}
0 commit comments