Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/OriginServer.java
41161 views
/*1* Copyright (c) 2001, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25* This is a HTTP test server.26*/2728import java.io.*;29import java.net.*;30import javax.net.*;3132/*33* OriginServer.java -- a simple server that can serve34* http requests in both clear and secure channel.35*/3637public abstract class OriginServer implements Runnable {3839private ServerSocket server = null;40Exception serverException = null;4142/**43* Constructs a OriginServer based on ss and44* obtains a response data's bytecodes using the method45* getBytes.46*/47protected OriginServer(ServerSocket ss) throws Exception48{49server = ss;50newListener();51if (serverException != null)52throw serverException;53}5455/**56* Returns an array of bytes containing the bytes for57* data sent in the response.58*59* @return the bytes for the information that is being sent60*/61public abstract byte[] getBytes();6263/**64* The "listen" thread that accepts a connection to the65* server, parses header and sends back the response66*/67public void run()68{69Socket socket;7071// accept a connection72try {73socket = server.accept();74} catch (IOException e) {75System.out.println("Class Server died: " + e.getMessage());76serverException = e;77return;78}79try {80DataOutputStream out =81new DataOutputStream(socket.getOutputStream());82try {83BufferedReader in =84new BufferedReader(new InputStreamReader(85socket.getInputStream()));86// read the request87readRequest(in);88// retrieve bytecodes89byte[] bytecodes = getBytes();90// send bytecodes in response (assumes HTTP/1.0 or later)91try {92out.writeBytes("HTTP/1.0 200 OK\r\n");93out.writeBytes("Content-Length: " + bytecodes.length +94"\r\n");95out.writeBytes("Content-Type: text/html\r\n\r\n");96out.write(bytecodes);97out.flush();98} catch (IOException ie) {99serverException = ie;100return;101}102103} catch (Exception e) {104// write out error response105out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");106out.writeBytes("Content-Type: text/html\r\n\r\n");107out.flush();108}109110} catch (IOException ex) {111System.out.println("Server: Error writing response: "112+ ex.getMessage());113serverException = ex;114115} finally {116try {117socket.close();118} catch (IOException e) {119serverException = e;120}121}122}123124/**125* Create a new thread to listen.126*/127private void newListener()128{129(new Thread(this)).start();130}131132/**133* read the response, don't care for the syntax of the request-line134*/135private static void readRequest(BufferedReader in)136throws IOException137{138String line = null;139do {140line = in.readLine();141System.out.println("Server recieved: " + line);142} while ((line.length() != 0) &&143(line.charAt(0) != '\r') && (line.charAt(0) != '\n'));144145// read the last line to clear the POST input buffer146// would be best to make sure all bytes are read, maybe later.147line = in.readLine();148System.out.println("Command received: " + line);149150System.out.println("");151}152}153154155