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, NUMERIC_HTTP_ADDR,URIBL_BLOCKED,WEIRD_PORT 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 A0B0B809EA; Sun, 21 Aug 2022 11:25:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1661081146; bh=LyUZ4cwtd/KuGPiRIkiRqIukNii8/Kev21zZfhWiomU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W4IJmC/mzq1IoKmz46gzcnxiM68QltMEbNN+jeb6c0Lp7ZliU9GbNY7sVGHJJYvwi GlNQUku7iu9XaDKrJmR0f7ga+m34ae/7Oqdhw4GBXGTZ4T1Qv0eZghdAwMUhrf6037 noertXieAzwF6iQeYCeSmRJKG6rw2VZWkF6MI2k8ikUALbrvgUYEgl/gxbMcW9Co4P cfLHtj2FrhxWDsSgtiwti6N/Db63ktEyUrdIcfmlGpNThAfS56H1QEah9+qDxkNDys 4Mii4W9f6lNzfNGRa01s9JOJ/8H29Nv0jOdetrHJd6l4brnTYkhleHkf4IcbL+MAub 3GNijaEYqteUw== From: Ammar Faizi To: Alviro Iskandar Setiawan Cc: Ammar Faizi , Muhammad Rizki , Kanna Scarlet , GNU/Weeb Mailing List Subject: [PATCH v1 13/22] chnet: node: Add set request header function in NodeJS Date: Sun, 21 Aug 2022 18:24:44 +0700 Message-Id: <20220821112453.3026255-14-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: This adds set request header functionality. Also, update the ring.js example to perform an HTTP request that executes this new function. Signed-off-by: Ammar Faizi --- chnet/chnet_node.cc | 41 +++++++++++++++++++++++++++++++++++++++++ tests/js/ring.js | 12 ++++++++++++ 2 files changed, 53 insertions(+) diff --git a/chnet/chnet_node.cc b/chnet/chnet_node.cc index cfa919b..1269577 100644 --- a/chnet/chnet_node.cc +++ b/chnet/chnet_node.cc @@ -352,6 +352,46 @@ static void CHN_NetSetMethod(const Napi::CallbackInfo &info) ch->ch_.SetMethod(method.c_str()); } +static void CHN_NetSetRequestHeader(const Napi::CallbackInfo &info) +{ + constexpr static const char err_msg[] = + "chnet.set_request_header must be at least given 2 string arguments"; + constexpr static const char err_msg2[] = + "chnet.set_request_header can only receive 3 arguments with the third argument being a boolean"; + + Napi::Env env = info.Env(); + bool overwrite = true; + NodeCHNet *ch; + int nr_arg; + + + nr_arg = info.Length(); + if (unlikely(nr_arg < 2 || !info[0].IsString() || + !info[1].IsString())) { + throw_js_exception(env, err_msg); + return; + } + + if (unlikely(nr_arg > 3)) { + throw_js_exception(env, err_msg2); + return; + } + + if (nr_arg == 3) { + if (unlikely(!info[2].IsBoolean())) { + throw_js_exception(env, err_msg2); + return; + } + overwrite = info[2].As(); + } + + ch = (NodeCHNet *)info.Data(); + const std::string &key = info[0].ToString().Utf8Value(); + const std::string &val = info[1].ToString().Utf8Value(); + ch->ch_.SetRequestHeader(key.c_str(), val.c_str(), overwrite); +} + + static Napi::Value CHN_NetReadBuf(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); @@ -422,6 +462,7 @@ static Napi::Object CHN_CreateNet(const Napi::CallbackInfo &info) 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_NetSetRequestHeader, "set_request_header"); 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"); diff --git a/tests/js/ring.js b/tests/js/ring.js index 3a4d25d..4b081ae 100644 --- a/tests/js/ring.js +++ b/tests/js/ring.js @@ -140,11 +140,23 @@ function test_simple_http() assert(h.ch.read_ret() === payload.length); } +function test_simple_http_set_header() +{ + const ua = "This is just a test user agent!"; + let h = new SimpleHttp("http://127.0.0.1:8000/index.php?action=user_agent", "GET"); + h.ch.set_request_header("User-Agent", ua); + h.prep_read(1024); + h.run(); + assert(h.get_buffer() === ua); + assert(h.ch.read_ret() === ua.length); +} + function main() { test_nop(); test_chnet_ring_multiple(); test_simple_http(); + test_simple_http_set_header(); } main(); -- Ammar Faizi