Skip to content

Commit b7c38f1

Browse files
authored
fix: improve SSL initialization and error handling for file responses (#700)
1 parent e329293 commit b7c38f1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

include/cinatra/coro_http_server.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class coro_http_server {
6464

6565
#ifdef CINATRA_ENABLE_SSL
6666
void init_ssl(const std::string &cert_file, const std::string &key_file,
67-
const std::string &passwd) {
67+
const std::string &passwd = "") {
6868
cert_file_ = cert_file;
6969
key_file_ = key_file;
7070
passwd_ = passwd;
@@ -414,15 +414,20 @@ class coro_http_server {
414414
coro_io::coro_file in_file{};
415415
in_file.open(file_name, std::ios::in);
416416
if (!in_file.is_open()) {
417+
#ifndef NDEBUG
417418
resp.set_status_and_content(status_type::not_found,
418-
file_name + "not found");
419+
file_name + " not found");
420+
#else
421+
resp.set_status(status_type::not_found);
422+
#endif
419423
co_return;
420424
}
421425

422426
size_t file_size = fs::file_size(file_name);
423427

424428
if (format_type_ == file_resp_format_type::chunked &&
425429
range_str.empty()) {
430+
resp.add_header("Content-Type", std::string{mime});
426431
resp.set_format_type(format_type::chunked);
427432
bool ok;
428433
if (ok = co_await resp.get_conn()->begin_chunked(); !ok) {
@@ -455,8 +460,7 @@ class coro_http_server {
455460
if (pos != std::string_view::npos) {
456461
range_str = range_str.substr(pos + 1);
457462
bool is_valid = true;
458-
auto ranges =
459-
parse_ranges(range_str, fs::file_size(file_name), is_valid);
463+
auto ranges = parse_ranges(range_str, file_size, is_valid);
460464
if (!is_valid) {
461465
resp.set_status(status_type::range_not_satisfiable);
462466
co_return;
@@ -821,7 +825,9 @@ class coro_http_server {
821825
header_str.append(content_range);
822826
}
823827
header_str.append("Content-Disposition: attachment;filename=");
824-
header_str.append(filename).append("\r\n");
828+
std::string short_name =
829+
std::filesystem::path(filename).filename().string();
830+
header_str.append(short_name).append("\r\n");
825831
header_str.append("Connection: keep-alive\r\n");
826832
header_str.append("Content-Type: ").append(mime).append("\r\n");
827833
header_str.append("Content-Length: ");

0 commit comments

Comments
 (0)