【发布时间】:2017-11-27 13:08:03
【问题描述】:
我的任务是实现简单的 HTTP 服务器。服务器发送奇怪的响应。 Wireshark 的这个输出。
GET / HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,ru;q=0.8
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Connection: keep-alive
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 50px;
background-color: #fff;
border-radius: 1em;
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
body {
background-color: #fff;
}
div {
width: auto;
margin: 0 auto;
border-radius: 0;
padding: 1em;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is established to be used for illustrative examples in documents. You may use this
domain in examples without prior coordination or asking for permission.</p>
<p><a href="http://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>
GET /favicon.ico HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://127.0.0.1:8080/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,ru;q=0.8
如您所见,响应结束时有新请求。我不明白为什么会这样。你对此有什么想法吗?
【问题讨论】:
-
HTTP 1.1 允许对多个请求重用同一个连接。
-
你说的是keep-alive属性吗?
-
我不是 100% 确定这是否是通过 keep-alive 控制的,但我猜是的。
-
需要保持活动才能重用连接。在 HTTP 1.1 中,默认情况下启用 keep-alives,除非客户端或服务器在
Connection标头中明确声明。但也要考虑 HTTP 流水线,客户端可以发送一个新请求(甚至是多个请求),而无需先等待先前的响应。但是,服务器仍然必须处理请求并按顺序发送响应(HTTP 2.0 允许无序响应)。