如何将 socat 与 heredoc 一起使用?

问题描述

如果我从终端输入文本,我可以通过这种方式运行 socat。

$ socat -d -d - TCP4:httpbin.org:80
2021/04/02 15:12:43 socat[22364] N reading from and writing to stdio
2021/04/02 15:12:43 socat[22364] N opening connection to LEN=16 AF=2 34.199.75.4:80
2021/04/02 15:12:43 socat[22364] N successfully connected from local address LEN=16 AF=2 10.1.186.126:57360
2021/04/02 15:12:43 socat[22364] N starting data transfer loop with FDs [0,1] and [7,7]
GET /get HTTP/1.1
Host: myhost

HTTP/1.1 200 OK
Date: Fri,02 Apr 2021 20:12:59 GMT
Content-Type: application/json
Content-Length: 190
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},"headers": {
    "Host": "myhost","X-Amzn-Trace-Id": "Root=1-60677acb-1e2c974a0fb9bee97b8072dd"
  },"origin": "XXX.XXX.XXX.XXX","url": "http://myhost/get"
}

但是当我使用 heredoc 时我没有得到相同的输出。有谁知道如何使它与 heredoc 一起工作?

$ socat -d -d - TCP4:httpbin.org:80 <<EOF
GET /get HTTP/1.1
Host: httpbin.org

EOF
2021/04/02 15:16:42 socat[22870] N reading from and writing to stdio
2021/04/02 15:16:42 socat[22870] N opening connection to LEN=16 AF=2 54.166.163.67:80
2021/04/02 15:16:42 socat[22870] N successfully connected from local address LEN=16 AF=2 10.1.186.126:58356
2021/04/02 15:16:42 socat[22870] N starting data transfer loop with FDs [0,7]
2021/04/02 15:16:42 socat[22870] N socket 1 (fd 0) is at EOF
2021/04/02 15:16:42 socat[22870] N socket 2 (fd 7) is at EOF
2021/04/02 15:16:42 socat[22870] N exiting with status 0

解决方法

我不觉得我完全理解发生了什么,但我认为 heredoc 的结尾是在 HTTP 请求返回之前发出关闭流的信号。解决此问题的一种方法是使用 FD 选项关闭无。

socat -d -d - TCP4:httpbin.org:80,shut-none <<EOF
GET /get HTTP/1.1
Host: httpbin.org

EOF

但我不知道这有多可靠。