From f3c8c104703851ec46e56ccbece5212d38d78e96 Mon Sep 17 00:00:00 2001 From: stefanocasazza Date: Tue, 9 Oct 2018 16:57:44 +0200 Subject: [PATCH] add U_WS_MESSAGE_TYPE_BROTLI --- include/ulib/net/client/websocket.h | 2 +- include/ulib/utility/websocket.h | 2 + src/ulib/net/server/plugin/usp/modsocket.usp | 2 +- src/ulib/net/socket.cpp | 16 ++++-- src/ulib/utility/websocket.cpp | 55 +++++++++++++++----- tests/examples/TSA/tsaserial | 2 +- 6 files changed, 59 insertions(+), 20 deletions(-) diff --git a/include/ulib/net/client/websocket.h b/include/ulib/net/client/websocket.h index cad00bad..88330469 100644 --- a/include/ulib/net/client/websocket.h +++ b/include/ulib/net/client/websocket.h @@ -67,7 +67,7 @@ public: U_RETURN(false); } - bool sendMessage(const UString& msg, int type = U_WS_MESSAGE_TYPE_TEXT) + bool sendMessage(const UString& msg, int type = U_WS_MESSAGE_TYPE_TEXT) // U_WS_MESSAGE_TYPE_BROTLI { U_TRACE(0, "UWebSocketClient::sendMessage(%V,%d)", msg.rep, type) diff --git a/include/ulib/utility/websocket.h b/include/ulib/utility/websocket.h index b54bc1e1..3385846b 100644 --- a/include/ulib/utility/websocket.h +++ b/include/ulib/utility/websocket.h @@ -19,6 +19,7 @@ #define U_WS_MESSAGE_TYPE_INVALID -1 #define U_WS_MESSAGE_TYPE_TEXT 0 +#define U_WS_MESSAGE_TYPE_BROTLI 6 #define U_WS_MESSAGE_TYPE_BINARY 128 #define U_WS_MESSAGE_TYPE_CLOSE 255 #define U_WS_MESSAGE_TYPE_PING 256 @@ -27,6 +28,7 @@ #define U_WS_OPCODE_CONTINUATION 0x0 #define U_WS_OPCODE_TEXT 0x1 #define U_WS_OPCODE_BINARY 0x2 +#define U_WS_OPCODE_BROTLI 0x6 #define U_WS_OPCODE_CLOSE 0x8 #define U_WS_OPCODE_PING 0x9 #define U_WS_OPCODE_PONG 0xA diff --git a/src/ulib/net/server/plugin/usp/modsocket.usp b/src/ulib/net/server/plugin/usp/modsocket.usp index e31f1286..d136194f 100644 --- a/src/ulib/net/server/plugin/usp/modsocket.usp +++ b/src/ulib/net/server/plugin/usp/modsocket.usp @@ -76,7 +76,7 @@ if (isMessageForAnotherUSP(*UWebSocket::message)) } else if (isMessageForAnotherWebSocket(*UWebSocket::message)) { - if (wsclient->connectServer(U_STRING_FROM_CONSTANT("ws://localhost:8888/websocket"))) (void) wsclient->sendMessage(*UWebSocket::message); + if (wsclient->connectServer(U_STRING_FROM_CONSTANT("ws://localhost:8888/websocket"))) (void) wsclient->sendMessage(*UWebSocket::message, U_WS_MESSAGE_TYPE_BROTLI); // ... } diff --git a/src/ulib/net/socket.cpp b/src/ulib/net/socket.cpp index f32a6beb..cca763f2 100644 --- a/src/ulib/net/socket.cpp +++ b/src/ulib/net/socket.cpp @@ -83,8 +83,6 @@ USocket::USocket(bool bSocketIsIPv6, int fd) U_TRACE_CTOR(0, USocket, "%b,%d", bSocketIsIPv6, fd) flags = O_RDWR; - iState = CLOSE; - iSockDesc = fd; iLocalPort = iRemotePort = 0; @@ -94,9 +92,21 @@ USocket::USocket(bool bSocketIsIPv6, int fd) U_socket_IPv6(this) = false; #endif - U_socket_Type(this) = 0; U_socket_LocalSet(this) = false; + if (fd == -1) + { + iSockDesc = -1; + iState = CLOSE; + U_socket_Type(this) = 0; + } + else + { + iSockDesc = fd; + iState = CONNECT; + U_socket_Type(this) = SK_STREAM; + } + #ifdef _MSWINDOWS_ fh = fd; #endif diff --git a/src/ulib/utility/websocket.cpp b/src/ulib/utility/websocket.cpp index e1978960..01822da7 100644 --- a/src/ulib/utility/websocket.cpp +++ b/src/ulib/utility/websocket.cpp @@ -175,7 +175,7 @@ loop: { status_code = U_WS_STATUS_CODE_INTERNAL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_INTERNAL_ERROR); } block = (unsigned char*) rbuffer->data(); @@ -199,7 +199,7 @@ loop: status_code = U_WS_STATUS_CODE_PROTOCOL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_PROTOCOL_ERROR); } fin = U_WS_FRAME_GET_FIN( block[block_offset]); @@ -217,7 +217,7 @@ loop: status_code = U_WS_STATUS_CODE_PROTOCOL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_PROTOCOL_ERROR); } frame = &UWebSocket::control_frame; @@ -236,7 +236,7 @@ loop: status_code = U_WS_STATUS_CODE_PROTOCOL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_PROTOCOL_ERROR); } frame->opcode = opcode; @@ -249,7 +249,7 @@ loop: status_code = U_WS_STATUS_CODE_PROTOCOL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_PROTOCOL_ERROR); } frame->fin = fin; @@ -295,7 +295,7 @@ loop: status_code = U_WS_STATUS_CODE_PROTOCOL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_PROTOCOL_ERROR); } framing_state = U_WS_DATA_FRAMING_PAYLOAD_LENGTH_EXT; // 3 @@ -329,7 +329,7 @@ loop: status_code = U_WS_STATUS_CODE_MESSAGE_TOO_LARGE; // Invalid payload length - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_MESSAGE_TOO_LARGE); } if (masking == 0) @@ -490,7 +490,7 @@ loop: status_code = U_WS_STATUS_CODE_INVALID_UTF8; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_INVALID_UTF8); } message_type = U_WS_MESSAGE_TYPE_TEXT; @@ -498,6 +498,7 @@ loop: break; case U_WS_OPCODE_BINARY: message_type = U_WS_MESSAGE_TYPE_BINARY; break; + case U_WS_OPCODE_BROTLI: message_type = U_WS_MESSAGE_TYPE_BROTLI; break; case U_WS_OPCODE_CLOSE: { @@ -505,7 +506,7 @@ loop: status_code = U_WS_STATUS_CODE_OK; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_OK); } case U_WS_OPCODE_PING: @@ -514,7 +515,7 @@ loop: { status_code = U_WS_STATUS_CODE_PROTOCOL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_PROTOCOL_ERROR); } } break; @@ -527,7 +528,7 @@ loop: status_code = U_WS_STATUS_CODE_PROTOCOL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_PROTOCOL_ERROR); } } @@ -547,7 +548,15 @@ loop: status_code = U_WS_STATUS_CODE_OK; - U_RETURN(status_code); +# ifdef USE_LIBBROTLI + if (message_type == U_WS_MESSAGE_TYPE_BROTLI && + UStringExt::isBrotli(*message)) + { + *message = UStringExt::unbrotli(*message); + } +# endif + + U_RETURN(U_WS_STATUS_CODE_OK); } frame->application_data = U_NULLPTR; @@ -569,7 +578,7 @@ loop: status_code = U_WS_STATUS_CODE_PROTOCOL_ERROR; - U_RETURN(status_code); + U_RETURN(U_WS_STATUS_CODE_PROTOCOL_ERROR); } } } @@ -591,7 +600,7 @@ bool UWebSocket::sendData(USocket* socket, int type, const char* data, uint32_t uint8_t opcode, masking_key[4]; uint32_t header_length = 6U + (len > 125U ? 2U : 0) + (len > 0xffff ? 8U : 0), ncount = header_length + len; - UString tmp(ncount); + UString tmp(ncount), compressed; unsigned char* header = (unsigned char*)tmp.data(); *((uint32_t*)masking_key) = u_get_num_random(); @@ -603,6 +612,24 @@ bool UWebSocket::sendData(USocket* socket, int type, const char* data, uint32_t opcode = U_WS_OPCODE_TEXT; break; + case U_WS_MESSAGE_TYPE_BROTLI: + { + opcode = U_WS_OPCODE_TEXT; + +# ifdef USE_LIBBROTLI + if (compressed = UStringExt::brotli(data, len, (U_PARALLELIZATION_CHILD ? BROTLI_MAX_QUALITY : UHTTP::brotli_level_for_dynamic_content))) + { + opcode = U_WS_OPCODE_BROTLI; + + len = compressed.size(); + data = compressed.data(); + + U_SRV_LOG("websocket compressed request: %u bytes - (%u%%) brotli compression ratio", len, 100-UStringExt::ratio); + } +# endif + } + break; + case U_WS_MESSAGE_TYPE_PING: opcode = U_WS_OPCODE_PING; break; case U_WS_MESSAGE_TYPE_PONG: opcode = U_WS_OPCODE_PONG; break; case U_WS_MESSAGE_TYPE_BINARY: opcode = U_WS_OPCODE_BINARY; break; diff --git a/tests/examples/TSA/tsaserial b/tests/examples/TSA/tsaserial index 6e6161fe..f114fa2f 100644 --- a/tests/examples/TSA/tsaserial +++ b/tests/examples/TSA/tsaserial @@ -1 +1 @@ -0424 +0432