From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [180.246.144.41]) by gnuweeb.org (Postfix) with ESMTPSA id D73E980A10; Sun, 21 Aug 2022 11:25:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1661081135; bh=+8l+KNtZ+wa8J0/vysWF3w5Ty8HG9cBqOvDtR5qfT7I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kR6RS+3QKTKk56r9KaRXYNF7feLYXlpyactBHz5fLzuq0S9TBfcEX3aa/uq5I0xe5 75wbAVYh0qASpKcV6WVQbHzZwHYUuixZaqPfluJIvYT68oPVIPSjKdPrSL6FmUxynT n5YVMojsdtnsP3tSqbQqle1sjR8lne+NDFFXCvlIc4dqHtrv40h8uh31Dx67rmqHjh wyj5Y1btLH9CNPvOnRrZZ0Rv/wNDK5m4vXugb6IEeItJ4t0PJu6F+PwTEbIttntnOG IvVDM4QpAHgtbF5Aa1Mi05lZenDZj55xa4M3aFHsBO9udOM+k9YZW3KLg74+cPhvqz AQE4+pu2wJTng== From: Ammar Faizi To: Alviro Iskandar Setiawan Cc: Ammar Faizi , Muhammad Rizki , Kanna Scarlet , GNU/Weeb Mailing List Subject: [PATCH v1 08/22] chnet: node: Add set_payload function to set HTTP req body Date: Sun, 21 Aug 2022 18:24:39 +0700 Message-Id: <20220821112453.3026255-9-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220821112453.3026255-1-ammarfaizi2@gnuweeb.org> References: <20220821112453.3026255-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Signed-off-by: Ammar Faizi --- chnet/chnet_node.cc | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/chnet/chnet_node.cc b/chnet/chnet_node.cc index 4a60b0e..a301140 100644 --- a/chnet/chnet_node.cc +++ b/chnet/chnet_node.cc @@ -32,6 +32,7 @@ struct NodeSQData { class NodeCHNet { public: CHNet ch_; + std::string payload_; }; static inline void throw_js_exception(Napi::Env &env, const char *err_msg) @@ -347,8 +348,8 @@ static void CHN_NetSetMethod(const Napi::CallbackInfo &info) } ch = (NodeCHNet *)info.Data(); - const std::string &url = info[0].ToString().Utf8Value(); - ch->ch_.SetMethod(url.c_str()); + const std::string &method =info[0].ToString().Utf8Value(); + ch->ch_.SetMethod(method.c_str()); } static Napi::Value CHN_NetReadBuf(const Napi::CallbackInfo &info) @@ -388,6 +389,25 @@ static Napi::Value CHN_NetGetError(const Napi::CallbackInfo &info) return env.Null(); } +static void CHN_NetSetPayload(const Napi::CallbackInfo &info) +{ + constexpr static const char err_msg[] = + "chnet.set_payload must be given exactly 1 string argument"; + + Napi::Env env = info.Env(); + const char *err; + NodeCHNet *ch; + + if (unlikely(info.Length() != 1 || !info[0].IsString())) { + throw_js_exception(env, err_msg); + return; + } + + ch = (NodeCHNet *)info.Data(); + ch->payload_ = info[0].ToString().Utf8Value(); + ch->ch_.SetPayload(ch->payload_.c_str(), ch->payload_.size()); +} + static void CHN_NetDestruct(Napi::Env env, NodeCHNet *ch) { delete ch; @@ -406,6 +426,7 @@ static Napi::Object CHN_CreateNet(const Napi::CallbackInfo &info) obj_add_func(env, obj, ch, CHN_NetReadRet, "read_ret"); obj_add_func(env, obj, ch, CHN_NetReadBuf, "read_buf"); obj_add_func(env, obj, ch, CHN_NetGetError, "get_error"); + obj_add_func(env, obj, ch, CHN_NetSetPayload, "set_payload"); ch_ptr = (int64_t) (intptr_t) ch; obj["__ch"] = Napi::Number::New(env, ch_ptr); -- Ammar Faizi