Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support gzip #507

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/linux_llvm_cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class coro_http_client : public std::enable_shared_from_this<coro_http_client> {
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;
Expand Down
27 changes: 25 additions & 2 deletions include/cinatra/coro_http_response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +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"
Expand Down Expand Up @@ -46,9 +49,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; }
Expand Down
12 changes: 5 additions & 7 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ using namespace std::chrono_literals;
using namespace cinatra;

#ifdef CINATRA_ENABLE_GZIP
std::string_view get_header_value(
std::vector<std::pair<std::string, std::string>> &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 {};
}
Expand All @@ -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();

Expand Down
Loading