summaryrefslogtreecommitdiff
path: root/src/libtipidee
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-10-24 12:38:53 +0000
committerLaurent Bercot <ska@appnovation.com>2023-10-24 12:38:53 +0000
commit305385e06f90ca3216e1aa4e43b20dd7d53c1222 (patch)
tree899d3460b511567889d5e5d78f77931af572b5fb /src/libtipidee
parent37d2f8cb438f68eaa1da8a56ea9ce5023091f128 (diff)
downloadtipidee-305385e06f90ca3216e1aa4e43b20dd7d53c1222.tar.xz
Make the switch to custom headers
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/libtipidee')
-rw-r--r--src/libtipidee/deps-lib/tipidee4
-rw-r--r--src/libtipidee/tipidee_response_error_nofile.c4
-rw-r--r--src/libtipidee/tipidee_response_file.c4
-rw-r--r--src/libtipidee/tipidee_response_header_builtin.c40
-rw-r--r--src/libtipidee/tipidee_response_header_common_put.c20
-rw-r--r--src/libtipidee/tipidee_response_header_writeall.c20
-rw-r--r--src/libtipidee/tipidee_response_header_writemerge.c54
7 files changed, 80 insertions, 66 deletions
diff --git a/src/libtipidee/deps-lib/tipidee b/src/libtipidee/deps-lib/tipidee
index d406e5c..b20898b 100644
--- a/src/libtipidee/deps-lib/tipidee
+++ b/src/libtipidee/deps-lib/tipidee
@@ -20,12 +20,12 @@ tipidee_log_start.o
tipidee_method.o
tipidee_response_error_nofile.o
tipidee_response_file.o
-tipidee_response_header_builtin.o
-tipidee_response_header_common_put.o
tipidee_response_header_date.o
tipidee_response_header_date_fmt.o
tipidee_response_header_lastmodified.o
tipidee_response_header_preparebuiltin.o
+tipidee_response_header_writeall.o
+tipidee_response_header_writemerge.o
tipidee_response_status.o
tipidee_rql_read.o
tipidee_uri_parse.o
diff --git a/src/libtipidee/tipidee_response_error_nofile.c b/src/libtipidee/tipidee_response_error_nofile.c
index 4f628ac..8a7cd53 100644
--- a/src/libtipidee/tipidee_response_error_nofile.c
+++ b/src/libtipidee/tipidee_response_error_nofile.c
@@ -8,7 +8,7 @@
#include <tipidee/method.h>
#include <tipidee/response.h>
-size_t tipidee_response_error_nofile (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, uint32_t options, tain const *stamp)
+size_t tipidee_response_error_nofile (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options, tain const *stamp)
{
static char const txt1[] = "<html>\n<head><title>" ;
static char const txt2[] = "</title></head>\n<body>\n<h1> " ;
@@ -16,7 +16,7 @@ size_t tipidee_response_error_nofile (buffer *b, tipidee_rql const *rql, unsigne
static char const txt4[] = "\n</p>\n</body>\n</html>\n" ;
char fmt[SIZE_FMT] ;
size_t n = tipidee_response_status(b, rql, status, reason) ;
- n += tipidee_response_header_common_put(b, options, stamp) ;
+ n += tipidee_response_header_writeall(b, rhdr, rhdrn, options, stamp) ;
n += buffer_putsnoflush(b, "Content-Type: text/html; charset=UTF-8\r\n") ;
n += buffer_putsnoflush(b, "Content-Length: ") ;
n += buffer_putnoflush(b, fmt, size_fmt(fmt, text ? sizeof(txt1) + sizeof(txt2) + sizeof(txt3) + sizeof(txt4) - 4 + 2 * strlen(reason) + strlen(text) : 0)) ;
diff --git a/src/libtipidee/tipidee_response_file.c b/src/libtipidee/tipidee_response_file.c
index 0cbe8f4..f2021ee 100644
--- a/src/libtipidee/tipidee_response_file.c
+++ b/src/libtipidee/tipidee_response_file.c
@@ -11,11 +11,11 @@
#include <tipidee/response.h>
#include <tipidee/util.h>
-size_t tipidee_response_file (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, struct stat const *st, char const *ct, uint32_t options, tain const *stamp)
+size_t tipidee_response_file (buffer *b, tipidee_rql const *rql, unsigned int status, char const *reason, struct stat const *st, char const *ct, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options, tain const *stamp)
{
char fmt[128] ;
size_t n = tipidee_response_status(b, rql, status, reason) ;
- n += tipidee_response_header_common_put(b, options & 1, stamp) ;
+ n += tipidee_response_header_writeall(b, rhdr, rhdrn, options & 1, stamp) ;
if (options & 2)
{
size_t l = tipidee_response_header_lastmodified(fmt, 128, st) ;
diff --git a/src/libtipidee/tipidee_response_header_builtin.c b/src/libtipidee/tipidee_response_header_builtin.c
deleted file mode 100644
index 0125cb8..0000000
--- a/src/libtipidee/tipidee_response_header_builtin.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/* ISC license. */
-
-#include <string.h>
-#include <stdlib.h>
-
-#include <tipidee/config.h>
-#include <tipidee/response.h>
-
-static tipidee_response_header_builtin const tipidee_response_header_builtin_table_[] =
-{
- { .key = "Accept-Ranges", .value = "none" },
- { .key = "Cache-Control", .value = "private" },
- { .key = "Content-Security-Policy", .value = "default-src 'self'; style-src 'self' 'unsafe-inline';" },
- { .key = "Referrer-Policy", .value = "no-referrer-when-downgrade" },
- { .key = "Server", .value = "tipidee/" TIPIDEE_VERSION },
- { .key = "Vary", .value = "Accept-Encoding" },
- { .key = "X-Content-Type-Options", .value = "nosniff" },
- { .key = "X-Frame-Options", .value = "DENY" },
- { .key = "X-XSS-Protection", .value = "1; mode=block" },
- { .key = 0, .value = 0 },
-} ;
-
-tipidee_response_header_builtin const *tipidee_response_header_builtin_table = tipidee_response_header_builtin_table_ ;
-
-static int tipidee_response_header_builtin_cmp (void const *a, void const *b)
-{
- return strcmp((char const *)a, ((tipidee_response_header_builtin const *)b)->key) ;
-}
-
-char const *tipidee_response_header_builtin_search (char const *key)
-{
- tipidee_response_header_builtin const *p = bsearch(
- key,
- tipidee_response_header_builtin_table_,
- sizeof(tipidee_response_header_builtin_table_) / sizeof(tipidee_response_header_builtin) - 1,
- sizeof(tipidee_response_header_builtin),
- &tipidee_response_header_builtin_cmp) ;
- return p ? p->value : 0 ;
-}
-
diff --git a/src/libtipidee/tipidee_response_header_common_put.c b/src/libtipidee/tipidee_response_header_common_put.c
deleted file mode 100644
index e9a1a07..0000000
--- a/src/libtipidee/tipidee_response_header_common_put.c
+++ /dev/null
@@ -1,20 +0,0 @@
-/* ISC license. */
-
-#include <skalibs/buffer.h>
-
-#include <tipidee/response.h>
-
-size_t tipidee_response_header_common_put (buffer *b, uint32_t options, tain const *stamp)
-{
- char fmt[128] ;
- size_t n = buffer_putnoflush(b, fmt, tipidee_response_header_date(fmt, 128, stamp)) ;
- for (tipidee_response_header_builtin const *p = tipidee_response_header_builtin_table ; p->key ; p++)
- {
- n += buffer_putsnoflush(b, p->key) ;
- n += buffer_putnoflush(b, ": ", 2) ;
- n += buffer_putsnoflush(b, p->value) ;
- n += buffer_putnoflush(b, "\r\n", 2) ;
- }
- if (options & 1) n += buffer_putsnoflush(b, "Connection: close\r\n") ;
- return n ;
-}
diff --git a/src/libtipidee/tipidee_response_header_writeall.c b/src/libtipidee/tipidee_response_header_writeall.c
new file mode 100644
index 0000000..a61a80d
--- /dev/null
+++ b/src/libtipidee/tipidee_response_header_writeall.c
@@ -0,0 +1,20 @@
+/* ISC license. */
+
+#include <skalibs/buffer.h>
+
+#include <tipidee/response.h>
+
+size_t tipidee_response_header_writeall (buffer *b, tipidee_response_header const *rhdr, uint32_t rhdrn, uint32_t options, tain const *stamp)
+{
+ char fmt[128] ;
+ size_t m = buffer_putnoflush(b, fmt, tipidee_response_header_date(fmt, 128, stamp)) ;
+ if (options & 1) m += buffer_putsnoflush(b, "Connection: close\r\n") ;
+ for (uint32_t i = 0 ; i < rhdrn ; i++)
+ {
+ m += buffer_putsnoflush(b, rhdr[i].key) ;
+ m += buffer_putnoflush(b, ": ", 2) ;
+ m += buffer_putsnoflush(b, rhdr[i].value) ;
+ m += buffer_putnoflush(b, "\r\n", 2) ;
+ }
+ return m ;
+}
diff --git a/src/libtipidee/tipidee_response_header_writemerge.c b/src/libtipidee/tipidee_response_header_writemerge.c
new file mode 100644
index 0000000..b8b1e10
--- /dev/null
+++ b/src/libtipidee/tipidee_response_header_writemerge.c
@@ -0,0 +1,54 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <stdlib.h>
+#include <strings.h>
+
+#include <skalibs/buffer.h>
+
+#include <tipidee/headers.h>
+#include <tipidee/response.h>
+
+static int tipidee_response_header_cmp (void const *a, void const *b)
+{
+ return strcasecmp((char const *)a, ((tipidee_response_header const *)b)->key) ;
+}
+
+size_t tipidee_response_header_writemerge (buffer *b, tipidee_response_header const *rhdr, uint32_t rhdrn, tipidee_headers const *hdr, uint32_t options, tain const *stamp)
+{
+ static char const *const nope_table[] =
+ {
+ "Connection",
+ "Content-Length",
+ "Date",
+ "Status"
+ } ;
+ char fmt[128] ;
+ size_t m = buffer_putnoflush(b, fmt, tipidee_response_header_date(fmt, 128, stamp)) ;
+ if (options & 1) m += buffer_putsnoflush(b, "Connection: close\r\n") ;
+
+ for (uint32_t i = 0 ; i < rhdrn ; i++)
+ {
+ if (rhdr[i].options & 1 && tipidee_headers_search(hdr, rhdr[i].key)) continue ;
+ m += buffer_putsnoflush(b, rhdr[i].key) ;
+ m += buffer_putnoflush(b, ": ", 2) ;
+ m += buffer_putsnoflush(b, rhdr[i].value) ;
+ m += buffer_putnoflush(b, "\r\n", 2) ;
+ }
+
+ for (uint32_t i = 0 ; i < hdr->n ; i++)
+ {
+ tipidee_response_header const *p ;
+ char const *key = hdr->buf + hdr->list[i].left ;
+ if (!strncasecmp(key, "X-CGI-", 6)) continue ;
+ if (bsearch(key, nope_table, sizeof(nope_table) / sizeof(char const *const), sizeof(char const *const), (int (*)(void const *, void const *))&strcasecmp)) continue ;
+ p = bsearch(key, rhdr, rhdrn, sizeof(tipidee_response_header), &tipidee_response_header_cmp) ;
+ if (p && !(p->options & 1)) continue ;
+ m += buffer_putsnoflush(b, key) ;
+ m += buffer_putnoflush(b, ": ", 2) ;
+ m += buffer_putsnoflush(b, hdr->buf + hdr->list[i].right) ;
+ m += buffer_putnoflush(b, "\r\n", 2) ;
+ }
+
+ return m ;
+}