HTTP服务器不发送完整的文件给wget,Firefox浏览器。将连接复位? [英] HTTP Server Not Sending Complete File To WGET, Firefox. Connection reset by peer?

查看:1337
本文介绍了HTTP服务器不发送完整的文件给wget,Firefox浏览器。将连接复位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个HTTP服务器,和我无法发送的文件。如果我抢他们的netcat,输出似乎完美。如果我使用浏览器或wget的,我只得到完整的文件的时候。 WGET不断连接被对方​​复位收到错误,请参见下面的输出。火狐说:连接被重置。

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);
}

下面是从wget的试图抓住文件的输出:

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.

从HTTP服务器输出调试:

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.

我倒是AP preciate另一套的眼睛在这!这是怎么回事,我该如何解决这个问题?

I'd appreciate another set of eyes on this! Why is this happening and how can I fix it?

推荐答案

我的猜测是,c您还没有这里显示的错误与$ C $。

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.

由于在接近的时间仍然有来自客户端的未读数据,这将导致在由对等重置连接。你不会看到这个NC的效果,因为你NC发送请求可能比从浏览器的请求短,从而从请求的所有数据在NC的情况下被读取,而不是在浏览器的情况下。

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.

除此之外,你的反应是无效的,即使浏览器接受。状态code,而不是添加的原因短语后,您的状态行(第一行)站,看到的 http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1 。此外,可以使用 \\ n 而不是 \\ r \\ n 作为行分隔符。

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.

这篇关于HTTP服务器不发送完整的文件给wget,Firefox浏览器。将连接复位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