📄 classserver.java
字号:
package com.zsusoft.zfl;/*** China Zhongshan University Software Research Institute fszfl borrow and revise.* 02/05/16* 中国中山大学软件研究所 佛山张峰岭借用和修改部分代码* 2002-5***//* * @(#)ClassServer.java 1.2 00/06/21 * * Copyright 1995-1998 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information"). You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. */import java.io.*;import java.net.*;import javax.net.*;/* * ClassServer.java -- a simple file server that can serve * Http get request in both clear and secure channel *//** * Based on ClassServer.java in tutorial/rmi */public abstract class ClassServer implements Runnable { protected SSL_with_signature ssl_with_signature = null; //This is added by fszfl to get the signature private ServerSocket server = null; /** * Constructs a ClassServer based on <b>ss</b> and * obtains a file's bytecodes using the method <b>getBytes</b>. * */ protected ClassServer(ServerSocket ss) { server = ss; newListener(); } protected ClassServer(ServerSocket ss, SSL_with_signature sign) { server = ss; ssl_with_signature = sign; newListener(); } /** * Returns an array of bytes containing the bytes for * the file represented by the argument <b>path</b>. * * @return the bytes for the file * @exception FileNotFoundException if the file corresponding * to <b>path</b> could not be loaded. * @exception IOException if error occurs reading the class */ public abstract byte[] getBytes(String path) throws IOException, FileNotFoundException; /** * The "listen" thread that accepts a connection to the * server, parses the header to obtain the file name * and sends back the bytes for the file (or error * if the file is not found or the response was malformed). */ public void run() { Socket socket; // accept a connection try { socket = server.accept(); } catch (IOException e) { System.out.println("Class Server died: " + e.getMessage()); //e.printStackTrace(); return; } // create a new thread to accept the next connection newListener(); try { DataOutputStream out = new DataOutputStream(socket.getOutputStream()); try { // get path to class file from header DataInputStream in = new DataInputStream(socket.getInputStream()); String path = getPath(in); byte[] bytecodes ; //The following codes were added by Foshan Zhang fengling. //I want to return the SSL information to client // :-> if( path.startsWith("getsignature")){ String remote_host = socket.getInetAddress().getHostName(); String Signdata_desc = path.substring(13, path.length()).trim(); String Signdata = path.substring(13, path.length()).trim(); SSLsignature_record signature_record = ssl_with_signature.request_partner_signature(remote_host,Signdata_desc,Signdata.getBytes()); String Html =null; Html = "<html>\r\n"; Html += "<head>\r\n"; Html += "<title>"; Html += "签名结果"; Html += "</title>\r\n"; Html += "</head>\r\n"; Html += "<body background=\"leaves.gif\">\r\n"; Html += "<h1 align=\"center\">签名结果</h1>"; if( signature_record == null ){ Html +="<h2 align=\"center\">拒绝签名</h2>\n"; } else { Html +="<h2 align=\"center\">同意签名,签名结果如下</h2>\n"; Html += "<center><table width=\"80%\"><tr><td>\r\n"; Html += "<pre align=\"left\">\r\n"; Html += signature_record.toString(); Html += "</pre>\r\n"; Html += "</td></tr></table>\r\n"; } Html += "</body>\r\n"; Html += "</html>\r\n"; bytecodes = Html.getBytes(); } else { if( path.equals("sslinfo") ){ String Html =null; String SSLMessage = SSL_with_signature.getSSLsessionMessage(socket); Html = "<html>\r\n"; Html += "<head>\r\n"; Html += "<title>"; Html += "SSL会话信息"; Html += "</title>\r\n"; Html += "</head>\r\n"; Html += "<body>\r\n"; Html += "<h1 align=\"center\">SSL会话信息</h1>"; Html += "<pre align=\"left\">\r\n"; Html += SSLMessage; Html += "</pre>\r\n"; Html += "</body>\r\n"; Html += "</html>\r\n"; bytecodes = Html.getBytes(); //The last codes were added by foshan Zhang fengling } else { // retrieve bytecodes bytecodes = getBytes(path); } } // send bytecodes in response (assumes HTTP/1.0 or later) try { if( bytecodes != null ){ out.writeBytes("HTTP/1.0 200 OK\r\n"); out.writeBytes("Content-Length: " + bytecodes.length + "\r\n"); out.writeBytes("Content-Type: text/html\r\n\r\n"); out.write(bytecodes); out.flush(); } else { out.writeBytes("HTTP/1.0 404 Can not find the file "+path+"!\r\n"); out.writeBytes("Content-Type: text/html\r\n\r\n"); out.writeBytes("I can not find the file \""+path+"\"!\r\n"); out.flush(); } } catch (IOException ie) { ie.printStackTrace(); out.writeBytes("HTTP/1.0 404 " + ie.getMessage() + "\r\n"); out.writeBytes("Content-Type: text/html\r\n\r\n"); out.flush(); return; } } catch (Exception e) { e.printStackTrace(); // write out error response out.writeBytes("HTTP/1.0 404 " + e.getMessage() + "\r\n"); out.writeBytes("Content-Type: text/html\r\n\r\n"); out.flush(); } } catch (IOException ex) { // eat exception (could log error to log file, but // write out to stdout for now). System.out.println("error writing response: " + ex.getMessage()); ex.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { } } } /** * Create a new thread to listen. */ private void newListener() { (new Thread(this)).start(); } /** * Returns the path to the file obtained from * parsing the HTML header. */ private static String getPath(DataInputStream in) throws IOException { String line = in.readLine(); System.err.println("\n"+line); String path = ""; // extract class from GET line if (line.startsWith("GET /")) { line = line.substring(5, line.length()-1).trim(); int index = line.indexOf(' '); if (index != -1) { path = line.substring(0, index); } } // eat the rest of header do { line = in.readLine(); System.err.println(line); } while ((line.length() != 0)); if (path.length() != 0) { return path; } else { return "default.html"; //throw new IOException("Malformed Header"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -