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 complete file sometimes. WGET keeps getting "connection reset by peer" errors, see the output below. Firefox says "the connection was reset".
Here's the relevant procedure that sends data to the client:
int handle_request(int sockfd, struct cached_file content) {
char buffer[1024]; // FIXME hardcoded arbitrary buffer size for header
unsigned int sent_bytes;
unsigned int total = 0;
unsigned int bytes_left = content.size;
printf("I have to 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 to send %u bytes of header, and I 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. I had %u left to send.\n", total, bytes_left);
if (sent_bytes == -1)
logprint("socket error!", errno);
}
Here's the output from wget trying to grab the 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 to localhost (localhost)|::1|:8081... failed: Connection refused.
Connecting to 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 at byte 372031/700895 (Connection reset by 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 to localhost (localhost)|::1|:8081... failed: Connection refused.
Connecting to 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 at byte 43711/700895 (Connection reset by peer). Giving up.
Debugging output from the http server:
I have to send 700895 bytes of content.
I wanted to send 65 bytes of header, and I sent 65.
I sent 700895 bytes of content. I had 0 left to send.
I'd appreciate another set of eyes on this! Why is this happening and how can I fix it?