11/*
2- * Copyright (c) 2015, 2022 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2015, 2024 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -195,6 +195,9 @@ class Connection {
195195 volatile InputStream clientIn , serverIn ;
196196 volatile OutputStream clientOut , serverOut ;
197197
198+ boolean proxyInClosed ; // only accessed from synchronized block
199+ boolean proxyOutClosed ; // only accessed from synchronized block
200+
198201 final static int CR = 13 ;
199202 final static int LF = 10 ;
200203
@@ -594,9 +597,7 @@ synchronized void proxyCommon(boolean log) throws IOException {
594597 if (log )
595598 System .out .printf ("Proxy Forwarding [request body]: total %d%n" , body );
596599 }
597- closing = true ;
598- serverSocket .close ();
599- clientSocket .close ();
600+ closeClientIn ();
600601 } catch (IOException e ) {
601602 if (!closing && debug ) {
602603 System .out .println ("Proxy: " + e );
@@ -615,9 +616,7 @@ synchronized void proxyCommon(boolean log) throws IOException {
615616 if (log ) System .out .printf ("Proxy Forwarding [response]: %s%n" , new String (bb , 0 , n , UTF_8 ));
616617 if (log ) System .out .printf ("Proxy Forwarding [response]: total %d%n" , resp );
617618 }
618- closing = true ;
619- serverSocket .close ();
620- clientSocket .close ();
619+ closeClientOut ();
621620 } catch (IOException e ) {
622621 if (!closing && debug ) {
623622 System .out .println ("Proxy: " + e );
@@ -641,6 +640,28 @@ void doTunnel(String dest) throws IOException {
641640 proxyCommon (false );
642641 }
643642
643+ synchronized void closeClientIn () throws IOException {
644+ closing = true ;
645+ proxyInClosed = true ;
646+ clientSocket .shutdownInput ();
647+ serverSocket .shutdownOutput ();
648+ if (proxyOutClosed ) {
649+ serverSocket .close ();
650+ clientSocket .close ();
651+ }
652+ }
653+
654+ synchronized void closeClientOut () throws IOException {
655+ closing = true ;
656+ proxyOutClosed = true ;
657+ serverSocket .shutdownInput ();
658+ clientSocket .shutdownOutput ();
659+ if (proxyInClosed ) {
660+ serverSocket .close ();
661+ clientSocket .close ();
662+ }
663+ }
664+
644665 @ Override
645666 public String toString () {
646667 return "Proxy connection " + id + ", client sock:" + clientSocket ;
0 commit comments