mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Fix crash if a file gets deleted but still registered.
This commit is contained in:
parent
4364fb9da8
commit
3694db51b3
@ -1,2 +1,3 @@
|
||||
bin/*
|
||||
|
||||
bin/**
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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);
|
||||
|
||||
|
@ -1 +0,0 @@
|
||||
ffqfqfq
|
@ -1 +0,0 @@
|
||||
qqq
|
@ -1,7 +1,5 @@
|
||||
<html>
|
||||
|
||||
<body>
|
||||
<p>dadadada</p>
|
||||
<p>Welcome</p>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user