Share Pastebin
Guest
Public paste!

Untitled

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