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 01/22] chnet: Add initial request body support
Date: Sun, 21 Aug 2022 18:24:32 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

This adds initial request body support, we can now perform HTTP method
other than GET with non-chunked buffer for now. By default if we don't
specify the HTTP method, it will perform an HTTP GET.

TODO(ammarfaizi2): Add chunked request body support.

Signed-off-by: Ammar Faizi <[email protected]>
---
 chnet/chnet.cc    | 55 +++++++++++++++++++++++++++++++++++++++++++++++
 chnet/chnet.h     | 29 +++++++++++++++++++++++++
 tests/cpp/ring.cc | 44 +++++++++++++++++++++++++++++++++++++
 tests/index.php   |  3 +++
 4 files changed, 131 insertions(+)

diff --git a/chnet/chnet.cc b/chnet/chnet.cc
index 9d19882..93abe84 100644
--- a/chnet/chnet.cc
+++ b/chnet/chnet.cc
@@ -13,6 +13,43 @@ using namespace std::chrono_literals;
 
 namespace net {
 
+CHNetDataStream::CHNetDataStream(CHNetDelegate *ch, const char *payload,
+				 size_t payload_len):
+	UploadDataStream(false, 0),
+	ch_(ch),
+	payload_(payload),
+	payload_len_(payload_len)
+{
+	(void)ch_;
+}
+
+int CHNetDataStream::InitInternal(const net::NetLogWithSource& net_log)
+{
+	SetSize(payload_len_);
+	return net::OK;
+}
+
+int CHNetDataStream::ReadInternal(net::IOBuffer *buf, int buf_len)
+{
+	int remaining_size;
+	int pos;
+	int ret;
+
+	pos = (int)position();
+	remaining_size = size() - pos;
+	if (remaining_size < buf_len)
+		ret = remaining_size;
+	else
+		ret = buf_len;
+
+	memcpy(buf->data(), &payload_[pos], ret);
+	return ret;
+}
+
+void CHNetDataStream::ResetInternal()
+{
+}
+
 CHNCallback::CHNCallback(void):
 	response_started_data_(nullptr),
 	response_started_(nullptr),
@@ -44,6 +81,8 @@ CHNetDelegate::CHNetDelegate(void):
 	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,
@@ -89,7 +128,13 @@ void CHNetDelegate::_Start(Waiter *sig)
 	url_req_ctx_ = url_req_ctx_b.Build();
 	url_req_ = url_req_ctx_->CreateRequest(url_, DEFAULT_PRIORITY, this,
 					       traffic_annotation, false);
+	url_req_->set_method(method_);
 	sig->Signal();
+
+	if (payload_)
+		url_req_->set_upload(std::make_unique<CHNetDataStream>(
+					this, payload_, payload_len_));
+
 	url_req_->Start();
 }
 
@@ -207,6 +252,16 @@ const char *CHNet::GetErrorStr(void)
 	return ch_->err_.c_str();
 }
 
+void CHNet::SetMethod(const char *method)
+{
+	ch_->SetMethod(method);
+}
+
+void CHNet::SetPayload(const char *buf, size_t len)
+{
+	ch_->SetPayload(buf, len);
+}
+
 int CHNet::read_ret(void)
 {
 	return ch_->read_ret();
diff --git a/chnet/chnet.h b/chnet/chnet.h
index 15ba8cd..b09d99d 100644
--- a/chnet/chnet.h
+++ b/chnet/chnet.h
@@ -53,6 +53,23 @@
  */
 namespace net {
 
+class CHNetDelegate;
+
+class CHNetDataStream: public UploadDataStream {
+public:
+	CHNetDataStream(CHNetDelegate *ch, const char *payload,
+			size_t payload_len);
+
+private:
+	CHNetDelegate *ch_;
+	const char *payload_;
+	size_t payload_len_;
+
+	int InitInternal(const net::NetLogWithSource& net_log) override;
+	int ReadInternal(net::IOBuffer *buf, int buf_len) override;
+	void ResetInternal() override;
+};
+
 struct CHNCallback {
 	CHNCallback(void);
 	~CHNCallback(void);
@@ -81,6 +98,12 @@ 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 SetPayload(const char *buf, size_t len)
+	{
+		payload_ = buf;
+		payload_len_ = len;
+	}
 
 	struct CHNCallback cb;
 
@@ -92,8 +115,12 @@ private:
 	std::unique_ptr<URLRequest> url_req_;
 	std::atomic<int> read_ret_;
 	base::Thread thread_;
+	const char *method_;
 	GURL url_;
 
+	const char *payload_;
+	size_t payload_len_;
+
 	bool url_is_set_ = false;
 	bool started_ = false;
 
@@ -119,6 +146,8 @@ public:
 	int Start(void);
 	int Read(int size);
 	const char *GetErrorStr(void);
+	void SetMethod(const char *method);
+	void SetPayload(const char *buf, size_t len);
 
 	int read_ret(void);
 	const char *read_buf(void);
diff --git a/tests/cpp/ring.cc b/tests/cpp/ring.cc
index d1d79fd..0d2d391 100644
--- a/tests/cpp/ring.cc
+++ b/tests/cpp/ring.cc
@@ -87,6 +87,49 @@ static void test_chnet_ring_single(void)
 	delete ch;
 }
 
+static void test_chnet_ring_single_post(void)
+{
+	constexpr static const char buf[] = "AAAA Hello World!\n";
+	CNRingCtx ring(4096);
+	CNRingSQE *sqe;
+	CNRingCQE *cqe;
+	CHNet *ch;
+
+	ch = new CHNet;
+	ch->SetURL("http://127.0.0.1:8000/index.php?action=body");
+	ch->SetMethod("POST");
+	ch->SetPayload(buf, sizeof(buf));
+
+	sqe = ring.GetSQE();
+	assert(sqe);
+	sqe->PrepStart(ch);
+	sqe->SetUserData(9999);
+	assert(ring.SubmitSQE() == 1);
+
+	ring.WaitCQE(1);
+	cqe = ring.HeadCQE();
+	assert(cqe->op == CNRING_OP_START);
+	assert(cqe->res == 0);
+	assert(cqe->user_data == 9999);
+	ring.CQAdvance(1);
+
+	sqe = ring.GetSQE();
+	assert(sqe);
+	sqe->PrepRead(ch, 1024);
+	sqe->SetUserData(9999);
+	assert(ring.SubmitSQE() == 1);
+
+	ring.WaitCQE(1);
+	cqe = ring.HeadCQE();
+	assert(cqe->op == CNRING_OP_READ);
+	assert(cqe->res == sizeof(buf));
+	assert(cqe->user_data == 9999);
+	assert(!strncmp(ch->read_buf(), buf, sizeof(buf)));
+	ring.CQAdvance(1);
+
+	delete ch;
+}
+
 #define NR_REQ 1024
 
 static void _test_chnet_ring_multiple(bool start_before_read, CNRingCtx *ring)
@@ -167,6 +210,7 @@ int main(void)
 	test_nop();
 	chnet_global_init();
 	test_chnet_ring_single();
+	test_chnet_ring_single_post();
 	test_chnet_ring_multiple(false);
 	test_chnet_ring_multiple(true);
 	chnet_global_stop();
diff --git a/tests/index.php b/tests/index.php
index 12dec5c..afa3f3a 100644
--- a/tests/index.php
+++ b/tests/index.php
@@ -14,4 +14,7 @@ switch ($_GET["action"]) {
 case "hello":
 	echo "Hello World!\n";
 	break;
+case "body":
+	echo file_get_contents("php://input");
+	break;
 }
-- 
Ammar Faizi


  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 ` Ammar Faizi [this message]
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 ` [PATCH v1 06/22] chnet: node: Add set_method function to set HTTP method Ammar Faizi
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