package com.hibegin.http.server.handler;

import com.hibegin.common.util.LoggerUtil;
import com.hibegin.http.server.api.HttpRequest;
import com.hibegin.http.server.api.HttpRequestDeCoder;
import com.hibegin.http.server.api.HttpResponse;
import com.hibegin.http.server.api.Interceptor;
import com.hibegin.http.server.config.ResponseConfig;
import com.hibegin.http.server.impl.ServerContext;
import com.hibegin.http.server.impl.SimpleHttpResponse;

import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;

public class HttpRequestHandler extends Thread {

    private static final Logger LOGGER = LoggerUtil.getLogger(HttpRequestHandler.class);

    private HttpRequest request;
    private ServerContext serverContext;

    private HttpResponse response;
    private SocketChannel channel;

    public HttpRequestHandler(HttpRequestDeCoder codec, ResponseConfig responseConfig, ServerContext serverContext) {
        this.serverContext = serverContext;
        this.request = codec.getRequest();
        this.response = new SimpleHttpResponse(codec.getRequest(), responseConfig);
        this.channel = codec.getRequest().getHandler().getChannel();
    }

    @Override
    public void run() {
        try {
            for (Interceptor interceptor : serverContext.getInterceptors()) {
                if (!interceptor.doInterceptor(request, response)) {
                    break;
                }
            }
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "dispose error ", e);
        } finally {
            boolean keepAlive = request.getHeader("Connection") != null && "keep-alive".equalsIgnoreCase(request.getHeader("Connection"));
            if (keepAlive) {
                keepAlive = response.getHeader().get("Connection") != null && !"close".equalsIgnoreCase(response.getHeader().get("Connection"));
            }
            if (!keepAlive) {
                Socket socket = channel.socket();
                // 渲染错误页面
                if (!socket.isClosed()) {
                    LOGGER.log(Level.WARNING, "forget close stream " + socket.toString());
                    response.renderCode(404);
                }
            }
            LOGGER.info(request.getUrl() + " " + (System.currentTimeMillis() - request.getCreateTime()) + " ms");
            serverContext.getHttpDeCoderMap().remove(channel);
        }
    }

    public HttpRequest getRequest() {
        return request;
    }

    public HttpResponse getResponse() {
        return response;
    }
}

@@ -332,16 +332,64 @@
 Config;%0A
+import com.hibegin.http.server.impl.HttpMethod;%0A
 import c
@@ -1634,24 +1634,89 @@
 %7D finally %7B%0A
+            if (request.getMethod() != HttpMethod.CONNECT) %7B%0A    
             
@@ -1850,24 +1850,28 @@
             
+ 
+   
 if (keepAliv
@@ -1867,32 +1867,36 @@
 f (keepAlive) %7B%0A
+    
                 
@@ -2028,34 +2028,42 @@
 ));%0A            
-%7D%0A
+    %7D%0A    
             if (
@@ -2092,16 +2092,20 @@
         
+  
+  
 Socket s
@@ -2146,16 +2146,20 @@
         
+    
 // %E6%B8%B2%E6%9F%93%E9%94%99%E8%AF%AF%E9%A1%B5
@@ -2172,24 +2172,28 @@
             
+  
+  
 if (!socket.
@@ -2202,24 +2202,28 @@
 Closed()) %7B%0A
+    
             
@@ -2321,16 +2321,20 @@
         
+  
+  
 response
@@ -2359,34 +2359,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -2383,32 +2383,36 @@
               %7D%0A
+    
             LOGG
@@ -2521,16 +2521,20 @@
         
+  
+  
 serverCo
@@ -2576,16 +2576,30 @@
 annel);%0A
+            %7D%0A
         

optimize connect deal way