[英]ncat: piping multiline request breaks HTTP
I just started using ncat, and playing around with simple HTTP requests, I came across the following:我刚开始使用 ncat,并尝试处理简单的 HTTP 请求,我遇到了以下问题:
Starting ncat and typing a two-line get request works fine:启动 ncat 并输入一个两行的 get 请求可以正常工作:
$ ncat 192.168.56.20 80
GET / HTTP/1.1
Host: 192.168.56.20
HTTP/1.1 200 OK
If however the request gets echoed to ncat, it apparently breaks somewhere:但是,如果请求被回显到 ncat,它显然会在某处中断:
$ echo 'GET / HTTP/1.1\nHost: 192.168.56.20' | ncat 192.168.56.20 80
HTTP/1.1 400 Bad Request
I don't get it.我不明白。
The \n
in the string is sent literally.字符串中的\n
按字面意思发送。 Use echo -e
to enable interpretation of backslash escapes.使用echo -e
启用对反斜杠转义的解释。 Also, the newline sequence for HTTP 1.1 is \r\n
(CRLF).此外, HTTP 1.1 的换行符序列是\r\n
(CRLF)。 And the header section ends with an additional end-of-line. header 部分以额外的行尾结尾。
Try:尝试:
echo -e 'GET / HTTP/1.1\r\nHost: 192.168.56.20\r\n\r\n' | ncat 192.168.56.20 80
Alternatively, the ncat has the option to convert new lines to CRLF:或者, ncat 可以选择将新行转换为 CRLF:
-C, --crlf Use CRLF for EOL sequence
Hence, you can write:因此,你可以写:
echo -e 'GET / HTTP/1.1\nHost: 192.168.56.20\n\n' | ncat -C 192.168.56.20 80
and you should get the same result.你应该得到相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.