16
16
import static org .asynchttpclient .providers .netty .commons .util .HttpUtils .HTTP ;
17
17
import static org .asynchttpclient .providers .netty .commons .util .HttpUtils .WEBSOCKET ;
18
18
import static org .asynchttpclient .util .AsyncHttpProviderUtils .followRedirect ;
19
- import static org .jboss .netty .handler .codec .http .HttpHeaders .Names .ACCEPT ;
20
- import static org .jboss .netty .handler .codec .http .HttpHeaders .Names .ACCEPT_CHARSET ;
21
- import static org .jboss .netty .handler .codec .http .HttpHeaders .Names .ACCEPT_ENCODING ;
22
- import static org .jboss .netty .handler .codec .http .HttpHeaders .Names .ACCEPT_LANGUAGE ;
23
- import static org .jboss .netty .handler .codec .http .HttpHeaders .Names .REFERER ;
24
- import static org .jboss .netty .handler .codec .http .HttpHeaders .Names .USER_AGENT ;
19
+ import static org .jboss .netty .handler .codec .http .HttpHeaders .Names .*;
25
20
import static org .jboss .netty .handler .codec .http .HttpResponseStatus .FOUND ;
26
21
import static org .jboss .netty .handler .codec .http .HttpResponseStatus .MOVED_PERMANENTLY ;
27
22
import static org .jboss .netty .handler .codec .http .HttpResponseStatus .SEE_OTHER ;
@@ -79,16 +74,6 @@ public abstract class Protocol {
79
74
REDIRECT_STATUSES .add (TEMPORARY_REDIRECT .getCode ());
80
75
}
81
76
82
- public static final Set <String > PROPAGATED_ON_REDIRECT_HEADERS = new HashSet <>();
83
- static {
84
- PROPAGATED_ON_REDIRECT_HEADERS .add (ACCEPT .toLowerCase (Locale .US ));
85
- PROPAGATED_ON_REDIRECT_HEADERS .add (ACCEPT_CHARSET .toLowerCase (Locale .US ));
86
- PROPAGATED_ON_REDIRECT_HEADERS .add (ACCEPT_ENCODING .toLowerCase (Locale .US ));
87
- PROPAGATED_ON_REDIRECT_HEADERS .add (ACCEPT_LANGUAGE .toLowerCase (Locale .US ));
88
- PROPAGATED_ON_REDIRECT_HEADERS .add (REFERER .toLowerCase (Locale .US ));
89
- PROPAGATED_ON_REDIRECT_HEADERS .add (USER_AGENT .toLowerCase (Locale .US ));
90
- }
91
-
92
77
public Protocol (ChannelManager channelManager , AsyncHttpClientConfig config , NettyAsyncHttpProviderConfig nettyConfig ,
93
78
NettyRequestSender requestSender ) {
94
79
this .channelManager = channelManager ;
@@ -108,15 +93,14 @@ public Protocol(ChannelManager channelManager, AsyncHttpClientConfig config, Net
108
93
109
94
public abstract void onClose (NettyResponseFuture <?> future );
110
95
111
- private FluentCaseInsensitiveStringsMap propagatedHeaders (Request request ) {
112
- FluentCaseInsensitiveStringsMap redirectHeaders = new FluentCaseInsensitiveStringsMap ();
113
- for (Map .Entry <String , List <String >> headerEntry : request .getHeaders ()) {
114
- String headerName = headerEntry .getKey ();
115
- List <String > headerValues = headerEntry .getValue ();
116
- if (PROPAGATED_ON_REDIRECT_HEADERS .contains (headerName .toLowerCase (Locale .US )))
117
- redirectHeaders .add (headerName , headerValues );
118
- }
119
- return redirectHeaders ;
96
+ private FluentCaseInsensitiveStringsMap propagatedHeaders (Request request , boolean switchToGet ) {
97
+
98
+ FluentCaseInsensitiveStringsMap originalHeaders = request .getHeaders ();
99
+ originalHeaders .remove (HOST );
100
+ originalHeaders .remove (CONTENT_LENGTH );
101
+ if (switchToGet )
102
+ originalHeaders .remove (CONTENT_TYPE );
103
+ return originalHeaders ;
120
104
}
121
105
122
106
protected boolean exitAfterHandlingRedirect (//
@@ -143,9 +127,9 @@ protected boolean exitAfterHandlingRedirect(//
143
127
144
128
// if we are to strictly handle 302, we should keep the original method (which browsers don't)
145
129
// 303 must force GET
146
- if ((statusCode == FOUND .getCode () && !config .isStrict302Handling ()) || statusCode == SEE_OTHER .getCode ())
130
+ boolean switchToGet = !request .getMethod ().equals ("GET" ) && (statusCode == 303 || (statusCode == 302 && !config .isStrict302Handling ()));
131
+ if (switchToGet )
147
132
requestBuilder .setMethod ("GET" );
148
-
149
133
// in case of a redirect from HTTP to HTTPS, future attributes might change
150
134
final boolean initialConnectionKeepAlive = future .isKeepAlive ();
151
135
final String initialPartition = future .getPartitionId ();
@@ -164,7 +148,7 @@ protected boolean exitAfterHandlingRedirect(//
164
148
requestBuilder .addOrReplaceCookie (c );
165
149
}
166
150
167
- requestBuilder .setHeaders (propagatedHeaders (future .getRequest ()));
151
+ requestBuilder .setHeaders (propagatedHeaders (future .getRequest (), switchToGet ));
168
152
169
153
final Request nextRequest = requestBuilder .setUrl (newUrl ).build ();
170
154
0 commit comments