내 작업은 간단한 HTTP 서버를 구현하는 것입니다. HTTP를 통해 다운로드하는 사이트를 열려고합니다. 내 서버에 html, css 및 js 파일이 제대로 표시되지만 이미지 (png, jpg)가 표시되지 않습니다. Wireshark를 사용하여 응답 패킷을 기록했습니다.c- 소켓의 HTTP 서버가 올바른 이미지를 표시하지 않습니다.
HTTP/1.1 200 OK
Connection: keep-alive
Transfer-Encoding: chunked
.PNG
.
또한 요청을 읽고 클라이언트에 응답을 보내는 기능입니다.
static int serve_request(int sock, struct conf_arg *arg, char version[])
{
FILE *html = NULL;
char buf[MAX_MSG];
const unsigned chunk = (unsigned)CHUNK_SIZE;
char *pbuf = NULL;
char tempbuf[CHUNK_SIZE + 3];
size_t len;
strcpy(buf, arg->root);
if(buf[strlen(buf) - 1] == '/')
strcat(buf, arg->defdoc);
html = fopen(buf, "rb");
if (!html) {
not_found(sock, version);
return -1;
}
good_responce(sock, version);
do {
if (fgets(buf, sizeof(buf), html) == NULL)
break;
pbuf = buf;
while ((len = strlen(pbuf)) != 0) {
if (len < chunk) {
//printf("LEN:%d\n", (int)len);
sprintf(tempbuf, "%x\r\n", (int)len);
write(sock, tempbuf, strlen(tempbuf));
write(sock, pbuf, strlen(pbuf));
//printf("%s", pbuf);
pbuf += len;
write(sock, "\r\n", 2);
} else {
sprintf(tempbuf, "%x\r\n", (int)chunk);
write(sock, tempbuf, strlen(tempbuf));
//strncpy(tempbuf, pbuf, chunk);
//printf("%d\n%s\n", (int)chunk, tempbuf);
write(sock, pbuf, chunk);
pbuf += chunk;
write(sock, "\r\n", 2);
}
}
} while (!feof(html));
strcpy(tempbuf, "0\r\n\r\n");
write(sock, tempbuf, strlen(tempbuf));
fclose(html);
return 0;
}
어디서 문제인지 파악할 수 없으므로 도와주세요.
UPD : 열린 모드를 rb로 변경했지만 도움이되지 않습니다. 나는 Wireshark에서 같은 결과를 얻는다.
파일을 텍스트 모드로 열면 문제가 될 수 있습니다. –
그리고 어떤 모드를 사용해야합니까? –
바이너리 모드. 그건 물론 문제가 –