简体   繁体   English

使用C的HTTP响应中的图像?

[英]Image in HTTP Response using C?

I am writing a web server in C . 我正在用C编写Web服务器。 I am able to send my html code in the response body but I tried an JPG image but I get " http://localhost:port/image.jpg " cannot be displayed because it contains errors. 我可以在响应正文中发送我的html代码,但是尝试了JPG图像,但由于包含错误,因此无法显示“ http:// localhost:port / image.jpg ”。

this is a snippet from my webserver code: 这是我的网络服务器代码的片段:

this is my response header: 这是我的响应头:

char * responseheader= "HTTP/1.1 200 OK\\r\\n "Content-Type: image/jpg\\r\\n" "Content-Length: 110000\\r\\n\\r\\n";

//read the file imagefile and store it in the buffer and append it to response header. //读取文件 imagefile 并将其存储在缓冲区中,并将其附加到响应头。

for (i=0;(i<(sizeof(buffer))&&((ch=fgetc(imagefile))!=EOF)&&(ch!='\\n')); i++) { printf("%c", ch) buffer[i] = ch; } for (i=0;(i<(sizeof(buffer))&&((ch=fgetc(imagefile))!=EOF)&&(ch!='\\n')); i++) { printf("%c", ch) buffer[i] = ch; } strcat(responseheader, buffer); for (i=0;(i<(sizeof(buffer))&&((ch=fgetc(imagefile))!=EOF)&&(ch!='\\n')); i++) { printf("%c", ch) buffer[i] = ch; } strcat(responseheader, buffer);

if (strncmp(requestbuff, "GET /image.jpg", 15) { printf("server: loading image...\\n"); send(socketfd, responseheader, sizeof(responseheader), 0); }

output: 输出:

on my webrowser firefox , I type localhost:port/index.html and the page loads fine. 在我的webrowser firefox上 ,我输入localhost:port / index.html,页面可以正常加载。 once my server reads the request GET /image.jpg ..., I send the buffer above. 一旦我的服务器读取了GET /image.jpg ...的请求,我就会在上面发送缓冲区。 Now, when my page loads, the image doesn't show up but as a small error box and when I right click on it and view image, it states the error above. 现在,当我的页面加载时,图像没有显示,但是显示为一个小错误框,当我右键单击它并查看图像时,它指出了上面的错误。

I'm not really sure how I can append the image with my response header. 我不太确定如何在图像中附加响应标头。 I tried sending the file directly to the web browser once I used open() after sending the response; 发送响应后,一旦我使用open(),就尝试将文件直接发送到Web浏览器; same story. 相同的故事。 One thing I do know is that the image is "binary data", full of characters that I can see when I use notepad. 我确实知道的一件事是图像是“二进制数据”,充满了使用记事本时可以看到的字符。

I've looked for hours, browsing the university library, and looking for books, but I haven't seen any implementation in HTTP/C to properly display an image. 我已经花了几个小时,浏览大学图书馆,寻找书籍,但是我没有看到HTTP / C中的任何实现来正确显示图像的实现。

Inside my index.html I used <img src=/image.jpg></img> which triggers the browser request to my web server. index.html中,我使用了<img src=/image.jpg></img> ,它触发了浏览器对我的Web服务器的请求。

Also, is there a book that any of you would recommend concerning HTTP and C web servers that covers this material? 另外,你们中有没有人推荐有关本书的HTTP和C Web服务器的书? Thanks. 谢谢。

Note: I understand this may be easier to implement in PHP/JAVA/JAVASCRIPT; 注意:我知道在PHP / JAVA / JAVASCRIPT中可能更容易实现; I can only write this in C at the moment 我现在只能用C编写

Best Regards. 最好的祝福。

There are multiple issues. 有多个问题。 firstly, you have a duplicate Content-Type header that is invalid (Forward-slash versus backslash, and also you're not sending HTML). 首先,您有一个重复的Content-Type标头,该标头无效(正斜杠与反斜杠,并且您也没有发送HTML)。 Remove that first off. 首先删除。

Secondly, a binary file (such as a JPEG image) may (and often does) contain '00' bytes, which strcat will consider as string terminators. 其次,二进制文件(例如JPEG图像)可能(通常确实)包含'00'字节, strcat会将其视为字符串终止符。

Thirdly, you are invoking strcat to append to a pointer that may (as per the C standard) be stored in read-only memory (as it is a string literal). 第三,您要调用strcat附加到一个指针(该指针可以按照C标准)存储在只读内存中(因为它是字符串文字)。

Finally, sizeof(responseheader) is simply 4 or 8 (whatever the size of a pointer is on your system). 最后, sizeof(responseheader)仅为4或8(无论系统上指针的大小如何)。 You are conflating the behavior of sizeof on an array with its behavior on a pointer. 您正在将sizeof在数组上的行为与其在指针上的行为混为一谈。 You really mean strlen(responseheader) in the send() call. 您的意思是在send()调用中为strlen(responseheader)

In order to fix this all, I would use two send() calls (since HTTP uses TCP, they will be seen as a single stream of bytes). 为了解决所有问题,我将使用两个send()调用(由于HTTP使用TCP,因此它们将被视为单个字节流)。 Do not modify responseheader , and simply send the header in the first call. 不要修改responseheader ,而只需在第一个调用中发送头即可。 Then, send the payload from the buffer in a second call, send(socketfd, buffer, i, 0); 然后,在第二个调用send(socketfd, buffer, i, 0);从缓冲区发送有效负载send(socketfd, buffer, i, 0); (after your for loop, i , will contain the number of bytes stored in buffer ). (在for循环之后, i将包含buffer存储的字节数)。

EDIT: also, you need to set Content-Length properly (to the size of your file of your file). 编辑:另外,您需要正确设置Content-Length (为文件的大小)。 Because of this change your response header creation to the following, with the snprintf() call AFTER your for loop to read the file: 因此,将您的响应标头创建更改为以下内容,并在for循环之后使用snprintf()调用来读取文件:

char responseheader[80];

snprintf(responseheader, sizeof(responseheader),
    "HTTP/1.1 200 OK\r\n" 
    "Content-Type: image/jpg\r\n"
    "Content-Length: %d\r\n\r\n", i
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM