nettyhtp高性能服务器,netty高性能http服务

package io.netty.example.http.snoop;

import static io.netty.handler.codec.http.HttpHeaders.getHost;

import static io.netty.handler.codec.http.HttpHeaders.isKeepAlive;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;

import static io.netty.handler.codec.http.HttpHeaders.Names.COOKIE;

import static io.netty.handler.codec.http.HttpHeaders.Names.SET_COOKIE;

import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;

import static io.netty.handler.codec.http.HttpResponseStatus.OK;

import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.Map.Entry;

import io.netty.buffer.ByteBuf;

import io.netty.buffer.Unpooled;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;

import io.netty.handler.codec.http.Cookie;

import io.netty.handler.codec.http.CookieDecoder;

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.HttpObject;

import io.netty.handler.codec.http.HttpRequest;

import io.netty.handler.codec.http.LastHttpContent;

import io.netty.handler.codec.http.QueryStringDecoder;

import io.netty.handler.codec.http.ServerCookieEncoder;

import io.netty.util.CharsetUtil;

public class HttpSnoopServiceTxt extends SimpleChannelInboundHandler {

private HttpRequest request;

/** Buffer that stores the response content */

private final StringBuilder buf = new StringBuilder();

@Override

protected void channelRead0(ChannelHandlerContext ctx, Object msg)

throws Exception {

// TODO Auto-generated method stub

if (msg instanceof HttpRequest) {

HttpRequest request = this.request = (HttpRequest) msg;

buf.setLength(0);

// hostname

buf.append("HOSTNAME:").append(getHost(request, "unknown"));

// url

buf.append("REQUEST_URI:").append(request.getUri());

// parm

QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());

Map> params = queryStringDecoder.parameters();

if (!params.isEmpty()) {

for (Entry> p : params.entrySet()) {

String key = p.getKey();

List vals = p.getValue();

for (String val : vals) {

buf.append("PARAM:").append(key).append("=")

.append(val);

}

}

}

//cookie

}

if (msg instanceof HttpContent) {

if (msg instanceof LastHttpContent) {

LastHttpContent trailer = (LastHttpContent) msg;

writeResponse(trailer, ctx);

WriterFile.printtxt(buf.toString());

}

}

}

@Override

public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

ctx.flush();

}

private boolean writeResponse(HttpObject currentObj,ChannelHandlerContext ctx) {

boolean keepAlive = isKeepAlive(request);

FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,

currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,

Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));

response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");

if (keepAlive) {

response.headers().set(CONTENT_LENGTH,

response.content().readableBytes());

response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);

}

ctx.write(response);

return keepAlive;

}

}

/*

* 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 io.netty.example.http.snoop;

import io.netty.bootstrap.ServerBootstrap;

import io.netty.channel.Channel;

import io.netty.channel.EventLoopGroup;

import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.nio.NioServerSocketChannel;

/**

* An HTTP server that sends back the content of the received HTTP request

* in a pretty plaintext form.

*/

public class HttpSnoopServer {

private final int port;

public HttpSnoopServer(int port) {

this.port = port;

}

public void run() throws Exception {

// Configure the server.

EventLoopGroup bossGroup = new NioEventLoopGroup();

EventLoopGroup workerGroup = new NioEventLoopGroup();

try {

ServerBootstrap b = new ServerBootstrap();

b.group(bossGroup, workerGroup)

.channel(NioServerSocketChannel.class)

.childHandler(new HttpSnoopServerInitializer());

Channel ch = b.bind(port).sync().channel();

ch.closeFuture().sync();

} finally {

bossGroup.shutdownGracefully();

workerGroup.shutdownGracefully();

}

}

public static void main(String[] args) throws Exception {

int port;

if (args.length > 0) {

port = 8080;

} else {

port = 8080;

}

new HttpSnoopServer(port).run();

}

}

/*

* 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 io.netty.example.http.snoop;

import io.netty.channel.ChannelInitializer;

import io.netty.channel.ChannelPipeline;

import io.netty.channel.socket.SocketChannel;

import io.netty.handler.codec.http.HttpRequestDecoder;

import io.netty.handler.codec.http.HttpResponseEncoder;

public class HttpSnoopServerInitializer extends ChannelInitializer {

@Override

public void initChannel(SocketChannel ch) throws Exception {

// Create a default pipeline implementation.

ChannelPipeline p = ch.pipeline();

// Uncomment the following line if you want HTTPS

//SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();

//engine.setUseClientMode(false);

//p.addLast("ssl", new SslHandler(engine));

p.addLast("decoder", new HttpRequestDecoder());

// Uncomment the following line if you don't want to handle HttpChunks.

//p.addLast("aggregator", new HttpObjectAggregator(1048576));

p.addLast("encoder", new HttpResponseEncoder());

// Remove the following line if you don't want automatic content compression.

//p.addLast("deflater", new HttpContentCompressor());

p.addLast("handler", new HttpSnoopServiceTxt());

//p.addLast("handler", new HttpSnoopServerHandler());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值