Skip to content

Commit 2323cbb

Browse files
committed
http client destroy_operation
1 parent d700761 commit 2323cbb

File tree

5 files changed

+52
-44
lines changed

5 files changed

+52
-44
lines changed

CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ find_package(PkgConfig REQUIRED)
1818
# Options
1919
set(PHOTON_CXX_STANDARD "14" CACHE STRING "C++ standard")
2020
option(PHOTON_BUILD_TESTING "enable build testing" OFF)
21+
option(PHOTON_BUILD_WITH_ASAN "build with asan" OFF)
2122
option(PHOTON_ENABLE_URING "enable io_uring function" OFF)
2223
option(PHOTON_ENABLE_FUSE "enable fuse function" OFF)
2324
option(PHOTON_ENABLE_SASL "enable sasl" OFF)
@@ -57,6 +58,13 @@ if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.0)
5758
endif ()
5859
add_compile_options(${global_compile_options})
5960

61+
if (PHOTON_BUILD_WITH_ASAN)
62+
if ((NOT CMAKE_BUILD_TYPE STREQUAL "Debug") OR (NOT CMAKE_SYSTEM_NAME STREQUAL "Linux"))
63+
message(FATAL_ERROR "Wrong environment")
64+
endif ()
65+
add_link_options(-fsanitize=address -static-libasan)
66+
endif ()
67+
6068
set(CMAKE_CXX_STANDARD ${PHOTON_CXX_STANDARD})
6169
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6270
set(CMAKE_CXX_EXTENSIONS OFF)
@@ -96,12 +104,6 @@ if (NOT CMAKE_BUILD_TYPE)
96104
set(CMAKE_BUILD_TYPE Release)
97105
endif ()
98106

99-
# If ccache exists, use it to speed up compiling
100-
find_program(CCACHE_FOUND ccache)
101-
if(CCACHE_FOUND)
102-
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
103-
endif(CCACHE_FOUND)
104-
105107
# CMake dirs
106108
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake)
107109
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/output)

