com.digisky.innerproxy.server.InnerProxyHttpServerHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.digisky.innerproxy.server.InnerProxyHttpServerHandler.java

Source

/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you 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:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.digisky.innerproxy.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.CharsetUtil;

import java.io.UnsupportedEncodingException;

import com.digisky.innerproxy.processserver.ProcessServerManager;
import com.digisky.locallog.LogMgr;

import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.*;

public class InnerProxyHttpServerHandler extends SimpleChannelInboundHandler<Object> {

    private HttpRequest request;

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
        LogMgr.debug("channelRead0()", "channelRead0");
        if (msg instanceof HttpRequest) {
            HttpRequest request = this.request = (HttpRequest) msg;
            if (HttpHeaders.is100ContinueExpected(request)) {
                send100Continue(ctx);
            }
        }
        if (msg instanceof HttpContent) {
            HttpContent httpContent = (HttpContent) msg;
            ByteBuf content = httpContent.content();
            LogMgr.debug(InnerProxyHttpServerHandler.class.getName(),
                    "content:" + content.toString(CharsetUtil.UTF_8));
            String jsonmsg = null;
            try {
                jsonmsg = java.net.URLDecoder.decode(content.toString(CharsetUtil.UTF_8), "utf-8");
            } catch (UnsupportedEncodingException e) {
                LogMgr.warn("", "URLDecoder exceptionn caught.");
                // e.printStackTrace();
            }
            if (msgCheck(jsonmsg) == false) { //contain ';', bad.
                LogMgr.error(InnerProxyHttpServerHandler.class.getName(),
                        "jsonmsg contain ; or ', close it. peer info:" + ctx.channel().remoteAddress().toString());
                ctx.channel().close();
                return;
            }
            LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "jsonmsg:" + jsonmsg);
            LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "uri:" + request.getUri());
            LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "method:" + request.getMethod());
            String type = request.getUri().split("/")[1];
            if (type.equals("email_activate") == true) { // GET?
                String[] tmp = request.getUri().split("=");
                String acode = tmp[tmp.length - 1];
                LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "acode:" + acode);
                ProcessServerManager.getInstance().process(ctx.channel(), type, "{\"acode\":\"" + acode + "\"}");
            } else { //POST
                LogMgr.debug(InnerProxyHttpServerHandler.class.getName(), "type:" + type);
                // ugly code
                ProcessServerManager.getInstance().process(ctx.channel(), type, jsonmsg.replace("val=", ""));
            }
        }
    }

    private static void send100Continue(ChannelHandlerContext ctx) {
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, CONTINUE);
        ctx.write(response);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        LogMgr.info(InnerProxyHttpServerHandler.class.getName(), "exceptionCaugt function called!");
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //      LogMgr.debug("", "channelActive called. peer ip_port:"
        //            + ctx.channel().remoteAddress());
    }

    //   private static void ResponseIllegalParameters(ChannelHandlerContext ctx) {
    //      return;
    //   }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) {
        LogMgr.debug("", "channelRegistered called. peer ip_port:" + ctx.channel().remoteAddress());
    }

    public void channelUnregistered(ChannelHandlerContext ctx) {
        LogMgr.debug("", "channelUnregistered called. peer ip_port:" + ctx.channel().remoteAddress());
    }

    //no ';' '\''
    private boolean msgCheck(String msg) {
        int i = msg.indexOf(';');
        if (i != -1) {
            return false;
        }
        i = msg.indexOf('\'');
        if (i != -1) {
            return false;
        }
        return true;
    }
}