summaryrefslogtreecommitdiff
path: root/plugins/static/static.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/static/static.c')
-rw-r--r--plugins/static/static.c58
1 files changed, 6 insertions, 52 deletions
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 <sys/stat.h>
#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);