user15483624
user15483624

Reputation:

How to use socat with heredoc?

I can run socat in this way, if I input text from the terminal.

$ 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"
}

But I don't get the same output when I use heredoc. Does anybody know how to make it work with 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,1] and [7,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

Upvotes: 3

Views: 325

Answers (1)

Bill
Bill

Reputation: 281

I don't feel I completely understand what is happening, but I think the end of the heredoc is signalling to shutdown the stream before the HTTP request returns. One way of getting around this is to use the FD option shut-none.

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

EOF

But I don't know how reliable this is.

Upvotes: 0

Related Questions