void handlefilesrequest(int fd) { struct http_request* request = httprequestparse(fd);…
- User
void handle_files_request(int fd) {
struct http_request* request = http_request_parse(fd);
if (request == NULL || request->path[0] != '/') {
http_start_response(fd, 400); http_send_header(fd, "Content-Type", "text/html"); http_end_headers(fd); close(fd); return;}
if (strstr(request->path, "..") != NULL) {
http_start_response(fd, 403); http_send_header(fd, "Content-Type", "text/html"); http_end_headers(fd); close(fd); return;}
/* Remove beginning
./*/char* path = malloc(2 + strlen(request->path) + 1);
path[0] = '.';
path[1] = '/';
memcpy(path + 2, request->path, strlen(request->path) + 1);
/*
-
TODO: PART 2 is to serve files. If the file given by
pathexists, -
call serve_file() on it. Else, serve a 404 Not Found error below.
-
The
stat()syscall will be useful here. -
TODO: PART 3 is to serve both files and directories. You will need to
-
determine when to call serve_file() or serve_directory() depending
-
on
path. Make your edits below here in this function.
*/
/* PART 2 & 3 BEGIN */
struct stat info;
int result=stat(path,&info);
if(result!=0)
{
http_start_response(fd, 404); http_send_header(fd, "Content-Type", "text/html"); http_end_headers(fd);}
else
{
if(S_ISDIR(info.st_mode)) { serve_directory(fd,path); } if(S_ISREG(info.st_mode)) { serve_file(fd,path); }close(fd);
return;
}
/* PART 2 & 3 END */
close(fd);
return;
}
-
- Assistant
The provided code is a function that handles a request for files. Here is a breakdown of its functionality:
- It first parses the HTTP request using the
http_request_parsefunction, which returns astruct http_request*containing information about the request. - If the request is
NULLor the requested path does not start with '/', it sends a 400 Bad Request response and closes the file descriptor. - If the requested path contains '..', it sends a 403 Forbidden response and closes the file descriptor. This is a security measure to prevent directory traversal attacks.
- It allocates memory for the
pathvariable and constructs it by prepending './' to the requested path. - It then checks if the file or directory specified by
pathexists using thestatfunction. If it does not exist, it sends a 404 Not Found response and closes the file descriptor.
- It first parses the HTTP request using the
- User
if(result!=0) { if (strcmp(request->path, “/”) == 0) { char index_path = malloc(strlen(path) + strlen(“index.html”) + 1); strcpy(index_path, path); strcat(index_path, “index.html”); result = stat(index_path, &info); if (result == 0) { serve_file(fd, index_path); } else { http_start_response(fd, 404); http_send_header(fd, “Content-Type”, “text/html”); http_end_headers(fd); } free(index_path); } else { http_start_response(fd, 404); http_send_header(fd, “Content-Type”, “text/html”); http_end_headers(fd); } } else { if(S_ISDIR(info.st_mode)) { serve_directory(fd,path); } if(S_ISREG(info.st_mode)) { serve_file(fd,path); } } /* PART 2 & 3 END */ close(fd); return; }
- Assistant
Here is the modified code with the additional functionality implemented for handling the case when the requested path is "/":
- User
if(result!=0) { if (strcmp(request->path, “/”) == 0) { char index_path = malloc(strlen(path) + strlen(“index.html”) + 1); strcpy(index_path, path); strcat(index_path, “index.html”); result = stat(index_path, &info); if (result == 0) { serve_file(fd, index_path); } else { http_start_response(fd, 404); http_send_header(fd, “Content-Type”, “text/html”); http_end_headers(fd); } free(index_path); } else { http_start_response(fd, 404); http_send_header(fd, “Content-Type”, “text/html”); http_end_headers(fd); } } else { if(S_ISDIR(info.st_mode)) { serve_directory(fd,path); } if(S_ISREG(info.st_mode)) { serve_file(fd,path); } }
- Assistant