c - HTTP Server Not Sending Complete File To WGET, Firefox. Connection reset by peer? -
i'm writing http server, , having trouble sending larger files. if grab them netcat, output seems perfect. if use browser or wget, complete file sometimes. wget keeps getting "connection reset peer" errors, see output below. firefox says "the connection reset".
here's relevant procedure sends data client:
int handle_request(int sockfd, struct cached_file content) { char buffer[1024]; // fixme hardcoded arbitrary buffer size header unsigned int sent_bytes; unsigned int total = 0; unsigned int bytes_left = content.size; printf("i have send %u bytes of content.\n", content.size); snprintf(buffer, 1024, "http/1.1 %s\ncontent-type: %s\ncontent-length: %s\n\n", content.statuscode, content.contenttype, content.sizestring); sent_bytes = send(sockfd, buffer, strlen(buffer), msg_more); printf("i wanted send %u bytes of header, , sent %u.\n", strlen(buffer), sent_bytes); while (total < bytes_left) { sent_bytes = send(sockfd, content.data+total, bytes_left, 0); if (sent_bytes == -1) { printf("send() returned -1\n"); break; } total += sent_bytes; bytes_left -= sent_bytes; } printf("i sent %u bytes of content. had %u left send.\n", total, bytes_left); if (sent_bytes == -1) logprint("socket error!", errno); }
here's output wget trying grab file:
wget --tries 1 http://localhost:8081/image.jpg --2015-07-01 13:21:42-- http://localhost:8081/image.jpg resolving localhost (localhost)... ::1, 127.0.0.1 connecting localhost (localhost)|::1|:8081... failed: connection refused. connecting localhost (localhost)|127.0.0.1|:8081... connected. http request sent, awaiting response... 200 ok length: 700895 (684k) [image/jpeg] saving to: ‘image.jpg.10’ image.jpg.10 53%[===============================> ] 363.31k --.-kb/s in 0.001s 2015-07-01 13:21:42 (688 mb/s) - read error @ byte 372031/700895 (connection reset peer). giving up. wget --tries 1 http://localhost:8081/image.jpg --2015-07-01 13:21:43-- http://localhost:8081/image.jpg resolving localhost (localhost)... ::1, 127.0.0.1 connecting localhost (localhost)|::1|:8081... failed: connection refused. connecting localhost (localhost)|127.0.0.1|:8081... connected. http request sent, awaiting response... 200 ok length: 700895 (684k) [image/jpeg] saving to: ‘image.jpg.11’ image.jpg.11 6%[==> ] 42.69k --.-kb/s in 0s 2015-07-01 13:21:43 (500 mb/s) - read error @ byte 43711/700895 (connection reset peer). giving up.
debugging output http server:
i have send 700895 bytes of content. wanted send 65 bytes of header, , sent 65. sent 700895 bytes of content. had 0 left send.
i'd appreciate set of eyes on this! why happening , how can fix it?
my guess error related code have not shown here.
it common mistake in simple implementation not read request instead read first line or bytes determine requested page, send response , close.
since @ time of close there still unread data client, result in connection reset peer. don't see effect nc because request send nc shorter request browser , data request read in case of nc, not in case of browser.
apart response invalid if browsers accept it. status line (the first line) stops after status code instead of adding reason phrase, see http://www.w3.org/protocols/rfc2616/rfc2616-sec6.html#sec6.1. also, use \n
instead of \r\n
line delimiter.
Comments
Post a Comment