利用Socket自己实现HTTP协议

利用Socket动手实现简单HTTP协议

一、实现

我们知道HTTP协议是在应用层解析内容的,所以我们只需要按照它的报文格式封装和解析数据即可。

关于具体的传输协议,使用的还是Socket,关于Socket,可以移步这篇文章:Java中的Socket你了解吗

这里我将带领大家使用NioServer实现一个简单的HTTP协议,注意,这里主要是出于了解HTTP协议是如何实现的,而不是真正实现一个HTTP协议。

因为HTTP协议是在接收到数据之后才会用到的,所以,我们只需要在Handler处理方法中实现就可以了。

我们首先来看一下效果。

当我们启动程序,然后在浏览器中输入http://localhost:8080/发起请求,这时控制台会打印如下信息:

GET / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9

Method: GET
url: /
HTTP Version: HTTP/1.1

GET /favicon.ico HTTP/1.1
Host: localhost:8080
Connection: keep-alive
sec-ch-ua: "Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
sec-ch-ua-platform: "Windows"
Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: image
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9

Method: GET
url: /favicon.ico
HTTP Version: HTTP/1.1

浏览器显示结果如图所示:

image-20240115221130814

这里只是一个简单的示例,目的是让大家了解HTTP协议中的实现方法,功能不够完善,而且也不能实现请求的处理。

对于了解HTTP协议实现的方法,个人觉得还是有必要看看的。

二、完整代码

思路:

整个过程非常简单,我们只需要按照报文的格式来读取和发送就可以了。

  • 接收到数据后按\r\n分割成每一行,空行前面都是报文头;
  • 空行下面如果有内容就是报文主体,注意,Get请求没有报文主体;

完整代码如下:

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;

/**
* @Author zal
* @Date 2024/01/15 21:10
* @Description: 基于NioServer实现简单HTTP协议
* @Version: 1.0
*/
public class NioServer {
public static void main(String[] args) throws Exception {
// 创建ServerSocketChannel,并监听8080端口
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().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;
}

/**
* 处理接受连接事件
*
* @throws IOException
*/
public void handleAccept() throws IOException {
// 通过服务器套接字通道接受客户端连接
SocketChannel sc = ((ServerSocketChannel) key.channel()).accept();
// 配置为非阻塞模式
sc.configureBlocking(false);
// 将客户端套接字通道注册到选择器,关注事件为可读,同时附带一个缓冲区
sc.register(key.selector(), SelectionKey.OP_READ, ByteBuffer.allocate(bufferSize));
}

/**
* 处理读取数据事件
*
* @throws IOException
*/
public void handleRead() throws IOException {
// 获取Channel
SocketChannel sc = (SocketChannel) key.channel();
// 获取附加到事件的缓冲区
ByteBuffer buffer = (ByteBuffer) key.attachment();
buffer.clear(); // 清空缓冲区,准备读取数据
// 从客户端通道读取数据到缓冲区,如果返回-1表示客户端关闭连接
if (sc.read(buffer) == -1) {
// 关闭channel
sc.close();
} else {
// 切换buffer为读模式
buffer.flip();
// 将buffer中的数据解码为字符串后保存到receivedString
String receivedString = Charset.forName(localCharset).newDecoder().decode(buffer).toString();
// 控制台打印请求报文头
String[] requsetMessage = receivedString.split("\r\n");
for (String s : requsetMessage) {
System.out.println(s);
// 遇到空行说明报文头已经打印完成
if (s.isEmpty()) {
break;
}
}
// 控制台打印首行信息
String[] firstLine = requsetMessage[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();
// 响应报文首行
sendString.append("HTTP/1.1 200 OK\r\n");
sendString.append("Content-Type:text/html;charset=" + localCharset + "\r\n");
sendString.append("\r\n");

sendString.append("<html><head><title>显示报文</title></head></body>");
sendString.append("接收到的请求报文是:<br/>");
for (String s : requsetMessage) {
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 e) {
e.printStackTrace();
}
}
}
}