From 53f600f88a4e0b4c313959c8850e5b4c268bbcc4 Mon Sep 17 00:00:00 2001 From: Franz Auernigg Date: Tue, 28 Feb 2023 12:38:05 +0100 Subject: [PATCH] http, sys: mv read_file to fs_fread and fix win32 (wrong size) via mbuf --- include/re_sys.h | 3 +++ src/http/client.c | 68 +++++++++-------------------------------------- src/sys/fs.c | 59 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 55 deletions(-) diff --git a/include/re_sys.h b/include/re_sys.h index 9164c2c90..ab09d84a6 100644 --- a/include/re_sys.h +++ b/include/re_sys.h @@ -32,6 +32,8 @@ #endif struct re_printf; +struct mbuf; + int sys_kernel_get(struct re_printf *pf, void *unused); int sys_build_get(struct re_printf *pf, void *unused); const char *sys_arch_get(void); @@ -71,6 +73,7 @@ int fs_gethome(char *path, size_t sz); bool fs_isdir(const char *path); bool fs_isfile(const char *file); int fs_fopen(FILE **fp, const char *file, const char *mode); +int fs_fread(struct mbuf **mbp, const char *path); void fs_stdio_hide(void); void fs_stdio_restore(void); diff --git a/src/http/client.c b/src/http/client.c index 856158e64..e423c920a 100644 --- a/src/http/client.c +++ b/src/http/client.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "http.h" @@ -27,6 +28,7 @@ #define DEBUG_LEVEL 5 #include +#define PEMBUF_SIZE 512 enum { CONN_TIMEOUT = 30000, @@ -45,8 +47,8 @@ struct http_cli { struct dnsc *dnsc; struct tls *tls; char *tlshn; - char *cert; - char *key; + struct mbuf *cert; + struct mbuf *key; struct sa laddr; #ifdef HAVE_INET6 struct sa laddr6; @@ -724,51 +726,6 @@ static void query_handler(int err, const struct dnshdr *hdr, struct list *ansl, } -#ifdef USE_TLS -static int read_file(char **pbuf, const char *path) -{ - FILE *f = NULL; - long s = 0; - size_t n = 0; - char *buf; - - if (!pbuf || !path) - return EINVAL; - - f = fopen(path, "r"); - if (!f) { - DEBUG_WARNING("Could not open cert file '%s'\n", path); - return EIO; - } - - fseek(f, 0L, SEEK_END); - s = ftell(f); - if (s < 0) { - fclose(f); - return errno; - } - fseek(f, 0L, SEEK_SET); - - buf = mem_zalloc(s + 1, NULL); - if (!buf) { - DEBUG_WARNING("Could not allocate cert file buffer\n"); - fclose(f); - return ENOMEM; - } - - n = fread(buf, 1, s, f); - fclose(f); - if (n < (size_t)s) { - mem_deref(buf); - return EIO; - } - - *pbuf = buf; - return 0; -} -#endif - - int http_uri_decode(struct http_uri *hu, const struct pl *uri) { int err = 0; @@ -901,12 +858,12 @@ int http_request(struct http_req **reqp, struct http_cli *cli, const char *met, #ifdef USE_TLS if (cli->cert && cli->key) { err = tls_set_certificate_pem(cli->tls, - cli->cert, strlen(cli->cert), - cli->key, strlen(cli->key)); + (char *)cli->cert->buf, cli->cert->end, + (char *)cli->key->buf, cli->key->end); } else if (cli->cert) { err = tls_set_certificate(cli->tls, - cli->cert, strlen(cli->cert)); + (char *)cli->cert->buf, cli->cert->end); } if (err) goto out; @@ -1137,7 +1094,7 @@ int http_client_set_cert(struct http_cli *cli, const char *path) return EINVAL; cli->cert = mem_deref(cli->cert); - err = read_file(&cli->cert, path); + err = fs_fread(&cli->cert, path); if (err) { cli->cert = mem_deref(cli->cert); return err; @@ -1161,8 +1118,8 @@ int http_client_set_certpem(struct http_cli *cli, const char *pem) return EINVAL; cli->cert = mem_deref(cli->cert); - - return str_dup(&cli->cert, pem); + cli->cert = mbuf_alloc(PEMBUF_SIZE); + return mbuf_write_str(cli->cert, pem); } @@ -1174,7 +1131,7 @@ int http_client_set_key(struct http_cli *cli, const char *path) return EINVAL; cli->key = mem_deref(cli->key); - err = read_file(&cli->key, path); + err = fs_fread(&cli->key, path); if (err) { cli->key = mem_deref(cli->key); return err; @@ -1191,7 +1148,8 @@ int http_client_set_keypem(struct http_cli *cli, const char *pem) cli->key = mem_deref(cli->key); - return str_dup(&cli->key, pem); + cli->key = mbuf_alloc(PEMBUF_SIZE); + return mbuf_write_str(cli->key, pem); } diff --git a/src/sys/fs.c b/src/sys/fs.c index cee305232..d66981c6d 100644 --- a/src/sys/fs.c +++ b/src/sys/fs.c @@ -26,6 +26,13 @@ #include #include #include +#include +#include + + +#define DEBUG_MODULE "fs" +#define DEBUG_LEVEL 5 +#include #ifdef WIN32 @@ -37,6 +44,8 @@ #define fileno _fileno #endif +#define MINBUF_SIZE 1024 + static int dup_stdout = -1; static int dup_stderr = -1; @@ -242,3 +251,53 @@ void fs_stdio_restore(void) (void)dup2(dup_stdout, fileno(stdout)); (void)dup2(dup_stderr, fileno(stderr)); } + + +int fs_fread(struct mbuf **mbp, const char *path) +{ + FILE *f = NULL; + size_t n = 0; + void *buf = NULL; + struct mbuf *mb = NULL; + int err; + + if (!mbp || !path) + return EINVAL; + + err = fs_fopen(&f, path, "r"); + if (err) { + DEBUG_WARNING("Could not open file '%s'\n", path); + return err; + } + + mb = mbuf_alloc(MINBUF_SIZE); + buf = mem_zalloc(MINBUF_SIZE, NULL); + if (!mb || !buf) { + err = ENOMEM; + goto out; + } + + while (1) { + n = fread(buf, 1, MINBUF_SIZE, f); + if (!n) + goto out; + + err = mbuf_write_mem(mb, buf, n); + if (err) { + DEBUG_WARNING("Error reading file '%s' (%m)\n", + path, err); + goto out; + } + } + +out: + fclose(f); + + mem_deref(buf); + if (err) + mem_deref(mb); + else + *mbp = mb; + + return 0; +}