@@ -79,6 +79,10 @@ final class H1Exchange implements HttpExchange {
7979 private String responseContentType ;
8080 private long responseContentLength = -1 ;
8181 private boolean responseChunked ;
82+ // Keep-alive override from the response Connection header: null = not specified (use protocol
83+ // default), TRUE = keep-alive, FALSE = close. Set by captureControlHeader alongside the other
84+ // response header fields.
85+ private Boolean responseKeepAlive ;
8286 private int statusCode = -1 ;
8387 private boolean requestWritten = false ;
8488 private boolean expectContinueHandled = false ;
@@ -115,6 +119,7 @@ H1Exchange init(HttpRequest request) throws IOException {
115119 this .responseContentType = null ;
116120 this .responseContentLength = -1 ;
117121 this .responseChunked = false ;
122+ this .responseKeepAlive = null ;
118123 this .statusCode = -1 ;
119124 this .requestWritten = false ;
120125 this .expectContinueHandled = false ;
@@ -607,7 +612,6 @@ private void parseStatusAndHeaders(int code, UnsyncBufferedInputStream in) throw
607612
608613 ModifiableHttpHeaders headers = HttpHeaders .ofModifiable ();
609614 int headerCount = 0 ;
610- Boolean keepAlive = null ;
611615
612616 int lineLen ;
613617 while ((lineLen = readLine (in )) > 0 ) {
@@ -625,52 +629,44 @@ private void parseStatusAndHeaders(int code, UnsyncBufferedInputStream in) throw
625629 int valueStart = H1Utils .headerValueStart (responseLineBuffer , colon , lineLen );
626630 int valueEnd = H1Utils .headerValueEnd (responseLineBuffer , valueStart , lineLen );
627631 String name = H1Utils .parseHeaderLine (responseLineBuffer , colon , valueStart , valueEnd , headers );
628- Boolean keepAliveOverride = captureControlHeader (responseLineBuffer , valueStart , valueEnd , name );
629- if (keepAliveOverride != null ) {
630- keepAlive = keepAliveOverride ;
631- }
632+ captureControlHeader (responseLineBuffer , valueStart , valueEnd , name );
632633 }
633634
634635 this .responseHeaders = headers ;
635636
636- if (keepAlive != null ) {
637- connection .setKeepAlive (keepAlive );
637+ if (responseKeepAlive != null ) {
638+ connection .setKeepAlive (responseKeepAlive );
638639 }
639640 }
640641
641- private Boolean captureControlHeader (byte [] line , int valueStart , int valueEnd , String name ) throws IOException {
642- return switch (name ) {
642+ // Records the content-length, transfer-encoding, content-type, and connection (keep-alive) response
643+ // headers into the corresponding instance fields. Other headers are ignored here.
644+ private void captureControlHeader (byte [] line , int valueStart , int valueEnd , String name ) throws IOException {
645+ switch (name ) {
643646 case "content-length" -> {
644647 long length = parseContentLength (line , valueStart , valueEnd );
645648 if (responseContentLength >= 0 && responseContentLength != length ) {
646649 throw new IOException ("Conflicting Content-Length headers: "
647650 + responseContentLength + " and " + length );
648651 }
649652 responseContentLength = length ;
650- yield null ;
651- }
652- case "transfer-encoding" -> {
653- responseChunked = containsChunked (line , valueStart , valueEnd );
654- yield null ;
655- }
656- case "content-type" -> {
657- responseContentType = new String (
658- line ,
659- valueStart ,
660- valueEnd - valueStart ,
661- StandardCharsets .US_ASCII );
662- yield null ;
663653 }
654+ case "transfer-encoding" -> responseChunked = containsChunked (line , valueStart , valueEnd );
655+ case "content-type" -> responseContentType = new String (
656+ line ,
657+ valueStart ,
658+ valueEnd - valueStart ,
659+ StandardCharsets .US_ASCII );
664660 case "connection" -> {
665661 if (equalsIgnoreCase (line , valueStart , valueEnd , "close" )) {
666- yield Boolean .FALSE ;
662+ responseKeepAlive = Boolean .FALSE ;
667663 } else if (equalsIgnoreCase (line , valueStart , valueEnd , "keep-alive" )) {
668- yield Boolean .TRUE ;
664+ responseKeepAlive = Boolean .TRUE ;
669665 }
670- yield null ;
671666 }
672- default -> null ;
673- };
667+ default -> {
668+ }
669+ }
674670 }
675671
676672 private static boolean isOWS (byte b ) {
0 commit comments