summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2019-11-23 23:39:04 +0100
committertomsmeding <tom.smeding@gmail.com>2019-11-23 23:39:04 +0100
commit0d2c5f9602db67810c1c9551ca59ac840339b56b (patch)
tree2f5caa1dae7cb029c25eb031da75ca005caa1ee3
parent4381cca95136b0a8222378a41ac0ebb1bc62ea80 (diff)
static: Move struct buffer to a separate file
-rw-r--r--plugins/static/buffer.c47
-rw-r--r--plugins/static/buffer.h21
-rw-r--r--plugins/static/static.c58
3 files changed, 74 insertions, 52 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#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 <stdbool.h>
+#include <stddef.h>
+
+
+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 <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);