import java.io.*; import java.net.*; class OneConnection { Socket sock; BufferedReader in = null; DataOutputStream out = null; OneConnection(Socket sock) throws Exception{ this.sock = sock; in = new BufferedReader( new InputStreamReader( sock.getInputStream() ) ); out = new DataOutputStream(sock.getOutputStream()); } String getRequest() throws Exception { String s=null; while ( (s=in.readLine())!=null) { System.out.println("got: "+s); } return s; } void sendFile(String fname) throws Exception { throw new Exception("only implemented in child class B"); } } ///////////////////// class OneConnection_A extends OneConnection { OneConnection_A(Socket sock) throws Exception { super(sock); } String getRequest() throws Exception { String s=null; while ( (s=in.readLine())!=null) { System.out.println("got: "+s); if (s.indexOf("GET") > -1) { out.writeBytes("HTTP-1.0 200 OK\r\n"); s = s.substring(4); int i = s.indexOf(" "); System.out.println("file: "+ s.substring(0, i)); return s.substring(0, i); } } return null; } } ///////////////////// class OneConnection_B extends OneConnection_A { OneConnection_B(Socket sock) throws Exception { super(sock); } void sendFile(String fname) throws Exception { String where = "/tmp/" + fname; // You should create dir if necessary // and copy try.html file to it. if (where.indexOf("..") > -1) throw new SecurityException("No access to parent dirs"); System.out.println("looking for " + where); File f = new File(where); DataInputStream din = new DataInputStream( new FileInputStream(f) ); int len = (int) f.length(); byte[] buf = new byte[len]; din.readFully(buf); out.writeBytes("Content-Length: " + len + "\r\n"); out.writeBytes("Content-Type: text/html\r\n\r\n"); out.write(buf); out.flush(); out.close(); } } /////////////////////