changed values for send_file_mode config parameter:

0 - winix will read the content of the file and send it back to the webserver
  1 - winix will use send_file_header header with a full path to the file
  2 - winix will use send_file_header header with a relative path to the file
This commit is contained in:
Tomasz Sowa 2021-09-17 03:35:56 +02:00
parent 55ac9a61ed
commit 26ed7b80be
5 changed files with 90 additions and 29 deletions

View File

@ -1494,25 +1494,28 @@ void App::PrepareHeaderContentType()
{ {
if( !cur.request->out_headers.has_key(L"Content-Type") ) if( !cur.request->out_headers.has_key(L"Content-Type") )
{ {
if( cur.request->return_json ) if( !cur.request->send_bin_stream )
{ {
cur.request->out_headers.add(L"Content-Type", L"application/json; charset=UTF-8"); if( cur.request->return_json )
}
else
{
switch( config.content_type_header )
{ {
case 1: cur.request->out_headers.add(L"Content-Type", L"application/json; charset=UTF-8");
cur.request->out_headers.add(L"Content-Type", L"application/xhtml+xml; charset=UTF-8"); }
break; else
{
case 2: switch( config.content_type_header )
cur.request->out_headers.add(L"Content-Type", L"application/xml; charset=UTF-8"); {
break; case 1:
cur.request->out_headers.add(L"Content-Type", L"application/xhtml+xml; charset=UTF-8");
break;
case 0: case 2:
default: cur.request->out_headers.add(L"Content-Type", L"application/xml; charset=UTF-8");
cur.request->out_headers.add(L"Content-Type", L"text/html; charset=UTF-8"); break;
case 0:
default:
cur.request->out_headers.add(L"Content-Type", L"text/html; charset=UTF-8");
}
} }
} }
} }

View File

@ -544,22 +544,25 @@ public:
std::wstring title_separator; std::wstring title_separator;
// how to send static files (uploaded by users) to the webserver // how to send static files (uploaded by users) to the webserver
// 0 - full path to a file in send_file_header header // 0 - winix will read the content of the file and send it back to the webserver
// 1 - relative path to a file in send_file_header (need http_send_file_relative_prefix set) // 1 - winix will use send_file_header header with a full path to the file
// 2 - winix will use send_file_header header with a relative path to the file
// default: 0 // default: 0
// for Apache set: 0 // 0 can be be used with all webservers but it requires to copy the whole file content
// for Lighttpd set: 0 // you can omit copying the content with values 1 or 2:
// for Nginx set: 1 // for Apache set: 1
// for Lighttpd set: 1
// for Nginx set: 2
int send_file_mode; int send_file_mode;
// http header recognized by www server as a file to send back // http header recognized by www server as a file to send back, used if send_file_mode is 1 or 2
// default: X-SENDFILE // default: X-SENDFILE
// for Apache set: X-SENDFILE // for Apache set: X-SENDFILE (Apache needs an external module: https://tn123.org/mod_xsendfile/)
// for Lighttpd set: X-LIGHTTPD-send-file // for Lighttpd set: X-LIGHTTPD-send-file (https://redmine.lighttpd.net/projects/1/wiki/X-LIGHTTPD-send-file)
// for Nginx set: X-Accel-Redirect // for Nginx set: X-Accel-Redirect (https://nginx.org/en/docs/http/ngx_http_core_module.html#internal)
std::wstring send_file_header; std::wstring send_file_header;
// relative prefix used for sending static files if send_file_mode is 1 // relative prefix used for sending static files if send_file_mode is 2
// default: "upload-files-internal" // default: "upload-files-internal"
// this prefix is added at the beginning of a relative file path e.g. // this prefix is added at the beginning of a relative file path e.g.
// /upload-files-internal/simplefs/normal/some_directories/file.jpg // /upload-files-internal/simplefs/normal/some_directories/file.jpg

View File

@ -1162,6 +1162,45 @@ bool GetUTF8File(const std::wstring & file_path, std::wstring & content, bool cl
} }
bool GetBinaryFile(const wchar_t * file_path, BinaryPage & content, bool clear_content)
{
char file[WINIX_OS_PATH_SIZE];
char buffer[4096];
size_t buffer_len = sizeof(buffer) / sizeof(char);
std::ifstream get_file_content;
if( clear_content )
content.clear();
if( !wide_to_utf8(file_path, file, WINIX_OS_PATH_SIZE) )
return false;
get_file_content.open(file, std::ios_base::in | std::ios_base::binary);
if( !get_file_content )
return false;
do
{
get_file_content.read(buffer, buffer_len);
content.write(buffer, get_file_content.gcount());
}
while( !get_file_content.eof() && get_file_content.good() );
get_file_content.close();
return true;
}
bool GetBinaryFile(const std::wstring & file_path, BinaryPage & content, bool clear_content)
{
return GetBinaryFile(file_path.c_str(), content, clear_content);
}
// if there is no an extension it returns a pointer to the last '\0' character // if there is no an extension it returns a pointer to the last '\0' character
const wchar_t * GetFileExt(const wchar_t * name) const wchar_t * GetFileExt(const wchar_t * name)
{ {

View File

@ -662,6 +662,8 @@ bool RenameFile(const std::wstring & from, const std::wstring & to);
bool GetUTF8File(const wchar_t * file_path, std::wstring & content, bool clear_content = true); bool GetUTF8File(const wchar_t * file_path, std::wstring & content, bool clear_content = true);
bool GetUTF8File(const std::wstring & file_path, std::wstring & content, bool clear_content = true); bool GetUTF8File(const std::wstring & file_path, std::wstring & content, bool clear_content = true);
bool GetBinaryFile(const wchar_t * file_path, BinaryPage & content, bool clear_content = true);
bool GetBinaryFile(const std::wstring & file_path, BinaryPage & content, bool clear_content = true);
const wchar_t * GetFileExt(const wchar_t * name); const wchar_t * GetFileExt(const wchar_t * name);

View File

@ -73,18 +73,32 @@ void Download::MakeGet()
cur->request->send_as_attachment = cur->request->IsParam(L"attachment"); 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")); bool is_thumb = (cur->request->item.item_content.file_has_thumb && cur->request->IsParam(L"thumb"));
if( config->send_file_mode == 0 ) if( config->send_file_mode == 0 || config->send_file_mode == 1 )
{ {
system->MakeFilePath(cur->request->item, cur->request->x_sendfile, is_thumb); system->MakeFilePath(cur->request->item, cur->request->x_sendfile, is_thumb);
if( config->send_file_mode == 0 )
{
log << log3 << "Download: reading content of file: " << cur->request->x_sendfile << logend;
if( !GetBinaryFile(cur->request->x_sendfile, cur->request->out_bin_stream) )
{
log << log1 << "Download: I cannot read the content of the file: " << cur->request->x_sendfile << logend;
}
// IMPROVEME it would be good to set Content-Type header too
cur->request->x_sendfile.clear();
cur->request->send_bin_stream = true;
}
} }
else else
if( config->send_file_mode == 1 ) if( config->send_file_mode == 2 )
{ {
system->MakeRelativeFilePath(cur->request->item, config->send_file_relative_prefix, cur->request->x_sendfile, is_thumb); system->MakeRelativeFilePath(cur->request->item, config->send_file_relative_prefix, cur->request->x_sendfile, is_thumb);
} }
else else
{ {
log << log1 << "Download: send_file_mode in the config should be either 0 or 1" << logend; log << log1 << "Download: send_file_mode in the config should be either 0, 1 or 2" << logend;
} }
} }