- void process(int timeout) throws IOException
- {
- final int firstLineTimeout = 2000;
- boolean isFirstLine = true;
- boolean keepLooping = true;
- try
- {
- this.fireConnectionInitiated();
- processLoop: while (keepLooping)
- {
- if (isFirstLine)
- {
- this.socket.setSoTimeout(firstLineTimeout);
- }
- String requestLine = HttpRequest.readLineAsString(in);
- if (requestLine == null)
- {
- keepLooping = false;
- break processLoop;
- }
- if (isFirstLine)
- {
- this.socket.setSoTimeout(timeout);
- isFirstLine = false;
- }
- HttpRequest request = new HttpRequest(this, requestLine, in);
- HttpResponse response = new HttpResponse(request, out);
- request.linkResponse(response);
- this.fireRequestReceived(request, response);
- String hostname = request.getHeader("Host");
- if (hostname != null)
- {
- hostname = Text.beforeIfAny(hostname, ':'); // strip port
- request.setHost(server.getHost(hostname));
- }
- try
- {
- if (request.getHost() == null)
- {
- response.setStatusCode(StatusCode.NOT_FOUND);
- response.setContentType("text/html");
- response.setKeepAlive(false);
- response.setTransferEncodingChunked(false);
- response.setContent("404: HostNotFound: " + request.getHeader("Host"));
- String reason = "Host not found: " + request.getHeader("Host");
- this.fireRequestDeclined(request, response, reason);
- continue processLoop;
- }
- try
- {
- do // back here on 'http forward'
- {
- if (request.forwardTo != null)
- request.applyForward();
- // System.out.println("HTTP REQUEST: " + request.getAction());
- // figure out http service
- HttpService service = request.getHost().getService(request);
- if (service == null)
- {
- response.setStatusCode(StatusCode.NOT_FOUND);
- response.setContentType("text/html");
- response.setKeepAlive(false);
- response.setTransferEncodingChunked(false);
- response.setContent("404: ServiceNotFound: " + request.getAction());
- String reason = "Service not found: " + request.getResourcePath();
- this.fireRequestDeclined(request, response, reason);
- continue processLoop;
- }
- request.setService(service);
- request.getHost().doBeforeRequest(request, response);
- try
- {
- if (request.getMethod().equals("GET"))
- {
- service.serveGET(request, response);
- }
- else if (request.getMethod().equals("POST"))
- {
- service.servePOST(request, response);
- }
- else if (request.getMethod().equals("HEAD"))
- {
- service.serveHEAD(request, response);
- }
- else
- {
- HttpService.sendMethodNotAllowed(request, response);
- this.fireRequestDeclined(request, response, "Method Not Allowed");
- }
- }
- catch (DieServiceHandling die)
- {
- // request forwards reach this code
- System.out.println("HTTP REQUEST DIED: " + request.getAction());
- }
- finally
- {
- request.getHost().doAfterRequest(request, response);
- }
- }
- while (request.forwardTo != null);
- }
- finally
- {
- if (request.getMethod().equals("POST"))
- {
- // System.out.println("HTTP REQUEST POST CLEANUP: " + request.getAction());
- if (request.isMultiPartPost())
- request.cleanupMultiPartFiles();
- request.checkValidPostState();
- }
- }
- this.fireResponseSent(request, response);
- boolean close = false;
- close |= request.getVersion().equals("HTTP/1.0");
- close |= request.hasHeader("Connection", "close");
- close |= response.hasHeader("Connection", "close");
- if (close)
- {
- // close the connection
- keepLooping = false;
- break processLoop;
- }
- }
- catch (HttpServerIOException exc)
- {
- keepLooping = false;
- break processLoop;
- }
- catch (Exception exc)
- {
- this.fireExceptionOccured(request, response, exc);
- HttpServerUtil.die(response, exc);
- }
- }
- }
- finally
- {
- try
- {
- this.fireConnectionClosed();
- }
- finally
- {
- // System.out.println("HTTP CONNECTION CLOSE");
- Streams.safeClose(this.in);
- Streams.safeClose(this.out);
- Streams.safeClose(this.socket);
- }
- }
- }
