public inbox for [email protected]
 help / color / mirror / Atom feed
From: Ammar Faizi <[email protected]>
To: Alviro Iskandar Setiawan <[email protected]>
Cc: Ammar Faizi <[email protected]>,
	Muhammad Rizki <[email protected]>,
	Kanna Scarlet <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v1 06/22] chnet: node: Add set_method function to set HTTP method
Date: Sun, 21 Aug 2022 18:24:37 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
---
 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<URLRequest> *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<URLRequest> url_req_;
 	std::atomic<int> 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


  parent reply	other threads:[~2022-08-21 11:25 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-21 11:24 [PATCH v1 00/22] ncns updates Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 01/22] chnet: Add initial request body support Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 02/22] chnet: node: Add set_user_data support on SQE Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 03/22] tests/js/ring: Update the unit test to utilize set_user_data Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 04/22] binding.gyp: Add `-ggdb3` flag for better debugging experience Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 05/22] binding.gyp: Add `-Wno-enum-constexpr-conversion` flag Ammar Faizi
2022-08-21 11:24 ` Ammar Faizi [this message]
2022-08-21 11:24 ` [PATCH v1 07/22] chnet: node: Add get_error function to return the error string Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 08/22] chnet: node: Add set_payload function to set HTTP req body Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 09/22] tests/js/ring: Add simple HTTP POST request example in NodeJS Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 10/22] chnet: Split construct URL req creation into a new function Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 11/22] chnet: Add set request header support Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 12/22] chnet: node: Fix unused variable warning Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 13/22] chnet: node: Add set request header function in NodeJS Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 14/22] tests/js/ring: Add more set header function test Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 15/22] chnet: node: Don't use static counter for data ID Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 16/22] tests/js/ring: Add JavaScript class wrapper example Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 17/22] chnet: Initial chunked request body support Ammar Faizi
2022-08-21 22:13   ` Alviro Iskandar Setiawan
2022-08-22  3:08     ` Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 18/22] chnet: Rework the chunked request body interface Ammar Faizi
2022-08-21 22:20   ` Alviro Iskandar Setiawan
2022-08-21 11:24 ` [PATCH v1 19/22] chnet: ring: Refactor the ring completely Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 20/22] chnet: Use busy-waiting for signal waiter Ammar Faizi
2022-08-21 22:29   ` Alviro Iskandar Setiawan
2022-08-21 11:24 ` [PATCH v1 21/22] chnet: ring: Bump max_entry to 2G Ammar Faizi
2022-08-21 11:24 ` [PATCH v1 22/22] tests/cpp: Delete basic.cpp as it's no longer relevant Ammar Faizi
2022-08-21 22:21   ` Alviro Iskandar Setiawan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox