通过C HTTP服务器发送HTTP响应时,图片不会加载到浏览器上

时间:2019-06-05 06:54:03

标签: c image http server

我已经尝试了好几天,以解决我的问题,即图像未显示在浏览器中,index.html可以很好地加载,css和js也可以,并且唯一加载的图像格式是.svg 所有的标题都很好地初始化和正确了。

我尝试了论坛中的大多数建议,例如使用us memcpy而不是strcat,使用二进制读取(“ rb”选项)进行fread以便将文件加载到缓冲区中。

以下是将文件内容复制到消息正文中的代码: (很抱歉法语中的内容)

void server_fileToSend(char* file, int len, char* rep, char* header,
                       char* message_body) {
    char* body_size_char = (char*)malloc(100 * sizeof(char));
    char* filename = (char*)calloc(100, sizeof(char));
    char* sendbody;

    if (file[0] == '/') {
        strncpy(filename, file + 1, len - 1);
    } else {
        strncpy(filename, file, len);
    }
    // printf(" filetruc %s\n",filename);
    FILE* f = fopen(filename, "rb+");
    if (f == NULL) {
        // Modification du type de réponse
        strcpy(rep, "HTTP/1.0 404 NOT FOUND\r\n");
        f = fopen("sites/notfound.html", "r");
    }
    fseek(f, 0, SEEK_END);
    body_size = ftell(f);
    rewind(f);
    strcat(header, "Content-Length: ");
    sprintf(body_size_char, "%ld", body_size);
    strcat(header, body_size_char);
    strcat(header, "\r\n");

    if (f != NULL && findContentType(filename) != NULL) {
        strcat(header, findContentType(filename));
    }
    strcat(header, "Connection: keep-alive\r\n");

    if ((sendbody = (char*)malloc(body_size)) == NULL) {
        memcpy(message_body, "this is a directory", 20);
        body_size = 20;
    } else {
        result = fread(sendbody, 1, body_size, f);
        if (result > 0) {
            memcpy(message_body, sendbody, body_size);
        }
    }
    fclose(f);
}

然后,我将message_body附加到响应中,并在标头之后:

memcpy(rep+strlen(rep),message_body, body_size);

没有实际错误,页面可以正常加载,仅.svg以外的所有图像均不加载。另外,当我使用firefox的网络开发人员模式检查它们时,图像只有几个字节,尺寸为0x0。

在此先感谢您提供的帮助。

编辑1: 这分别是来自firefox和Responses的一些请求:

对于index.html

#########################################
Demande recue depuis le client 9
Client [9] [127.0.0.1:46918]
Contenu de la demande GET /www.toto.com/index.html HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0


-----------Réponse-------------------
HTTP/1.0 200 OK
Date: Wed, 05 Jun 2019 07:32:39 GMT
Server: groupe15 (Debian9)
Content-Length: 621
Content-Type: text/html
Connection: keep-alive

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="index.css">
<script src="index.js"></script> 
<title>Index page</title>
</head>
<body>

<h1>This is a all images i have got so far !!! </h1>
<img onmouseover="big(this)" src="index.gif" alt="gif"> 
<img onmouseover="big(this)" src="index.png" alt="png"> 
<img onmouseover="big(this)" src="index.svg" alt="svg"> 
<img onmouseover="big(this)" src="index.jpg" alt="jpg"> 
<img onmouseover="big(this)" src="index.jpeg" alt="jpeg"> 
<img onmouseover="big(this)" src="index.tif" alt="tif"> 
<img onmouseover="big(this)" src="index.tiff" alt="tiff"> 

</body>
</html> 

对于图片:

#########################################
Demande recue depuis le client 8
Client [8] [127.0.0.1:46926]
Contenu de la demande GET /www.toto.com/index.png HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0
Accept: image/webp,*/*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8080/www.toto.com/index.html
Connection: keep-alive
Cache-Control: max-age=0

-----------Réponse-------------------
HTTP/1.0 200 OK
Date: Wed, 05 Jun 2019 07:32:39 GMT
Server: groupe15 (Debian9)
Content-Length: 2574
Content-Type: image/png
Connection: keep-alive

�PNG

如您所见,问题出在邮件正文中

0 个答案:

没有答案