【发布时间】:2015-06-10 06:59:33
【问题描述】:
我正在制作一个套接字服务器,例如使用“localhost:8080/WWW/index.html”之类的输入,它将返回 index.html 文件。我能够解析请求,以便当有人输入我将有一个“/WWW/index.html”的缓冲区并且如果我执行 vim /WWW/index.html 从我的程序的位置我会打开如果但是出于某种原因,我的代码总是说找不到该文件。下面是代码...
char* parseRequest(char* request) {
//assume file paths are no more than 256 bytes + 1 for null.
char *buffer = malloc(sizeof(char)*257);
memset(buffer, 0, 257);
if(fnmatch("GET * HTTP/1.*", request, 0)) return 0;
sscanf(request, "GET %s HTTP/1.", buffer);
return buffer;
}
int fileExists(const char *fname){
FILE *file;
if(file = fopen(fname, "r"))
{
fclose(file);
return 1;
}
return 0;
}
recv(sock,buffer,255,0);
//reading inputted directory
char *dir = parseRequest(buffer);
if(fileExists(dir) == 1){
send(sock, "File found", 200, 0);
}else{
send(sock, "404: File not found", 200, 0);
}
【问题讨论】:
-
发布的代码正试图访问一个网页以供阅读:index.html。除非服务器设置非常草率,否则服务器应该拒绝客户端访问读取 index.html 文件。服务器中有一个特定区域,通常/应该设置,允许客户端访问文件。所有其他目录/文件都应设置为“拒绝”权限。