HTTP 流水线请求文本示例 [英] HTTP pipelining request text example

查看:33
本文介绍了HTTP 流水线请求文本示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是请求单个页面的 HTTP 1.1 调用示例:

GET/jq.js HTTP/1.1主机:127.0.0.1接受: */*

我了解使用 HTTP Pipelining,可以在不中断连接的情况下发送多个请求.

  • 有人可以发布一些关于如何将此请求发送到服务器的文本示例,我希望能够通过命令行或使用 PHP 套接字来完成.
  • 是否也需要在网络服务器上启用对流水线的支持?
  • 主流网络服务器(apache、nginx)是否默认支持流水线,或者是否需要启用它
  • 解决方案

    来自 w3c 协议详情:

    <块引用>

    8.1.2.2 流水线

    支持持久连接的客户端可以管道"其请求(即发送多个请求而无需等待每个响应).服务器必须按照接收请求的顺序发送对这些请求的响应.

    在连接建立后立即假定持久连接和管道的客户端应该准备在第一次管道尝试失败时重试其连接.如果客户端进行了这样的重试,它在知道连接是持久的之前不得管道.如果服务器在发送所有相应的响应之前关闭连接,客户端必须还准备重新发送他们的请求.

    客户端不应使用非幂等方法或非幂等方法序列的管道请求(请参阅第 9.1.2 节).否则,传输连接的提前终止可能导致不确定的结果.希望发送非幂等请求的客户端应该等待发送该请求,直到它收到前一个请求的响应状态.

    因此,第一个事实是您应该处于 KeepAlive 状态.因此,您应该在请求标头中添加 Connection: keep-alive 关键字,但某些网络服务器可能仍会接受没有这种保持活动状态的流水线.另一方面,这可能会被服务器拒绝,服务器可能会也可能不会接受您在保活模式下的连接.因此,在任何时候,无论是否处于 keepalived 状态,您都可以在一个连接中发送 3 个流水线请求,但只能得到一个响应.

    这个要点,我们可以找到一种使用 telnet 进行测试的好方法.>

    使用 Connection: keep-alive 头请求 keepalive:

    (echo -en "GET/index.html HTTP/1.1\nHost: foo.com\nConnection: keep-alive\n\nGET/index.html HTTP/1.1\nHost: foo.com\n\n"; 睡觉 10) |远程登录本地主机 80正在尝试 127.0.0.1 ...连接到 localhost.lan.转义字符是^]".HTTP/1.1 200 正常日期:2013 年 10 月 27 日星期日 17:51:58 GMT服务器:Apache/2.2.22 (Debian)上次修改时间:2012 年 3 月 4 日星期日 15:00:29 GMTETag:56176e-3e-4ba6c121c4761"接受范围:字节内容长度:62变化:接受编码保持活动:超时=5,最大=100 <======== 保持活动!连接:保持活动内容类型:文本/html;字符集=utf-8<身体><h1>测试</h1></html>HTTP/1.1 200 正常日期:2013 年 10 月 27 日星期日 17:51:58 GMT服务器:Apache/2.2.22 (Debian)上次修改时间:2012 年 3 月 4 日星期日 15:00:29 GMTETag:56176e-3e-4ba6c121c4761"接受范围:字节内容长度:62变化:接受编码内容类型:文本/html;字符集=utf-8<身体><h1>测试</h1></html>

    它有效.

    不要求Keepalive:

    (echo -en "GET/index.html HTTP/1.1\nHost: foo.com\nConnection: keep-alive\n\nGET/index.html HTTP/1.1\nHost: foo.com\n\n"; 睡觉 10) |远程登录本地主机 80正在尝试 127.0.0.1 ...连接到 localhost.lan.转义字符是^]".HTTP/1.1 200 正常日期:2013 年 10 月 27 日星期日 17:49:37 GMT服务器:Apache/2.2.22 (Debian)上次修改时间:2012 年 3 月 4 日星期日 15:00:29 GMTETag:56176e-3e-4ba6c121c4761"接受范围:字节内容长度:62变化:接受编码内容类型:文本/html;字符集=utf-8<身体><h1>测试</h1></html>HTTP/1.1 200 正常日期:2013 年 10 月 27 日星期日 17:49:37 GMT服务器:Apache/2.2.22 (Debian)上次修改时间:2012 年 3 月 4 日星期日 15:00:29 GMTETag:56176e-3e-4ba6c121c4761"接受范围:字节内容长度:62变化:接受编码内容类型:文本/html;字符集=utf-8<身体><h1>测试</h1></html>外部主机关闭连接.

    同样的结果,我没有要求它,但它看起来像一个 Keepalive 答案(在 5 秒后关闭,这是在 Apache 中设置的值).一个流水线式的答案,我得到了我的两页.

    现在,如果我通过设置阻止在 Apache 中使用任何 Keepalive 连接:

    Keepalive 关闭

    并重新启动它:

    (echo -en "GET/index.html HTTP/1.1\nHost: foo.com\nConnection: keep-alive\n\nGET/index.html HTTP/1.1\nHost: foo.com\n\n"; 睡觉 10) |远程登录本地主机 80正在尝试 127.0.0.1 ...连接到 localhost.lan.转义字符是^]".HTTP/1.1 200 正常日期:2013 年 10 月 27 日星期日 18:02:41 GMT服务器:Apache/2.2.22 (Debian)上次修改时间:2012 年 3 月 4 日星期日 15:00:29 GMTETag:56176e-3e-4ba6c121c4761"接受范围:字节内容长度:62变化:接受编码连接:关闭内容类型:文本/html;字符集=utf-8<身体><h1>测试</h1></html>外部主机关闭连接.

    只有一个答案...所以服务器可以拒绝我的流水线请求.

    现在,对于服务器和浏览器的支持,我认为你的维基百科来源已经足够了:-)

    Below is an example HTTP 1.1 call with a single page requested :

    GET /jq.js HTTP/1.1
    Host: 127.0.0.1
    Accept: */*
    

    I understand with HTTP Pipelining, multiple requests can be sent without breaking the connection.

  • Can someone post, some text example of how this request will be sent to the server, I want to be able to do it over the command line or with PHP sockets.
  • Does support for pipelining need to enabled on the web-server as well?
  • Is pipelining supported by major Web-servers(apache, nginx) by default or does it need to be enabled
  • 解决方案

    From w3c protocol details:

    8.1.2.2 Pipelining

    A client that supports persistent connections MAY "pipeline" its requests (i.e., send multiple requests without waiting for each response). A server MUST send its responses to those requests in the same order that the requests were received.

    Clients which assume persistent connections and pipeline immediately after connection establishment SHOULD be prepared to retry their connection if the first pipelined attempt fails. If a client does such a retry, it MUST NOT pipeline before it knows the connection is persistent. Clients MUST also be prepared to resend their requests if the server closes the connection before sending all of the corresponding responses.

    Clients SHOULD NOT pipeline requests using non-idempotent methods or non-idempotent sequences of methods (see section 9.1.2). Otherwise, a premature termination of the transport connection could lead to indeterminate results. A client wishing to send a non-idempotent request SHOULD wait to send that request until it has received the response status for the previous request.

    So, first fact is that you should be in a KeepAlive status. So you should add Connection: keep-alive keyword in your request headers, but some webservers may still accept pipelining without this keep alive status. On the other hand, this could be rejected by the server, the server may or may not accept your connection in keepalive mode. So, at any time, being in keepalived or not, you may send 3 requests pipelined in one connection, and get only one response.

    From this gist we can find a nice way to test it with telnet.

    Asking for keepalive with Connection: keep-alive header:

    (echo -en "GET /index.html HTTP/1.1\nHost: foo.com\nConnection: keep-alive\n\nGET /index.html HTTP/1.1\nHost: foo.com\n\n"; sleep 10) | telnet localhost 80
    
    Trying 127.0.0.1...
    Connected to localhost.lan.
    Escape character is '^]'.
    HTTP/1.1 200 OK
    Date: Sun, 27 Oct 2013 17:51:58 GMT
    Server: Apache/2.2.22 (Debian)
    Last-Modified: Sun, 04 Mar 2012 15:00:29 GMT
    ETag: "56176e-3e-4ba6c121c4761"
    Accept-Ranges: bytes
    Content-Length: 62
    Vary: Accept-Encoding
    Keep-Alive: timeout=5, max=100  <======= Keepalive!
    Connection: Keep-Alive
    Content-Type: text/html; charset=utf-8
    
    <html>
        <body>
            <h1>test</h1>
        </body>
    </html>
    HTTP/1.1 200 OK
    Date: Sun, 27 Oct 2013 17:51:58 GMT
    Server: Apache/2.2.22 (Debian)
    Last-Modified: Sun, 04 Mar 2012 15:00:29 GMT
    ETag: "56176e-3e-4ba6c121c4761"
    Accept-Ranges: bytes
    Content-Length: 62
    Vary: Accept-Encoding
    Content-Type: text/html; charset=utf-8
    
    <html>
        <body>
            <h1>test</h1>
        </body>
    </html>
    

    It works.

    Without asking for Keepalive:

    (echo -en "GET /index.html HTTP/1.1\nHost: foo.com\nConnection: keep-alive\n\nGET /index.html HTTP/1.1\nHost: foo.com\n\n"; sleep 10) | telnet localhost 80
    
    Trying 127.0.0.1...
    Connected to localhost.lan.
    Escape character is '^]'.
    HTTP/1.1 200 OK
    Date: Sun, 27 Oct 2013 17:49:37 GMT
    Server: Apache/2.2.22 (Debian)
    Last-Modified: Sun, 04 Mar 2012 15:00:29 GMT
    ETag: "56176e-3e-4ba6c121c4761"
    Accept-Ranges: bytes
    Content-Length: 62
    Vary: Accept-Encoding
    Content-Type: text/html; charset=utf-8
    
    <html>
        <body>
            <h1>test</h1>
        </body>
    </html>
    HTTP/1.1 200 OK
    Date: Sun, 27 Oct 2013 17:49:37 GMT
    Server: Apache/2.2.22 (Debian)
    Last-Modified: Sun, 04 Mar 2012 15:00:29 GMT
    ETag: "56176e-3e-4ba6c121c4761"
    Accept-Ranges: bytes
    Content-Length: 62
    Vary: Accept-Encoding
    Content-Type: text/html; charset=utf-8
    
    <html>
        <body>
            <h1>test</h1>
        </body>
    </html>
    Connection closed by foreign host.
    

    Same result, I did not ask for it but it looks like a Keepalive answer (closing after 5s which is the value set in Apache). And a pipelined answer, I get my two pages.

    Now if I prevent usage of any Keepalive connection in Apache by setting:

    Keepalive Off
    

    And restarting it:

    (echo -en "GET /index.html HTTP/1.1\nHost: foo.com\nConnection: keep-alive\n\nGET /index.html HTTP/1.1\nHost: foo.com\n\n"; sleep 10) | telnet localhost 80
    
    Trying 127.0.0.1...
    Connected to localhost.lan.
    Escape character is '^]'.
    HTTP/1.1 200 OK
    Date: Sun, 27 Oct 2013 18:02:41 GMT
    Server: Apache/2.2.22 (Debian)
    Last-Modified: Sun, 04 Mar 2012 15:00:29 GMT
    ETag: "56176e-3e-4ba6c121c4761"
    Accept-Ranges: bytes
    Content-Length: 62
    Vary: Accept-Encoding
    Connection: close
    Content-Type: text/html; charset=utf-8
    
    <html>
        <body>
            <h1>test</h1>
        </body>
    </html>
    Connection closed by foreign host.
    

    Only one answer... So the server can reject my request for pipelining.

    Now, for support on servers and browsers, I think your wikipedia source tells enough :-)

    这篇关于HTTP 流水线请求文本示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