我正在用 C 语言编写 HTTP 服务器。
当我发送客户端(firefox)请求的资源时,它仅发送 148 字节并且始终发送 148 字节。
这是代码:
// Send Header
if (write(writefd, http->response->response, strlen(http->response->response)) == -1)
{
printf("%s:%d : write() failed : %s\n", __FILE__, __LINE__, strerror(errno));
return INTERNAL_SERVER_ERROR;
}
printf("Response sent.\n");
size_t bytesread;
char buf[BUFSIZ];
int fdindex = open("/home/zecksidd/Documents/Code/C/lightweighttpd/site/www/index.html", O_RDONLY);
while ((bytesread = read(fdindex, buf, sizeof(buf))))
{
if (write(writefd, buf, bytesread) == -1)
{
printf("%s:%d : write() failed : %s\n", __FILE__, __LINE__, strerror(errno));
return INTERNAL_SERVER_ERROR;
}
}
printf("File sent.\n\n");
在我的终端上返回时,我收到 sigpipe 错误 13:连接被对等方重置。
发送的字节也是它们:
<div class="menu">
<h1>LightWeigHTTPd</h1>
<hr width="1850">
A light weight HTTP web server.
</div>
</body>
</html>
代替他们:
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>LightWeigHTTPd</title>
<link rel="stylesheet" type="text/css" href="/css/menu.css">
<link rel="stylesheet" type="text/css" href="/css/footer.css">
<link rel="stylesheet" type="text/css" href="/css/style.css">
</head>
<body>
<div class="menu">
<h1>LightWeigHTTPd</h1>
<hr width="1850">
A light weight HTTP web server.
</div>
</body>
</html>
我到处都放了 printf(),内容长度很好,内容类型很好,buf 中存储的内容很好,一切看起来都很好。
这是设置标题的部分(我知道这很丑,它是临时的)。
char *mime = PROCESS_GetMime();
char *key;
char *value;
// Content-type
key = alloc_n_cpy(STR_CONTENT_TYPE, strlen(STR_CONTENT_TYPE));
value = alloc_n_cpy(mime, strlen(mime));
snprintf(tmpbuf, sizeof(tmpbuf), "%s: %s\r\n", key, value);
strcat(buf, tmpbuf);
memset(tmpbuf, 0, sizeof(tmpbuf));
free(key);
free(value);
// Content-length
key = alloc_n_cpy(STR_CONTENT_LENGTH, strlen(STR_CONTENT_LENGTH));
snprintf(tmpbuf, BUFSIZ, "%ld", PROCESS_GetFileSize());
value = alloc_n_cpy(tmpbuf, strlen(tmpbuf));
printf("value = %s\n", value);
memset(tmpbuf, 0, sizeof(tmpbuf));
snprintf(tmpbuf, sizeof(tmpbuf), "%s: %s\r\n", key, value);
strcat(buf, tmpbuf);
memset(tmpbuf, 0, sizeof(tmpbuf));
free(key);
free(value);
res = (char *)calloc(strlen(buf)+1, sizeof(char));
strcpy(res, buf);