package com.hibegin.http.server.handler;

import com.hibegin.common.util.LoggerUtil;
import com.hibegin.http.server.api.*;
import com.hibegin.http.server.config.ResponseConfig;
import com.hibegin.http.server.impl.HttpMethod;
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 HttpRequestHandlerThread extends Thread {

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

    private HttpRequest request;
    private ServerContext serverContext;

    private HttpResponse response;
    private SocketChannel channel;

    public HttpRequestHandlerThread(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 {
            if (!serverContext.getServerConfig().getHttpRequestListenerList().isEmpty()) {
                for (HttpRequestListener httpRequestListener : serverContext.getServerConfig().getHttpRequestListenerList()) {
                    httpRequestListener.create(request, response);
                }
            }
            for (Interceptor interceptor : serverContext.getInterceptors()) {
                if (!interceptor.doInterceptor(request, response)) {
                    break;
                }
            }
        } catch (Exception e) {
            LOGGER.log(Level.SEVERE, "dispose error ", e);
        } finally {
            if (request.getMethod() != HttpMethod.CONNECT) {
                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.getMethod() + ": " + request.getUrl() + " " + (System.currentTimeMillis() - request.getCreateTime()) + " ms");
                serverContext.getHttpDeCoderMap().remove(channel);
            }
        }
    }

    public HttpRequest getRequest() {
        return request;
    }

    public HttpResponse getResponse() {
        return response;
    }
}

@@ -2778,16 +2778,221 @@
 annel);%0A
+                for (HttpRequestListener requestListener : serverContext.getServerConfig().getHttpRequestListenerList()) %7B%0A                    requestListener.destroy(request, response);%0A                %7D%0A
         

add requestListener