Skip to content

Commit 9994366

Browse files
committed
Fixed Download() not setting header
1 parent 6b34aa6 commit 9994366

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

cpr/session.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,10 @@ std::shared_ptr<CurlHolder> Session::Impl::GetCurlHolder() {
675675

676676
Response Session::Impl::makeDownloadRequest() {
677677
assert(curl_->handle);
678+
679+
// Set Header:
680+
SetHeaderInternal();
681+
678682
const std::string parametersContent = parameters_.GetContent(*curl_);
679683
if (!parametersContent.empty()) {
680684
Url new_url{url_ + "?" + parametersContent};

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ add_cpr_test(structures)
5656
add_cpr_test(encoded_auth)
5757
add_cpr_test(proxy_auth)
5858
add_cpr_test(version)
59+
add_cpr_test(download)
5960

6061
if (ENABLE_SSL_TESTS)
6162
add_cpr_test(ssl)

test/download_tests.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <cstddef>
2+
#include <gtest/gtest.h>
3+
4+
#include <string>
5+
6+
#include <cpr/cpr.h>
7+
8+
#include "cpr/api.h"
9+
#include "cpr/callback.h"
10+
#include "cpr/cprtypes.h"
11+
#include "cpr/session.h"
12+
#include "httpServer.hpp"
13+
14+
15+
static cpr::HttpServer* server = new cpr::HttpServer();
16+
17+
bool write_data(std::string /*data*/, intptr_t /*userdata*/) {
18+
return true;
19+
}
20+
21+
TEST(DownloadTests, DownloadGzip) {
22+
cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
23+
cpr::Session session;
24+
session.SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
25+
session.SetUrl(url);
26+
cpr::Response response = session.Download(cpr::WriteCallback{write_data, 0});
27+
EXPECT_EQ(url, response.url);
28+
EXPECT_EQ(200, response.status_code);
29+
EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
30+
}
31+
32+
int main(int argc, char** argv) {
33+
::testing::InitGoogleTest(&argc, argv);
34+
::testing::AddGlobalTestEnvironment(server);
35+
return RUN_ALL_TESTS();
36+
}

test/httpServer.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,32 @@ void HttpServer::OnRequestPatchNotAllowed(mg_connection* conn, http_message* msg
609609
}
610610
}
611611

612+
void HttpServer::OnRequestDownloadGzip(mg_connection* conn, http_message* msg) {
613+
if (std::string{msg->method.p, msg->method.len} == std::string{"DOWNLOAD"}) {
614+
mg_http_send_error(conn, 405, "Method Not Allowed");
615+
} else {
616+
std::string encoding;
617+
for (size_t i = 0; i < sizeof(msg->header_names) / sizeof(mg_str); i++) {
618+
if (!msg->header_names[i].p) {
619+
continue;
620+
}
621+
622+
std::string name = std::string(msg->header_names[i].p, msg->header_names[i].len);
623+
if (std::string{"Accept-Encoding"} == name) {
624+
encoding = std::string(msg->header_values[i].p, msg->header_values[i].len);
625+
break;
626+
}
627+
}
628+
if (encoding.find("gzip") == std::string::npos) {
629+
mg_http_send_error(conn, 405, ("Invalid encoding: " + encoding).c_str());
630+
} else {
631+
std::string response = "Download!";
632+
mg_send_head(conn, 200, static_cast<int>(response.length()), "");
633+
mg_send(conn, response.c_str(), static_cast<int>(response.length()));
634+
}
635+
}
636+
}
637+
612638
void HttpServer::OnRequest(mg_connection* conn, http_message* msg) {
613639
std::string uri = std::string(msg->uri.p, msg->uri.len);
614640
if (uri == "/") {
@@ -671,6 +697,8 @@ void HttpServer::OnRequest(mg_connection* conn, http_message* msg) {
671697
OnRequestPatch(conn, msg);
672698
} else if (uri == "/patch_unallowed.html") {
673699
OnRequestPatchNotAllowed(conn, msg);
700+
} else if (uri == "/download_gzip.html") {
701+
OnRequestDownloadGzip(conn, msg);
674702
} else {
675703
OnRequestNotFound(conn, msg);
676704
}

test/httpServer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class HttpServer : public AbstractServer {
5050
static void OnRequestPutNotAllowed(mg_connection* conn, http_message* msg);
5151
static void OnRequestPatch(mg_connection* conn, http_message* msg);
5252
static void OnRequestPatchNotAllowed(mg_connection* conn, http_message* msg);
53+
static void OnRequestDownloadGzip(mg_connection* conn, http_message* msg);
5354

5455
protected:
55-
mg_connection* initServer(mg_mgr* mgr,
56-
MG_CB(mg_event_handler_t event_handler, void* user_data)) override;
56+
mg_connection* initServer(mg_mgr* mgr, MG_CB(mg_event_handler_t event_handler, void* user_data)) override;
5757
};
5858
} // namespace cpr
5959

0 commit comments

Comments
 (0)