@@ -87,7 +87,7 @@ class Client {
87
87
std::string basic_auth;
88
88
89
89
/* for response parser */
90
- char *tmp_header_field_name = nullptr ;
90
+ const char *tmp_header_field_name = nullptr ;
91
91
int tmp_header_field_name_len = 0 ;
92
92
String *body = nullptr ;
93
93
#ifdef SW_HAVE_COMPRESSION
@@ -119,12 +119,16 @@ class Client {
119
119
zval *zobject = &_zobject;
120
120
zval zsocket;
121
121
zend::Callable *write_func = nullptr ;
122
+ /* *
123
+ * Retain the send buffer object of the Socket after the Socket object is destroyed,
124
+ * allowing access to the sent Request data even after the connection has been closed.
125
+ */
122
126
String *tmp_write_buffer = nullptr ;
123
127
bool connection_close = false ;
124
128
125
- Client (zval *zobject, std::string host, zend_long port = 80 , zend_bool ssl = false );
129
+ Client (const zval *zobject, const std::string & host, zend_long port = 80 , zend_bool ssl = false );
126
130
127
- bool is_available () {
131
+ bool is_available () const {
128
132
if (sw_unlikely (!socket || !socket->is_connected ())) {
129
133
php_swoole_socket_set_error_properties (zobject, SW_ERROR_CLIENT_NO_CONNECTION);
130
134
return false ;
@@ -143,9 +147,8 @@ class Client {
143
147
#ifdef SW_HAVE_ZSTD
144
148
ZSTD_DStream *zstd_stream = nullptr ;
145
149
#endif
146
- bool bind (std::string address, int port = 0 );
147
150
bool connect ();
148
- void set_error (int error, const char *msg, int status);
151
+ void set_error (int error, const char *msg, int status) const ;
149
152
bool keep_liveness ();
150
153
bool send_request ();
151
154
void reset ();
@@ -165,10 +168,9 @@ class Client {
165
168
166
169
static void create_token (int length, char *buf) {
167
170
char characters[] = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\" §$%&/()=[]{}" ;
168
- int i;
169
171
assert (length < 1024 );
170
- for (i = 0 ; i < length; i++) {
171
- buf[i] = characters[rand () % (sizeof (characters) - 1 )];
172
+ for (int i = 0 ; i < length; i++) {
173
+ buf[i] = characters[swoole_random_int () % (sizeof (characters) - 1 )];
172
174
}
173
175
buf[length] = ' \0 ' ;
174
176
}
@@ -177,23 +179,21 @@ class Client {
177
179
#ifdef SW_HAVE_COMPRESSION
178
180
bool decompress_response (const char *in, size_t in_len);
179
181
#endif
180
- void apply_setting (zval *zset, const bool check_all = true );
182
+ void apply_setting (zval *zset, bool check_all = true );
181
183
void set_basic_auth (const std::string &username, const std::string &password);
182
- bool exec (std::string _path);
184
+ bool exec (const std::string & _path);
183
185
bool recv_response (double timeout = 0 );
184
186
bool recv_websocket_frame (zval *zframe, double timeout = 0 );
185
- void add_header (const char *key, size_t key_len, const char *str, size_t length);
186
- bool upgrade (std::string path);
187
+ void add_header (const char *key, size_t key_len, const char *str, size_t length) const ;
188
+ bool upgrade (const std::string & path);
187
189
bool push (zval *zdata, zend_long opcode = websocket::OPCODE_TEXT, uint8_t flags = websocket::FLAG_FIN);
188
- bool close (const bool should_be_reset = true );
190
+ bool close (bool should_be_reset = true );
189
191
void socket_dtor ();
190
192
191
193
void get_header_out (zval *return_value) {
192
194
String *buffer = nullptr ;
193
195
if (socket == nullptr ) {
194
- if (tmp_write_buffer) {
195
- buffer = tmp_write_buffer;
196
- }
196
+ buffer = tmp_write_buffer;
197
197
} else {
198
198
buffer = socket->get_write_buffer ();
199
199
}
@@ -384,17 +384,17 @@ void php_swoole_http_parse_set_cookies(const char *at, size_t length, zval *zcoo
384
384
}
385
385
386
386
static int http_parser_on_header_field (swoole_http_parser *parser, const char *at, size_t length) {
387
- Client *http = ( Client *) parser->data ;
388
- http->tmp_header_field_name = ( char *) at;
387
+ auto *http = static_cast < Client *>( parser->data ) ;
388
+ http->tmp_header_field_name = at;
389
389
http->tmp_header_field_name_len = length;
390
390
return 0 ;
391
391
}
392
392
393
393
static int http_parser_on_header_value (swoole_http_parser *parser, const char *at, size_t length) {
394
- Client *http = ( Client *) parser->data ;
394
+ auto *http = static_cast < Client *>( parser->data ) ;
395
395
zval *zobject = (zval *) http->zobject ;
396
396
397
- char *header_name = http->tmp_header_field_name ;
397
+ const char *header_name = http->tmp_header_field_name ;
398
398
size_t header_len = http->tmp_header_field_name_len ;
399
399
zend::CharPtr _header_name;
400
400
@@ -428,7 +428,7 @@ static int http_parser_on_header_value(swoole_http_parser *parser, const char *a
428
428
}
429
429
#ifdef SW_HAVE_COMPRESSION
430
430
else if (SW_STREQ (header_name, header_len, " content-encoding" )) {
431
- if (0 ) {
431
+ if (false ) {
432
432
}
433
433
#ifdef SW_HAVE_BROTLI
434
434
else if (SW_STR_ISTARTS_WITH (at, length, " br" )) {
@@ -459,7 +459,7 @@ static int http_parser_on_header_value(swoole_http_parser *parser, const char *a
459
459
}
460
460
461
461
static int http_parser_on_headers_complete (swoole_http_parser *parser) {
462
- Client *http = ( Client *) parser->data ;
462
+ auto *http = static_cast < Client *>( parser->data ) ;
463
463
if (http->method == SW_HTTP_HEAD || parser->status_code == SW_HTTP_NO_CONTENT) {
464
464
return 1 ;
465
465
}
@@ -475,7 +475,7 @@ static inline ssize_t http_client_co_write(int sockfd, const void *buf, size_t c
475
475
}
476
476
477
477
static int http_parser_on_body (swoole_http_parser *parser, const char *at, size_t length) {
478
- Client *http = ( Client *) parser->data ;
478
+ auto *http = static_cast < Client *>( parser->data ) ;
479
479
if (http->write_func ) {
480
480
zval zargv[2 ];
481
481
zargv[0 ] = *http->zobject ;
@@ -533,8 +533,8 @@ static int http_parser_on_body(swoole_http_parser *parser, const char *at, size_
533
533
}
534
534
535
535
static int http_parser_on_message_complete (swoole_http_parser *parser) {
536
- Client *http = ( Client *) parser->data ;
537
- zval *zobject = (zval *) http->zobject ;
536
+ auto *http = static_cast < Client *>( parser->data ) ;
537
+ const zval *zobject = http->zobject ;
538
538
539
539
if (parser->upgrade && !http->websocket ) {
540
540
// not support, continue.
@@ -559,9 +559,9 @@ static int http_parser_on_message_complete(swoole_http_parser *parser) {
559
559
}
560
560
}
561
561
562
- Client::Client (zval *zobject, std::string host, zend_long port, zend_bool ssl) {
563
- this ->socket_type = network::Socket::convert_to_type (host);
562
+ Client::Client (const zval *zobject, const std::string &host, zend_long port, zend_bool ssl) {
564
563
this ->host = host;
564
+ this ->socket_type = network::Socket::convert_to_type (this ->host );
565
565
this ->use_default_port = port == 0 ;
566
566
if (this ->use_default_port ) {
567
567
port = ssl ? 443 : 80 ;
@@ -609,7 +609,7 @@ bool Client::decompress_response(const char *in, size_t in_len) {
609
609
gzip_stream.avail_in = in_len;
610
610
gzip_stream.total_in = 0 ;
611
611
612
- while (1 ) {
612
+ while (true ) {
613
613
total_out = gzip_stream.total_out ;
614
614
gzip_stream.avail_out = body->size - body->length ;
615
615
gzip_stream.next_out = (Bytef *) (body->str + body->length );
@@ -657,7 +657,7 @@ bool Client::decompress_response(const char *in, size_t in_len) {
657
657
658
658
const char *next_in = in;
659
659
size_t available_in = in_len;
660
- while (1 ) {
660
+ while (true ) {
661
661
size_t available_out = body->size - body->length , reserved_available_out = available_out;
662
662
char *next_out = body->str + body->length ;
663
663
size_t total_out;
@@ -780,9 +780,7 @@ void Client::apply_setting(zval *zset, const bool check_all) {
780
780
}
781
781
#endif
782
782
if (php_swoole_array_get_value (vht, " write_func" , ztmp)) {
783
- if (write_func) {
784
- delete write_func;
785
- }
783
+ delete write_func;
786
784
write_func = sw_callable_create (ztmp);
787
785
}
788
786
}
@@ -811,7 +809,7 @@ void Client::set_basic_auth(const std::string &username, const std::string &pass
811
809
}
812
810
}
813
811
814
- void Client::add_header (const char *key, size_t key_len, const char *str, size_t length) {
812
+ void Client::add_header (const char *key, size_t key_len, const char *str, size_t length) const {
815
813
zval *zheaders =
816
814
sw_zend_read_and_convert_property_array (swoole_http_client_coro_ce, zobject, ZEND_STRL (" headers" ), 0 );
817
815
@@ -863,7 +861,7 @@ bool Client::connect() {
863
861
socket->set_timeout (_timeout, Socket::TIMEOUT_CONNECT);
864
862
socket->set_resolve_context (&resolve_context_);
865
863
socket->set_dtor ([this ](Socket *_socket) { socket_dtor (); });
866
- // socket->set_buffer_allocator(&SWOOLE_G(zend_string_allocator ));
864
+ socket->set_buffer_allocator (sw_zend_string_allocator ( ));
867
865
868
866
if (!socket->connect (host, port)) {
869
867
set_error (socket->errCode , socket->errMsg , HTTP_ESTATUS_CONNECT_FAILED);
@@ -876,7 +874,7 @@ bool Client::connect() {
876
874
return true ;
877
875
}
878
876
879
- void Client::set_error (int error, const char *msg, int status) {
877
+ void Client::set_error (int error, const char *msg, int status) const {
880
878
auto ce = swoole_http_client_coro_ce;
881
879
auto obj = SW_Z8_OBJ_P (zobject);
882
880
zend_update_property_long (ce, obj, ZEND_STRL (" errCode" ), error);
@@ -907,7 +905,7 @@ bool Client::send_request() {
907
905
uint32_t header_flag = 0x0 ;
908
906
zval *zmethod, *zheaders, *zbody, *zupload_files, *zcookies, *z_download_file;
909
907
910
- if (path.length () == 0 ) {
908
+ if (path.empty () ) {
911
909
php_swoole_socket_set_error_properties (zobject, SW_ERROR_INVALID_PARAMS);
912
910
return false ;
913
911
}
@@ -1071,7 +1069,7 @@ bool Client::send_request() {
1071
1069
if (SW_STRCASEEQ (key, keylen, " Connection" )) {
1072
1070
header_flag |= HTTP_HEADER_CONNECTION;
1073
1071
if (SW_STRCASEEQ (str_value.val (), str_value.len (), " close" )) {
1074
- keep_alive = 0 ;
1072
+ keep_alive = false ;
1075
1073
}
1076
1074
}
1077
1075
}
@@ -1196,16 +1194,16 @@ bool Client::send_request() {
1196
1194
// upload files
1197
1195
SW_HASHTABLE_FOREACH_START2 (Z_ARRVAL_P (zupload_files), key, keylen, keytype, zvalue) {
1198
1196
HashTable *ht = Z_ARRVAL_P (zvalue);
1199
- if (!(zname = zend_hash_str_find (ht, ZEND_STRL (" name" )))) {
1197
+ if (!(( zname = zend_hash_str_find (ht, ZEND_STRL (" name" ) )))) {
1200
1198
continue ;
1201
1199
}
1202
- if (!(zfilename = zend_hash_str_find (ht, ZEND_STRL (" filename" )))) {
1200
+ if (!(( zfilename = zend_hash_str_find (ht, ZEND_STRL (" filename" ) )))) {
1203
1201
continue ;
1204
1202
}
1205
- if (!(zsize = zend_hash_str_find (ht, ZEND_STRL (" size" )))) {
1203
+ if (!(( zsize = zend_hash_str_find (ht, ZEND_STRL (" size" ) )))) {
1206
1204
continue ;
1207
1205
}
1208
- if (!(ztype = zend_hash_str_find (ht, ZEND_STRL (" type" )))) {
1206
+ if (!(( ztype = zend_hash_str_find (ht, ZEND_STRL (" type" ) )))) {
1209
1207
continue ;
1210
1208
}
1211
1209
// strlen("%.*s")*4 = 16
@@ -1383,7 +1381,7 @@ bool Client::send_request() {
1383
1381
return true ;
1384
1382
}
1385
1383
1386
- bool Client::exec (std::string _path) {
1384
+ bool Client::exec (const std::string & _path) {
1387
1385
path = _path;
1388
1386
// bzero when make a new reqeust
1389
1387
resolve_context_ = {};
@@ -1504,7 +1502,7 @@ bool Client::recv_response(double timeout) {
1504
1502
* TODO: Sec-WebSocket-Accept check
1505
1503
*/
1506
1504
if (websocket) {
1507
- socket->open_length_check = 1 ;
1505
+ socket->open_length_check = true ;
1508
1506
socket->protocol .package_length_size = SW_WEBSOCKET_HEADER_LEN;
1509
1507
socket->protocol .package_length_offset = 0 ;
1510
1508
socket->protocol .package_body_offset = 0 ;
@@ -1546,22 +1544,22 @@ bool Client::recv_websocket_frame(zval *zframe, double timeout) {
1546
1544
}
1547
1545
}
1548
1546
1549
- bool Client::upgrade (std::string path) {
1547
+ bool Client::upgrade (const std::string & path) {
1550
1548
defer = false ;
1551
1549
char buf[SW_WEBSOCKET_KEY_LENGTH + 1 ];
1552
1550
zval *zheaders =
1553
1551
sw_zend_read_and_convert_property_array (swoole_http_client_coro_ce, zobject, ZEND_STRL (" requestHeaders" ), 0 );
1554
1552
zend_update_property_string (swoole_http_client_coro_ce, SW_Z8_OBJ_P (zobject), ZEND_STRL (" requestMethod" ), " GET" );
1555
1553
create_token (SW_WEBSOCKET_KEY_LENGTH, buf);
1556
- add_assoc_string (zheaders, " Connection" , ( char *) " Upgrade" );
1557
- add_assoc_string (zheaders, " Upgrade" , ( char *) " websocket" );
1558
- add_assoc_string (zheaders, " Sec-WebSocket-Version" , ( char *) SW_WEBSOCKET_VERSION);
1554
+ add_assoc_string (zheaders, " Connection" , " Upgrade" );
1555
+ add_assoc_string (zheaders, " Upgrade" , " websocket" );
1556
+ add_assoc_string (zheaders, " Sec-WebSocket-Version" , SW_WEBSOCKET_VERSION);
1559
1557
add_assoc_str_ex (zheaders,
1560
1558
ZEND_STRL (" Sec-WebSocket-Key" ),
1561
1559
php_base64_encode ((const unsigned char *) buf, SW_WEBSOCKET_KEY_LENGTH));
1562
1560
#ifdef SW_HAVE_ZLIB
1563
1561
if (websocket_compression) {
1564
- add_assoc_string (zheaders, " Sec-Websocket-Extensions" , ( char *) SW_WEBSOCKET_EXTENSION_DEFLATE);
1562
+ add_assoc_string (zheaders, " Sec-Websocket-Extensions" , SW_WEBSOCKET_EXTENSION_DEFLATE);
1565
1563
}
1566
1564
#endif
1567
1565
return exec (path);
@@ -1640,13 +1638,11 @@ void Client::reset() {
1640
1638
}
1641
1639
1642
1640
void Client::socket_dtor () {
1643
- zend_update_property_bool (Z_OBJCE_P (zobject), SW_Z8_OBJ_P (zobject), ZEND_STRL (" connected" ), 0 );
1644
- zend_update_property_null (Z_OBJCE_P (zobject), SW_Z8_OBJ_P (zobject), ZEND_STRL (" socket" ));
1645
- if (tmp_write_buffer) {
1646
- delete tmp_write_buffer;
1647
- }
1641
+ delete tmp_write_buffer;
1648
1642
tmp_write_buffer = socket->pop_write_buffer ();
1649
1643
socket = nullptr ;
1644
+ zend_update_property_bool (Z_OBJCE_P (zobject), SW_Z8_OBJ_P (zobject), ZEND_STRL (" connected" ), 0 );
1645
+ zend_update_property_null (Z_OBJCE_P (zobject), SW_Z8_OBJ_P (zobject), ZEND_STRL (" socket" ));
1650
1646
zval_ptr_dtor (&zsocket);
1651
1647
ZVAL_NULL (&zsocket);
1652
1648
}
@@ -1678,22 +1674,17 @@ bool Client::close(const bool should_be_reset) {
1678
1674
1679
1675
Client::~Client () {
1680
1676
close ();
1681
- if (body) {
1682
- delete body;
1683
- }
1684
- if (tmp_write_buffer) {
1685
- delete tmp_write_buffer;
1686
- }
1687
- if (write_func) {
1688
- delete write_func;
1689
- }
1677
+ delete body;
1678
+ delete tmp_write_buffer;
1679
+ delete write_func;
1690
1680
}
1691
1681
1692
1682
static sw_inline HttpClientObject *http_client_coro_fetch_object (zend_object *obj) {
1693
- return (HttpClientObject *) ((char *) obj - swoole_http_client_coro_handlers.offset );
1683
+ return reinterpret_cast <HttpClientObject *>(reinterpret_cast <char *>(obj) -
1684
+ swoole_http_client_coro_handlers.offset );
1694
1685
}
1695
1686
1696
- static sw_inline Client *http_client_coro_get_client (zval *zobject) {
1687
+ static sw_inline Client *http_client_coro_get_client (const zval *zobject) {
1697
1688
Client *phc = http_client_coro_fetch_object (Z_OBJ_P (zobject))->client ;
1698
1689
if (UNEXPECTED (!phc)) {
1699
1690
swoole_fatal_error (SW_ERROR_WRONG_OPERATION, " must call constructor first" );
@@ -1711,7 +1702,7 @@ static void http_client_coro_free_object(zend_object *object) {
1711
1702
}
1712
1703
1713
1704
static zend_object *http_client_coro_create_object (zend_class_entry *ce) {
1714
- HttpClientObject *hcc = (HttpClientObject *) zend_object_alloc (sizeof (HttpClientObject), ce);
1705
+ auto *hcc = (HttpClientObject *) zend_object_alloc (sizeof (HttpClientObject), ce);
1715
1706
zend_object_std_init (&hcc->std , ce);
1716
1707
object_properties_init (&hcc->std , ce);
1717
1708
hcc->std .handlers = &swoole_http_client_coro_handlers;
@@ -1782,7 +1773,7 @@ static PHP_METHOD(swoole_http_client_coro, __construct) {
1782
1773
char *host;
1783
1774
size_t host_len;
1784
1775
zend_long port = 0 ;
1785
- zend_bool ssl = 0 ;
1776
+ zend_bool ssl = false ;
1786
1777
1787
1778
ZEND_PARSE_PARAMETERS_START_EX (ZEND_PARSE_PARAMS_THROW, 1 , 3 )
1788
1779
Z_PARAM_STRING (host, host_len)
@@ -1841,7 +1832,7 @@ static PHP_METHOD(swoole_http_client_coro, getDefer) {
1841
1832
1842
1833
static PHP_METHOD (swoole_http_client_coro, setDefer) {
1843
1834
Client *phc = http_client_coro_get_client (ZEND_THIS);
1844
- zend_bool defer = 1 ;
1835
+ zend_bool defer = true ;
1845
1836
1846
1837
ZEND_PARSE_PARAMETERS_START (0 , 1 )
1847
1838
Z_PARAM_OPTIONAL
0 commit comments