Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 20th, 2010 | Syntax: None | Size: 5.81 KB | Hits: 151 | Expires: Never
Copy text to clipboard
  1.    void process(int timeout) throws IOException
  2.    {
  3.       final int firstLineTimeout = 2000;
  4.  
  5.       boolean isFirstLine = true;
  6.  
  7.       try
  8.       {
  9.          this.fireConnectionInitiated();
  10.  
  11.          processLoop: while (true)
  12.          {
  13.             if (isFirstLine)
  14.             {
  15.                this.socket.setSoTimeout(firstLineTimeout);
  16.             }
  17.  
  18.             String requestLine = HttpRequest.readLineAsString(in);
  19.             if (requestLine == null)
  20.                break;
  21.  
  22.             if (isFirstLine)
  23.             {
  24.                this.socket.setSoTimeout(timeout);
  25.                isFirstLine = false;
  26.             }
  27.  
  28.             HttpRequest request = new HttpRequest(this, requestLine, in);
  29.             HttpResponse response = new HttpResponse(request, out);
  30.             request.linkResponse(response);
  31.  
  32.             this.fireRequestReceived(request, response);
  33.  
  34.             String hostname = request.getHeader("Host");
  35.             if (hostname != null)
  36.             {
  37.                hostname = Text.beforeIfAny(hostname, ':'); // strip port
  38.                request.setHost(server.getHost(hostname));
  39.             }
  40.  
  41.             try
  42.             {
  43.                if (request.getHost() == null)
  44.                {
  45.                   response.setStatusCode(StatusCode.NOT_FOUND);
  46.                   response.setContentType("text/html");
  47.                   response.setKeepAlive(false);
  48.                   response.setTransferEncodingChunked(false);
  49.                   response.setContent("404: HostNotFound: " + request.getHeader("Host"));
  50.  
  51.                   String reason = "Host not found: " + request.getHeader("Host");
  52.                   this.fireRequestDeclined(request, response, reason);
  53.  
  54.                   continue;
  55.                }
  56.  
  57.                try
  58.                {
  59.                   do // back here on 'http forward'
  60.                   {
  61.                      if (request.forwardTo != null)
  62.                         request.applyForward();
  63.  
  64.                      //   System.out.println("HTTP REQUEST: " + request.getAction());
  65.  
  66.                      // figure out http service
  67.                      HttpService service = request.getHost().getService(request);
  68.  
  69.                      if (service == null)
  70.                      {
  71.                         response.setStatusCode(StatusCode.NOT_FOUND);
  72.                         response.setContentType("text/html");
  73.                         response.setKeepAlive(false);
  74.                         response.setTransferEncodingChunked(false);
  75.                         response.setContent("404: ServiceNotFound: " + request.getAction());
  76.  
  77.                         String reason = "Service not found: " + request.getResourcePath();
  78.                         this.fireRequestDeclined(request, response, reason);
  79.  
  80.                         continue processLoop;
  81.                      }
  82.  
  83.                      request.setService(service);
  84.  
  85.                      request.getHost().doBeforeRequest(request, response);
  86.  
  87.                      try
  88.                      {
  89.                         if (request.getMethod().equals("GET"))
  90.                         {
  91.                            service.serveGET(request, response);
  92.                         }
  93.                         else if (request.getMethod().equals("POST"))
  94.                         {
  95.                            service.servePOST(request, response);
  96.                         }
  97.                         else if (request.getMethod().equals("HEAD"))
  98.                         {
  99.                            service.serveHEAD(request, response);
  100.                         }
  101.                         else
  102.                         {
  103.                            HttpService.sendMethodNotAllowed(request, response);
  104.                            this.fireRequestDeclined(request, response, "Method Not Allowed");
  105.                         }
  106.                      }
  107.                      catch (DieServiceHandling die)
  108.                      {
  109.                         // request forwards reach this code
  110.                         System.out.println("HTTP REQUEST DIED: " + request.getAction());
  111.                      }
  112.                      finally
  113.                      {
  114.                         request.getHost().doAfterRequest(request, response);
  115.                      }
  116.                   }
  117.                   while (request.forwardTo != null);
  118.                }
  119.                finally
  120.                {
  121.                   if (request.getMethod().equals("POST"))
  122.                   {
  123.                      //  System.out.println("HTTP REQUEST POST CLEANUP: " + request.getAction());
  124.                      if (request.isMultiPartPost())
  125.                         request.cleanupMultiPartFiles();
  126.                      request.checkValidPostState();
  127.                   }
  128.                }
  129.  
  130.                this.fireResponseSent(request, response);
  131.  
  132.                boolean close = false;
  133.                close |= request.getVersion().equals("HTTP/1.0");
  134.                close |= request.hasHeader("Connection", "close");
  135.                close |= response.hasHeader("Connection", "close");
  136.                if (close)
  137.                {
  138.                   // close the connection
  139.                   break;
  140.                }
  141.             }
  142.             catch (HttpServerIOException exc)
  143.             {
  144.                break;
  145.             }
  146.             catch (Exception exc)
  147.             {
  148.                this.fireExceptionOccured(request, response, exc);
  149.  
  150.                HttpServerUtil.die(response, exc);
  151.             }
  152.          }
  153.       }
  154.       finally
  155.       {
  156.          try
  157.          {
  158.             this.fireConnectionClosed();
  159.          }
  160.          finally
  161.          {
  162.             //  System.out.println("HTTP CONNECTION CLOSE");
  163.  
  164.             Streams.safeClose(this.in);
  165.             Streams.safeClose(this.out);
  166.             Streams.safeClose(this.socket);
  167.          }
  168.       }
  169.    }