如何手写HTTP协议?

>>最全面的Java面试大纲及答案解析(建议收藏)  

一个典型HTTP1.1的请求协议报文结构,大体上可以分为三块,即请求行、头部、消息主体。

如何手写HTTP协议?

因为HTTP协议是在接收到数据之后才会用到的,所以我们只需要修改NioServer中的Handler就可以了,在修改后的HttpHandler中首先获取到请求报文并打印出报文的头部(包含首行)、请求的方法类型、Url和Http版本,最后将接收到的请求报文信息封装到响应报文的主体中返回给客户端。这里的HttpHandler使用了单独的线程来执行,而且把SelectionKey中操作类型的选择也放在了HttpHandler中,代码如下:

package com.company.http;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;

public class HttpServer {

    public static void main(String[] args) throws IOException {
        //创建ServerSocketChannel
        ServerSocketChannel ssc = ServerSocketChannel.open();
        //监听8080端口
        ssc.bind(new InetSocketAddress(8080));
        //设置为非阻塞模式
        ssc.configureBlocking(false);
        //为ssc注册选择器
        Selector selector = Selector.open();
        ssc.register(selector, SelectionKey.OP_ACCEPT);
        //创建处理器
        while (true) {
            //等待请求,每次等待阻塞3s,超过3s后县城继续向下运行,如果传入0或者不传参数将一直阻塞
            if (selector.select(3000) == 0) {
                continue;
            }
            //获取待处理的SelectionKey
            Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();

            while (keyIterator.hasNext()) {
                SelectionKey key = keyIterator.next();
                //启动新线程处理SelectionKey
                new Thread(new HttpHandler(key)).run();
                //处理完成后,从待处理的SelectionKey迭代器中移除当前所使用的key
                keyIterator.remove();
            }
        }
    }

    private static class HttpHandler implements Runnable {
        private int bufferSize = 1024;// 缓冲器容量
        private String localCharset = "UTF-8";// 编码格式
        private SelectionKey key;

        public HttpHandler(SelectionKey key) {
            this.key = key;
        }

        public void handleAccept() throws IOException {
            SocketChannel clientChannel = ((ServerSocketChannel) key.channel()).accept();
            clientChannel.configureBlocking(false);
            clientChannel.register(key.selector(), SelectionKey.OP_READ, ByteBuffer.allocate(bufferSize));
        }

        public void handleRead() throws IOException {
            //获取channel
            SocketChannel sc = (SocketChannel) key.channel();
            //获取buffer并重置
            ByteBuffer buffer = (ByteBuffer) key.attachment();
            buffer.clear();
            //没有读到内容则关闭
            if (sc.read(buffer) == -1) {
                sc.close();
            } else {
                //接收请求数据
                buffer.flip();
                String receivedString = Charset.forName(localCharset).newDecoder().decode(buffer).toString();
                //控制台打印请求报文头
                String[] requestMessage = receivedString.split("rn");
                for (String s : requestMessage) {
                    System.out.println(s);
                    //遇到空行说明报文头已经打印完
                    if (s.isEmpty()) {
                        break;
                    }
                }
                //控制台打印首行信息
                String[] firstLine = requestMessage[0].split(" ");
                System.out.println();
                System.out.println("Method:t" + firstLine[0]);
                System.out.println("url:t" + firstLine[1]);
                System.out.println("http version:t" + firstLine[2]);
                System.out.println();
                //返回客户端
                StringBuffer sendString = new StringBuffer();
                //响应报文首行,200表示处理成功
                sendString.append("HTTP/1.1 200 OKrn");
                sendString.append("Content-Type:text/html;charset=" + localCharset + "rn");
                //报文头结束后加一个空行
                sendString.append("rn");
                sendString.append("<html><head><title>显示报文</title></head><body>");
                sendString.append("接收到请求报文是:");
                for (String s : requestMessage) {
                    sendString.append(s + "<br/>");
                }
                sendString.append("</body></html>");
                buffer = ByteBuffer.wrap(sendString.toString().getBytes(localCharset));
                sc.write(buffer);
                sc.close();
            }
        }

        @Override
        public void run() {
            try {
                //接收到连接请求时
                if (key.isAcceptable()) {
                    handleAccept();
                }
                //读数据
                if (key.isReadable()) {
                    handleRead();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

浏览器访问http://localhost:8080,Java控制台打印如下所示:

如何手写HTTP协议?

浏览器中显示结果如下所示:

如何手写HTTP协议?


原文始发于微信公众号(一盏红茶):如何手写HTTP协议?