首页 > 学院 > 开发设计 > 正文

Mina、Netty、Twisted一起学(八):HTTP服务器

2019-11-14 21:12:22
字体:
来源:转载
供稿:网友
Mina、Netty、Twisted一起学(八):HTTP服务器

HTTP协议应该是目前使用最多的应用层协议了,用浏览器打开一个网站就是使用HTTP协议进行数据传输。

HTTP协议也是基于TCP协议,所以也有服务器和客户端。HTTP客户端一般是浏览器,当然还有可能是其他东西。HTTP服务器,也就是Web服务器,目前已经有很多成熟的产品,例如Apache HTTP Server、Tomcat、Nginx、IIS等。

本文的内容不是讲解如何使用以上的HTTP服务器,而是要分别用MINA、Netty、Twisted实现一个简单的HTTP服务器。

首先,要简单了解一下HTTP协议。

HTTP协议是请求/响应式的协议,客户端需要发送一个请求,服务器才会返回响应内容。例如在浏览器上输入一个网址按下Enter,或者提交一个Form表单,浏览器就会发送一个请求到服务器,而打开的网页的内容,就是服务器返回的响应。

下面了解一下HTTP请求和响应包含的内容。

HTTP请求有很多种method,最常用的就是GET和POST,每种method的请求之间会有细微的区别。下面分别分析一下GET和POST请求。

GET请求:

下面是浏览器对http://localhost:8081/test?name=XXG&age=23的GET请求时发送给服务器的数据:

可以看出请求包含request line和header两部分。其中request line中包含method(例如GET、POST)、request uri和PRotocol version三部分,三个部分之间以空格分开。request line和每个header各占一行,以换行符CRLF(即/r/n)分割。

POST请求:

下面是浏览器对http://localhost:8081/test的POST请求时发送给服务器的数据,同样带上参数name=XXG&age=23:

可以看出,上面的请求包含三个部分:request line、header、message,比之前的GET请求多了一个message body,其中header和message body之间用一个空行分割。POST请求的参数不在URL中,而是在message body中,header中多了一项Content-Length用于表示message body的字节数,这样服务器才能知道请求是否发送结束。这也就是GET请求和POST请求的主要区别。

HTTP响应和HTTP请求非常相似,HTTP响应包含三个部分:status line、header、massage body。其中status line包含protocol version、状态码(status code)、reason phrase三部分。状态码用于描述HTTP响应的状态,例如200表示成功,404表示资源未找到,500表示服务器出错。

HTTP响应:

在上面的HTTP响应中,Header中的Content-Length同样用于表示message body的字节数。Content-Type表示message body的类型,通常浏览网页其类型是HTML,当然还会有其他类型,比如图片、视频等。

学习了HTTP协议后,那么就可以分别通过MINA、Netty、Twisted实现针对请求的解码器和针对响应的编码器来实现一个HTTP服务器。实际上HTTP协议的细节还有很多,自己实现起来没那么容易。不过,MINA、Netty、Twisted都已经提供了针对HTTP协议的编码解码器和一些实用的API。

下面分别用MINA、Netty、Twisted来实现一个HTTP服务器,用浏览器访问:

http://localhost:8080/?name=叉叉哥

就可以打开一个页面,将参数显示在页面上:

MINA:

MINA中有一个mina-http-2.0.7.jar包,专门用于处理HTTP协议。在下面的代码中,需要将这个jar包引入到项目中。

HTTP协议的请求解码器和响应编码器即HttpServerCodec,它会将HTTP客户端请求转成HttpRequest对象,将HttpResponse对象编码成HTTP响应发送给客户端。需要注意的是,HttpRequest和HttpResponse的实现类对象都没有包含message body部分,所以下面代码中body还通过原始的IoBuffer类型来构造。

