logo

httpc

barebones HTTP client, intended for simple usage like downloading filesgit clone https://hacktivis.me/git/httpc.git
commit: 4cd3e2b5ca0938dde5501be83c019f106282ac89
parent 775c33f397eeaf0277c3d6ba3bafaa8aec60b4aa
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon, 24 Jun 2024 18:10:28 +0200

httpc: Take a single URI argument

Diffstat:

Mhttpc.c23++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/httpc.c b/httpc.c @@ -86,9 +86,19 @@ main(int argc, char *argv[]) return 1; } - const char *hostname = "lanodan.eu"; - const char *target = "/"; - const char *port = "443"; + if(argc != 1) + { + fprintf(stderr, "httpc: Expected 1 argument, got %d\n", argc); + return 1; + } + + char *errstr = NULL; + struct URI *uri = decode_uri(argv[0], &errstr); + if(uri == NULL) + { + fprintf(stderr, "httpc: Error while decoding <%s>: %s\n", argv[0], errstr); + return 1; + } struct tls *tls_ctx = tls_client(); struct tls_config *tls_cfg = tls_config_new(); @@ -96,13 +106,15 @@ main(int argc, char *argv[]) if(tls_configure(tls_ctx, tls_cfg) != 0) { fprintf(stderr, "httpc: TLS Configuration Error: %s\n", tls_error(tls_ctx)); + decode_uri_finish(uri); tls_config_free(tls_cfg); return 1; } - if(tls_connect(tls_ctx, hostname, port) != 0) + if(tls_connect(tls_ctx, uri->host, uri->port ? uri->port : "443") != 0) { fprintf(stderr, "httpc: TLS Connection Error: %s\n", tls_error(tls_ctx)); + decode_uri_finish(uri); tls_config_free(tls_cfg); return 1; } @@ -124,7 +136,7 @@ Connection: close\r\n\ \r\n"; char req[PAGESIZE]; - snprintf(req, PAGESIZE, req_fmt, target, hostname); + snprintf(req, PAGESIZE, req_fmt, uri->path ? uri->path : "/", uri->host); if(verbose) write(STDERR_FILENO, req, strlen(req)); @@ -173,6 +185,7 @@ Connection: close\r\n\ } cleanup: + decode_uri_finish(uri); tls_close(tls_ctx); tls_free(tls_ctx); tls_config_free(tls_cfg);