kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit d30921103bb172252e40cfefd9bdbb2a1852e713
parent a8917e355a3a953cbbefb824a4625adfa7732c5c
Author: Joris Vink <joris@coders.se>
Date:   Fri,  8 Jul 2016 10:02:18 +0200

Code cleanup, several API breaking changes in here

Diffstat:
examples/generic/src/example.c | 8++++----
examples/integers/src/check_integers.c | 3++-
examples/ktunnel/src/ktunnel.c | 2+-
includes/http.h | 4++--
includes/kore.h | 30+++++++++++++++---------------
src/buf.c | 11+++++------
src/http.c | 14+++++++-------
src/net.c | 20++++++++++----------
src/utils.c | 14++++++++++----
9 files changed, 56 insertions(+), 50 deletions(-)

diff --git a/examples/generic/src/example.c b/examples/generic/src/example.c @@ -118,7 +118,7 @@ int serve_b64test(struct http_request *req) { int i; - u_int32_t len; + size_t len; struct kore_buf *res; u_int8_t *data; @@ -141,7 +141,7 @@ serve_file_upload(struct http_request *req) u_int8_t *d; struct kore_buf *b; struct http_file *f; - u_int32_t len; + size_t len; char *name, buf[BUFSIZ]; b = kore_buf_create(asset_len_upload_html); @@ -185,7 +185,7 @@ void test_base64(u_int8_t *src, u_int32_t slen, struct kore_buf *res) { char *in; - u_int32_t len; + size_t len; u_int8_t *out; kore_buf_appendf(res, "test '%s'\n", src); @@ -238,7 +238,7 @@ serve_params_test(struct http_request *req) { struct kore_buf *b; u_int8_t *d; - u_int32_t len; + size_t len; int r, i; char *test, name[10]; diff --git a/examples/integers/src/check_integers.c b/examples/integers/src/check_integers.c @@ -11,8 +11,9 @@ page(struct http_request *req) int32_t s32; int64_t s64; u_int64_t u64; + u_int32_t u32; + size_t len; struct kore_buf *buf; - u_int32_t u32, len; u_int8_t c, *data; http_populate_get(req); diff --git a/examples/ktunnel/src/ktunnel.c b/examples/ktunnel/src/ktunnel.c @@ -161,7 +161,7 @@ ktunnel_pipe_data(struct netbuf *nb) struct connection *src = nb->owner; struct connection *dst = src->hdlr_extra; - printf("received %d bytes on pipe %p (-> %p)\n", nb->s_off, src, dst); + printf("received %zu bytes on pipe %p (-> %p)\n", nb->s_off, src, dst); net_send_queue(dst, nb->buf, nb->s_off); net_send_flush(dst); diff --git a/includes/http.h b/includes/http.h @@ -221,9 +221,9 @@ void http_request_sleep(struct http_request *); void http_request_wakeup(struct http_request *); void http_process_request(struct http_request *); ssize_t http_body_read(struct http_request *, void *, size_t); -void http_response(struct http_request *, int, void *, u_int32_t); +void http_response(struct http_request *, int, const void *, size_t); void http_response_stream(struct http_request *, int, void *, - u_int64_t, int (*cb)(struct netbuf *), void *); + size_t, int (*cb)(struct netbuf *), void *); int http_request_header(struct http_request *, const char *, char **); void http_response_header(struct http_request *, diff --git a/includes/kore.h b/includes/kore.h @@ -106,9 +106,9 @@ struct http_request; struct netbuf { u_int8_t *buf; - u_int32_t s_off; - u_int32_t b_len; - u_int32_t m_len; + size_t s_off; + size_t b_len; + size_t m_len; u_int8_t type; u_int8_t flags; @@ -327,8 +327,8 @@ struct kore_validator { struct kore_buf { u_int8_t *data; - u_int64_t length; - u_int64_t offset; + size_t length; + size_t offset; }; struct kore_pool_region { @@ -531,8 +531,8 @@ int kore_split_string(char *, char *, char **, size_t); void kore_strip_chars(char *, const char, char **); int kore_snprintf(char *, size_t, int *, const char *, ...); long long kore_strtonum(const char *, int, long long, long long, int *); -int kore_base64_encode(u_int8_t *, u_int32_t, char **); -int kore_base64_decode(char *, u_int8_t **, u_int32_t *); +int kore_base64_encode(u_int8_t *, size_t, char **); +int kore_base64_decode(char *, u_int8_t **, size_t *); void *kore_mem_find(void *, size_t, void *, size_t); char *kore_text_trim(char *, size_t); char *kore_read_line(FILE *, char *, size_t); @@ -608,21 +608,21 @@ int net_read(struct connection *, int *); int net_read_ssl(struct connection *, int *); int net_write(struct connection *, int, int *); int net_write_ssl(struct connection *, int, int *); -void net_recv_reset(struct connection *, u_int32_t, +void net_recv_reset(struct connection *, size_t, int (*cb)(struct netbuf *)); void net_remove_netbuf(struct netbuf_head *, struct netbuf *); -void net_recv_queue(struct connection *, u_int32_t, int, +void net_recv_queue(struct connection *, size_t, int, int (*cb)(struct netbuf *)); -void net_recv_expand(struct connection *c, u_int32_t, +void net_recv_expand(struct connection *c, size_t, int (*cb)(struct netbuf *)); -void net_send_queue(struct connection *, const void *, u_int32_t); +void net_send_queue(struct connection *, const void *, size_t); void net_send_stream(struct connection *, void *, - u_int32_t, int (*cb)(struct netbuf *), struct netbuf **); + size_t, int (*cb)(struct netbuf *), struct netbuf **); void kore_buf_free(struct kore_buf *); -struct kore_buf *kore_buf_create(u_int32_t); -void kore_buf_append(struct kore_buf *, const void *, u_int32_t); -u_int8_t *kore_buf_release(struct kore_buf *, u_int32_t *); +struct kore_buf *kore_buf_create(size_t); +void kore_buf_append(struct kore_buf *, const void *, size_t); +u_int8_t *kore_buf_release(struct kore_buf *, size_t *); void kore_buf_reset(struct kore_buf *); char *kore_buf_stringify(struct kore_buf *, size_t *); diff --git a/src/buf.c b/src/buf.c @@ -22,7 +22,7 @@ #include "kore.h" struct kore_buf * -kore_buf_create(u_int32_t initial) +kore_buf_create(size_t initial) { struct kore_buf *buf; @@ -35,7 +35,7 @@ kore_buf_create(u_int32_t initial) } void -kore_buf_append(struct kore_buf *buf, const void *d, u_int32_t len) +kore_buf_append(struct kore_buf *buf, const void *d, size_t len) { if ((buf->offset + len) < len) fatal("overflow in kore_buf_append"); @@ -53,7 +53,7 @@ void kore_buf_appendb(struct kore_buf *buf, struct kore_buf *src) { u_int8_t *d; - u_int32_t len; + size_t len; d = kore_buf_release(src, &len); kore_buf_append(buf, d, len); @@ -108,7 +108,7 @@ kore_buf_stringify(struct kore_buf *buf, size_t *len) } u_int8_t * -kore_buf_release(struct kore_buf *buf, u_int32_t *len) +kore_buf_release(struct kore_buf *buf, size_t *len) { u_int8_t *p; @@ -129,9 +129,8 @@ kore_buf_free(struct kore_buf *buf) void kore_buf_replace_string(struct kore_buf *b, char *src, void *dst, size_t len) { - u_int32_t blen, off, off2; - size_t nlen, klen; char *key, *end, *tmp, *p; + size_t blen, off, off2, nlen, klen; off = 0; klen = strlen(src); diff --git a/src/http.c b/src/http.c @@ -40,7 +40,7 @@ static int http_body_rewind(struct http_request *); static void http_error_response(struct connection *, int); static void http_argument_add(struct http_request *, const char *, char *); static void http_response_normal(struct http_request *, - struct connection *, int, void *, u_int32_t); + struct connection *, int, const void *, size_t); static void multipart_add_field(struct http_request *, struct kore_buf *, const char *, const char *, const int); static void multipart_file_add(struct http_request *, struct kore_buf *, @@ -481,9 +481,9 @@ http_request_free(struct http_request *req) } void -http_response(struct http_request *req, int status, void *d, u_int32_t l) +http_response(struct http_request *req, int status, const void *d, size_t l) { - kore_debug("http_response(%p, %d, %p, %d)", req, status, d, l); + kore_debug("http_response(%p, %d, %p, %zu)", req, status, d, l); req->status = status; @@ -500,7 +500,7 @@ http_response(struct http_request *req, int status, void *d, u_int32_t l) void http_response_stream(struct http_request *req, int status, void *base, - u_int64_t len, int (*cb)(struct netbuf *), void *arg) + size_t len, int (*cb)(struct netbuf *), void *arg) { struct netbuf *nb; @@ -1385,7 +1385,7 @@ http_error_response(struct connection *c, int status) static void http_response_normal(struct http_request *req, struct connection *c, - int status, void *d, u_int32_t len) + int status, const void *d, size_t len) { struct http_header *hdr; char *conn; @@ -1439,12 +1439,12 @@ http_response_normal(struct http_request *req, struct connection *c, if (status != 204 && status >= 200 && !(req->flags & HTTP_REQUEST_NO_CONTENT_LENGTH)) { kore_buf_appendf(header_buf, - "content-length: %d\r\n", len); + "content-length: %zu\r\n", len); } } else { if (status != 204 && status >= 200) { kore_buf_appendf(header_buf, - "content-length: %d\r\n", len); + "content-length: %zu\r\n", len); } } diff --git a/src/net.c b/src/net.c @@ -44,13 +44,13 @@ net_cleanup(void) } void -net_send_queue(struct connection *c, const void *data, u_int32_t len) +net_send_queue(struct connection *c, const void *data, size_t len) { const u_int8_t *d; struct netbuf *nb; - u_int32_t avail; + size_t avail; - kore_debug("net_send_queue(%p, %p, %d)", c, data, len); + kore_debug("net_send_queue(%p, %p, %zu)", c, data, len); d = data; nb = TAILQ_LAST(&(c->send_queue), netbuf_head); @@ -93,12 +93,12 @@ net_send_queue(struct connection *c, const void *data, u_int32_t len) } void -net_send_stream(struct connection *c, void *data, u_int32_t len, +net_send_stream(struct connection *c, void *data, size_t len, int (*cb)(struct netbuf *), struct netbuf **out) { struct netbuf *nb; - kore_debug("net_send_stream(%p, %p, %d)", c, data, len); + kore_debug("net_send_stream(%p, %p, %zu)", c, data, len); nb = kore_pool_get(&nb_pool); nb->cb = cb; @@ -116,9 +116,9 @@ net_send_stream(struct connection *c, void *data, u_int32_t len, } void -net_recv_reset(struct connection *c, u_int32_t len, int (*cb)(struct netbuf *)) +net_recv_reset(struct connection *c, size_t len, int (*cb)(struct netbuf *)) { - kore_debug("net_recv_reset(): %p %d", c, len); + kore_debug("net_recv_reset(): %p %zu", c, len); if (c->rnb->type != NETBUF_RECV) fatal("net_recv_reset(): wrong netbuf type"); @@ -137,10 +137,10 @@ net_recv_reset(struct connection *c, u_int32_t len, int (*cb)(struct netbuf *)) } void -net_recv_queue(struct connection *c, u_int32_t len, int flags, +net_recv_queue(struct connection *c, size_t len, int flags, int (*cb)(struct netbuf *)) { - kore_debug("net_recv_queue(): %p %d %d", c, len, flags); + kore_debug("net_recv_queue(): %p %zu %d", c, len, flags); if (c->rnb != NULL) fatal("net_recv_queue(): called incorrectly for %p", c); @@ -158,7 +158,7 @@ net_recv_queue(struct connection *c, u_int32_t len, int flags, } void -net_recv_expand(struct connection *c, u_int32_t len, int (*cb)(struct netbuf *)) +net_recv_expand(struct connection *c, size_t len, int (*cb)(struct netbuf *)) { kore_debug("net_recv_expand(): %p %d", c, len); diff --git a/src/utils.c b/src/utils.c @@ -377,18 +377,21 @@ kore_time_ms(void) } int -kore_base64_encode(u_int8_t *data, u_int32_t len, char **out) +kore_base64_encode(u_int8_t *data, size_t len, char **out) { + u_int32_t b; struct kore_buf *res; + size_t plen, idx; u_int8_t n, *pdata; int i, padding; - u_int32_t idx, b, plen; if ((len % 3) != 0) { padding = 3 - (len % 3); plen = len + padding; - pdata = kore_malloc(plen); + if (plen < len) + fatal("plen wrapped"); + pdata = kore_malloc(plen); memcpy(pdata, data, len); memset(pdata + len, 0, padding); } else { @@ -430,6 +433,9 @@ kore_base64_encode(u_int8_t *data, u_int32_t len, char **out) kore_mem_free(pdata); pdata = kore_buf_release(res, &plen); + if ((plen + 1) < plen) + fatal("plen wrapped"); + *out = kore_malloc(plen + 1); (void)kore_strlcpy(*out, (char *)pdata, plen + 1); kore_mem_free(pdata); @@ -438,7 +444,7 @@ kore_base64_encode(u_int8_t *data, u_int32_t len, char **out) } int -kore_base64_decode(char *in, u_int8_t **out, u_int32_t *olen) +kore_base64_decode(char *in, u_int8_t **out, size_t *olen) { int i, c; struct kore_buf *res;