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 825EA80615; Sun, 21 Aug 2022 11:25:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1661081131; bh=llzf1E2klokjxuNMZBG1TvL3yub+J9uBB9dZRnJIzlM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S/YexFZaxTszjEaFOeydnH2JaMm9ff4Y0vciPKMxXdzx0ZGWeGkDps/Br76O/ZSy/ CZfqFoLKYALQ3mnFxVbkdggvfYyTMsdPDo2ElULyI+N3X+fKn0EGGo+j3/eGhBwgOw 0LBAaVC1gLB/N5Jf03EfhcpZ/TKJPvedUibhF55N2gWROCmfhm3W5ss8KdHsOIDmBx mlN8y7UgglCVA8g+/rVFWdMygYv6PqokxWwsg0XkZKfyIZNvnc5mroUsOr3tDzLU+9 3luTFG6QI83PYX4or5GKH6hy26yba7dpfx7HQ6E70DMHjtZZOMYsDPbHgB14srkz/L rXxSVpXXtPgJw== From: Ammar Faizi To: Alviro Iskandar Setiawan Cc: Ammar Faizi , Muhammad Rizki , Kanna Scarlet , GNU/Weeb Mailing List Subject: [PATCH v1 06/22] chnet: node: Add set_method function to set HTTP method Date: Sun, 21 Aug 2022 18:24:37 +0700 Message-Id: <20220821112453.3026255-7-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: By defautlt, the chromium HTTP request will perform an HTTP GET. Make it possible to set the HTTP method from the NodeJS. Also, change the internal string storage to std::string, because if we use a `const char *`, it make the lifetime management harder with respoect to the caller. Signed-off-by: Ammar Faizi --- chnet/chnet.cc | 6 +++--- chnet/chnet.h | 4 ++-- chnet/chnet_node.cc | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/chnet/chnet.cc b/chnet/chnet.cc index 93abe84..058af89 100644 --- a/chnet/chnet.cc +++ b/chnet/chnet.cc @@ -76,13 +76,13 @@ net::DefineNetworkTrafficAnnotation("CHNetDelegate", R"( })"); CHNetDelegate::CHNetDelegate(void): - thread_("chromium_thread") + thread_("chromium_thread"), + method_("GET") { base::Thread::Options options(base::MessagePumpType::IO, 0); CHECK(thread_.StartWithOptions(std::move(options))); read_ret_.store(0, std::memory_order_relaxed); payload_ = nullptr; - method_ = "GET"; } static void CHNetDelegateDestruct(std::unique_ptr *url_req, @@ -254,7 +254,7 @@ const char *CHNet::GetErrorStr(void) void CHNet::SetMethod(const char *method) { - ch_->SetMethod(method); + ch_->SetMethod(std::string(method)); } void CHNet::SetPayload(const char *buf, size_t len) diff --git a/chnet/chnet.h b/chnet/chnet.h index b09d99d..7f6359b 100644 --- a/chnet/chnet.h +++ b/chnet/chnet.h @@ -98,7 +98,7 @@ public: inline int read_ret(void) { return read_ret_.load(); } inline const char *read_buf(void) { return read_buf_->data(); } inline bool started(void) { return started_; } - inline void SetMethod(const char *method) { method_ = method; } + inline void SetMethod(std::string method) { method_ = method; } inline void SetPayload(const char *buf, size_t len) { payload_ = buf; @@ -115,7 +115,7 @@ private: std::unique_ptr url_req_; std::atomic read_ret_; base::Thread thread_; - const char *method_; + std::string method_; GURL url_; const char *payload_; diff --git a/chnet/chnet_node.cc b/chnet/chnet_node.cc index 7d0c6bc..fdac525 100644 --- a/chnet/chnet_node.cc +++ b/chnet/chnet_node.cc @@ -333,6 +333,24 @@ static void CHN_NetSetURL(const Napi::CallbackInfo &info) ch->ch_.SetURL(url.c_str()); } +static void CHN_NetSetMethod(const Napi::CallbackInfo &info) +{ + constexpr static const char err_msg[] = + "chnet.set_method must be given exactly 1 string argument"; + + Napi::Env env = info.Env(); + NodeCHNet *ch; + + if (unlikely(info.Length() != 1 || !info[0].IsString())) { + throw_js_exception(env, err_msg); + return; + } + + ch = (NodeCHNet *)info.Data(); + const std::string &url = info[0].ToString().Utf8Value(); + ch->ch_.SetMethod(url.c_str()); +} + static Napi::Value CHN_NetReadBuf(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); @@ -370,6 +388,7 @@ static Napi::Object CHN_CreateNet(const Napi::CallbackInfo &info) int64_t ch_ptr; obj_add_func(env, obj, ch, CHN_NetSetURL, "set_url"); + obj_add_func(env, obj, ch, CHN_NetSetMethod, "set_method"); obj_add_func(env, obj, ch, CHN_NetReadRet, "read_ret"); obj_add_func(env, obj, ch, CHN_NetReadBuf, "read_buf"); -- Ammar Faizi