bunkum

an old and silly c99 web server with some fun features
Log | Files | Refs | README

commit d237bbe52df14b068b0948ff7d099ac47b4e0c63
parent c980c94a0e840e1ea2c1e9a596039be08e8e647c
Author: quantumish <freifeld.david@gmail.com>
Date:   Sat,  8 Apr 2023 22:45:09 -0700

Add more headers

Diffstat:
Mhttp.c | 13+++++++++++--
Mhttp/response.c | 8++++++++
Autils/time.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Autils/time.h | 10++++++++++
4 files changed, 90 insertions(+), 2 deletions(-)

diff --git a/http.c b/http.c @@ -16,6 +16,7 @@ #include <zlib.h> +#include "utils/time.h" #include "utils/shitvec.h" #include "http/response.h" @@ -85,7 +86,15 @@ void* handle_conn(void* ctxt) { resp_add_hdr(&r, "Content-Type", "image/svg+xml"); } else if (strcmp(ext, "jpeg") == 0) { resp_add_hdr(&r, "Content-Type", "image/jpeg"); - } else escape = true; + } else { + resp_add_hdr(&r, "Content-Type", "text/html"); + escape = true; + } + + char datebuf[32]; + time_to_str(st.st_mtim.tv_sec, datebuf); + resp_add_hdr(&r, "Last-Modified", datebuf); + if (escape) { char* efbuf = malloc((size_t)(st.st_size * 2)); // FIXME dubious assumption scuffed_htmlescape(efbuf, fbuf, st.st_size); @@ -112,7 +121,7 @@ void* handle_conn(void* ctxt) { resp_add_content(&r, "Fuck off.", 9); send(ns, r.content, r.sz, 0); free(r.content); - } + } printf("Done.\n"); } } diff --git a/http/response.c b/http/response.c @@ -4,12 +4,20 @@ #include <stdio.h> #include "../utils/hashmap.h" +#include "../utils/time.h" #include "response.h" response_t __resp_new(enum StatusCode code, char* code_name) { response_t resp; memset(resp.header, 0, 512); sprintf(resp.header, "HTTP/1.1 %d %s\r\n", code, code_name); + + resp_add_hdr(&resp, "Server", "Bunkum/0.0.1"); + + char datebuf[32]; + time_to_str(time(0), datebuf); + resp_add_hdr(&resp, "Date", datebuf); + return resp; } diff --git a/utils/time.c b/utils/time.c @@ -0,0 +1,61 @@ +#include <stdio.h> + +#include "time.h" + +char* day_to_str(int day) { + switch (day) { + case 0: + return "Sun"; + case 1: + return "Mon"; + case 2: + return "Tue"; + case 3: + return "Wed"; + case 4: + return "Thu"; + case 5: + return "Fri"; + case 6: + return "Sat"; + } + return "Wat"; +} + +char* mon_to_str(int mon) { + switch (mon) { + case 0: + return "Jan"; + case 1: + return "Feb"; + case 2: + return "Mar"; + case 3: + return "Apr"; + case 4: + return "May"; + case 5: + return "Jun"; + case 6: + return "Jul"; + case 7: + return "Aug"; + case 8: + return "Sep"; + case 9: + return "Oct"; + case 10: + return "Nov"; + case 11: + return "Dec"; + } + return "Wat"; +} + + +void time_to_str(time_t t, char* buf) { + struct tm* gmt = gmtime(&t); + sprintf(buf, "%s, %02d %s %d %02d:%02d:%02d GMT", + day_to_str(gmt->tm_wday), gmt->tm_mday, mon_to_str(gmt->tm_mon), + gmt->tm_year + 1900, gmt->tm_hour, gmt->tm_min, gmt->tm_sec); +} diff --git a/utils/time.h b/utils/time.h @@ -0,0 +1,10 @@ +#ifndef UTIL_TIME_H +#define UTIL_TIME_H + +#include <time.h> + +char* day_to_str(int day); +char* mon_to_str(int mon); +void time_to_str(time_t t, char* buf); + +#endif