import com.mortbay.Base.*; import com.mortbay.Util.*; import java.io.*; import java.net.*; /* ------------------------------------------------------------ ---- */ /** HTTP Listener * <p> Instances of HttpListener handle a single receiving HTTP * connection. They make calls into HttpServer to handle the * requests that they receive. * * <p><h4>Notes</h4> * <p> On JDK1.0.2 it is not possible to specify the listening InetAddress * * @see class HttpServer * @version $Id: HttpListener.java,v 2.16 2000/11/07 15:34:38 gregwilkins Exp $ * @author Greg Wilkins */ public class HttpListener extends ThreadedServer { public static boolean frameDebug = System.getProperties().get(" FRAMEDEBUG")!=null; /* ------------------------------ ------------------------------ */ public static Class[] ConstructArgs = { com.mortbay.Util.InetAddrPort. class, com.mortbay.HTTP.HttpServer.cl ass, Integer.TYPE,Integer.TYPE,Inte ger.TYPE }; /* ------------------------------ ------------------------------ */ InetAddrPort address=null; HttpServer server=null; /* ------------------------------ ------------------------------ */ public InetAddrPort getAddress() { return address; } /* ------------------------------ ------------------------------ */ /** Construct a HttpListener * @param address The InetAddress and port on which to listen * If address.inetAddress==null, * InetAddrPort.getLocalHost() is used and set in address. * If address.port==0, 80 is used and set in address. * @param server The HttpServer to pass requests to. */ public HttpListener(InetAddrPort address, HttpServer server) throws IOException { this(address,server,0,0,20000) ; } /* ------------------------------ ------------------------------ */ /** Constructor. * @param address The InetAddress and port on which to listen * If address.inetAddress==null, * InetAddrPort.getLocalHost() is used and set in address. * If address.port==0, 80 is used and set in address. * @param server The HttpServer to pass requests to. * @param minThreads * @param maxThreads * @param maxIdleTimeMs * @exception IOException */ public HttpListener(InetAddrPort address, HttpServer server, int minThreads, int maxThreads, int maxIdleTimeMs) throws IOException { super(address,minThreads,maxTh reads,maxIdleTimeMs); if (address.getPort()==0) { address.setPort(80); super.setAddress(address.getIn etAddress(),address.getPort()) ; } this.address=address; this.server=server; } /* ------------------------------ ------------------------------ */ public void start() throws IOException { super.start(); Log.event(this.getClass().getN ame()+ " started on " +getAddress() ); } /* ------------------------------ ------------------------------ */ /** Allow the Listener a chance to customise the request * before the server does its stuff. * <br> This allows extra attributes to be set for SSL connections. */ protected void customiseRequest(Socket connection, HttpRequest request) {} /* ------------------------------ ------------------------------ */ /** Handle a connection to the server by trying to read a HttpRequest * and finding the right type of handler for that request, which * provides the HttpResponse. */ public void handleConnection(Socket connection) { try { if(frameDebug) Log.event("CONNECT: "+connection); while(true) { HttpRequest request = null; HttpResponse response = null; try { Code.debug("Waiting for request..."); request = new HttpRequest(server,connection, address); if (Code.debug()) Code.debug("Received HTTP request:", request.getMethod(), " ", request.getRequestURI()); if(frameDebug) Log.event("REQUEST: "+request.getMethod()+ " "+request.getRequestURI()); response=new HttpResponse(connection.getOut putStream(), request); customiseRequest(connection, request); server.handle(request,response ); response.complete(); if(frameDebug) Log.event("RESPONSE: "+response.getStatus()); String connection_header =response.getHeader(response.C onnection); if (connection_header!=null) connection_header= StringUtil.asciiToLowerCase(co nnection_header); // Break request loop if 1.0 and not keep-alive if (HttpHeader.HTTP_1_0.equals(re quest.getProtocol())&& !"keep-alive".equals(connectio n_header)) break; // Break request loop of close requested if (HttpHeader.Close.equals(conne ction_header)) { Code.debug("Closing persistent connection"); break; } // Read any remaining input. if (request.getContentLength()>0) { HttpInputStream in=request.getHttpInputStream( ); try{ // Skip/read remaining input while(in.getContentLength()>0 && (in.skip(4096)>0 || in.read()>=0)); } catch(IOException e) { Code.ignore(e); } } } catch (InterruptedIOException e) { Code.ignore(e); } catch (HeadException e) { Code.ignore(e); } catch (Exception e) { if(frameDebug) Code.warning(e); else Code.debug(e); // If no respones - must have a request error if (response==null) { // try to write BAD_REQUEST response=new HttpResponse(connection.getOut putStream(), null); response.setHeader(HttpHeader. Connection, HttpHeader.Close); response.sendError(HttpRespons e.SC_BAD_REQUEST); break; } } finally { if (request!=null) request.destroy(); if (response!=null) response.destroy(); } if(frameDebug) Log.event("KEEPALIVE: "+connection); } } catch (Exception e) { Code.debug("Request problem:",e); } } };
This website is all about Java & Php.
Most of the articles are my pratical implementations. They are not made pretty but are working solutions.
Please feel free to leave comments for improvements. Do use the Google Search Blog tool. Its pretty Neat
Sunday, February 13, 2011
Mini Http Web Server Java Based
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment