@@ -271,7 +271,7 @@ zend_string *php_swoole_http_get_date() {
271271}
272272
273273static void http_response_set_date_header (String *response) {
274- auto date_str = php_swoole_http_get_date ();
274+ auto date_str = php_swoole_http_get_date ();
275275 response->append (ZEND_STRL (" Date: " ));
276276 response->append (ZSTR_VAL (date_str), ZSTR_LEN (date_str));
277277 response->append (ZEND_STRL (" \r\n " ));
@@ -605,15 +605,16 @@ bool HttpContext::compress(const char *data, size_t length) {
605605 compression_level = Z_BEST_COMPRESSION;
606606 }
607607
608- size_t memory_size = ((size_t ) ((double ) length * (double ) 1.015 )) + 10 + 8 + 4 + 1 ;
608+ size_t memory_size = ((size_t )((double ) length * (double ) 1.015 )) + 10 + 8 + 4 + 1 ;
609609 zlib_buffer = std::make_shared<String>(memory_size);
610610
611611 z_stream zstream = {};
612612
613613 zstream.zalloc = php_zlib_alloc;
614614 zstream.zfree = php_zlib_free;
615615
616- int status = deflateInit2 (&zstream, compression_level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
616+ int status =
617+ deflateInit2 (&zstream, compression_level, Z_DEFLATED, encoding, SW_ZLIB_DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
617618 if (status != Z_OK) {
618619 swoole_warning (" deflateInit2() failed, Error: [%d]" , status);
619620 return false ;
@@ -1302,10 +1303,10 @@ ssize_t WebSocket::send_frame(const swoole::WebSocketSettings &settings,
13021303 * return_value is false means socket.read() method returning -1.
13031304 * return_value is null means other error.
13041305 * return_value is empry string means socket is closed.
1305- * the opcode is returned so the caller can decide when to release the frame_buffer .
1306+ * the opcode is returned so the caller can decide when to release the continue_frame_buffer .
13061307 */
13071308void WebSocket::recv_frame (const WebSocketSettings &settings,
1308- std::shared_ptr<String> &frame_buffer ,
1309+ std::shared_ptr<String> &continue_frame_buffer ,
13091310 Socket *sock,
13101311 zval *return_value,
13111312 double timeout) {
@@ -1366,39 +1367,46 @@ void WebSocket::recv_frame(const WebSocketSettings &settings,
13661367 }
13671368
13681369 if (opcode == WebSocket::OPCODE_CONTINUATION) {
1369- if (sw_unlikely (!frame_buffer )) {
1370+ if (sw_unlikely (!continue_frame_buffer )) {
13701371 swoole_warning (" A continuation frame cannot stand alone and MUST be preceded by an initial frame whose "
13711372 " opcode indicates either text or binary data." );
13721373 RETURN_NULL ();
13731374 }
13741375
13751376 if (sw_likely (frame.payload )) {
1376- frame_buffer ->append (frame.payload , frame.payload_length );
1377+ continue_frame_buffer ->append (frame.payload , frame.payload_length );
13771378 }
13781379
13791380 if (frame.header .FIN ) {
13801381 uchar complete_opcode = 0 ;
13811382 uchar complete_flags = 0 ;
1382- WebSocket::parse_ext_flags (frame_buffer ->offset , &complete_opcode, &complete_flags);
1383+ WebSocket::parse_ext_flags (continue_frame_buffer ->offset , &complete_opcode, &complete_flags);
13831384
13841385 if (complete_flags & WebSocket::FLAG_RSV1) {
1385- if (sw_unlikely (!FrameObject::uncompress (&zpayload, frame_buffer->str , frame_buffer->length ))) {
1386+ if (sw_unlikely (!FrameObject::uncompress (
1387+ &zpayload, continue_frame_buffer->str , continue_frame_buffer->length ))) {
1388+ continue_frame_buffer.reset ();
13861389 swoole_set_last_error (SW_ERROR_PROTOCOL_ERROR);
13871390 RETURN_NULL ();
13881391 }
13891392 } else {
1390- zend::assign_zend_string_by_val (&zpayload, frame_buffer->str , frame_buffer->length );
1393+ zend::assign_zend_string_by_val (
1394+ &zpayload, continue_frame_buffer->str , continue_frame_buffer->length );
13911395 Z_TRY_ADDREF (zpayload);
1392- frame_buffer.reset ();
13931396 }
13941397
13951398 WebSocket::construct_frame (return_value, complete_opcode, &zpayload, complete_flags);
13961399 zend::object_set (return_value, ZEND_STRL (" fd" ), sock->get_fd ());
13971400 zval_ptr_dtor (&zpayload);
1401+ /* *
1402+ * The final frame of the continuous frame sequence has been received,
1403+ * and the complete message has been assembled. Memory can be released immediately.
1404+ */
1405+ continue_frame_buffer.reset ();
13981406 return ;
13991407 }
14001408 } else {
1401- if (sw_unlikely (frame_buffer )) {
1409+ if (sw_unlikely (continue_frame_buffer )) {
14021410 swoole_warning (" All fragments of a message, except for the initial frame, must use the continuation "
14031411 " frame opcode(0)." );
14041412 RETURN_NULL ();
@@ -1417,12 +1425,12 @@ void WebSocket::recv_frame(const WebSocketSettings &settings,
14171425 zval_ptr_dtor (&zpayload);
14181426 return ;
14191427 } else {
1420- frame_buffer = std::make_shared<String>(
1428+ continue_frame_buffer = std::make_shared<String>(
14211429 (frame.payload_length > 0 ? frame.payload_length : SW_WEBSOCKET_DEFAULT_BUFFER),
14221430 sw_zend_string_allocator ());
1423- frame_buffer ->offset = WebSocket::get_ext_flags (frame.header .OPCODE , frame.get_flags ());
1431+ continue_frame_buffer ->offset = WebSocket::get_ext_flags (frame.header .OPCODE , frame.get_flags ());
14241432 if (sw_likely (frame.payload )) {
1425- frame_buffer ->append (frame.payload , frame.payload_length );
1433+ continue_frame_buffer ->append (frame.payload , frame.payload_length );
14261434 }
14271435 }
14281436 }
@@ -1447,7 +1455,8 @@ static PHP_METHOD(swoole_http_response, recv) {
14471455 Z_PARAM_DOUBLE (timeout)
14481456 ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE);
14491457
1450- WebSocket::recv_frame (ctx->websocket_settings , ctx->frame_buffer , ctx->get_co_socket (), return_value, timeout);
1458+ WebSocket::recv_frame (
1459+ ctx->websocket_settings , ctx->continue_frame_buffer , ctx->get_co_socket (), return_value, timeout);
14511460 if (ZVAL_IS_EMPTY_STRING (return_value)) {
14521461 ctx->close (ctx);
14531462 return ;
0 commit comments