doudun5009 2019-02-13 13:11
浏览 127
已采纳

如果content-length错误,Chrome将关闭tcp连接?

I am interested in the action of web server to http keep-alive header. So I built a simple http server based on http server. The server does nothing but response to the client with a simple html http body.

The code of server is here:

package main

import (
    "fmt"
    "net"
    "os"
)

func main() {
    l, err := net.Listen("tcp", "localhost:9765")
    if err != nil {
        fmt.Println("Error listening:", err.Error())
        os.Exit(1)
    }
    defer l.Close()
    for {
        conn, err := l.Accept()
        fmt.Println("New connection...")
        if err != nil {
            fmt.Println("Error accepting: ", err.Error())
            os.Exit(1)
        }
        go handleRequest(conn)
    }
}

// handler
func handleRequest(conn net.Conn) {
    for {
        buf := make([]byte, 512)
        _, err := conn.Read(buf)
        if err != nil {
            fmt.Println("Error reading:", err.Error())
            conn.Close()
            break
        }
        fmt.Printf("%s", buf)
        str := `HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 8
Content-Type: application/javascript

alert(1)

`
        conn.Write([]byte(str))
    }
}

I added a demo html to local nginx static server:

<head>
    <meta charset="UTF-8">
    <script type=text/javascript src="test.js"></script>
    <script type=text/javascript src="http://localhost:9765/3"></script>
    <script type=text/javascript src="http://localhost:9765/2"></script>
    <script type=text/javascript src="http://localhost:9765/1"></script>
    <script type=text/javascript src="http://localhost:9765/17"></script>
    <script type=text/javascript src="http://localhost:9765/16"></script>
    <script type=text/javascript src="http://localhost:9765/15"></script>
    <script type=text/javascript src="http://localhost:9765/14"></script>
    <script type=text/javascript src="http://localhost:9765/13"></script>
    <script type=text/javascript src="http://localhost:9765/12"></script>
    <script type=text/javascript src="http://localhost:9765/30"></script>
    <script type=text/javascript src="http://localhost:9765/29"></script>
    <script type=text/javascript src="http://localhost:9765/28"></script>
    <script type=text/javascript src="http://localhost:9765/27"></script>
    <script type=text/javascript src="http://localhost:9765/26"></script>
    <script type=text/javascript src="http://localhost:9765/25"></script>
    <script type=text/javascript src="http://localhost:9765/24"></script>
    <script type=text/javascript src="http://localhost:9765/23"></script>
    <script type=text/javascript src="http://localhost:9765/22></script>
    <script type=text/javascript src="http://localhost:9765/21"></script>
</head>

<body>
<h1>loader测试页面</h1>
<span>hello world</span>
</body>

</html>

When visit the file with chrome, I found all response is normal, but tcp connection was closed after every single http request. The TCP got EOF error after sending http response.

  • 写回答

1条回答 默认 最新

  • dongya8378 2019-02-13 14:29
    关注

    If you send the wrong content length in the header, the browser will either hang (waiting for content that will never come) or close the connection (when it sees invalid junk after the content). What else could it do?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开