为什么使用bash和网络重定向时从文件描述符3读取会挂起?

huangapple go评论54阅读模式
英文:

Why does reading from file descriptor 3 hangs with bash and network redirection?

问题

以下是翻译好的部分:

我正在使用bash和网络重定向发送HTTP请求。

这是我发送的请求:

exec 3<> $initial_url
path="/webhdfs/v1$hdfs_target?user.name=hdfs&op=OPEN"
printf "GET $path HTTP/1.1\r\nHost: $url:$port\r\nAccept: */*\r\n\r\n" >&3
echo toto
cat <&3
echo titi
exec 3<&
我已经正确接收到响应。然而,`cat` 永远不会结束。以下是执行输出(通过ctrl+c终止):

$ time bash toto.sh
toto
HTTP/1.1 307 TEMPORARY_REDIRECT
Cache-Control: no-cache
Expires: Thu, 20 Jul 2023 12:29:41 GMT
Date: Thu, 20 Jul 2023 12:29:41 GMT
Pragma: no-cache
Expires: Thu, 20 Jul 2023 12:29:41 GMT
Date: Thu, 20 Jul 2023 12:29:41 GMT
Pragma: no-cache
X-FRAME-OPTIONS: SAMEORIGIN
Set-Cookie: hadoop.auth="redacted_for_SO"; Path=/; HttpOnly
Location: redacted_for_SO
Content-Type: application/octet-stream
Content-Length: 0


real    0m19.042s
user    0m0.124s
sys     0m0.733s

这个输出包含我需要的一切,但正如所说,`cat` 永远不会结束。

- 为什么`cat` 挂起?它在等待什么?
- 当数据已接收时,如何防止它挂起?
英文:

I am using bash and network redirection to send HTTP requests.

Here is the request I am sending:

exec 3<> $initial_url
path="/webhdfs/v1$hdfs_target?user.name=hdfs&op=OPEN"
printf "GET $path HTTP/1.1\r\nHost: $url:$port\r\nAccept: */*\r\n\r\n" >&3
echo toto
cat <&3
echo titi
exec 3<&

I am recieving the response correctly. However, the cat just hangs indefinitely. Here is an execution output (killed with ctrl+c):

$ time bash toto.sh
toto
HTTP/1.1 307 TEMPORARY_REDIRECT
Cache-Control: no-cache
Expires: Thu, 20 Jul 2023 12:29:41 GMT
Date: Thu, 20 Jul 2023 12:29:41 GMT
Pragma: no-cache
Expires: Thu, 20 Jul 2023 12:29:41 GMT
Date: Thu, 20 Jul 2023 12:29:41 GMT
Pragma: no-cache
X-FRAME-OPTIONS: SAMEORIGIN
Set-Cookie: hadoop.auth="redacted_for_SO"; Path=/; HttpOnly
Location: redacted_for_SO
Content-Type: application/octet-stream
Content-Length: 0



real    0m19.042s
user    0m0.124s
sys     0m0.733s

There is everything I need in this output, but as said, the cat never ends.

  • Why is the cat hanging ? What is it waiting for ?
  • How to prevent it from hanging when the data has been recieved ?

答案1

得分: 2

你应该在你的请求中添加一个Connection: close头部,这样服务器在发送完响应后会关闭连接。另请参阅这里:https://serverfault.com/questions/790197/what-does-connection-close-mean-when-used-in-the-response-message

英文:

You should add a Connection: close header to your request so that the server will close the connection once it's finished sending the response. See also here: https://serverfault.com/questions/790197/what-does-connection-close-mean-when-used-in-the-response-message

huangapple
  • 本文由 发表于 2023年7月20日 20:32:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定