From 80cfc2ef118eeae70e3e4989970e0eeb25cb640a Mon Sep 17 00:00:00 2001 From: Lanzheng Liu Date: Thu, 16 May 2024 11:45:36 +0800 Subject: [PATCH 1/3] support customized UserAgent in http client (#479) Signed-off-by: liulanzheng --- net/http/client.cpp | 5 ++-- net/http/client.h | 4 +++ net/http/test/client_function_test.cpp | 38 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/net/http/client.cpp b/net/http/client.cpp index 802eb873..6ec2bea8 100644 --- a/net/http/client.cpp +++ b/net/http/client.cpp @@ -252,9 +252,10 @@ class ClientImpl : public Client { LOG_ERROR_RETURN(EINVAL, ROUNDTRIP_FAILED, "Content-Length and Transfer-Encoding conflicted"); } - op->req.headers.insert("User-Agent", USERAGENT); - op->req.headers.insert("Connection", "keep-alive"); op->req.headers.merge(m_common_headers); + op->req.headers.insert("User-Agent", m_user_agent.empty() ? std::string_view(USERAGENT) + : std::string_view(m_user_agent)); + op->req.headers.insert("Connection", "keep-alive"); if (m_cookie_jar && m_cookie_jar->set_cookies_to_headers(&op->req) != 0) LOG_ERROR_RETURN(0, -1, "set_cookies_to_headers failed"); Timeout tmo(std::min(op->timeout.timeout(), m_timeout)); diff --git a/net/http/client.h b/net/http/client.h index c1a80cb3..53a27ae6 100644 --- a/net/http/client.h +++ b/net/http/client.h @@ -116,6 +116,9 @@ class Client : public Object { m_proxy_url.from_string(proxy); m_proxy = true; } + void set_user_agent(std::string_view user_agent) { + m_user_agent = std::string(user_agent); + } StoredURL* get_proxy() { return &m_proxy_url; } @@ -136,6 +139,7 @@ class Client : public Object { bool secure = false, uint64_t timeout = -1UL) = 0; protected: StoredURL m_proxy_url; + std::string m_user_agent; uint64_t m_timeout = -1UL; bool m_proxy = false; }; diff --git a/net/http/test/client_function_test.cpp b/net/http/test/client_function_test.cpp index 494fa249..79af5284 100644 --- a/net/http/test/client_function_test.cpp +++ b/net/http/test/client_function_test.cpp @@ -573,6 +573,44 @@ TEST(http_client, unix_socket) { ASSERT_EQ(200, op2.resp.status_code()); } +int ua_check_handler(void*, Request &req, Response &resp, std::string_view) { + auto ua = req.headers["User-Agent"]; + LOG_DEBUG(VALUE(ua)); + EXPECT_EQ(ua, "TEST_UA"); + resp.set_result(200); + std::string str = "success"; + resp.headers.content_length(7); + resp.write((void*)str.data(), str.size()); + return 0; +} + +TEST(http_client, user_agent) { + auto tcpserver = new_tcp_socket_server(); + DEFER(delete tcpserver); + tcpserver->bind(18731); + tcpserver->listen(); + auto server = new_http_server(); + DEFER(delete server); + server->add_handler({nullptr, &ua_check_handler}); + tcpserver->set_handler(server->get_connection_handler()); + tcpserver->start_loop(); + + std::string target_get = "http://localhost:18731/file"; + auto client = new_http_client(); + client->set_user_agent("TEST_UA"); + DEFER(delete client); + auto op = client->new_operation(Verb::GET, target_get); + DEFER(delete op); + op->req.headers.content_length(0); + client->call(op); + EXPECT_EQ(op->status_code, 200); + std::string buf; + buf.resize(op->resp.headers.content_length()); + op->resp.read((void*)buf.data(), op->resp.headers.content_length()); + LOG_DEBUG(VALUE(buf)); + EXPECT_EQ(true, buf == "success"); +} + TEST(url, url_escape_unescape) { EXPECT_EQ( url_escape("?a=x:b&b=cd&c= feg&d=2/1[+]@alibaba.com&e='!bad';"), From 58fab4c5fe82a6bd18fbf25a5fa734b212bf846b Mon Sep 17 00:00:00 2001 From: lihuiba Date: Mon, 20 May 2024 16:54:08 +0800 Subject: [PATCH 2/3] fix gcc13 --- common/iovector.h | 3 ++- common/test/test.cpp | 5 +++++ rpc/rpc.cpp | 4 +++- thread/thread11.h | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/common/iovector.h b/common/iovector.h index 2dc11f0f..404d2492 100644 --- a/common/iovector.h +++ b/common/iovector.h @@ -40,7 +40,8 @@ limitations under the License. #include #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#if __GNUC__ >= 13 +// #pragma GCC diagnostic ignored "-Wunknown-warning-option" #pragma GCC diagnostic ignored "-Wzero-length-bounds" inline bool operator == (const iovec& a, const iovec& b) diff --git a/common/test/test.cpp b/common/test/test.cpp index e9b2b27e..28aa6a22 100644 --- a/common/test/test.cpp +++ b/common/test/test.cpp @@ -243,7 +243,12 @@ TEST(Callback, virtual_function) Callback dd(lambda); // Callback ee([&](int x){ return RET + x/2; }); +#pragma GCC diagnostic push +#if __GNUC__ >= 13 +#pragma GCC diagnostic ignored "-Wdangling-pointer" +#endif THIS = (BB*)&c; +#pragma GCC diagnostic pop LOG_DEBUG(VALUE(THIS), VALUE(&c)); for (int i=0; i<100; ++i) diff --git a/rpc/rpc.cpp b/rpc/rpc.cpp index 0fc401dc..a0a7950b 100644 --- a/rpc/rpc.cpp +++ b/rpc/rpc.cpp @@ -325,8 +325,10 @@ namespace rpc { return -1; #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#if __GNUC__ >= 13 +// #pragma GCC diagnostic ignored "-Wunknown-warning-option" #pragma GCC diagnostic ignored "-Wdangling-pointer" +#endif ThreadLink node; m_list.push_back(&node); #pragma GCC diagnostic pop diff --git a/thread/thread11.h b/thread/thread11.h index 7bd6fd1b..7f4cf34c 100644 --- a/thread/thread11.h +++ b/thread/thread11.h @@ -135,7 +135,7 @@ namespace photon { void asdf() { - int a; char b; + int a = 0; char b = 0; auto func = &__Example_of_Thread11__::member_function; auto cfunc = &__Example_of_Thread11__::const_member_function; From e5a7cac0b1f4f086a2f585acca0c758282eff3ba Mon Sep 17 00:00:00 2001 From: lihuiba Date: Mon, 20 May 2024 17:09:23 +0800 Subject: [PATCH 3/3] fix --- common/iovector.h | 1 + 1 file changed, 1 insertion(+) diff --git a/common/iovector.h b/common/iovector.h index 404d2492..27a24c9c 100644 --- a/common/iovector.h +++ b/common/iovector.h @@ -43,6 +43,7 @@ limitations under the License. #if __GNUC__ >= 13 // #pragma GCC diagnostic ignored "-Wunknown-warning-option" #pragma GCC diagnostic ignored "-Wzero-length-bounds" +#endif inline bool operator == (const iovec& a, const iovec& b) {