【Tomcat8.5源码分析】【3】一个简单的HTTP服务器

     通过上面两篇文章的学习,我们知道了HTTP\SOCK\TCP\IP之间的关系以及区别,接下来我们手写一个HTTP服务器

      首先我们定义一个HttpServer类,该类主要用以创建一个ServerSocket,当有请求进入的时候会通过accept()方法生成一个套接字-Socket对象,然后通过Socket对象的输入输出流进行读取请求数据,并返回请求结果。封装的输入流对象为Request,封装的输出流对象为Response,并且在最后如果输入的请求里包含./shutdown,则关闭服务器。

     代码如下:HttpServer类

package cn.tomcat;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @description:
 * @author: gongqi
 * @create: 2018/11/07 15:09
 */
public class HttpServer {
    //是否关闭服务器
    private boolean shutdown = false;
    //监听的端口号
    private Integer port = 9011;
    //资源所在的根路径
    public static final String WEB_ROOT = System.getProperty("user.dir") + File.separator + "webroot";
    //停止服务的命令
    private static final String SHUTDOWN_CMD = "./shutdown";

    
    /**
    *启动主类
    */
    public static void main(String[] args) {
        HttpServer server = new HttpServer();
        server.await();
    }
    
    /**
    *等待请求
    */
    public void await() {

        //
        ServerSocket serverSocket = null;
        try {
            //启动一个ServerSocket并监听对应的端口号
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //当未收到关闭命令的时候一直循环接受请求
        while (!shutdown) {

            Socket socket;
            InputStream inputStream;
            OutputStream outputStream;

            System.out.println(WEB_ROOT);
            //
            try {
                //此方法是一个阻塞方法
                //当有请求进来的时候会生成一个Socket对象
                socket = serverSocket.accept();
                //获取输入流
                inputStream = socket.getInputStream();
                //获取输出流
                outputStream = socket.getOutputStream();
                
                //封装输入流
                Request request = new Request(inputStream);
                request.parse();
                
                //封装输出流
                Response response = new Response(outputStream);
                response.setRequest(request);
                response.sendStaticResource();
                //关闭当次Socket连接
                socket.close();

                shutdown = SHUTDOWN_CMD.equals(request.getUri());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
Request类
package cn.tomcat;


import java.io.IOException;
import java.io.InputStream;

/**
 * @description:
 * @author: gongqi
 * @create: 2018/11/07 16:20
 */
public class Request {

    private InputStream input;
    private String uri;

    public Request(InputStream input) {
        this.input = input;
    }

    public void parse() {
        //
        StringBuffer request = new StringBuffer(2048);
        int i;
        byte[] buffer = new byte[2048];
        try {
            //在输入流里读取2048个byte,方便起见姑且先死2048吧
            i = input.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            i = -1;
        }
        //循环byte数组,然后一个字符一个字符的刷入StringBuffer
        for (int j = 0; j < i; j++) {
            request.append((char)buffer[j]);
        }
        //打印出请求的内容
        System.out.println(request.toString());
        uri = parseUri(request.toString());
    }
    

    private String parseUri(String requestString) {
        int index1, index2;
        index1 = requestString.indexOf(' ');
        if (index1 != -1) {
            index2 = requestString.indexOf(' ', index1 + 1);
            if (index2 > index1) {
                return requestString.substring(index1 + 1, index2);

            }
        }
        return null;
    }

    public String getUri() {
        return uri;
    }


}

 

Response类:

package cn.tomcat;

import java.io.*;

/**
 * @description:
 * @author: gongqi
 * @create: 2018/11/07 16:36
 */
public class Response {

    private static final int BUFFER_SIZE = 1024;
    private Request request;
    private OutputStream output;

    public Response(OutputStream output) {
        this.output = output;
    }

    public void setRequest(Request request) {
        this.request = request;
    }


    public void sendStaticResource() throws IOException {
        byte[] bytes = new byte[BUFFER_SIZE];
        File file = new File(HttpServer.WEB_ROOT + request.getUri());
        if (file.exists()) {
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
                int ch = fileInputStream.read(bytes, 0, BUFFER_SIZE);
                while (ch != -1) {
                    output.write(bytes, 0, ch);
                    ch=fileInputStream.read(bytes,0,BUFFER_SIZE);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            }
        } else {
            String mesg = "HTTP/1.1 404 File not found\r\n"
                    + "Content-Type: text/html\r\n"
                    + "Content-Length: 23\r\n"
                    + "\r\n"
                    + "

File not found

"; // try { output.write(mesg.getBytes()); } catch (IOException e) { e.printStackTrace(); } } } }

对应的HTML:




    


你好哇,你成功访问到我了!

 

这样一个简单的HTTP服务器就写好啦,拿去跑一下看吧,哈哈哈哈!!

 

你可能感兴趣的:(tomcat源码分析)