jdk/test/sun/net/www/protocol/https/HttpsClient/OriginServer.java
changeset 41809 834f1177e70b
parent 41808 1b2343088086
parent 41771 18c9669e76ca
child 41810 7beaa4ce63fa
child 41901 48a55de1ba10
equal deleted inserted replaced
41808:1b2343088086 41809:834f1177e70b
     1 /*
       
     2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  *
       
    26  * This is a HTTP test server used by the regression test
       
    27  * for the bug fixes: 4323990, 4413069
       
    28  */
       
    29 
       
    30 import java.io.*;
       
    31 import java.net.*;
       
    32 import javax.net.*;
       
    33 
       
    34 /*
       
    35  * OriginServer.java -- a simple server that can serve
       
    36  * Http get request in both clear and secure channel
       
    37  */
       
    38 
       
    39 public abstract class OriginServer implements Runnable, Closeable {
       
    40 
       
    41     private ServerSocket server = null;
       
    42     Exception serverException = null;
       
    43     private volatile boolean closed;
       
    44 
       
    45     /**
       
    46      * Constructs a OriginServer based on ss and
       
    47      * obtains a response data's bytecodes using the method
       
    48      * getBytes.
       
    49      */
       
    50     protected OriginServer(ServerSocket ss) throws Exception
       
    51     {
       
    52         server = ss;
       
    53         newListener();
       
    54         if (serverException != null)
       
    55             throw serverException;
       
    56     }
       
    57 
       
    58     @Override
       
    59     public void close() throws IOException {
       
    60         if (closed)
       
    61             return;
       
    62         closed = true;
       
    63         server.close();
       
    64     }
       
    65 
       
    66     /**
       
    67      * Returns an array of bytes containing the bytes for
       
    68      * the data sent in the response.
       
    69      *
       
    70      * @return the bytes for the information that is being sent
       
    71      */
       
    72     public abstract byte[] getBytes();
       
    73 
       
    74     /**
       
    75      * The "listen" thread that accepts a connection to the
       
    76      * server, parses the header and sends back the response
       
    77      */
       
    78     public void run()
       
    79     {
       
    80         Socket socket;
       
    81 
       
    82         // accept a connection
       
    83         try {
       
    84             socket = server.accept();
       
    85         } catch (IOException e) {
       
    86             if (!closed) {
       
    87                 System.out.println("Class Server died: " + e.getMessage());
       
    88                 serverException = e;
       
    89             }
       
    90             return;
       
    91         }
       
    92         try {
       
    93             DataOutputStream out =
       
    94                 new DataOutputStream(socket.getOutputStream());
       
    95             try {
       
    96                 BufferedReader in =
       
    97                     new BufferedReader(new InputStreamReader(
       
    98                                 socket.getInputStream()));
       
    99                 // read the request
       
   100                 readRequest(in);
       
   101                 // retrieve bytecodes
       
   102                 byte[] bytecodes = getBytes();
       
   103                 // send bytecodes in response (assumes HTTP/1.0 or later)
       
   104                 try {
       
   105                     out.writeBytes("HTTP/1.0 200 OK\r\n");
       
   106                     out.writeBytes("Content-Length: " + bytecodes.length +
       
   107                                    "\r\n");
       
   108                     out.writeBytes("Content-Type: text/html\r\n\r\n");
       
   109                     out.write(bytecodes);
       
   110                     out.flush();
       
   111                 } catch (IOException ie) {
       
   112                     serverException = ie;
       
   113                     return;
       
   114                 }
       
   115 
       
   116             } catch (Exception e) {
       
   117                 // write out error response
       
   118                 out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");
       
   119                 out.writeBytes("Content-Type: text/html\r\n\r\n");
       
   120                 out.flush();
       
   121             }
       
   122 
       
   123         } catch (IOException ex) {
       
   124             System.out.println("error writing response: " + ex.getMessage());
       
   125             serverException = ex;
       
   126 
       
   127         } finally {
       
   128             try {
       
   129                 socket.close();
       
   130             } catch (IOException e) {
       
   131                 serverException = e;
       
   132             }
       
   133         }
       
   134     }
       
   135 
       
   136     /**
       
   137      * Create a new thread to listen.
       
   138      */
       
   139     private void newListener()
       
   140     {
       
   141         (new Thread(this)).start();
       
   142     }
       
   143 
       
   144     /**
       
   145      * read the response, don't care for the syntax of the request-line
       
   146      * for this testing
       
   147      */
       
   148     private static void readRequest(BufferedReader in)
       
   149         throws IOException
       
   150     {
       
   151         String line = null;
       
   152         System.out.println("Server received: ");
       
   153         do {
       
   154             if (line != null)
       
   155                 System.out.println(line);
       
   156             line = in.readLine();
       
   157         } while ((line.length() != 0) &&
       
   158                 (line.charAt(0) != '\r') && (line.charAt(0) != '\n'));
       
   159     }
       
   160 }