1e509ee18Sopenharmony_ci/* 2e509ee18Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3e509ee18Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e509ee18Sopenharmony_ci * you may not use this file except in compliance with the License. 5e509ee18Sopenharmony_ci * You may obtain a copy of the License at 6e509ee18Sopenharmony_ci * 7e509ee18Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e509ee18Sopenharmony_ci * 9e509ee18Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e509ee18Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e509ee18Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e509ee18Sopenharmony_ci * See the License for the specific language governing permissions and 13e509ee18Sopenharmony_ci * limitations under the License. 14e509ee18Sopenharmony_ci */ 15e509ee18Sopenharmony_ci 16e509ee18Sopenharmony_ci#ifndef ARKCOMPILER_TOOLCHAIN_WEBSOCKET_FRAME_BUILDER_H 17e509ee18Sopenharmony_ci#define ARKCOMPILER_TOOLCHAIN_WEBSOCKET_FRAME_BUILDER_H 18e509ee18Sopenharmony_ci 19e509ee18Sopenharmony_ci#include "web_socket_frame.h" 20e509ee18Sopenharmony_ci 21e509ee18Sopenharmony_ci#include <string> 22e509ee18Sopenharmony_ci 23e509ee18Sopenharmony_cinamespace OHOS::ArkCompiler::Toolchain { 24e509ee18Sopenharmony_citemplate <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>> 25e509ee18Sopenharmony_ciinline void PushNumberPerByte(std::string& message, T number) 26e509ee18Sopenharmony_ci{ 27e509ee18Sopenharmony_ci constexpr size_t bytesCount = sizeof(T); 28e509ee18Sopenharmony_ci constexpr size_t bitsCount = 8; 29e509ee18Sopenharmony_ci size_t shiftCount = (bytesCount - 1) * bitsCount; 30e509ee18Sopenharmony_ci for (size_t i = 0; i < bytesCount; ++i, shiftCount -= bitsCount) { 31e509ee18Sopenharmony_ci message.push_back((number >> shiftCount) & 0xff); 32e509ee18Sopenharmony_ci } 33e509ee18Sopenharmony_ci} 34e509ee18Sopenharmony_ci 35e509ee18Sopenharmony_ciclass ServerFrameBuilder { 36e509ee18Sopenharmony_cipublic: 37e509ee18Sopenharmony_ci // force users to specify opcode and final bit 38e509ee18Sopenharmony_ci ServerFrameBuilder() = delete; 39e509ee18Sopenharmony_ci ServerFrameBuilder(bool final, FrameType opcode) : fin_(final), opcode_(opcode) 40e509ee18Sopenharmony_ci { 41e509ee18Sopenharmony_ci } 42e509ee18Sopenharmony_ci ~ServerFrameBuilder() noexcept = default; 43e509ee18Sopenharmony_ci 44e509ee18Sopenharmony_ci ServerFrameBuilder& SetFinal(bool fin); 45e509ee18Sopenharmony_ci ServerFrameBuilder& SetOpcode(FrameType opcode); 46e509ee18Sopenharmony_ci ServerFrameBuilder& SetPayload(const std::string& payload); 47e509ee18Sopenharmony_ci ServerFrameBuilder& SetPayload(std::string&& payload); 48e509ee18Sopenharmony_ci ServerFrameBuilder& AppendPayload(const std::string& payload); 49e509ee18Sopenharmony_ci 50e509ee18Sopenharmony_ci std::string Build() const; 51e509ee18Sopenharmony_ci 52e509ee18Sopenharmony_ciprotected: 53e509ee18Sopenharmony_ci void PushHeader(std::string& message, uint8_t payloadLenField) const; 54e509ee18Sopenharmony_ci void PushPayloadLength(std::string& message, uint8_t payloadLenField) const; 55e509ee18Sopenharmony_ci virtual void PushFullHeader(std::string& message, size_t additionalReservedMem) const; 56e509ee18Sopenharmony_ci virtual void PushPayload(std::string& message) const; 57e509ee18Sopenharmony_ci 58e509ee18Sopenharmony_ciprotected: 59e509ee18Sopenharmony_ci bool fin_; 60e509ee18Sopenharmony_ci FrameType opcode_; 61e509ee18Sopenharmony_ci std::string payload_; 62e509ee18Sopenharmony_ci}; 63e509ee18Sopenharmony_ci 64e509ee18Sopenharmony_ciclass ClientFrameBuilder final : public ServerFrameBuilder { 65e509ee18Sopenharmony_cipublic: 66e509ee18Sopenharmony_ci ClientFrameBuilder(bool final, FrameType opcode, const uint8_t maskingKey[WebSocketFrame::MASK_LEN]); 67e509ee18Sopenharmony_ci 68e509ee18Sopenharmony_ci ClientFrameBuilder& SetMask(const uint8_t maskingKey[WebSocketFrame::MASK_LEN]); 69e509ee18Sopenharmony_ci 70e509ee18Sopenharmony_ciprivate: 71e509ee18Sopenharmony_ci void PushMask(std::string& message) const; 72e509ee18Sopenharmony_ci void PushFullHeader(std::string& message, size_t additionalReservedMem) const override; 73e509ee18Sopenharmony_ci void PushPayload(std::string& message) const override; 74e509ee18Sopenharmony_ci 75e509ee18Sopenharmony_ciprivate: 76e509ee18Sopenharmony_ci uint8_t maskingKey_[WebSocketFrame::MASK_LEN] = {0}; 77e509ee18Sopenharmony_ci}; 78e509ee18Sopenharmony_ci} // namespace OHOS::ArkCompiler::Toolchain 79e509ee18Sopenharmony_ci 80e509ee18Sopenharmony_ci#endif // ARKCOMPILER_TOOLCHAIN_WEBSOCKET_FRAME_BUILDER_H 81