【发布时间】:2018-06-01 19:07:10
【问题描述】:
我用C编程语言编写了一个Web服务器,并根据URL中提供的请求,Web服务器完全成功地检索了相关网页!以下是处理URL请求的部分代码!
if ( strncmp(reqline[1], "/\0", 2)==0 )
reqline[1] = "/index.html";
//If no file is specified, index.html will be opened by default
strcpy(path, ROOT);
strcpy(&path[strlen(ROOT)], reqline[1]);
printf("file: %s\n", path);
if ( (fd=open(path, O_RDONLY))!=-1 ) //If a html file is found
{
send(clients[n], "HTTP/1.0 200 OK\n\n", 17, 0);
while ( (bytes_read=read(fd, data_to_send, BYTES))>0 )
write (clients[n], data_to_send, bytes_read);
}
else { //If html file not found
write (clients[n], "File not found", 15);
}
当用户提供一个不存在的 html 文件的 URL 而不是文本“文件不找到”当前显示的!
【问题讨论】:
-
为什么它与你为
index.html做的方式不同? -
有什么关系?您想读取文件并将其发送给客户端。你的代码中已经有了这个序列。
-
那么你需要了解为什么它不起作用。
-
真的是你的代码吗?
-
我很难相信你写了这段代码,但不知道如何返回 404 页面。似乎不是您编写代码,现在要求您为其添加更多功能。
标签: html c error-handling webserver webpage