Jetty没有关闭连接

时间:2016-05-06 09:17:59

标签: java gradle jetty gretty

我使用Gradle创建了一个简单的服务器Java应用程序。作为嵌入式服务器,我使用的是Jetty。我也在使用Gretty插件,因为它支持最新的Jetty版本。

项目运行得很好。我试图强调测试它。作为测试的一部分,我需要检查响应时间,因此我通过"Connection:Close"发送curl标题。

我的响应是一个很长的JSON字符串,我只看到它的一部分,之后连接挂起。我想知道它为什么会发生,我该如何解决它。

注意:

  1. 发送Connection:Keep-alive标题时,一切正常
  2. 来自服务器的响应不是长字符串,而是较小。它工作正常(不挂)
  3. 从gradle尝试标准的Jetty插件,结果是一样的。
  4. 如何测试:

    1. 从控制台./gradlew appRun
    2. 构建并运行my project
    3. 从bash控制台运行curl -H "Connection:Close" -i "http://localhost:8080/Environment/example"
    4. 查看部分响应并且连接仍然存在...

1 个答案:

答案 0 :(得分:3)

似乎您在HTTP/1.0HTTP/1.1之间混淆持久连接模式。

要么是这样,要么您使用的是真正旧版本的curl仍默认为HTTP/1.0

默认情况下,

HTTP/1.0持久连接,因此要使用持久连接,我们会发送Connection: keep-alive

HTTP/1.1默认使用持久连接,因此要禁用它,我们可以发送Connection: close

使用HTTP/1.0Connection: close就像发送此内容一样......

GET /Environment/example HTTP/1.0
Host: localhost:8080
Connection: close

...根据Connection规范

HTTP/1.0生成无效的标头值

让我们使用curl的详细功能来查看真正发生的事情明智的连接......

示例:HTTP/1.1正常运行:

$ curl --verbose --http1.1 http://apache.org/ -so /dev/null
*   Trying 88.198.26.2...
* Connected to apache.org (88.198.26.2) port 80 (#0)
> GET / HTTP/1.1
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:05:39 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:05:39 GMT
< Content-Type: text/html
< 
{ [1125 bytes data]
* Connection #0 to host apache.org left intact

请注意,它表示保持连接完好无损?

示例:HTTP/1.1手动Connection: close操作:

$ curl --verbose --http1.1 --header "Connection: close" http://apache.org/ -so /dev/null
*   Trying 140.211.11.105...
* Connected to apache.org (140.211.11.105) port 80 (#0)
> GET / HTTP/1.1
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
> Connection: close
> 
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:06:35 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:06:35 GMT
< Connection: close
< Content-Type: text/html
< 
{ [1106 bytes data]
* Closing connection 0

啊,HTTP响应标头说服务器将关闭,curl看到连接被关闭。我们想要什么。

示例:HTTP/1.0正常运行:

$ curl --verbose --http1.0 http://apache.org/ -so /dev/null
*   Trying 140.211.11.105...
* Connected to apache.org (140.211.11.105) port 80 (#0)
> GET / HTTP/1.0
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:08:27 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:08:27 GMT
< Connection: close
< Content-Type: text/html
< 
{ [4002 bytes data]
* Closing connection 0

查看HTTP响应标头如何表示服务器将关闭?

Curl也看到了连接被关闭。

这就是我们对正常HTTP/1.0操作所期望的。

示例:HTTP/1.0具有持久连接:

$ curl --verbose --http1.0 --header "Connection: keep-alive" http://apache.org/ -so /dev/null
*   Trying 88.198.26.2...
* Connected to apache.org (88.198.26.2) port 80 (#0)
> GET / HTTP/1.0
> Host: apache.org
> User-Agent: curl/7.43.0
> Accept: */*
> Connection: keep-alive
> 
< HTTP/1.1 200 OK
< Date: Fri, 06 May 2016 12:08:37 GMT
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Fri, 06 May 2016 11:10:20 GMT
< ETag: "cf64-5322a812896a8"
< Accept-Ranges: bytes
< Content-Length: 53092
< Vary: Accept-Encoding
< Cache-Control: max-age=3600
< Expires: Fri, 06 May 2016 13:08:37 GMT
< Keep-Alive: timeout=30, max=100
< Connection: Keep-Alive
< Content-Type: text/html
< 
{ [3964 bytes data]
* Connection #0 to host apache.org left intact

是的,服务器表明它也将使用Keep-Alive(按照HTTP / 1.0规范),并且卷曲甚至同意并说连接保持不变。