From 80cfc2ef118eeae70e3e4989970e0eeb25cb640a Mon Sep 17 00:00:00 2001 From: Lanzheng Liu Date: Thu, 16 May 2024 11:45:36 +0800 Subject: [PATCH 1/5] 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/5] 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/5] 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) { From 40507a627db156107fc5ea5e4cca4ad7ab38938a Mon Sep 17 00:00:00 2001 From: Bob Chen Date: Thu, 23 May 2024 13:57:17 +0800 Subject: [PATCH 4/5] cherry-pick fix for timer stack overflow (#489) --- thread/timer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thread/timer.h b/thread/timer.h index c8e3dda5..d8e789b2 100644 --- a/thread/timer.h +++ b/thread/timer.h @@ -35,7 +35,7 @@ namespace photon // it has a `stack_size`, and the `on_timer` is invoked within the thread's context. // The timer object is deleted automatically after it is finished. Timer(uint64_t default_timeout, Entry on_timer, bool repeating = true, - uint64_t stack_size = 1024 * 64) + uint64_t stack_size = DEFAULT_STACK_SIZE) { _on_timer = on_timer; _default_timeout = default_timeout; From 2244d0d2e2757cb92421fd270ee93328bf782838 Mon Sep 17 00:00:00 2001 From: Lanzheng Liu Date: Fri, 31 May 2024 01:45:59 +0800 Subject: [PATCH 5/5] fix dns resolver discard_cache (#497) Signed-off-by: liulanzheng --- net/utils.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/net/utils.cpp b/net/utils.cpp index ed514ed9..010678f3 100644 --- a/net/utils.cpp +++ b/net/utils.cpp @@ -304,8 +304,10 @@ class DefaultResolver : public Resolver { void discard_cache(const char *host, IPAddr ip) override { auto ipaddr = dnscache_.borrow(host); - if (ip.undefined() || ipaddr->empty()) ipaddr.recycle(true); - else { + if (ip.undefined() || ipaddr->empty()) { + ipaddr->delete_all(); + ipaddr.recycle(true); + } else { for (auto itr = ipaddr->rbegin(); itr != ipaddr->rend(); itr++) { if ((*itr)->addr == ip) { ipaddr->erase(*itr);