8000 http: fix read_file on win32 (wrong filesize) and use mbuf by fAuernigg · Pull Request #711 · baresip/re · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

http: fix read_file on win32 (wrong filesize) and use mbuf #711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/re_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
68 changes: 13 additions & 55 deletions src/http/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#include <re_dns.h>
#include <re_msg.h>
#include <re_http.h>
#include <re_sys.h>
#include "http.h"


#define DEBUG_MODULE "http_client"
#define DEBUG_LEVEL 5
#include <re_dbg.h>

#define PEMBUF_SIZE 512

enum {
CONN_TIMEOUT = 30000,
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}


Expand All @@ -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;
Expand All @@ -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);
}


Expand Down
59 changes: 59 additions & 0 deletions src/sys/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
#include <re_types.h>
#include <re_fmt.h>
#include <re_sys.h>
#include <re_mem.h>
#include <re_mbuf.h>


#define DEBUG_MODULE "fs"
#define DEBUG_LEVEL 5
#include <re_dbg.h>


#ifdef WIN32
Expand All @@ -37,6 +44,8 @@
#define fileno _fileno
#endif

#define MINBUF_SIZE 1024


static int dup_stdout = -1;
static int dup_stderr = -1;
Expand Down Expand Up @@ -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;
}
0