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

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

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

问题

以下是翻译好的部分:

  1. 我正在使用bash和网络重定向发送HTTP请求。
  2. 这是我发送的请求:
  3. exec 3<> $initial_url
  4. path="/webhdfs/v1$hdfs_target?user.name=hdfs&op=OPEN"
  5. printf "GET $path HTTP/1.1\r\nHost: $url:$port\r\nAccept: */*\r\n\r\n" >&3
  6. echo toto
  7. cat <&3
  8. echo titi
  9. exec 3<&
  10. 我已经正确接收到响应。然而,`cat` 永远不会结束。以下是执行输出(通过ctrl+c终止):
  11. $ time bash toto.sh
  12. toto
  13. HTTP/1.1 307 TEMPORARY_REDIRECT
  14. Cache-Control: no-cache
  15. Expires: Thu, 20 Jul 2023 12:29:41 GMT
  16. Date: Thu, 20 Jul 2023 12:29:41 GMT
  17. Pragma: no-cache
  18. Expires: Thu, 20 Jul 2023 12:29:41 GMT
  19. Date: Thu, 20 Jul 2023 12:29:41 GMT
  20. Pragma: no-cache
  21. X-FRAME-OPTIONS: SAMEORIGIN
  22. Set-Cookie: hadoop.auth="redacted_for_SO"; Path=/; HttpOnly
  23. Location: redacted_for_SO
  24. Content-Type: application/octet-stream
  25. Content-Length: 0
  26. real 0m19.042s
  27. user 0m0.124s
  28. sys 0m0.733s
  29. 这个输出包含我需要的一切,但正如所说,`cat` 永远不会结束。
  30. - 为什么`cat` 挂起?它在等待什么?
  31. - 当数据已接收时,如何防止它挂起?
英文:

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

Here is the request I am sending:

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

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

  1. $ time bash toto.sh
  2. toto
  3. HTTP/1.1 307 TEMPORARY_REDIRECT
  4. Cache-Control: no-cache
  5. Expires: Thu, 20 Jul 2023 12:29:41 GMT
  6. Date: Thu, 20 Jul 2023 12:29:41 GMT
  7. Pragma: no-cache
  8. Expires: Thu, 20 Jul 2023 12:29:41 GMT
  9. Date: Thu, 20 Jul 2023 12:29:41 GMT
  10. Pragma: no-cache
  11. X-FRAME-OPTIONS: SAMEORIGIN
  12. Set-Cookie: hadoop.auth="redacted_for_SO"; Path=/; HttpOnly
  13. Location: redacted_for_SO
  14. Content-Type: application/octet-stream
  15. Content-Length: 0
  16. real 0m19.042s
  17. user 0m0.124s
  18. 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:

确定