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 { 40 41 private ServerSocket server = null; 42 Exception serverException = null; 43 /** 44 * Constructs a OriginServer based on ss and 45 * obtains a response data's bytecodes using the method 46 * getBytes. 47 */ 48 protected OriginServer(ServerSocket ss) throws Exception 49 { 50 server = ss; 51 newListener(); 52 if (serverException != null) 53 throw serverException; 54 } 55 56 /** 57 * Returns an array of bytes containing the bytes for 58 * the data sent in the response. 59 * 60 * @return the bytes for the information that is being sent 61 */ 62 public abstract byte[] getBytes(); 63 64 /** 65 * The "listen" thread that accepts a connection to the 66 * server, parses the header and sends back the response 67 */ 68 public void run() 69 { 70 Socket socket; 71 72 // accept a connection 73 try { 74 socket = server.accept(); 75 } catch (IOException e) { 76 System.out.println("Class Server died: " + e.getMessage()); 77 serverException = e; 78 return; 79 } 80 try { 81 DataOutputStream out = 82 new DataOutputStream(socket.getOutputStream()); 83 try { 84 BufferedReader in = 85 new BufferedReader(new InputStreamReader( 86 socket.getInputStream())); 87 // read the request 88 readRequest(in); 89 // retrieve bytecodes 90 byte[] bytecodes = getBytes(); 91 // send bytecodes in response (assumes HTTP/1.0 or later) 92 try { 93 out.writeBytes("HTTP/1.0 200 OK\r\n"); 94 out.writeBytes("Content-Length: " + bytecodes.length + 95 "\r\n"); 96 out.writeBytes("Content-Type: text/html\r\n\r\n"); 97 out.write(bytecodes); 98 out.flush(); 99 } catch (IOException ie) { 100 serverException = ie; 101 return; 102 } 103 104 } catch (Exception e) { 105 // write out error response 106 out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n"); 107 out.writeBytes("Content-Type: text/html\r\n\r\n"); 108 out.flush(); 109 } 110 111 } catch (IOException ex) { 112 System.out.println("error writing response: " + ex.getMessage()); 113 serverException = ex; 114 115 } finally { 116 try { 117 socket.close(); 118 } catch (IOException e) { 119 serverException = e; 120 } 121 } 122 } 123 124 /** 125 * Create a new thread to listen. 126 */ 127 private void newListener() 128 { 129 (new Thread(this)).start(); 130 } 131 132 /** 133 * read the response, don't care for the syntax of the request-line 134 * for this testing 135 */ 136 private static void readRequest(BufferedReader in) 137 throws IOException 138 { 139 String line = null; 140 System.out.println("Server received: "); 141 do { 142 if (line != null) 143 System.out.println(line); 144 line = in.readLine(); 145 } while ((line.length() != 0) && 146 (line.charAt(0) != '\r') && (line.charAt(0) != '\n')); 147 } 148 }