深入剖析Tomcat之ERR_INVALID_HTTP_RESPONSE

ERR_INVALID_HTTP_RESPONSE

  • 前言
  • 前置知识
    • HTTP
      • HTTP请求
        • 请求:
        • 响应:
    • Socket类
  • 问题简述
    • 问题代码
    • 错误排查
    • 最终代码

前言

前段时间刚学完《Java网络编程》,最近着手学习《深入剖析Tomcat》,但是这里第一个案例就出现了问题。建议稍微有点网络基础的同学看。

书上源码多自己思考,根据已有知识排错。

前置知识

HTTP

  • 基于可靠TCP建立连接。
  • 发送请求、响应请求
  • 断开连接

HTTP请求

请求:

  • 请求方法、URI、协议版本
  • 请求头
  • 实体

请求头和请求体之间有一个空行。

响应:

  • 协议、状态码、描述
  • 响应头
  • 响应实体

响应头和响应体之火箭有一个空行

Socket类

不细说了,参见net模块的笔记。

问题简述

问题代码

package chapter01.demo01;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.util.Objects;

/**
 * @description HTTP服务器
 * 阻塞的监听指定的应用程序,如果发现接收到请求就解析成Request
 * 如果uri是关闭,就不再循环;否则将对应的uri资源封装成response返回
 *
 * @date:2022/11/3 15:52
 * @author: qyl
 */
public class HttpServer {
    // 指定资源路径
    public static final String WEB_ROOT = Objects.requireNonNull(HttpServer.class.getClassLoader().getResource("webroot")).getPath();
    private static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
    private boolean shutdown = false;

    public static void main(String[] args) {
        HttpServer server = new HttpServer();
        server.await();
    }

    private void await() {
        int port = 8080;
        try (ServerSocket serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"))){

            while (!shutdown) {
                try (Socket socket = serverSocket.accept()) {
                    // 解析请求
                    InputStream input = socket.getInputStream();
                    Request request = new Request(input);
                    request.parse();
                    // 封装响应
                    OutputStream output = socket.getOutputStream();
                    Response response = new Response(output);
                    response.setRequest(request);
                    // 发送响应
                    response.sendStaticResource();
                    shutdown = request.getUri().endsWith(SHUTDOWN_COMMAND);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

```java
package chapter01.demo01;

import java.io.*;
import java.net.URLConnection;

/**
 * @description
 * @date:2022/11/3 16:10
 * @author: qyl
 */
public class Response {
    private static final int BUFFER_SIZE = 1024;
    Request request;
    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];
        FileInputStream fis = null;
        try{
            String filename = request.getUri();
            File file = new File(HttpServer.WEB_ROOT,filename);
            if (file.exists()){
                String mimeType = URLConnection.getFileNameMap().getContentTypeFor(filename);

                fis = new FileInputStream(file);
                int ch ;
                while ((ch = fis.read(bytes,0,BUFFER_SIZE)) != -1 ){
                    output.write(bytes,0,ch);
                }
            }else {
                String errorMessage = "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

"
; output.write(errorMessage.getBytes()); } } catch (IOException e) { e.printStackTrace(); }finally { if (fis != null){ fis.close(); } } } }
package chapter01.demo01;

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

/**
 * @description
 * @date:2022/11/3 16:01
 * @author: qyl
 */
public class Request {
    private InputStream input;
    private String uri;

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

    public void parse() {
        StringBuilder request = new StringBuilder(2048);
        int i;
        byte[] buffer = new byte[2048];
        try {
            i = input.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            i = -1;
        }
        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;
    }
}

之后启动HttpServer,并且打开浏览器输入http://localhost:8080/index.html出现了如下错误:
深入剖析Tomcat之ERR_INVALID_HTTP_RESPONSE_第1张图片

错误排查

明确表示了响应无效,根据前置知识:请求成功解析并封装,那么我们查看Response类。

有两种可能

  • 文件找不到,我们的错误响应写错了。即如下这代码:
    深入剖析Tomcat之ERR_INVALID_HTTP_RESPONSE_第2张图片
    故进行如下测试,发现没有问题:
    深入剖析Tomcat之ERR_INVALID_HTTP_RESPONSE_第3张图片

  • 找到了文件,但是响应写错了。即如下代码
    深入剖析Tomcat之ERR_INVALID_HTTP_RESPONSE_第4张图片
    其实很明显就有问题,根据前置知识,这里只有响应体,没有响应头。
    添加响应头内容,进行测试,成功解决。
    深入剖析Tomcat之ERR_INVALID_HTTP_RESPONSE_第5张图片

最终代码

package chapter01.demo01;

import java.io.*;
import java.net.URLConnection;

/**
 * @description
 * @date:2022/11/3 16:10
 * @author: qyl
 */
public class Response {
    private static final int BUFFER_SIZE = 1024;
    Request request;
    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];
        FileInputStream fis = null;
        try{
            String filename = request.getUri();
            File file = new File(HttpServer.WEB_ROOT,filename);
            if (file.exists()){
                String mimeType = URLConnection.getFileNameMap().getContentTypeFor(filename);
                String header = "HTTP/1.1 200 OK\r\n"
                        + "Server: OneFile 2.0\r\n"
                        + "Content-type: " + mimeType + ";\r\n\r\n";
                output.write(header.getBytes());
                fis = new FileInputStream(file);
                int ch ;
                while ((ch = fis.read(bytes,0,BUFFER_SIZE)) != -1 ){
                    output.write(bytes,0,ch);
                }
            }else {
                String errorMessage = "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

"
; output.write(errorMessage.getBytes()); } } catch (IOException e) { e.printStackTrace(); }finally { if (fis != null){ fis.close(); } } } }

你可能感兴趣的:(java,计算机网络,tomcat,http,java)