13
13
*/
14
14
package org .asynchttpclient .providers .netty .handler ;
15
15
16
+ import static io .netty .handler .codec .http .HttpHeaders .Names .ACCEPT ;
17
+ import static io .netty .handler .codec .http .HttpHeaders .Names .ACCEPT_CHARSET ;
18
+ import static io .netty .handler .codec .http .HttpHeaders .Names .ACCEPT_ENCODING ;
19
+ import static io .netty .handler .codec .http .HttpHeaders .Names .ACCEPT_LANGUAGE ;
20
+ import static io .netty .handler .codec .http .HttpHeaders .Names .REFERER ;
21
+ import static io .netty .handler .codec .http .HttpHeaders .Names .USER_AGENT ;
16
22
import static io .netty .handler .codec .http .HttpResponseStatus .FOUND ;
17
23
import static io .netty .handler .codec .http .HttpResponseStatus .MOVED_PERMANENTLY ;
18
24
import static io .netty .handler .codec .http .HttpResponseStatus .SEE_OTHER ;
26
32
27
33
import java .io .IOException ;
28
34
import java .util .HashSet ;
35
+ import java .util .List ;
36
+ import java .util .Locale ;
37
+ import java .util .Map ;
29
38
import java .util .Set ;
30
39
31
40
import org .asynchttpclient .AsyncHandler ;
32
41
import org .asynchttpclient .AsyncHttpClientConfig ;
42
+ import org .asynchttpclient .FluentCaseInsensitiveStringsMap ;
33
43
import org .asynchttpclient .HttpResponseHeaders ;
34
44
import org .asynchttpclient .HttpResponseStatus ;
35
45
import org .asynchttpclient .MaxRedirectException ;
@@ -72,6 +82,16 @@ public abstract class Protocol {
72
82
REDIRECT_STATUSES .add (TEMPORARY_REDIRECT .code ());
73
83
}
74
84
85
+ public static final Set <String > PROPAGATED_ON_REDIRECT_HEADERS = new HashSet <String >();
86
+ static {
87
+ PROPAGATED_ON_REDIRECT_HEADERS .add (ACCEPT .toLowerCase (Locale .US ));
88
+ PROPAGATED_ON_REDIRECT_HEADERS .add (ACCEPT_CHARSET .toLowerCase (Locale .US ));
89
+ PROPAGATED_ON_REDIRECT_HEADERS .add (ACCEPT_ENCODING .toLowerCase (Locale .US ));
90
+ PROPAGATED_ON_REDIRECT_HEADERS .add (ACCEPT_LANGUAGE .toLowerCase (Locale .US ));
91
+ PROPAGATED_ON_REDIRECT_HEADERS .add (REFERER .toLowerCase (Locale .US ));
92
+ PROPAGATED_ON_REDIRECT_HEADERS .add (USER_AGENT .toLowerCase (Locale .US ));
93
+ }
94
+
75
95
public Protocol (ChannelManager channelManager , AsyncHttpClientConfig config , NettyAsyncHttpProviderConfig nettyConfig ,
76
96
NettyRequestSender requestSender ) {
77
97
this .channelManager = channelManager ;
@@ -90,6 +110,17 @@ public Protocol(ChannelManager channelManager, AsyncHttpClientConfig config, Net
90
110
91
111
public abstract void onClose (NettyResponseFuture <?> future );
92
112
113
+ private FluentCaseInsensitiveStringsMap propagatedHeaders (Request request ) {
114
+ FluentCaseInsensitiveStringsMap redirectHeaders = new FluentCaseInsensitiveStringsMap ();
115
+ for (Map .Entry <String , List <String >> headerEntry : request .getHeaders ()) {
116
+ String headerName = headerEntry .getKey ();
117
+ List <String > headerValues = headerEntry .getValue ();
118
+ if (PROPAGATED_ON_REDIRECT_HEADERS .contains (headerName .toLowerCase (Locale .US )))
119
+ redirectHeaders .add (headerName , headerValues );
120
+ }
121
+ return redirectHeaders ;
122
+ }
123
+
93
124
protected boolean exitAfterHandlingRedirect (//
94
125
Channel channel ,//
95
126
NettyResponseFuture <?> future ,//
@@ -138,6 +169,8 @@ protected boolean exitAfterHandlingRedirect(//
138
169
requestBuilder .addOrReplaceCookie (c );
139
170
}
140
171
172
+ requestBuilder .setHeaders (propagatedHeaders (future .getRequest ()));
173
+
141
174
Callback callback = channelManager .newDrainCallback (future , channel , initialConnectionKeepAlive , initialPoolKey );
142
175
143
176
if (HttpHeaders .isTransferEncodingChunked (response )) {
0 commit comments