commit cc9debcf1492a32d26676902610025bee53d6e40
parent 1a3017b4c2f07c7546e12661920ae8e86d320f51
Author: quantumish <freifeld.david@gmail.com>
Date: Fri, 14 Apr 2023 14:28:25 -0700
Add dynamic port selection, more MIME type mappings
Diffstat:
2 files changed, 14 insertions(+), 7 deletions(-)
diff --git a/http.c b/http.c
@@ -81,7 +81,7 @@ response_t serve_file(request_t req) {
if (ok && (hdr = hashmap_get(&req.headers, "Accept-Encoding"))) {
ok = false;
- shitvec_t mtypes = hdr_parse_accept(hdr); // abuse of this func
+ shitvec_t mtypes = hdr_parse_accept(hdr); // abuse of this func
for (int j = 0; j < mtypes.vec_sz; j++) {
struct req_mimetype* a_mtype = shitvec_get(&mtypes, j);
if (strcmp(a_mtype->item, "gzip") == 0) {
@@ -199,7 +199,7 @@ int main() {
name.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (struct sockaddr*)&name, sizeof(name)) < 0) {
- name.sin_port = htons(8081);
+ name.sin_port = htons(0);
if (bind(s, (struct sockaddr*)&name, sizeof(name)) < 0) {
die("bind");
}
@@ -211,9 +211,10 @@ int main() {
hashmap_set(&path_redirs, "/", "/index.html");
list_files_sv(&paths, "public");
- // TODO handle special non-file paths
-
- log_debug("Open on port 8082.");
+
+ int nlen;
+ getsockname(s, (struct sockaddr*)&name, (socklen_t*)&nlen);
+ log_debug("Open on port %d.", htons(name.sin_port));
int namelen;
char pathbuf[MAX_PATH_LEN];
@@ -234,4 +235,7 @@ int main() {
// - TODO HTML parsing maybe but that makes me want to cry
// - TODO nice ways of dynamically *generating* html
// - TODO dynamically profile requests
+
+
+
// - TODO some kinda config file parsing
diff --git a/http/response.c b/http/response.c
@@ -42,9 +42,12 @@ void resp_add_content(response_t* r, char* content, size_t content_len) {
memcpy(r->content+header_len, content, content_len);
}
-const char* exts[] = {"html", "css", "js", "png", "gif", "jpeg", "svg", "ttf", "woff", "woff2", NULL}; // TODO sentinel sketchy
+const char* exts[] = {"html", "css", "js", "png", "gif", "jpeg", "svg", "ttf", "woff", "woff2",
+ "pdf", "csv", "gz", "tar", "zip", "json", NULL}; // TODO sentinel sketchy
const char* mtypes[] = {"text/html", "text/css", "text/javascript", "image/png",
- "image/gif", "image/jpeg", "image/svg+xml", "font/ttf", "font/woff", "font/woff2"};
+ "image/gif", "image/jpeg", "image/svg+xml", "font/ttf", "font/woff", "font/woff2",
+ "application/pdf", "text/csv", "application/gzip", "application/x-tar", "application/zip",
+ "application/json"};
const char* ext_to_mtype(char* ext) {
if (ext == NULL) return"text/plain";