我无法在基于 C 的代理中显示图像

I can't get images to display in my C based proxy

本文关键字:代理 显示 显示图 图像      更新时间:2023-10-16

因此,我正在尝试创建一个基于C的代理系统,并且很难阅读通过连接的图像数据。我可以大部分时间访问页面,但是当我尝试将图像数据从服务器发送回浏览器时,我看不到图像。以下是用于从服务器接收数据的代码,然后将消息发送到浏览器。我希望有人能有一个线索可以帮助我,谢谢!*代理阻止奇数字节内容显示,这是那里的代码的一部分,但是我要加载的图像甚至是字节#。

    do {
                    //Receive message from server
                    bytes = recv(clientsockfd, messagein, MAX_MESSAGE_LENGTH, 0);
                    if(bytes > 0)
                        strncpy(messageout,messagein,bytes);
                    else if(bytes == 0) {
                        printf("nConnection closedn");
                        strcpy(messageout,"HTTP/1.1 200 OKrnrn");
                    }
                    printf("Answer received from server: n");
                    printf("%snnTotal Bytes: %dnn", messagein, bytes);
                    //Copy the messagein to a tempStr so we can leave original message untouched
                    strncpy(tempStr,messagein,bytes);
                    //Check to see if the content is even or odd only if the message in has a content length field, later we will determine if we do anything with it
                    if(strstr(messagein,"Content-Length:") != NULL)
                        evenContent = checkContent(tempStr);
                    //If the content is odd then we want to change the message we'll send back to the browser
                    if(!evenContent) {
                        //If the page is odd then I redirect the user to carey's error page he made
                        if(strstr(messagein,"text/html") != NULL)
                            strcpy(messageout,"HTTP/1.1 302 FoundrnLocation: http://pages.cpsc.ucalgary.ca/~carey/CPSC441/errorpage.htmlrnrn");
                        else if(strstr(messagein,"image/") != NULL) {
                            //If the image is odd then I just send back empty data to the browser
                            strcpy(messageout,"HTTP/1.1 200 OKrnrn");
                        }
                    }
#ifdef DEBUG
                    printf("nnAbout to send message to the web browser:n%s",messageout);
#endif
                    /* send the result message back to the browser */
                    send(childsockfd, messageout, bytes, 0);
                    /* initialize message strings just to be safe (null-terminated) */
                    for( i = 0; i < MAX_MESSAGE_LENGTH; i++ )
                    {
                        messagein[i] = '';
                        messageout[i] = '';
                        tempStr[i] = '';
                    }
                } while (bytes != 0);

您在此处假设通过单个呼叫recv一起获得标题和内容。但是对于可能不正确的大要求,图像通常很大)。要进行解析,您应该致电recv,直到您进行解析为止,包括内容长度,然后致电recv,直到收到内容长度的字节。