深入剖析Tomcat之ERR_INVALID_HTTP_RESPONSE

前言

前段时间刚学完《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"+
                        "<h1>File Not Found</h1>";
                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出现了如下错误:
在这里插入图片描述

错误排查

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

有两种可能

  • 文件找不到,我们的错误响应写错了。即如下这代码:
    在这里插入图片描述
    故进行如下测试,发现没有问题:
    在这里插入图片描述

  • 找到了文件,但是响应写错了。即如下代码
    在这里插入图片描述
    其实很明显就有问题,根据前置知识,这里只有响应体,没有响应头。
    添加响应头内容,进行测试,成功解决。
    在这里插入图片描述

最终代码

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"+
                        "<h1>File Not Found</h1>";
                output.write(errorMessage.getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis != null){
                fis.close();
            }
        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERR_INVALID_HTTP_RESPONSE是一个常见的网络错误,它表示在进行HTTP通信时,收到了无效的HTTP响应。这个错误通常发生在以下几种情况下: 1. 服务器返回的HTTP响应格式不正确:HTTP协议规定了一种特定的响应格式,包括状态码、头部信息和响应体等。如果服务器返回的响应格式不符合规范,就会导致浏览器无法正确解析响应,从而触发ERR_INVALID_HTTP_RESPONSE错误。 2. 代理服务器或防火墙的问题:有时候,代理服务器或防火墙可能会对HTTP响应进行修改或拦截,导致响应格式被破坏,从而引发ERR_INVALID_HTTP_RESPONSE错误。 3. 网络连接问题:不稳定的网络连接或者网络中断也可能导致HTTP响应被截断或损坏,进而引发该错误。 解决这个错误的方法有以下几种: 1. 清除浏览器缓存:有时候浏览器缓存中存储的无效数据可能导致ERR_INVALID_HTTP_RESPONSE错误。清除浏览器缓存可以尝试解决该问题。 2. 检查代理服务器或防火墙设置:如果你使用了代理服务器或防火墙,可以尝试禁用它们或者调整其设置,看是否能够解决该错误。 3. 检查网络连接确保你的网络连接稳定,并且没有中断或其他问题。 4. 尝试使用其他浏览器或设备:有时候ERR_INVALID_HTTP_RESPONSE错误可能与特定的浏览器或设备有关。尝试使用其他浏览器或设备进行访问,看看是否能够正常加载页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值