HTTP Server Not Sending Complete File To WGET, Firefox. Connection reset by peer?

前端 未结 2 1454
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 17:03

I\'m writing an HTTP server, and am having trouble sending larger files. If I grab them with netcat, the output seems perfect. If I use a browser or wget, I only get the complet

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-26 17:41

    My guess is that the error is related to code you have not shown here.

    It is a common mistake in simple implementation to not fully read the request but instead only read the first line or some bytes to determine the requested page, then send the response and finally close.

    Since at the time of the close there are still unread data from the client, this will result in Connection reset by peer. You don't see this effect with nc because the request you send with nc is probably shorter than the request from the browser and thus all data from the request are read in case of nc, but not in case of browser.

    Apart from that your response is invalid even if browsers accept it. Your status line (the first line) stops after the status code instead of adding the reason phrase, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1. Also, you use \n instead of \r\n as line delimiter.

提交回复
热议问题