From 62cd1cdc9809a9eb79e51473d3e663e8b6c5bd3c Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sun, 12 Mar 2023 03:37:57 +0000 Subject: [PATCH] Misc changes. --- TODO.txt | 14 ++++++------- src/include/Telodendria.h | 2 +- tools/bin/tt | 12 ++++++++--- tools/src/http.c | 43 +++++++++++++++++++++++++++++++++------ 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/TODO.txt b/TODO.txt index d6f627d..a754ab4 100644 --- a/TODO.txt +++ b/TODO.txt @@ -17,24 +17,24 @@ Milestone: v0.3.0 [ ] Multi-output [ ] Move UtilStreamCopy() [~] HTTP Client API - [ ] Document HttpParseHeaders() + [x] Document HttpParseHeaders() [ ] HttpClient man page [ ] Uri man page [x] Test on other platforms -[~] Option to pretty-print Json - [ ] Document JsonEncode() and JsonEncodeValue() +[x] Option to pretty-print Json + [x] Document JsonEncode() and JsonEncodeValue() [ ] Update man page for td -[ ] Document Telodendria and Main +[x] Document Telodendria and Main [ ] Document tt and http-debug-server -[~] Simple command line tool to make matrix requests +[x] Simple command line tool to make matrix requests [x] Built on HTTP client API - [ ] http man page + [x] http man page [~] Simple command line tool for working with JSON [x] Pretty-print Json [x] Query fields for use in shell scripts. [x] Encode user-provided JSON strings - [ ] json man page + [x] json man page [ ] Update man pages for tp and send-patch [ ] Move configuration to database diff --git a/src/include/Telodendria.h b/src/include/Telodendria.h index 27dcb08..f4ce9f3 100644 --- a/src/include/Telodendria.h +++ b/src/include/Telodendria.h @@ -49,6 +49,6 @@ extern void TelodendriaMemoryIterator(MemoryInfo *, void *); extern void - TelodendriaPrintHeader(LogConfig * lc); + TelodendriaPrintHeader(LogConfig *); #endif diff --git a/tools/bin/tt b/tools/bin/tt index e2ada05..1a5bec2 100755 --- a/tools/bin/tt +++ b/tools/bin/tt @@ -6,6 +6,8 @@ PASSWORD=$(json -e 'p@s$w0rd') ENDPOINT="$1" +: "${METH:=GET}" + # Check if user is available. If it is, register it. user_available=$(http "$BASE/_matrix/client/v3/register/available?username=$USERNAME" | json -s "available") if [ "$user_available" = "true" ]; then @@ -20,7 +22,7 @@ if [ "$user_available" = "true" ]; then printf ' "password": %s,' "$PASSWORD" printf ' "inhibit_login": true ' printf '}' - ) | http -X POST "$BASE/_matrix/client/v3/register" > /dev/null + ) | http -X POST -d @- "$BASE/_matrix/client/v3/register" > /dev/null fi # Log in @@ -33,7 +35,7 @@ ACCESS_TOKEN=$(( printf ' "type": "m.login.password",' printf ' "password": %s' "$PASSWORD" printf '}' -) | http -X POST "$BASE/_matrix/client/v3/login" | json -s "access_token->@decode") +) | http -X POST -d @- "$BASE/_matrix/client/v3/login" | json -s "access_token->@decode") if [ -z "$ACCESS_TOKEN" ]; then echo "Unable to log in." @@ -41,7 +43,11 @@ if [ -z "$ACCESS_TOKEN" ]; then fi # Make request -http -X POST -H "Authorization: Bearer $ACCESS_TOKEN" "$BASE$ENDPOINT" | json +if [ -n "$DATA" ]; then + http -X "$METH" -d "$DATA" -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE}${ENDPOINT}" | json +else + http -X "$METH" -H "Authorization: Bearer $ACCESS_TOKEN" "${BASE}${ENDPOINT}" | json +fi # Log out http -X POST -H "Authorization: Bearer $ACCESS_TOKEN" \ diff --git a/tools/src/http.c b/tools/src/http.c index b87bb7a..db41bcc 100644 --- a/tools/src/http.c +++ b/tools/src/http.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -39,7 +40,7 @@ static void usage(char *prog) { - fprintf(stderr, "Usage: %s [-i -X method -H header] url\n", prog); + fprintf(stderr, "Usage: %s [-i -X method -H header -d data] url\n", prog); } int @@ -49,6 +50,7 @@ main(int argc, char **argv) HttpStatus res; HttpRequestMethod method = HTTP_GET; Uri *uri; + char *data = NULL; HashMap *requestHeaders = HashMapCreate(); char *key; @@ -59,7 +61,7 @@ main(int argc, char **argv) int ch; - while ((ch = getopt(argc, argv, "iH:X:")) != -1) + while ((ch = getopt(argc, argv, "iH:X:d:")) != -1) { switch (ch) { @@ -93,6 +95,9 @@ main(int argc, char **argv) HashMapSet(requestHeaders, key, StrDuplicate(val)); break; + case 'd': + data = optarg; + break; default: usage(argv[0]); return 1; @@ -154,11 +159,37 @@ main(int argc, char **argv) HttpRequestSendHeaders(cx); HashMapFree(requestHeaders); - /* Only send stdin if it's not attached to a TTY. This prevents us - * from blocking if no pipe is provided */ - if (!isatty(fileno(stdin))) + if (data) { - UtilStreamCopy(stdin, HttpClientStream(cx)); + if (*data == '@') + { + FILE *in; + + data++; + + if (strcmp(data, "-") == 0) + { + in = stdin; + } + else + { + in = fopen(data, "r"); + } + + if (!in) + { + fprintf(stderr, "%s: %s\n", data, strerror(errno)); + return 1; + } + + UtilStreamCopy(in, HttpClientStream(cx)); + + fclose(in); + } + else + { + fprintf(HttpClientStream(cx), "%s", data); + } } res = HttpRequestSend(cx);