From 0d2c5f9602db67810c1c9551ca59ac840339b56b Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 23 Nov 2019 23:39:04 +0100 Subject: static: Move struct buffer to a separate file --- plugins/static/buffer.c | 47 +++++++++++++++++++++++++++++++++++++++ plugins/static/buffer.h | 21 ++++++++++++++++++ plugins/static/static.c | 58 +++++-------------------------------------------- 3 files changed, 74 insertions(+), 52 deletions(-) create mode 100644 plugins/static/buffer.c create mode 100644 plugins/static/buffer.h diff --git a/plugins/static/buffer.c b/plugins/static/buffer.c new file mode 100644 index 0000000..04db027 --- /dev/null +++ b/plugins/static/buffer.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include "memory.h" +#include "buffer.h" + + +struct buffer buffer_make(size_t capacity) { + struct buffer buffer = (struct buffer){malloc(capacity, char), capacity + 1, 0}; + return buffer; +} + +void buffer_free(struct buffer buffer) { + free(buffer.buf); +} + +void buffer_append_mem(struct buffer *buffer, const char *str, size_t len) { + if (buffer->len + len >= buffer->cap) { + do buffer->cap *= 2; while (buffer->len + len >= buffer->cap); + buffer->buf = realloc(buffer->buf, buffer->cap, char); + } + memcpy(buffer->buf + buffer->len, str, len); + buffer->len += len; + buffer->buf[buffer->len] = 0; +} + +void buffer_append_str(struct buffer *buffer, const char *str) { + buffer_append_mem(buffer, str, strlen(str)); +} + +// returns whether successful +bool buffer_append_file(struct buffer *buffer, const char *path) { + FILE *f = fopen(path, "rb"); + if (!f) return false; + + char block[4096]; + while (true) { + size_t nr = fread(block, 1, sizeof block, f); + if (nr > 0) buffer_append_mem(buffer, block, nr); + if (nr < sizeof block) break; + } + + bool has_error = ferror(f); + fclose(f); + return !has_error; +} diff --git a/plugins/static/buffer.h b/plugins/static/buffer.h new file mode 100644 index 0000000..0237f7c --- /dev/null +++ b/plugins/static/buffer.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + + +struct buffer { + char *buf; + size_t cap, len; +}; + +struct buffer buffer_make(size_t capacity); + +void buffer_free(struct buffer buffer); + +void buffer_append_mem(struct buffer *buffer, const char *str, size_t len); + +void buffer_append_str(struct buffer *buffer, const char *str); + +// returns whether successful +bool buffer_append_file(struct buffer *buffer, const char *path); diff --git a/plugins/static/static.c b/plugins/static/static.c index 011a92f..2fb6e0f 100644 --- a/plugins/static/static.c +++ b/plugins/static/static.c @@ -8,6 +8,7 @@ #include #include "plugin.h" #include "util.h" +#include "buffer.h" static char *static_file_dir = NULL; @@ -60,55 +61,8 @@ static ssize_t query_file_size(const char *path) { } } -struct Buffer { - char *buf; - size_t cap, len; -}; - -static struct Buffer buffer_make(size_t capacity) { - struct Buffer buffer = (struct Buffer){malloc(capacity), capacity + 1, 0}; - assert(buffer.buf); - return buffer; -} - -static void buffer_free(struct Buffer buffer) { - free(buffer.buf); -} - -static void buffer_append_mem(struct Buffer *buffer, const char *str, size_t len) { - if (buffer->len + len >= buffer->cap) { - do buffer->cap *= 2; while (buffer->len + len >= buffer->cap); - buffer->buf = realloc(buffer->buf, buffer->cap); - assert(buffer->buf); - } - memcpy(buffer->buf + buffer->len, str, len); - buffer->len += len; - buffer->buf[buffer->len] = 0; -} - -static void buffer_append_str(struct Buffer *buffer, const char *str) { - buffer_append_mem(buffer, str, strlen(str)); -} - -// returns whether successful -static bool buffer_append_file(struct Buffer *buffer, const char *path) { - FILE *f = fopen(path, "rb"); - if (!f) return false; - - char block[4096]; - while (true) { - size_t nr = fread(block, 1, sizeof block, f); - if (nr > 0) buffer_append_mem(buffer, block, nr); - if (nr < sizeof block) break; - } - - bool has_error = ferror(f); - fclose(f); - return !has_error; -} - -static struct Buffer build_response_headers(const char *status, const char *content_type, size_t body_len) { - struct Buffer buffer = buffer_make(200 + body_len); +static struct buffer build_response_headers(const char *status, const char *content_type, size_t body_len) { + struct buffer buffer = buffer_make(200 + body_len); buffer_append_str(&buffer, "HTTP/1.1 "); buffer_append_str(&buffer, status); @@ -131,14 +85,14 @@ static struct Buffer build_response_headers(const char *status, const char *cont static void send_404(int sock) { const char *body = "404 Not found"; - struct Buffer buffer = build_response_headers("404 Not found", NULL, strlen(body)); + struct buffer buffer = build_response_headers("404 Not found", NULL, strlen(body)); buffer_append_str(&buffer, body); sendall(sock, buffer.buf, buffer.len); } static void send_500(int sock) { const char *body = "500 Internal server error"; - struct Buffer buffer = build_response_headers("500 Internal server error", NULL, strlen(body)); + struct buffer buffer = build_response_headers("500 Internal server error", NULL, strlen(body)); buffer_append_str(&buffer, body); sendall(sock, buffer.buf, buffer.len); } @@ -162,7 +116,7 @@ static Handler_ret_t connection_handler(int sock, Headers *headers) { } } - struct Buffer buffer = build_response_headers("200 OK", "text/plain; charset=UTF-8", size); + struct buffer buffer = build_response_headers("200 OK", "text/plain; charset=UTF-8", size); if (!buffer_append_file(&buffer, path)) { buffer_free(buffer); send_500(sock); -- cgit v1.2.3