net/http/client.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ class ICookieJar : public Object {
4040

4141
class Client : public Object {
4242
public:
43+
class Operation;
44+
Operation* new_operation(Verb v, std::string_view url, uint16_t buf_size = UINT16_MAX) {
45+
return Operation::create(this, v, url, buf_size);
46+
}
47+
Operation* new_operation(uint16_t buf_size = UINT16_MAX) {
48+
return Operation::create(this, buf_size);
49+
}
50+
void destroy_operation(Operation* op) {
51+
op->destroy();
52+
}
53+
4354
class Operation {
4455
public:
4556
Request req; // request
@@ -68,8 +79,9 @@ class Client : public Object {
6879
this->~Operation();
6980
free(this);
7081
}
71-
void set_enable_proxy(bool enable) { enable_proxy = enable; }
72-
82+
void set_enable_proxy(bool enable) {
83+
enable_proxy = enable;
84+
}
7385
int call() {
7486
if (!_client) return -1;
7587
return _client->call(this);
@@ -91,24 +103,18 @@ class Client : public Object {
91103
: req(_buf, buf_size),
92104
enable_proxy(c->has_proxy()),
93105
_client(c) {}
94-
Operation(uint16_t buf_size) : req(_buf, buf_size) {}
106+
explicit Operation(uint16_t buf_size) : req(_buf, buf_size), _client(nullptr) {}
107+
Operation() = delete;
108+
~Operation() = default;
95109
};
96110

97-
Operation* new_operation(Verb v, std::string_view url, uint16_t buf_size = UINT16_MAX) {
98-
return Operation::create(this, v, url, buf_size);
99-
}
100-
101-
Operation* new_operation(uint16_t buf_size = UINT16_MAX) {
102-
return Operation::create(this, buf_size);
103-
}
104-
105111
template<uint16_t BufferSize = UINT16_MAX>
106112
class OperationOnStack : public Operation {
107113
char _buf[BufferSize];
108114
public:
109115
OperationOnStack(Client* c, Verb v, std::string_view url):
110116
Operation(c, v, url, BufferSize) {}
111-
OperationOnStack(Client* c): Operation(c, BufferSize) {};
117+
explicit OperationOnStack(Client* c): Operation(c, BufferSize) {};
112118
OperationOnStack(): Operation(BufferSize) {}
113119
};
114120

net/http/test/client_function_test.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TEST(http_client, get) {
9898
auto client = new_http_client();
9999
DEFER(delete client);
100100
auto op2 = client->new_operation(Verb::GET, target);
101-
DEFER(op2->destroy());
101+
DEFER(client->destroy_operation(op2));
102102
op2->req.headers.content_length(0);
103103
int ret = client->call(op2);
104104
GTEST_ASSERT_EQ(0, ret);
@@ -112,7 +112,7 @@ TEST(http_client, get) {
112112
EXPECT_EQ(0, strcmp(resp_body_buf, socket_buf));
113113

114114
auto op3 = client->new_operation(Verb::GET, target);
115-
DEFER(op3->destroy());
115+
DEFER(client->destroy_operation(op3));
116116
op3->req.headers.content_length(0);
117117
op3->req.headers.range(10, 19);
118118
client->call(op3);
@@ -123,7 +123,7 @@ TEST(http_client, get) {
123123
LOG_DEBUG(resp_body_buf_range);
124124

125125
auto op4 = client->new_operation(Verb::GET, target);
126-
DEFER(op4->destroy());
126+
DEFER(client->destroy_operation(op4));
127127
op4->req.headers.content_length(0);
128128
op4->call();
129129
EXPECT_EQ(sizeof(socket_buf), op4->resp.resource_size());
@@ -140,7 +140,7 @@ TEST(http_client, get) {
140140

141141
static const char target_tb[] = "http://www.taobao.com?x";
142142
auto op5 = client->new_operation(Verb::GET, target_tb);
143-
DEFER(op5->destroy());
143+
DEFER(client->destroy_operation(op5));
144144
op5->req.headers.content_length(0);
145145
op5->call();
146146
EXPECT_EQ(op5->resp.status_code(), 200);
@@ -193,7 +193,7 @@ TEST(http_client, post) {
193193

194194
// body stream test
195195
auto op1 = client->new_operation(Verb::POST, target);
196-
DEFER(op1->destroy());
196+
DEFER(client->destroy_operation(op1));
197197
struct stat st;
198198
EXPECT_EQ(0, file->fstat(&st));
199199
op1->req.headers.content_length(st.st_size);
@@ -207,7 +207,7 @@ TEST(http_client, post) {
207207

208208
// body writer test
209209
auto op2 = client->new_operation(Verb::POST, target);
210-
DEFER(op2->destroy());
210+
DEFER(client->destroy_operation(op2));
211211
op2->req.headers.content_length(st.st_size);
212212
auto writer = [&](Request *req)-> ssize_t {
213213
file->lseek(0, SEEK_SET);
@@ -348,7 +348,7 @@ TEST(http_client, chunked) {
348348
DEFER(delete client);
349349
auto url = to_url(server, "/");
350350
auto op = client->new_operation(Verb::GET, url);
351-
DEFER(op->destroy());
351+
DEFER(client->destroy_operation(op));
352352
std::string buf;
353353

354354
op->call();
@@ -362,7 +362,7 @@ TEST(http_client, chunked) {
362362

363363
server->set_handler({nullptr, &chunked_handler_complict});
364364
auto opc = client->new_operation(Verb::GET, url);
365-
DEFER(opc->destroy());
365+
DEFER(client->destroy_operation(opc));
366366
opc->call();
367367
EXPECT_EQ(200, opc->status_code);
368368
buf.resize(20000);
@@ -382,7 +382,7 @@ TEST(http_client, chunked) {
382382
server->set_handler({nullptr, &chunked_handler_pt});
383383
for (auto tmp = 0; tmp < 20; tmp++) {
384384
auto op_test = client->new_operation(Verb::GET, url);
385-
DEFER(op_test->destroy());
385+
DEFER(client->destroy_operation(op_test));
386386
op_test->call();
387387
EXPECT_EQ(200, op_test->status_code);
388388
buf.resize(std_data_size);
@@ -459,7 +459,7 @@ TEST(http_client, debug) {
459459
auto client = new_http_client();
460460
DEFER(delete client);
461461
auto op_test = client->new_operation(Verb::GET, to_url(server, "/"));
462-
DEFER(op_test->destroy());
462+
DEFER(client->destroy_operation(op_test));
463463
op_test->call();
464464
EXPECT_EQ(200, op_test->status_code);
465465
std::string buf;
@@ -492,7 +492,7 @@ TEST(http_client, server_no_resp) {
492492
auto client = new_http_client();
493493
DEFER(delete client);
494494
auto op = client->new_operation(Verb::GET, to_url(server, "/wtf"));
495-
DEFER(op->destroy());
495+
DEFER(client->destroy_operation(op));
496496
op->req.headers.content_length(0);
497497
client->call(op);
498498
EXPECT_EQ(-1, op->status_code);
@@ -521,7 +521,7 @@ TEST(http_client, partial_body) {
521521
auto client = new_http_client();
522522
DEFER(delete client);
523523
auto op = client->new_operation(Verb::GET, target_get);
524-
DEFER(op->destroy());
524+
DEFER(client->destroy_operation(op));
525525
op->req.headers.content_length(0);
526526
client->call(op);
527527
EXPECT_EQ(sizeof(socket_buf), op->resp.resource_size());
@@ -540,7 +540,7 @@ TEST(DISABLED_http_client, ipv6) { // make sure runing in a ipv6-ready environm
540540
DEFER(delete client);
541541
// here is an ipv6-only website
542542
auto op = client->new_operation(Verb::GET, "http://test6.ustc.edu.cn");
543-
DEFER(op->destroy());
543+
DEFER(client->destroy_operation(op));
544544
op->call();
545545
EXPECT_EQ(200, op->resp.status_code());
546546
}

net/http/test/client_tls_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ TEST(client_tls, basic) {
7272
auto client = net::http::new_http_client(nullptr, ctx);
7373
DEFER(delete client);
7474
auto op = client->new_operation(net::http::Verb::GET, to_surl(tcpserver, "/test"));
75-
DEFER(op->destroy());
75+
DEFER(client->destroy_operation(op));
7676
auto exp_len = 20;
7777
op->req.headers.range(0, exp_len - 1);
7878
op->call();
@@ -91,7 +91,7 @@ TEST(http_client, DISABLED_SNI) {
9191
auto client = photon::net::http::new_http_client(nullptr, tls);
9292
DEFER(delete client);
9393
auto op = client->new_operation(photon::net::http::Verb::GET, "https://debug.fly.dev");
94-
DEFER(op->destroy());
94+
DEFER(client->destroy_operation(op));
9595
op->retry = 0;
9696
int res = op->call();
9797
ASSERT_EQ(0, res);

net/http/test/server_function_test.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ TEST(http_server, headers) {
6363
auto client = new_http_client();
6464
DEFER(delete client);
6565
auto op = client->new_operation(Verb::GET, to_url(tcpserver, "/test"));
66-
DEFER(op->destroy());
66+
DEFER(client->destroy_operation(op));
6767
auto exp_len = 20;
6868
op->req.headers.range(0, exp_len - 1);
6969
op->call();
@@ -104,7 +104,7 @@ TEST(http_server, post) {
104104
auto client = new_http_client();
105105
DEFER(delete client);
106106
auto op = client->new_operation(Verb::POST, to_url(tcpserver, "/test"));
107-
DEFER(op->destroy());
107+
DEFER(client->destroy_operation(op));
108108
op->req.headers.content_length(10);
109109
std::string body = "1234567890";
110110
auto writer = [&](Request *req)-> ssize_t {
@@ -125,7 +125,7 @@ std::string fs_handler_std_str = "01234567890123456789";
125125
void test_case(Client* client, estring_view url, off_t st, size_t len, size_t exp_content_length, bool invalid = false) {
126126
LOG_INFO("test case start");
127127
auto op = client->new_operation(Verb::GET, url);
128-
DEFER(op->destroy());
128+
DEFER(client->destroy_operation(op));
129129
op->req.headers.range(st, st + len - 1);
130130
auto ret = op->call();
131131
LOG_INFO("call finished");
@@ -151,7 +151,7 @@ void test_case(Client* client, estring_view url, off_t st, size_t len, size_t ex
151151
void test_head_case(Client* client, estring_view url, off_t st, size_t len, size_t exp_content_length) {
152152
LOG_INFO("test HEAD case start");
153153
auto op = client->new_operation(Verb::HEAD, url);
154-
DEFER(op->destroy());
154+
DEFER(client->destroy_operation(op));
155155
op->req.headers.range(st, st + len - 1);
156156
op->req.headers.content_length(fs_handler_std_str.size());
157157
auto ret = op->call();
@@ -299,7 +299,7 @@ TEST(http_server, proxy_handler_get) {
299299
tcpserver->start_loop();
300300
//----------------------------------------------------
301301
auto op = client->new_operation(Verb::GET, to_url(tcpserver, "/filename"));
302-
DEFER(op->destroy());
302+
DEFER(client->destroy_operation(op));
303303
ret = op->call();
304304
EXPECT_EQ(0, ret);
305305
std::string data_buf;
@@ -341,7 +341,7 @@ TEST(http_server, proxy_handler_post) {
341341
tcpserver->start_loop();
342342
//----------------------------------------------------
343343
auto op = client->new_operation(Verb::POST, to_url(tcpserver, "/filename"));
344-
DEFER(op->destroy());
344+
DEFER(client->destroy_operation(op));
345345
std::string body = "1234567890";
346346
op->req.headers.content_length(10);
347347
auto writer = [&](Request *req)-> ssize_t {
@@ -402,7 +402,7 @@ TEST(http_server, proxy_handler_post_forward) {
402402
DEFER(delete client1);
403403
client1->set_proxy(to_url(tcpserver, "/"));
404404
auto op = client1->new_operation(Verb::POST, to_url(source_server, "/filename"));
405-
DEFER(op->destroy());
405+
DEFER(client1->destroy_operation(op));
406406
std::string body = "1234567890";
407407
op->req.headers.content_length(10);
408408
auto writer = [&](Request *req)-> ssize_t {
@@ -441,7 +441,7 @@ TEST(http_server, proxy_handler_failure) {
441441
//----------------------------------------------------
442442
auto url = to_url(tcpserver, "/filename");
443443
auto op = client->new_operation(Verb::GET, url);
444-
DEFER(op->destroy());
444+
DEFER(client->destroy_operation(op));
445445
auto ret = op->call();
446446
EXPECT_EQ(0, ret);
447447
EXPECT_EQ(502, op->resp.status_code());
@@ -492,7 +492,7 @@ TEST(http_server, mux_handler) {
492492
//----------------------------------------------------
493493
//--------------test static service--------------------
494494
auto op_static = client->new_operation(Verb::GET, to_url(tcpserver, "/static_service/fs_handler_test"));
495-
DEFER(op_static->destroy());
495+
DEFER(client->destroy_operation(op_static));
496496
ret = op_static->call();
497497
EXPECT_EQ(0, ret);
498498
EXPECT_EQ(200, op_static->resp.status_code());
@@ -503,7 +503,7 @@ TEST(http_server, mux_handler) {
503503
EXPECT_EQ(true, data_buf == fs_handler_std_str);
504504
//--------------test proxy service---------------------
505505
auto op_proxy = client->new_operation(Verb::GET, to_url(tcpserver, "/proxy/filename_not_important"));
506-
DEFER(op_proxy->destroy());
506+
DEFER(client->destroy_operation(op_proxy));
507507
ret = op_proxy->call();
508508
EXPECT_EQ(0, ret);
509509
EXPECT_EQ(200, op_proxy->resp.status_code());
@@ -514,7 +514,7 @@ TEST(http_server, mux_handler) {
514514
EXPECT_EQ(true, data_buf == std_data);
515515
//-------------test mux default handler---------------
516516
auto op_default = client->new_operation(Verb::GET, to_url(tcpserver, "/not_recorded/should_be_404"));
517-
DEFER(op_default->destroy());
517+
DEFER(client->destroy_operation(op_default));
518518
ret = op_default->call();
519519
EXPECT_EQ(0, ret);
520520
EXPECT_EQ(404, op_default->resp.status_code());

0 commit comments

Comments
 (0)