test/jdk/java/net/httpclient/ProxyServer.java
changeset 49944 4690a2871b44
parent 49765 ee6f7a61f3a5
child 53521 41fa3e6f2785
child 56507 2294c51eae30
equal deleted inserted replaced
49943:8e1ed2a15845 49944:4690a2871b44
    71      * Shuts down the proxy, probably aborting any connections
    71      * Shuts down the proxy, probably aborting any connections
    72      * currently open
    72      * currently open
    73      */
    73      */
    74     public void close() throws IOException {
    74     public void close() throws IOException {
    75         if (debug) System.out.println("Proxy: closing");
    75         if (debug) System.out.println("Proxy: closing");
    76             done = true;
    76         done = true;
    77         listener.close();
    77         listener.close();
    78         for (Connection c : connections) {
    78         for (Connection c : connections) {
    79             if (c.running()) {
    79             c.close();
    80                 c.close();
       
    81             }
       
    82         }
    80         }
    83     }
    81     }
    84 
    82 
    85     List<Connection> connections;
    83     List<Connection> connections;
    86 
    84 
   177 
   175 
   178         boolean running() {
   176         boolean running() {
   179             return out.isAlive() || in.isAlive();
   177             return out.isAlive() || in.isAlive();
   180         }
   178         }
   181 
   179 
   182         public void close() throws IOException {
   180         private volatile boolean closing;
       
   181         public synchronized void close() throws IOException {
       
   182             closing = true;
   183             if (debug) System.out.println("Closing connection (proxy)");
   183             if (debug) System.out.println("Closing connection (proxy)");
   184             if (serverSocket != null) serverSocket.close();
   184             if (serverSocket != null) serverSocket.close();
   185             if (clientSocket != null) clientSocket.close();
   185             if (clientSocket != null) clientSocket.close();
   186         }
   186         }
   187 
   187 
   236                     buf[i--] = (byte)cmdLine.charAt(x--);
   236                     buf[i--] = (byte)cmdLine.charAt(x--);
   237                 }
   237                 }
   238                 i++;
   238                 i++;
   239 
   239 
   240                 commonInit(dest, 80);
   240                 commonInit(dest, 80);
   241                 serverOut.write(buf, i, buf.length-i);
   241                 OutputStream sout;
       
   242                 synchronized (this) {
       
   243                     if (closing) return;
       
   244                     sout = serverOut;
       
   245                 }
       
   246                 // might fail if we're closing but we don't care.
       
   247                 sout.write(buf, i, buf.length-i);
   242                 proxyCommon();
   248                 proxyCommon();
   243 
   249 
   244             } catch (URISyntaxException e) {
   250             } catch (URISyntaxException e) {
   245                 throw new IOException(e);
   251                 throw new IOException(e);
   246             }
   252             }
   247         }
   253         }
   248 
   254 
   249         void commonInit(String dest, int defaultPort) throws IOException {
   255         synchronized void commonInit(String dest, int defaultPort) throws IOException {
       
   256             if (closing) return;
   250             int port;
   257             int port;
   251             String[] hostport = dest.split(":");
   258             String[] hostport = dest.split(":");
   252             if (hostport.length == 1) {
   259             if (hostport.length == 1) {
   253                 port = defaultPort;
   260                 port = defaultPort;
   254             } else {
   261             } else {
   259             serverOut = serverSocket.getOutputStream();
   266             serverOut = serverSocket.getOutputStream();
   260 
   267 
   261             serverIn = new BufferedInputStream(serverSocket.getInputStream());
   268             serverIn = new BufferedInputStream(serverSocket.getInputStream());
   262         }
   269         }
   263 
   270 
   264         void proxyCommon() throws IOException {
   271         synchronized void proxyCommon() throws IOException {
       
   272             if (closing) return;
   265             out = new Thread(() -> {
   273             out = new Thread(() -> {
   266                 try {
   274                 try {
   267                     byte[] bb = new byte[8000];
   275                     byte[] bb = new byte[8000];
   268                     int n;
   276                     int n;
   269                     while ((n = clientIn.read(bb)) != -1) {
   277                     while ((n = clientIn.read(bb)) != -1) {
   270                         serverOut.write(bb, 0, n);
   278                         serverOut.write(bb, 0, n);
   271                     }
   279                     }
       
   280                     closing = true;
   272                     serverSocket.close();
   281                     serverSocket.close();
   273                     clientSocket.close();
   282                     clientSocket.close();
   274                 } catch (IOException e) {
   283                 } catch (IOException e) {
   275                     if (debug) {
   284                     if (debug) {
   276                         System.out.println (e);
   285                         System.out.println (e);
   282                     byte[] bb = new byte[8000];
   291                     byte[] bb = new byte[8000];
   283                     int n;
   292                     int n;
   284                     while ((n = serverIn.read(bb)) != -1) {
   293                     while ((n = serverIn.read(bb)) != -1) {
   285                         clientOut.write(bb, 0, n);
   294                         clientOut.write(bb, 0, n);
   286                     }
   295                     }
       
   296                     closing = true;
   287                     serverSocket.close();
   297                     serverSocket.close();
   288                     clientSocket.close();
   298                     clientSocket.close();
   289                 } catch (IOException e) {
   299                 } catch (IOException e) {
   290                     if (debug) {
   300                     if (debug) {
   291                         System.out.println(e);
   301                         System.out.println(e);
   300             out.start();
   310             out.start();
   301             in.start();
   311             in.start();
   302         }
   312         }
   303 
   313 
   304         void doTunnel(String dest) throws IOException {
   314         void doTunnel(String dest) throws IOException {
       
   315             if (closing) return; // no need to go further.
   305             commonInit(dest, 443);
   316             commonInit(dest, 443);
       
   317             // might fail if we're closing, but we don't care.
   306             clientOut.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
   318             clientOut.write("HTTP/1.1 200 OK\r\n\r\n".getBytes());
   307             proxyCommon();
   319             proxyCommon();
   308         }
   320         }
   309     }
   321     }
   310 
   322