From 75ecb2272491ca86846aaf34d283f3310dab0fab Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Wed, 1 Nov 2023 08:28:35 -0400 Subject: [PATCH] Remove int64 and uint64; add http. --- tools/http.c | 260 +++++++++++++++++++++++++++++++++++++++++++++++++ tools/int64.c | 145 --------------------------- tools/uint64.c | 119 ---------------------- 3 files changed, 260 insertions(+), 264 deletions(-) create mode 100644 tools/http.c delete mode 100644 tools/int64.c delete mode 100644 tools/uint64.c diff --git a/tools/http.c b/tools/http.c new file mode 100644 index 0000000..0b32b74 --- /dev/null +++ b/tools/http.c @@ -0,0 +1,260 @@ +/* + * Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net> + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation files + * (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#define FLAG_HEADERS (1 << 0) + +static void +usage(char *prog) +{ + StreamPrintf(StreamStderr(), "Usage: %s [-i -X method -H header -d data] url\n", prog); +} + +int +Main(Array * args) +{ + HttpClientContext *cx = NULL; + HttpStatus res; + HttpRequestMethod method = HTTP_GET; + Uri *uri = NULL; + char *data = NULL; + + HashMap *requestHeaders = HashMapCreate(); + char *key; + char *val; + + ArgParseState arg; + int flags = 0; + int requestFlags = HTTP_FLAG_NONE; + + int ch; + int ret = 1; + + ArgParseStateInit(&arg); + while ((ch = ArgParse(&arg, args, "iH:X:d:")) != -1) + { + switch (ch) + { + case 'i': + flags |= FLAG_HEADERS; + break; + case 'X': + method = HttpRequestMethodFromString(arg.optArg); + if (!method) + { + StreamPrintf(StreamStderr(), "Unknown request method: %s\n", arg.optArg); + return 1; + } + break; + case 'H': + key = arg.optArg; + val = arg.optArg; + + while (*val && *val != ':') + { + val++; + } + + *val = '\0'; + val++; + + while (*val && isspace((unsigned char) *val)) + { + val++; + } + + HashMapSet(requestHeaders, key, StrDuplicate(val)); + break; + case 'd': + data = arg.optArg; + break; + default: + usage(ArrayGet(args, 0)); + goto finish; + } + } + + if (ArraySize(args) - arg.optInd < 1) + { + usage(ArrayGet(args, 0)); + goto finish; + } + + uri = UriParse(ArrayGet(args, arg.optInd)); + if (!uri) + { + StreamPrintf(StreamStderr(), "Failed to parse URI: %s\n", ArrayGet(args, arg.optInd)); + goto finish; + } + + if (!uri->port) + { + if (StrEquals(uri->proto, "https")) + { + uri->port = 443; + } + else if (StrEquals(uri->proto, "http")) + { + uri->port = 80; + } + } + + if (!uri->port) + { + StreamPrintf(StreamStderr(), "Unknown protocol: %s\n", uri->proto); + goto finish; + } + + if (StrEquals(uri->proto, "https")) + { + requestFlags |= HTTP_FLAG_TLS; + } + + cx = HttpRequest(method, requestFlags, uri->port, uri->host, uri->path); + + if (!cx) + { + StreamPuts(StreamStderr(), "Failed to connect.\n"); + goto finish; + } + + while (HashMapIterate(requestHeaders, &key, (void **) &val)) + { + HttpRequestHeader(cx, key, val); + Free(val); + } + + if (data) + { + if (*data == '@') + { + Stream *in; + int len; + + data++; + + if (StrEquals(data, "-")) + { + in = StreamStdin(); + } + else + { + in = StreamOpen(data, "r"); + } + + if (!in) + { + StreamPrintf(StreamStderr(), "%s: %s\n", data, strerror(errno)); + goto finish; + } + + len = StreamSeek(in, 0, SEEK_END); + if (len > -1) + { + char *lenStr; + int nBytes; + + StreamSeek(in, 0, SEEK_SET); + nBytes = snprintf(NULL, 0, "%d", len); + + lenStr = Malloc(nBytes + 1); + snprintf(lenStr, nBytes + 1, "%d", len); + + HttpRequestHeader(cx, "Content-Length", lenStr); + Free(lenStr); + } + + HttpRequestSendHeaders(cx); + StreamCopy(in, HttpClientStream(cx)); + if (in != StreamStdin()) + { + StreamClose(in); + } + } + else + { + char *lenStr; + int len = strlen(data); + int nBytes = snprintf(NULL, 0, "%d", len); + + lenStr = Malloc(nBytes + 1); + snprintf(lenStr, nBytes + 1, "%d", len); + + HttpRequestHeader(cx, "Content-Length", lenStr); + Free(lenStr); + HttpRequestSendHeaders(cx); + StreamPuts(HttpClientStream(cx), data); + } + } + else + { + HttpRequestSendHeaders(cx); + } + + res = HttpRequestSend(cx); + + if (!res) + { + StreamPuts(StreamStderr(), "Failed to send request.\n"); + goto finish; + } + + if (flags & FLAG_HEADERS) + { + HashMap *responseHeaders = HttpResponseHeaders(cx); + + StreamPrintf(StreamStdout(), "HTTP/1.0 %d %s\n", res, HttpStatusToString(res)); + + while (HashMapIterate(responseHeaders, &key, (void **) &val)) + { + StreamPrintf(StreamStdout(), "%s: %s\n", key, val); + } + + StreamPutc(StreamStdout(), '\n'); + } + + StreamCopy(HttpClientStream(cx), StreamStdout()); + + ret = !(res == HTTP_OK); + +finish: + HashMapFree(requestHeaders); + + HttpClientContextFree(cx); + UriFree(uri); + + return ret; +} diff --git a/tools/int64.c b/tools/int64.c deleted file mode 100644 index fb9c5cf..0000000 --- a/tools/int64.c +++ /dev/null @@ -1,145 +0,0 @@ -#include - -#include - -/* AssertEquals(actual, expected) */ -int -AssertEquals(char *msg, Int64 x, Int64 y) -{ - if (!Int64Eq(x, y)) - { - Log(LOG_ERR, "%s: Expected 0x%X 0x%X, got 0x%X 0x%X", msg, - Int64High(y), Int64Low(y), - Int64High(x), Int64Low(x)); - - return 0; - } - - return 1; -} - -int -Main(void) -{ - Int64 x, y; - - Log(LOG_INFO, "sizeof(Int64) = %lu", sizeof(Int64)); - -#ifdef INT64_NATIVE - Log(LOG_INFO, "Using native 64-bit integers."); -#else - Log(LOG_INFO, "Using emulated 64-bit integers."); -#endif - - /* BSR Tests */ - - x = Int64Create(0x000000FF, 0x00000000); - - y = Int64Sra(x, 4); - AssertEquals("x >> 4", y, Int64Create(0x0000000F, 0xF0000000)); - - y = Int64Sra(x, 8); - AssertEquals("x >> 8", y, Int64Create(0x00000000, 0xFF000000)); - - y = Int64Sra(x, 36); - AssertEquals("x >> 36", y, Int64Create(0x00000000, 0x0000000F)); - - x = Int64Create(0xFF000000, 0x00000000); - - y = Int64Sra(x, 4); - AssertEquals("x >> 4", y, Int64Create(0xFFF00000, 0x00000000)); - - y = Int64Sra(x, 8); - AssertEquals("x >> 8", y, Int64Create(0xFFFF0000, 0x00000000)); - - y = Int64Sra(x, 63); - AssertEquals("x >> 63", y, Int64Create(0xFFFFFFFF, 0xFFFFFFFF)); - - /* BSL Tests */ - - x = Int64Create(0x00000000, 0xFF000000); - - y = Int64Sll(x, 4); - AssertEquals("x << 4", y, Int64Create(0x0000000F, 0xF0000000)); - - y = Int64Sll(x, 8); - AssertEquals("x << 8", y, Int64Create(0x000000FF, 0x00000000)); - - y = Int64Sll(x, 36); - AssertEquals("x << 36", y, Int64Create(0xF0000000, 0x00000000)); - - /* ADD Tests */ - - x = Int64Create(0x00000000, 0xF0000001); - y = Int64Create(0x00000000, 0x00000002); - AssertEquals("0xF0000001 + 0x00000002", Int64Add(x, y), Int64Create(0x00000000, 0xF0000003)); - - x = Int64Create(0x00000000, 0xF0000000); - y = Int64Create(0x00000000, 0x10000000); - AssertEquals("0xF0000000 + 0x10000000", Int64Add(x, y), Int64Create(0x00000001, 0x00000000)); - - x = Int64Create(0, 5); - y = Int64Neg(Int64Create(0, 10)); - AssertEquals("5 + (-10)", Int64Add(x, y), Int64Neg(Int64Create(0, 5))); - - /* SUB Tests */ - x = Int64Create(0x00000000, 0x00000005); - y = Int64Create(0x00000000, 0x00000002); - AssertEquals("0x00000005 - 0x00000002", Int64Sub(x, y), Int64Create(0x00000000, 0x00000003)); - - x = Int64Create(0x00000001, 0x00000000); - y = Int64Create(0x00000000, 0x00000001); - AssertEquals("0x00000001 0x00000000 - 0x00000001", Int64Sub(x, y), Int64Create(0x00000000, 0xFFFFFFFF)); - - x = Int64Create(0, 5); - y = Int64Create(0, 10); - AssertEquals("5 - 10", Int64Sub(x, y), Int64Neg(Int64Create(0, 5))); - - x = Int64Create(0, 5); - y = Int64Neg(Int64Create(0, 10)); - AssertEquals("5 - (-10)", Int64Sub(x, y), Int64Create(0, 15)); - - /* MUL Tests */ - x = Int64Create(0, 18); - y = Int64Create(0, 1); - AssertEquals("18 * 1", Int64Mul(x, y), Int64Create(0, 18)); - - x = Int64Create(0, 20); - y = Int64Create(0, 12); - AssertEquals("20 * 12", Int64Mul(x, y), Int64Create(0, 240)); - - x = Int64Create(0x00000000, 0x00000005); - y = Int64Create(0x00000000, 0x00000005); - AssertEquals("0x00000005 * 0x00000005", Int64Mul(x, y), Int64Create(0x00000000, 0x00000019)); - - x = Int64Create(0x00000001, 0x00000000); - y = Int64Create(0x00000000, 0x00000005); - AssertEquals("0x00000001 0x00000000 * 0x00000005", Int64Mul(x, y), Int64Create(0x00000005, 0x00000000)); - - /* DIV Tests */ - x = Int64Create(0, 12); - y = Int64Create(0, 4); - AssertEquals("12 / 4", Int64Div(x, y), Int64Create(0, 3)); - - /* MOD Tests */ - x = Int64Create(0x000000FF, 0x00000000); - y = Int64Create(0x00000000, 0x00000010); - AssertEquals("0x000000FF 0x00000000 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0)); - - x = Int64Create(0x00000000, 0xFF000000); - y = Int64Create(0x00000000, 0x00000010); - AssertEquals("0x00000000 0xFF000000 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0)); - - x = Int64Create(0xFF000000, 0x00000000); - y = Int64Create(0x00000000, 0x00000010); - AssertEquals("0xFF000000 0x00000000 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0)); - - x = Int64Create(0x00000000, 0x000000F0); - y = Int64Create(0x00000000, 0x00000010); - AssertEquals("0x00000000 0x000000F0 mod 0x00000010", Int64Rem(x, y), Int64Create(0, 0)); - - /* TODO: Add more tests for negative multiplication, division, and - * mod */ - - return 0; -} diff --git a/tools/uint64.c b/tools/uint64.c deleted file mode 100644 index 8e9f5f5..0000000 --- a/tools/uint64.c +++ /dev/null @@ -1,119 +0,0 @@ -#include - -#include - -/* AssertEquals(actual, expected) */ -int -AssertEquals(char *msg, UInt64 x, UInt64 y) -{ - if (!UInt64Eq(x, y)) - { - Log(LOG_ERR, "%s: Expected 0x%X 0x%X, got 0x%X 0x%X", msg, - UInt64High(y), UInt64Low(y), - UInt64High(x), UInt64Low(x)); - - return 0; - } - - return 1; -} - -int -Main(void) -{ - UInt64 x, y; - - Log(LOG_INFO, "sizeof(UInt64) = %lu", sizeof(UInt64)); - -#ifdef UINT64_NATIVE - Log(LOG_INFO, "Using native 64-bit integers."); -#else - Log(LOG_INFO, "Using emulated 64-bit integers."); -#endif - - /* BSR Tests */ - - x = UInt64Create(0x000000FF, 0x00000000); - - y = UInt64Srl(x, 4); - AssertEquals("x >> 4", y, UInt64Create(0x0000000F, 0xF0000000)); - - y = UInt64Srl(x, 8); - AssertEquals("x >> 8", y, UInt64Create(0x00000000, 0xFF000000)); - - y = UInt64Srl(x, 36); - AssertEquals("x >> 36", y, UInt64Create(0x00000000, 0x0000000F)); - - /* BSL Tests */ - - x = UInt64Create(0x00000000, 0xFF000000); - - y = UInt64Sll(x, 4); - AssertEquals("x << 4", y, UInt64Create(0x0000000F, 0xF0000000)); - - y = UInt64Sll(x, 8); - AssertEquals("x << 8", y, UInt64Create(0x000000FF, 0x00000000)); - - y = UInt64Sll(x, 36); - AssertEquals("x << 36", y, UInt64Create(0xF0000000, 0x00000000)); - - /* ADD Tests */ - - x = UInt64Create(0x00000000, 0xF0000001); - y = UInt64Create(0x00000000, 0x00000002); - AssertEquals("0xF0000001 + 0x00000002", UInt64Add(x, y), UInt64Create(0x00000000, 0xF0000003)); - - x = UInt64Create(0x00000000, 0xF0000000); - y = UInt64Create(0x00000000, 0x10000000); - AssertEquals("0xF0000000 + 0x10000000", UInt64Add(x, y), UInt64Create(0x00000001, 0x00000000)); - - /* SUB Tests */ - x = UInt64Create(0x00000000, 0x00000005); - y = UInt64Create(0x00000000, 0x00000002); - AssertEquals("0x00000005 - 0x00000002", UInt64Sub(x, y), UInt64Create(0x00000000, 0x00000003)); - - x = UInt64Create(0x00000001, 0x00000000); - y = UInt64Create(0x00000000, 0x00000001); - AssertEquals("0x00000001 0x00000000 - 0x00000001", UInt64Sub(x, y), UInt64Create(0x00000000, 0xFFFFFFFF)); - - /* MUL Tests */ - x = UInt64Create(0, 18); - y = UInt64Create(0, 1); - AssertEquals("18 * 1", UInt64Mul(x, y), UInt64Create(0, 18)); - - x = UInt64Create(0, 20); - y = UInt64Create(0, 12); - AssertEquals("20 * 12", UInt64Mul(x, y), UInt64Create(0, 240)); - - x = UInt64Create(0x00000000, 0x00000005); - y = UInt64Create(0x00000000, 0x00000005); - AssertEquals("0x00000005 * 0x00000005", UInt64Mul(x, y), UInt64Create(0x00000000, 0x00000019)); - - x = UInt64Create(0x00000001, 0x00000000); - y = UInt64Create(0x00000000, 0x00000005); - AssertEquals("0x00000001 0x00000000 * 0x00000005", UInt64Mul(x, y), UInt64Create(0x00000005, 0x00000000)); - - /* DIV Tests */ - x = UInt64Create(0, 12); - y = UInt64Create(0, 4); - AssertEquals("12 / 4", UInt64Div(x, y), UInt64Create(0, 3)); - - /* MOD Tests */ - x = UInt64Create(0x000000FF, 0x00000000); - y = UInt64Create(0x00000000, 0x00000010); - AssertEquals("0x000000FF 0x00000000 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0)); - - x = UInt64Create(0x00000000, 0xFF000000); - y = UInt64Create(0x00000000, 0x00000010); - AssertEquals("0x00000000 0xFF000000 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0)); - - x = UInt64Create(0xFF000000, 0x00000000); - y = UInt64Create(0x00000000, 0x00000010); - AssertEquals("0xFF000000 0x00000000 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0)); - - x = UInt64Create(0x00000000, 0x000000F0); - y = UInt64Create(0x00000000, 0x00000010); - AssertEquals("0x00000000 0x000000F0 mod 0x00000010", UInt64Rem(x, y), UInt64Create(0, 0)); - - return 0; -}