@@ -70,8 +70,44 @@ public class DefaultConnectionReuseStrategy implements ConnectionReuseStrategy {
70
70
71
71
public static final DefaultConnectionReuseStrategy INSTANCE = new DefaultConnectionReuseStrategy ();
72
72
73
+ /**
74
+ * Flag to determine whether the connection should be forcibly closed on receiving a 408 status code.
75
+ * If {@code true}, the connection will be closed when a 408 (Request Timeout) response is encountered,
76
+ * regardless of the "Connection" header's value.
77
+ * @since 5.5
78
+ */
79
+ private final boolean forceCloseOn408 ;
80
+
81
+ /**
82
+ * Default constructor that initializes the strategy with the default behavior:
83
+ * connections are not forcibly closed on a 408 status code unless explicitly signaled by the server.
84
+ * <p>
85
+ * This constructor maintains backward compatibility and adheres to the HTTP protocol as it is,
86
+ * meaning that connections will be kept alive by default unless the server includes a "Connection: close"
87
+ * header or other headers that imply the connection should be closed.
88
+ */
73
89
public DefaultConnectionReuseStrategy () {
74
- super ();
90
+ this (false ); // Default behavior: do not force-close on 408
91
+ }
92
+
93
+ /**
94
+ * Constructor to initialize the strategy with a customizable behavior for handling 408 responses.
95
+ * <p>
96
+ * When {@code forceCloseOn408} is set to {@code true}, the strategy will forcefully close connections
97
+ * upon encountering a 408 (Request Timeout) response, regardless of the presence of the "Connection: close"
98
+ * header in the response. This is particularly useful when interacting with servers that send 408 responses
99
+ * without properly indicating that the connection should be closed.
100
+ * <p>
101
+ * If {@code forceCloseOn408} is set to {@code false}, the strategy will follow the standard HTTP protocol
102
+ * behavior, only closing the connection if the server explicitly signals to do so (e.g., by including a
103
+ * "Connection: close" header or other relevant headers).
104
+ *
105
+ * @param forceCloseOn408 {@code true} to force connection close on 408 responses;
106
+ * {@code false} to use the default HTTP behavior.
107
+ * @since 5.5
108
+ */
109
+ public DefaultConnectionReuseStrategy (final boolean forceCloseOn408 ) {
110
+ this .forceCloseOn408 = forceCloseOn408 ;
75
111
}
76
112
77
113
// see interface ConnectionReuseStrategy
@@ -80,6 +116,10 @@ public boolean keepAlive(
80
116
final HttpRequest request , final HttpResponse response , final HttpContext context ) {
81
117
Args .notNull (response , "HTTP response" );
82
118
119
+ if (forceCloseOn408 && response .getCode () == HttpStatus .SC_REQUEST_TIMEOUT ) {
120
+ return false ; // Force connection close on 408 if configured to do so
121
+ }
122
+
83
123
if (request != null ) {
84
124
// Consider framing of a request message with both Content-Length and Content-Length headers faulty
85
125
if (request .containsHeader (HttpHeaders .CONTENT_LENGTH ) && request .containsHeader (HttpHeaders .TRANSFER_ENCODING )) {
0 commit comments