/*******************************************************************************
 * Copyright (c) 2024-2025 R3944Realms. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 *     http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.r3944realms.dg_lab.websocket.handler.server;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The type Http request handler.
 */
public class HttpRequestHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    private static final Logger logger = LoggerFactory.getLogger(HttpRequestHandler.class);
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
        // 读取原始请求的 URI
        String requestUri = request.uri();
        // 修改 URI，去掉路径部分，只保留 "/"
        String modifiedUri = "/";

        // 检查是否是 WebSocket 升级请求
        String upgradeHeader = request.headers().get(HttpHeaderNames.UPGRADE);
        String webSocketVersion = request.headers().get(HttpHeaderNames.SEC_WEBSOCKET_VERSION);

        if ("WebSocket".equalsIgnoreCase(upgradeHeader) &&
                "13".equals(webSocketVersion)) {  // 注意：WebSocket 版本通常为 "13"

            // 创建一个新的 FullHttpRequest 实例，修改 URI
            FullHttpRequest modifiedRequest = new DefaultFullHttpRequest(
                    HttpVersion.HTTP_1_1,
                    request.method(),
                    modifiedUri,
                    request.content().retain(),  // 保留内容以避免意外释放
                    request.headers().copy(),
                    request.trailingHeaders().copy()
            );

            // 将修改后的 URI 设置到上下文中
            ctx.channel().attr(AttributeKey.valueOf("ws-uri")).set(modifiedUri);

            // 传递修改后的请求到下一个处理器
            ctx.fireChannelRead(modifiedRequest);
        } else {
            // 如果不是 WebSocket 升级请求，返回拒绝
            sendForbiddenResponse(ctx, request);
        }
    }
    private void sendForbiddenResponse(ChannelHandlerContext ctx, FullHttpRequest request) {
        FullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN);

        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
        response.content().writeBytes("Access denied".getBytes());

        // Write the response and close the connection
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        logger.error(cause.getMessage(), cause);
        ctx.close();
    }
}
