From 5602072424608fc51c9b21d42b9c7002057e0417 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 29 Jan 2024 16:46:23 +0800 Subject: [PATCH 1/8] support gzip --- include/cinatra/coro_http_response.hpp | 25 +++++++++++++++++++++++-- tests/test_cinatra.cpp | 12 +++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/include/cinatra/coro_http_response.hpp b/include/cinatra/coro_http_response.hpp index 4b8c3b88..02d37f60 100644 --- a/include/cinatra/coro_http_response.hpp +++ b/include/cinatra/coro_http_response.hpp @@ -12,6 +12,7 @@ #include "async_simple/coro/SyncAwait.h" #include "cookie.hpp" #include "define.h" +#include "gzip.hpp" #include "response_cv.hpp" #include "time_util.hpp" #include "utils.hpp" @@ -46,9 +47,29 @@ class coro_http_response { content_ = std::move(content); has_set_content_ = true; } - void set_status_and_content(status_type status, std::string content = "") { + void set_status_and_content( + status_type status, std::string content = "", + content_encoding encoding = content_encoding::none) { status_ = status; - content_ = std::move(content); +#ifdef CINATRA_ENABLE_GZIP + if (encoding == content_encoding::gzip) { + std::string encode_str; + bool r = gzip_codec::compress( + std::string_view(content.data(), content.length()), encode_str, true); + if (!r) { + set_status_and_content(status_type::internal_server_error, + "gzip compress error"); + } + else { + add_header("Content-Encoding", "gzip"); + set_content(std::move(encode_str)); + } + } + else +#endif + { + content_ = std::move(content); + } has_set_content_ = true; } void set_delay(bool r) { delay_ = r; } diff --git a/tests/test_cinatra.cpp b/tests/test_cinatra.cpp index 69812a43..d57f8ac5 100644 --- a/tests/test_cinatra.cpp +++ b/tests/test_cinatra.cpp @@ -24,12 +24,10 @@ using namespace std::chrono_literals; using namespace cinatra; #ifdef CINATRA_ENABLE_GZIP -std::string_view get_header_value( - std::vector> &resp_headers, - std::string_view key) { - for (const auto &p : resp_headers) { - if (p.first == key) - return std::string_view(p.second.data(), p.second.size()); +std::string_view get_header_value(auto &resp_headers, std::string_view key) { + for (const auto &[k, v] : resp_headers) { + if (k == key) + return v; } return {}; } @@ -40,7 +38,7 @@ TEST_CASE("test for gzip") { "/gzip", [](coro_http_request &req, coro_http_response &res) { CHECK(req.get_header_value("Content-Encoding") == "gzip"); res.set_status_and_content(status_type::ok, "hello world", - req_content_type::none); // TODO + content_encoding::gzip); }); server.async_start(); From 0dbfa765f9e408ca03aa5a41164d02295f308627 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 29 Jan 2024 16:59:22 +0800 Subject: [PATCH 2/8] for win --- include/cinatra/coro_http_response.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/cinatra/coro_http_response.hpp b/include/cinatra/coro_http_response.hpp index 02d37f60..d035a466 100644 --- a/include/cinatra/coro_http_response.hpp +++ b/include/cinatra/coro_http_response.hpp @@ -12,7 +12,9 @@ #include "async_simple/coro/SyncAwait.h" #include "cookie.hpp" #include "define.h" +#ifdef CINATRA_ENABLE_GZIP #include "gzip.hpp" +#endif #include "response_cv.hpp" #include "time_util.hpp" #include "utils.hpp" From aa4757fb5b600630b2574ab7d55bac83acb971ad Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 29 Jan 2024 17:03:57 +0800 Subject: [PATCH 3/8] verify none --- include/cinatra/coro_http_client.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 3d8b46b2..43a40260 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -16,6 +16,7 @@ #include "asio/dispatch.hpp" #include "asio/error.hpp" +#include "asio/ssl/verify_mode.hpp" #include "asio/streambuf.hpp" #include "async_simple/Future.h" #include "async_simple/Unit.h" @@ -246,7 +247,7 @@ class coro_http_client : public std::enable_shared_from_this { return true; } - [[nodiscard]] bool init_ssl(int verify_mode = asio::ssl::verify_peer, + [[nodiscard]] bool init_ssl(int verify_mode = asio::ssl::verify_none, std::string full_path = "", const std::string &sni_hostname = "") { std::string base_path; From ac7e17b61a65a80226f1c4e133a332a05ca68813 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 29 Jan 2024 17:09:28 +0800 Subject: [PATCH 4/8] compile --- include/cinatra/coro_http_client.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 43a40260..c0265174 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -247,8 +247,7 @@ class coro_http_client : public std::enable_shared_from_this { return true; } - [[nodiscard]] bool init_ssl(int verify_mode = asio::ssl::verify_none, - std::string full_path = "", + [[nodiscard]] bool init_ssl(int verify_mode = 0, std::string full_path = "", const std::string &sni_hostname = "") { std::string base_path; std::string cert_file; From d3c9ad98bdd2ca6147e5670c8c5b895b35cb29ad Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 29 Jan 2024 17:12:39 +0800 Subject: [PATCH 5/8] compile --- include/cinatra/coro_http_client.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index c0265174..43a40260 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -247,7 +247,8 @@ class coro_http_client : public std::enable_shared_from_this { return true; } - [[nodiscard]] bool init_ssl(int verify_mode = 0, std::string full_path = "", + [[nodiscard]] bool init_ssl(int verify_mode = asio::ssl::verify_none, + std::string full_path = "", const std::string &sni_hostname = "") { std::string base_path; std::string cert_file; From d761404683d6d4567eddfbdc42d755aea89faee2 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 29 Jan 2024 17:19:01 +0800 Subject: [PATCH 6/8] remove header --- include/cinatra/coro_http_client.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/cinatra/coro_http_client.hpp b/include/cinatra/coro_http_client.hpp index 43a40260..34219e57 100644 --- a/include/cinatra/coro_http_client.hpp +++ b/include/cinatra/coro_http_client.hpp @@ -16,7 +16,6 @@ #include "asio/dispatch.hpp" #include "asio/error.hpp" -#include "asio/ssl/verify_mode.hpp" #include "asio/streambuf.hpp" #include "async_simple/Future.h" #include "async_simple/Unit.h" From a8e6921447a383de5dcbb30408da302014ccd670 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 29 Jan 2024 17:26:58 +0800 Subject: [PATCH 7/8] ignore --- .github/workflows/linux_llvm_cov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linux_llvm_cov.yml b/.github/workflows/linux_llvm_cov.yml index 41fb0d67..e7269165 100644 --- a/.github/workflows/linux_llvm_cov.yml +++ b/.github/workflows/linux_llvm_cov.yml @@ -51,7 +51,7 @@ jobs: echo "Code Coverage Report" > tmp.log echo "for detail, [goto summary](https://github.com/${{ github.repository_owner }}/${{ github.event.repository.name }}/actions/runs/${{github.run_id}}) download Artifacts `llvm-cov`" >> tmp.log echo "\`\`\`" >> tmp.log - llvm-cov report ./test_cinatra -instr-profile=test_cinatra.profdata -ignore-filename-regex="thirdparty|tests" -show-region-summary=false >> tmp.log + llvm-cov report ./test_cinatra -instr-profile=test_cinatra.profdata -ignore-filename-regex="asio|cmdline|async_simple|tests" -show-region-summary=false >> tmp.log echo "\`\`\`" >> tmp.log - name: Create Comment From f2f1187a866a3e44e8167f099016d4f78ea80cbc Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 29 Jan 2024 17:32:59 +0800 Subject: [PATCH 8/8] ignore --- .github/workflows/linux_llvm_cov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/linux_llvm_cov.yml b/.github/workflows/linux_llvm_cov.yml index e7269165..76ae2b09 100644 --- a/.github/workflows/linux_llvm_cov.yml +++ b/.github/workflows/linux_llvm_cov.yml @@ -36,7 +36,7 @@ jobs: cd tests ./test_cinatra ./test_corofile llvm-profdata merge -sparse test_cinatra-*.profraw -o test_cinatra.profdata - llvm-cov show ./test_cinatra -instr-profile=test_cinatra.profdata -format=html -output-dir=../.coverage_llvm_cov -ignore-filename-regex="async_simple|thirdparty|tests|asio|util" -show-instantiations=false + llvm-cov show ./test_cinatra -instr-profile=test_cinatra.profdata -format=html -output-dir=../.coverage_llvm_cov -ignore-filename-regex="asio|cmdline|async_simple|tests" -show-instantiations=false echo "Done!" - name: Upload Coverage Results