public class HttpServer {    public static void main(String[] args) throws IOException {        IoAcceptor acceptor = new NioSocketAcceptor();        acceptor.getFilterChain().addLast("codec", new HttpServerCodec());        acceptor.setHandler(new HttpServerHandle());        acceptor.bind(new InetSocketAddress(8080));    }}class HttpServerHandle extends IoHandlerAdapter {        @Override    public void exceptionCaught(Iosession session, Throwable cause)            throws Exception {        cause.printStackTrace();    }    @Override    public void messageReceived(IoSession session, Object message)            throws Exception {                if (message instanceof HttpRequest) {                        // 请求,解码器将请求转换成HttpRequest对象            HttpRequest request = (HttpRequest) message;                        // 获取请求参数            String name = request.getParameter("name");            name = URLDecoder.decode(name, "UTF-8");            // 响应HTML            String responseHtml = "<html><body>Hello, " + name + "</body></html>";            byte[] responseBytes = responseHtml.getBytes("UTF-8");            int contentLength = responseBytes.length;                        // 构造HttpResponse对象,HttpResponse只包含响应的status line和header部分            Map<String, String> headers = new HashMap<String, String>();            headers.put("Content-Type", "text/html; charset=utf-8");            headers.put("Content-Length", Integer.toString(contentLength));            HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK, headers);                        // 响应BODY            IoBuffer responseIoBuffer = IoBuffer.allocate(contentLength);            responseIoBuffer.put(responseBytes);            responseIoBuffer.flip();                        session.write(response); // 响应的status line和header部分            session.write(responseIoBuffer); // 响应body部分        }    }}

Netty:

Netty和MINA非常类似。唯一有区别的地方就是FullHttpResponse包含响应的message body。

public class HttpServer {    public static void main(String[] args) throws InterruptedException {        EventLoopGroup bossGroup = new NioEventLoopGroup();        EventLoopGroup workerGroup = new NioEventLoopGroup();        try {            ServerBootstrap b = new ServerBootstrap();            b.group(bossGroup, workerGroup)                    .channel(NioServerSocketChannel.class)                    .childHandler(new ChannelInitializer<SocketChannel>() {                        @Override                        public void initChannel(SocketChannel ch) throws Exception {                            ChannelPipeline pipeline = ch.pipeline();                            pipeline.addLast(new HttpServerCodec());                            pipeline.addLast(new HttpServerHandler());                        }                    });            ChannelFuture f = b.bind(8080).sync();            f.channel().closeFuture().sync();        } finally {            workerGroup.shutdownGracefully();            bossGroup.shutdownGracefully();        }    }}class HttpServerHandler extends ChannelInboundHandlerAdapter {    @Override    public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {                if (msg instanceof HttpRequest) {                        // 请求,解码器将请求转换成HttpRequest对象            HttpRequest request = (HttpRequest) msg;                        // 获取请求参数            QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());            String name = queryStringDecoder.parameters().get("name").get(0);                                    // 响应HTML            String responseHtml = "<html><body>Hello, " + name + "</body></html>";            byte[] responseBytes = responseHtml.getBytes("UTF-8");            int contentLength = responseBytes.length;                        // 构造FullHttpResponse对象,FullHttpResponse包含message body            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes));            response.headers().set("Content-Type", "text/html; charset=utf-8");            response.headers().set("Content-Length", Integer.toString(contentLength));            ctx.writeAndFlush(response);        }    }    @Override    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {        cause.printStackTrace();        ctx.close();    }}

Twisted:

Twisted的HTTP相比MINA、Netty来说功能最完善。Twisted不但包含HTTP协议的编码器和解码器以及相关API,还提供了一整套Web应用解决方案。想完整学习的话可以参考官方文档。

# -*- coding:utf-8 –*-from twisted.web import server, resourcefrom twisted.internet import reactorclass MainResource(resource.Resource):        isLeaf = True        # 用于处理GET类型请求    def render_GET(self, request):                # name参数        name = request.args['name'][0]                # 设置响应编码        request.responseHeaders.addRawHeader("Content-Type", "text/html; charset=utf-8")                        # 响应的内容直接返回        return "<html><body>Hello, " + name + "</body></html>"site = server.Site(MainResource())reactor.listenTCP(8080, site)reactor.run()


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表