2

我想在netty中制作一种日志代理。目标是能够让 Web 浏览器向 netty 服务器发出 HTTP 请求,将它们传递到后端 Web 服务器,还能够基于 HTTP 特定的事物采取某些行动。

有几个有用的网络示例,HexDumpProxy(它执行代理部分,与协议无关),我从 HttpSnoopServerHandler 中获取了一些代码。

我的代码现在看起来像这样: HexDumpProxyInboundHandler 可以在http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/proxy/HexDumpProxyInboundHandler.html找到

//in HexDumpProxyPipelineFactory
  public ChannelPipeline getPipeline() throws Exception {
      ChannelPipeline p = pipeline(); // Note the static import.
      p.addLast("handler", new HexDumpProxyInboundHandler(cf, remoteHost, remotePort));

      p.addLast("decoder", new HttpRequestDecoder());
      p.addLast("handler2", new HttpSnoopServerHandler());
      return p;
  }


//HttpSnoopServerHandler
public class HttpSnoopServerHandler extends SimpleChannelUpstreamHandler {
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    HttpRequest request = (HttpRequest) e.getMessage();
    System.out.println(request.getUri());
    //going to do things based on the URI
  }
}

不幸的是,HttpSnoopServerHandler 中的 messageReceived 从未被调用 - 看起来 HexDumpProxyInboundHandler 消耗了所有事件。

我怎么能有两个处理程序,其中一个需要解码器,但另一个不需要(我宁愿拥有 HexDumpProxy,它不需要理解 HTTP,它只是代理所有连接,但我的 HttpSnoopHandler前面需要有HttpRequestDecoder)?

4

1 回答 1

1

I've not tried it but you could extend HexDumpProxyInboundHandler and override messageReceived with something like

super.messageReceived(ctx, e);
ctx.sendUpstream(e);

Alternatively you could modify HexDumpProxyInboundHandler directly to that the last thing messageReceived does is call super.messageReceived(ctx,e).

This would only work for inbound data from the client. Data from the service you're proxy-ing would still be passed through without you code seeing it.

于 2012-04-18T08:10:01.397 回答