Fix crash if a file gets deleted but still registered.

This commit is contained in:
Relintai 2020-11-25 13:22:43 +01:00
parent 4364fb9da8
commit 3694db51b3
7 changed files with 16 additions and 7 deletions

View File

@ -1,2 +1,3 @@
bin/*
bin/**

View File

@ -95,6 +95,14 @@ void Application::send_file(const std::string &path, Request *request) {
std::string fp = FileCache::get_instance()->wwwroot + path;
FILE *f = fopen(fp.c_str(), "rb");
if (!f) {
printf("Error: Registered file doesn't exists anymore! %s\n", path.c_str());
send_error(404, request);
return;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
@ -105,6 +113,8 @@ void Application::send_file(const std::string &path, Request *request) {
fread(&body[0], 1, fsize, f);
fclose(f);
//TODO set mimetype?
request->response->setBody(body);
request->finalized = true;
}

View File

@ -13,7 +13,7 @@ void FileCache::deregister_file(const std::string &file_path) {
}
bool FileCache::has_file(const std::string &file_path) {
return registered_files.find(file_path) != registered_files.end();
return registered_files.find(file_path) != registered_files.end();
}
void FileCache::refresh() {

View File

@ -8,6 +8,8 @@ class FileCache {
public:
std::string wwwroot;
//Note: file path should be the url you want to access the file with, inculding lead slash
//e.g. http://127.0.0.1/a/b/d.jpg -> /a/b/d.jpg
void register_file(const std::string &file_path);
void deregister_file(const std::string &file_path);

View File

@ -1 +0,0 @@
ffqfqfq

View File

@ -1 +0,0 @@
qqq

View File

@ -1,7 +1,5 @@
<html>
<body>
<p>dadadada</p>
<p>Welcome</p>
</body>
</html>