package org.yukung.sandbox.http;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

class SimpleHttpServer {

    void start() {
        try (ServerSocket server = new ServerSocket(8080)) {
            while (true) {
                this.serverProcess(server);
            }
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }

    }

    private void serverProcess(ServerSocket server) throws IOException {
        try (
            Socket socket = server.accept();
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream()
        ) {
            HttpRequest request = new HttpRequest(in);
            HttpHeader header = request.getHeader();

            if (header.isGetMethod()) {
                File file = new File(".", header.getPath());
                if (file.exists() && file.isFile()) {
                    respondLocalFile(file, out);
                } else {
                    respondNotFoundError(out);
                }
            } else {
                respondOk(out);
            }
        } catch (EmptyRequestException e) {
            e.printStackTrace();
        }
    }

    private static void respondOk(OutputStream out) throws IOException {
        HttpResponse response = new HttpResponse(HttpStatus.OK);
        response.writeTo(out);
    }

    private static void respondNotFoundError(OutputStream out) throws IOException {
        HttpResponse response = new HttpResponse(HttpStatus.NOT_FOUND);
        response.addHeader("Content-Type", ContentType.TEXT_PLAIN);
        response.setBody("404 Not Found");
        response.writeTo(out);
    }

    private static void respondLocalFile(File file, OutputStream out) throws IOException {
        HttpResponse response = new HttpResponse(HttpStatus.OK);
        response.setBody(file);
        response.writeTo(out);
    }
}
@@ -125,32 +125,69 @@
 o.OutputStream;%0A
+import java.io.UncheckedIOException;%0A
 import java.net.
@@ -228,33 +228,189 @@
 et;%0A
-%0Aclass SimpleHttpServer %7B
+import java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.Executors;%0A%0Aclass SimpleHttpServer %7B%0A%0A    private ExecutorService service = Executors.newCachedThreadPool();
 %0A%0A  
@@ -744,26 +744,8 @@
 n %7B%0A
-        try (%0A    
     
@@ -777,24 +777,79 @@
 r.accept();%0A
+%0A        service.execute(() -%3E %7B%0A            try (%0A    
             
@@ -886,24 +886,28 @@
 utStream();%0A
+    
             
@@ -958,16 +958,20 @@
         
+    
 ) %7B%0A    
@@ -974,24 +974,28 @@
             
+ 
+   
 HttpRequest 
@@ -1025,16 +1025,20 @@
 st(in);%0A
+    
         
@@ -1091,24 +1091,28 @@
             
+    
 if (header.i
@@ -1143,16 +1143,20 @@
         
+   
+ 
 File fil
@@ -1204,24 +1204,28 @@
             
+    
 if (file.exi
@@ -1266,24 +1266,28 @@
             
+  
+  
 respondLocal
@@ -1299,24 +1299,28 @@
 file, out);%0A
+    
             
@@ -1348,24 +1348,28 @@
             
+    
 respondNotFo
@@ -1391,34 +1391,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D el
@@ -1442,24 +1442,28 @@
             
+  
+  
 respondOk(ou
@@ -1470,34 +1470,42 @@
 t);%0A            
-%7D%0A
+    %7D%0A    
         %7D catch 
@@ -1524,32 +1524,36 @@
 tException e) %7B%0A
+    
             e.pr
@@ -1566,28 +1566,326 @@
 ckTrace(
-);%0A        %7D
+System.err);%0A            %7D catch (IOException e) %7B%0A                throw new UncheckedIOException(e);%0A            %7D finally %7B%0A                try %7B%0A                    socket.close();%0A                %7D catch (IOException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A

Support the multi threading.