Skip to content

Commit

Permalink
fix path (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 17, 2023
1 parent 5ebe333 commit a001eab
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion include/cinatra/coro_http_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "sha1.hpp"
#include "string_resize.hpp"
#include "websocket.hpp"
#include "ylt/coro_io/coro_file.hpp"
#include "ylt/coro_io/coro_io.hpp"

namespace cinatra {
Expand Down Expand Up @@ -108,7 +109,7 @@ class coro_http_connection
auto [ec, size] = co_await async_read_until(head_buf_, TWO_CRCF);
if (ec) {
if (ec != asio::error::eof) {
CINATRA_LOG_ERROR << "read http header error: " << ec.message();
CINATRA_LOG_WARNING << "read http header error: " << ec.message();
}
close();
break;
Expand Down
18 changes: 13 additions & 5 deletions include/cinatra/coro_http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "cinatra/utils.hpp"
#include "cinatra_log_wrapper.hpp"
#include "coro_http_connection.hpp"
#include "ylt/coro_io/coro_file.hpp"
#include "ylt/coro_io/coro_io.hpp"
#include "ylt/coro_io/io_context_pool.hpp"

Expand Down Expand Up @@ -188,8 +189,8 @@ class coro_http_server {

void set_transfer_chunked_size(size_t size) { chunked_size_ = size; }

void set_static_res_handler(std::string_view uri_suffix = "",
std::string file_path = "www") {
void set_static_res_dir(std::string_view uri_suffix = "",
std::string file_path = "www") {
bool has_double_dot = (file_path.find("..") != std::string::npos) ||
(uri_suffix.find("..") != std::string::npos);
if (std::filesystem::path(file_path).has_root_path() ||
Expand Down Expand Up @@ -228,9 +229,16 @@ class coro_http_server {
if (size_t pos = relative_path.find('\\') != std::string::npos) {
replace_all(relative_path, "\\", "/");
}
uri = std::string("/")
.append(static_dir_router_path_)
.append(relative_path);

if (static_dir_router_path_.empty()) {
uri = relative_path;
}
else {
uri = fs::path("/")
.append(static_dir_router_path_)
.concat(relative_path)
.string();
}

set_http_handler<cinatra::GET>(
uri,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ TEST_CASE("test coro_http_client chunked upload and download") {
{
// chunked download, not in cache
coro_http_server server(1, 8090);
server.set_static_res_handler("download");
server.set_static_res_dir("download");
server.set_max_size_of_cache_files(100);
server.set_transfer_chunked_size(100);
server.async_start();
Expand Down
2 changes: 1 addition & 1 deletion tests/test_coro_http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ TEST_CASE("test http download server") {
// curl http://127.0.0.1:9001/download/test_download.txt will download
// test_download.txt file
server.set_transfer_chunked_size(100);
server.set_static_res_handler("download", "");
server.set_static_res_dir("download", "");
server.async_start();
std::this_thread::sleep_for(200ms);

Expand Down

0 comments on commit a001eab

Please sign in to comment.