added possibility to send static files to nginx via X-Accel-Redirect header

added to config:
int send_file_mode;
    // 0 - full path to a file in send_file_header header
    // 1 - relative path to a file in send_file_header (need http_send_file_relative_prefix set) (used for nginx)
std::wstring send_file_header;
    // default: X-SENDFILE
    // for Apache set: X-SENDFILE
    // for Lighttpd set: X-LIGHTTPD-send-file
    // for Nginx set: X-Accel-Redirect
std::wstring send_file_relative_prefix;
    // relative prefix used for sending static files if send_file_mode is 1
    // default: "upload-files-internal"
This commit is contained in:
Tomasz Sowa 2021-09-15 20:28:34 +02:00
parent 7673264fe1
commit 55ac9a61ed
6 changed files with 90 additions and 8 deletions

View File

@ -1481,7 +1481,11 @@ void App::PrepareHeadersStatic()
AddHeader(L"Status", L"200 OK");
if( AddHeader(config.http_header_send_file, path) )
/*
* FIX ME now we can send full path (apache, lighttpd) and relative path (nginx)
* but this feature for mounting static content probably will be removed
*/
if( AddHeader(config.send_file_header, path) )
log << log2 << "App: sending a file from a static mountpoint: " << path << logend;
}
@ -1558,7 +1562,7 @@ void App::PrepareHeadersSendFile()
{
AddHeader(L"Status", L"200 OK");
if( AddHeader(config.http_header_send_file, cur.request->x_sendfile) )
if( AddHeader(config.send_file_header, cur.request->x_sendfile) )
log << log2 << "App: sending file: " << cur.request->x_sendfile << logend;
}

View File

@ -251,7 +251,9 @@ void Config::AssignValues(bool stdout_is_closed)
title_separator = Text(L"title_separator", L" / ");
http_header_send_file = Text(L"http_header_send_file", L"X-LIGHTTPD-send-file");
send_file_mode = Int(L"send_file_mode", 0);
send_file_header = Text(L"send_file_header", L"X-SENDFILE");
send_file_relative_prefix = Text(L"send_file_relative_prefix", L"upload-files-internal");
editors_html_safe_mode = Bool(L"editors_html_safe_mode", true);
editors_html_safe_mode_skip_root = Bool(L"editors_html_safe_mode_skip_root", true);

View File

@ -543,9 +543,36 @@ public:
// separator used in <title> html tag
std::wstring title_separator;
// how to send static files (uploaded by users) to the webserver
// 0 - full path to a file in send_file_header header
// 1 - relative path to a file in send_file_header (need http_send_file_relative_prefix set)
// default: 0
// for Apache set: 0
// for Lighttpd set: 0
// for Nginx set: 1
int send_file_mode;
// http header recognized by www server as a file to send back
// default: X-LIGHTTPD-send-file
std::wstring http_header_send_file;
// default: X-SENDFILE
// for Apache set: X-SENDFILE
// for Lighttpd set: X-LIGHTTPD-send-file
// for Nginx set: X-Accel-Redirect
std::wstring send_file_header;
// relative prefix used for sending static files if send_file_mode is 1
// default: "upload-files-internal"
// this prefix is added at the beginning of a relative file path e.g.
// /upload-files-internal/simplefs/normal/some_directories/file.jpg
//
// in Nginx config file use 'location' with the prefix, e.g:
// server {
// .....
// location /upload-files-internal/ {
// alias /path/to/winix/upload/; # trailing slash at the end
// internal;
// }
// }
std::wstring send_file_relative_prefix;
// in editors (emacs, ckeditor,...) the html will be filtered and unsafe tags
// will be dropped (script, frame, etc.)

View File

@ -971,6 +971,43 @@ return true;
}
bool System::MakeRelativeFilePath(const Item & item, const std::wstring & path_prefix, std::wstring & path, bool thumb)
{
path.clear();
if( item.item_content.file_path.empty() || item.item_content.file_type == WINIX_ITEM_FILETYPE_NONE )
{
log << log1 << "System: MakePath: this item has not a static file" << logend;
return false;
}
// we allow the prefix to be empty
if( !path_prefix.empty() )
{
if( path_prefix[0] != '/' )
path += '/';
path += path_prefix;
TrimLast(path, '/');
}
if( item.item_content.file_fs == mounts.MountFsHashfs() )
path += L"/hashfs";
else
path += L"/simplefs";
if( thumb )
path += L"/thumb";
else
path += L"/normal";
path += '/';
path += item.item_content.file_path;
return true;
}
// item can be a directory, file or a symlink
// if item is a directory then the path will be with a slash at the end
bool System::MakePath(const Item & item, std::wstring & path, bool clear_path)

View File

@ -178,6 +178,8 @@ public:
bool CreateNewFile(Item & item);
bool MakeFilePath(const Item & item, std::wstring & path, bool thumb = false, bool create_dir = false, int chmod = 0755, int group = -1);
bool MakeRelativeFilePath(const Item & item, const std::wstring & path_prefix, std::wstring & path, bool thumb = false);
bool MakePath(const Item & item, std::wstring & path, bool clear_path = true);
bool AddFile(Item & item, int notify_code = 0, bool call_plugins = true);

View File

@ -71,11 +71,21 @@ void Download::MakeGet()
}
cur->request->send_as_attachment = cur->request->IsParam(L"attachment");
bool is_thumb = (cur->request->item.item_content.file_has_thumb && cur->request->IsParam(L"thumb"));
if( cur->request->item.item_content.file_has_thumb && cur->request->IsParam(L"thumb") )
system->MakeFilePath(cur->request->item, cur->request->x_sendfile, true);
if( config->send_file_mode == 0 )
{
system->MakeFilePath(cur->request->item, cur->request->x_sendfile, is_thumb);
}
else
system->MakeFilePath(cur->request->item, cur->request->x_sendfile);
if( config->send_file_mode == 1 )
{
system->MakeRelativeFilePath(cur->request->item, config->send_file_relative_prefix, cur->request->x_sendfile, is_thumb);
}
else
{
log << log1 << "Download: send_file_mode in the config should be either 0 or 1" << logend;
}
}