commit - b8a11905ab2402519db36742b5eb16646ad0347b
commit + dca29a891c8adedd8ab5f84d8df83a07a46573db
blob - dd4aed5ca5e4940451abf38fb462bc8f67886df8
blob + 7cedaf5a03b59e15e93d21af7ec4eee35dfa6c23
--- web/bufio.c
+++ web/bufio.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <tls.h>
#include <unistd.h>
#include "bufio.h"
void
buf_drain(struct buffer *buf, size_t l)
{
- buf->cur = 0;
-
if (l >= buf->len) {
buf->len = 0;
return;
int
bufio_reset(struct bufio *bio)
{
- if (bio->ctx)
- tls_close(bio->ctx);
if (bio->fd != -1)
close(bio->fd);
bio->fd = fd;
}
-int
-bufio_starttls(struct bufio *bio, const char *host, int insecure)
-{
- struct tls_config *conf;
-
- if ((conf = tls_config_new()) == NULL)
- return (-1);
-
- if (insecure) {
- tls_config_insecure_noverifycert(conf);
- tls_config_insecure_noverifyname(conf);
- tls_config_insecure_noverifytime(conf);
- }
-
- if ((bio->ctx = tls_client()) == NULL) {
- tls_config_free(conf);
- return (-1);
- }
-
- if (tls_configure(bio->ctx, conf) == -1) {
- tls_config_free(conf);
- return (-1);
- }
-
- tls_config_free(conf);
-
- if (tls_connect_socket(bio->ctx, bio->fd, host) == -1)
- return (-1);
-
- return (0);
-}
-
short
bufio_pollev(struct bufio *bio)
{
short ev;
- if (bio->pflags)
- return (bio->pflags);
-
ev = POLLIN;
if (bio->wbuf.len != 0)
ev |= POLLOUT;
assert(rbuf->cap >= rbuf->len);
if (rbuf->cap - rbuf->len < BIO_CHUNK) {
if (buf_grow(rbuf) == -1)
- return (-1);
- }
-
- if (bio->ctx) {
- r = tls_read(bio->ctx, rbuf->buf + rbuf->len,
- rbuf->cap - rbuf->cap);
- switch (r) {
- case TLS_WANT_POLLIN:
- case TLS_WANT_POLLOUT:
- bio->pflags = POLLIN | POLLOUT;
- errno = EAGAIN;
- return (-1);
- case -1:
return (-1);
- default:
- bio->pflags = 0;
- rbuf->len += r;
- return (r);
- }
}
r = read(bio->fd, rbuf->buf + rbuf->len, rbuf->cap - rbuf->len);
struct buffer *wbuf = &bio->wbuf;
ssize_t w;
- if (bio->ctx) {
- switch (w = tls_write(bio->ctx, wbuf->buf, wbuf->len)) {
- case TLS_WANT_POLLIN:
- case TLS_WANT_POLLOUT:
- bio->pflags = POLLIN | POLLOUT;
- errno = EAGAIN;
- return (-1);
- case -1:
- return (-1);
- default:
- bio->pflags = 0;
- buf_drain(wbuf, w);
- return (w);
- }
- }
-
w = write(bio->fd, wbuf->buf, wbuf->len);
if (w == -1)
return (-1);
free(str);
return (r);
}
-
-void
-bufio_rewind_cursor(struct bufio *bio)
-{
- bio->rbuf.cur = 0;
-}
-
-int
-bufio_get_cb(void *d)
-{
- struct bufio *bio = d;
- struct buffer *rbuf = &bio->rbuf;
-
- if (rbuf->cur >= rbuf->len)
- return (EOF);
- return (rbuf->buf[rbuf->cur++]);
-}
-
-int
-bufio_peek_cb(void *d)
-{
- struct bufio *bio = d;
- struct buffer *rbuf = &bio->rbuf;
-
- if (rbuf->cur >= rbuf->len)
- return (EOF);
- return (rbuf->buf[rbuf->cur]);
-}
blob - 372ba5655fc53797a09fcac4603f8391fce721a2
blob + 7499431eb14d026039097dadf7ae55ba7a76e48e
--- web/bufio.h
+++ web/bufio.h
uint8_t *buf;
size_t len;
size_t cap;
- size_t cur;
};
struct bufio {
int fd;
- struct tls *ctx;
- int pflags; /* poll flags */
struct buffer wbuf;
struct buffer rbuf;
};
int bufio_reset(struct bufio *);
int bufio_reset(struct bufio *);
void bufio_set_fd(struct bufio *, int);
-int bufio_starttls(struct bufio *, const char *, int);
short bufio_pollev(struct bufio *);
ssize_t bufio_read(struct bufio *);
ssize_t bufio_write(struct bufio *);
int bufio_compose_str(struct bufio *, const char *);
int bufio_compose_fmt(struct bufio *, const char *, ...)
__attribute__((__format__ (printf, 2, 3)));
-void bufio_rewind_cursor(struct bufio *);
-
-/* callbacks for pdjson */
-int bufio_get_cb(void *);
-int bufio_peek_cb(void *);