summaryrefslogtreecommitdiff
path: root/src/tipideed
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/tipideed
parent37d2f8cb438f68eaa1da8a56ea9ce5023091f128 (diff)
downloadtipidee-305385e06f90ca3216e1aa4e43b20dd7d53c1222.tar.xz
Make the switch to custom headers
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/tipideed')
-rw-r--r--src/tipideed/cgi.c44
-rw-r--r--src/tipideed/options.c2
-rw-r--r--src/tipideed/regular.c6
-rw-r--r--src/tipideed/responses.c10
-rw-r--r--src/tipideed/tipideed-internal.h2
-rw-r--r--src/tipideed/tipideed.c6
-rw-r--r--src/tipideed/trace.c2
7 files changed, 25 insertions, 47 deletions
diff --git a/src/tipideed/cgi.c b/src/tipideed/cgi.c
index 0d7e104..be969c8 100644
--- a/src/tipideed/cgi.c
+++ b/src/tipideed/cgi.c
@@ -255,36 +255,6 @@ static inline int local_redirect (tipidee_rql *rql, char const *docroot, char co
return 1 ;
}
-static inline void print_cgi_headers (tipidee_headers const *hdr, size_t rbodylen)
-{
- static char const *const nope_table[] =
- {
- "Connection",
- "Content-Length",
- "Date",
- "Status",
- 0
- } ;
- char fmt[SIZE_FMT] ;
- for (size_t i = 0 ; i < hdr->n ; i++)
- {
- char const *key = hdr->buf + hdr->list[i].left ;
- char const *const *p = nope_table ;
- if (tipidee_response_header_builtin_search(key)) continue ;
- if (str_start(key, "X-CGI-")) continue ;
- for (; *p ; p++) if (!strcasecmp(key, *p)) break ;
- if (*p) continue ;
- buffer_putsnoflush(buffer_1, key) ;
- buffer_putnoflush(buffer_1, ": ", 2) ;
- buffer_putsnoflush(buffer_1, hdr->buf + hdr->list[i].right) ;
- buffer_putnoflush(buffer_1, "\r\n", 2) ;
- }
- fmt[size_fmt(fmt, rbodylen)] = 0 ;
- buffer_putsnoflush(buffer_1, "Content-Length: ") ;
- buffer_putsnoflush(buffer_1, fmt) ;
- buffer_putnoflush(buffer_1, "\r\n", 2) ;
-}
-
static inline int process_cgi_output (tipidee_rql *rql, char const *docroot, tipidee_headers const *hdr, char const *rbody, size_t rbodylen, char *uribuf, char const *cginame)
{
char const *location = tipidee_headers_search(hdr, "Location") ;
@@ -353,8 +323,14 @@ static inline int process_cgi_output (tipidee_rql *rql, char const *docroot, tip
}
tipidee_response_status(buffer_1, rql, status, reason) ;
- tipidee_response_header_common_put_g(buffer_1, !g.cont) ;
- print_cgi_headers(hdr, rbodylen) ;
+ tipidee_response_header_writemerge_g(buffer_1, g.rhdr, g.rhdrn, hdr, !g.cont) ;
+ {
+ char fmt[SIZE_FMT] ;
+ fmt[size_fmt(fmt, rbodylen)] = 0 ;
+ buffer_putsnoflush(buffer_1, "Content-Length: ") ;
+ buffer_putsnoflush(buffer_1, fmt) ;
+ buffer_putnoflush(buffer_1, "\r\n", 2) ;
+ }
if (buffer_timed_put_g(buffer_1, "\r\n", 2, &deadline) < 2)
strerr_diefu1sys(111, "write to stdout") ;
tipidee_log_answer(g.logv, rql, status, rbodylen) ;
@@ -372,9 +348,9 @@ static inline int do_cgi (tipidee_rql *rql, char const *docroot, char const *con
{
static stralloc sa = STRALLOC_ZERO ;
tipidee_headers hdr ;
- char hdrbuf[2048] ;
+ char hdrbuf[4096] ;
sa.len = 0 ;
- tipidee_headers_init(&hdr, hdrbuf, 2048) ;
+ tipidee_headers_init(&hdr, hdrbuf, 4096) ;
if (!run_cgi(rql, docroot, argv, envp, body, bodylen, &hdr, &sa)) return 0 ;
return process_cgi_output(rql, docroot, &hdr, sa.s, sa.len, uribuf, argv[0]) ;
}
diff --git a/src/tipideed/options.c b/src/tipideed/options.c
index a023401..ce25a81 100644
--- a/src/tipideed/options.c
+++ b/src/tipideed/options.c
@@ -15,7 +15,7 @@ int respond_options (tipidee_rql const *rql, uint32_t flags)
{
tain deadline ;
tipidee_response_status(buffer_1, rql, 200, "OK") ;
- tipidee_response_header_common_put_g(buffer_1, 0) ;
+ tipidee_response_header_writeall_g(buffer_1, g.rhdr, g.rhdrn, 0) ;
buffer_putsnoflush(buffer_1, "Content-Length: 0\r\nAllow: GET, HEAD") ;
if (flags & 1) buffer_putsnoflush(buffer_1, ", POST") ;
buffer_putnoflush(buffer_1, "\r\n\r\n", 4) ;
diff --git a/src/tipideed/regular.c b/src/tipideed/regular.c
index aa937ad..a075cc0 100644
--- a/src/tipideed/regular.c
+++ b/src/tipideed/regular.c
@@ -22,7 +22,7 @@ int respond_regular (tipidee_rql const *rql, char const *docroot, char const *fn
if (rql->m == TIPIDEE_METHOD_HEAD)
{
tain deadline ;
- tipidee_response_file_g(buffer_1, rql, 200, "OK", st, ra->content_type, 2 | !g.cont) ;
+ tipidee_response_file_g(buffer_1, rql, 200, "OK", st, ra->content_type, g.rhdr, g.rhdrn, 2 | !g.cont) ;
tipidee_log_answer(g.logv, rql, 200, st->st_size) ;
tain_add_g(&deadline, &g.writetto) ;
if (!buffer_timed_flush_g(buffer_1, &deadline))
@@ -40,7 +40,7 @@ int respond_regular (tipidee_rql const *rql, char const *docroot, char const *fn
}
else die500sys(rql, 111, docroot, "open ", fn) ;
}
- tipidee_response_file_g(buffer_1, rql, 200, "OK", st, ra->content_type, 2 | !g.cont) ;
+ tipidee_response_file_g(buffer_1, rql, 200, "OK", st, ra->content_type, g.rhdr, g.rhdrn, 2 | !g.cont) ;
tipidee_log_answer(g.logv, rql, 200, st->st_size) ;
send_file(fd, st->st_size, fn) ;
fd_close(fd) ;
@@ -53,7 +53,7 @@ int respond_304 (tipidee_rql const *rql, char const *fn, struct stat const *st)
tain deadline ;
char fmt[128] ;
size_t n = tipidee_response_status(buffer_1, rql, 304, "Not Modified") ;
- n += tipidee_response_header_common_put_g(buffer_1, !g.cont) ;
+ n += tipidee_response_header_writeall_g(buffer_1, g.rhdr, g.rhdrn, !g.cont) ;
{
size_t l = tipidee_response_header_lastmodified(fmt, 128, st) ;
if (l) n += buffer_putnoflush(buffer_1, fmt, l) ;
diff --git a/src/tipideed/responses.c b/src/tipideed/responses.c
index 81b8315..8155b55 100644
--- a/src/tipideed/responses.c
+++ b/src/tipideed/responses.c
@@ -23,7 +23,7 @@
void response_error_early (tipidee_rql const *rql, unsigned int status, char const *reason, char const *text, uint32_t options)
{
tain deadline ;
- tipidee_response_error_nofile_g(buffer_1, rql, status, reason, text, options & 1 || !g.cont) ;
+ tipidee_response_error_nofile_g(buffer_1, rql, status, reason, text, g.rhdr, g.rhdrn, options & 1 || !g.cont) ;
tain_add_g(&deadline, &g.writetto) ;
if (!buffer_timed_flush_g(buffer_1, &deadline))
strerr_diefu1sys(111, "write to stdout") ;
@@ -79,7 +79,7 @@ void response_error (tipidee_rql const *rql, char const *docroot, unsigned int s
}
else
{
- tipidee_response_file_g(buffer_1, rql, status, dt.reason, &st, tipidee_conf_get_content_type(&g.conf, g.sa.s + salen + g.cwdlen + 1), options) ;
+ tipidee_response_file_g(buffer_1, rql, status, dt.reason, &st, tipidee_conf_get_content_type(&g.conf, g.sa.s + salen + g.cwdlen + 1), g.rhdr, g.rhdrn, options) ;
tipidee_log_answer(g.logv, rql, status, st.st_size) ;
send_file(fd, st.st_size, g.sa.s + salen + g.cwdlen + 1) ;
fd_close(fd) ;
@@ -88,7 +88,7 @@ void response_error (tipidee_rql const *rql, char const *docroot, unsigned int s
}
}
- tipidee_response_error_nofile_g(buffer_1, rql, status, dt.reason, dt.text, options & 1 || !g.cont) ;
+ tipidee_response_error_nofile_g(buffer_1, rql, status, dt.reason, dt.text, g.rhdr, g.rhdrn, options & 1 || !g.cont) ;
tipidee_log_answer(g.logv, rql, status, 0) ;
tain_add_g(&deadline, &g.writetto) ;
if (!buffer_timed_flush_g(buffer_1, &deadline))
@@ -112,7 +112,7 @@ void exit_405_ (tipidee_rql const *rql, uint32_t options)
{
tain deadline ;
tipidee_response_status(buffer_1, rql, 405, "Method Not Allowed") ;
- tipidee_response_header_common_put_g(buffer_1, 1) ;
+ tipidee_response_header_writeall_g(buffer_1, g.rhdr, g.rhdrn, 1) ;
buffer_putsnoflush(buffer_1, "Allow: GET, HEAD") ;
if (options & 1) buffer_putsnoflush(buffer_1, ", POST") ;
buffer_putnoflush(buffer_1, "\r\n\r\n", 4) ;
@@ -129,7 +129,7 @@ void respond_30x (tipidee_rql const *rql, tipidee_redirection const *rd)
static char const *const reason[4] = { "Temporary Redirect", "Permanent Redirect", "Found", "Moved Permanently" } ;
tain deadline ;
tipidee_response_status(buffer_1, rql, status[rd->type], reason[rd->type]) ;
- tipidee_response_header_common_put_g(buffer_1, 0) ;
+ tipidee_response_header_writeall_g(buffer_1, g.rhdr, g.rhdrn, 0) ;
buffer_putsnoflush(buffer_1, "Content-Length: 0\r\nLocation: ") ;
buffer_putsnoflush(buffer_1, rd->location) ;
if (rd->sub) buffer_putsnoflush(buffer_1, rd->sub) ;
diff --git a/src/tipideed/tipideed-internal.h b/src/tipideed/tipideed-internal.h
index 3629f84..344840c 100644
--- a/src/tipideed/tipideed-internal.h
+++ b/src/tipideed/tipideed-internal.h
@@ -32,6 +32,7 @@ struct global_s
char const *indexnames[16] ;
tipidee_response_header const *rhdr ;
int p[2] ;
+ uint32_t rhdrn ;
uint32_t logv ;
uint32_t maxrqbody ;
uint32_t maxcgibody ;
@@ -54,6 +55,7 @@ struct global_s
.indexnames = { 0 }, \
.rhdr = 0, \
.p = { -1, -1 }, \
+ .rhdrn = 0, \
.logv = TIPIDEE_LOG_DEFAULT, \
.maxrqbody = 0, \
.maxcgibody = 0, \
diff --git a/src/tipideed/tipideed.c b/src/tipideed/tipideed.c
index f4d18a6..cf961db 100644
--- a/src/tipideed/tipideed.c
+++ b/src/tipideed/tipideed.c
@@ -352,7 +352,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
stralloc bodysa = STRALLOC_ZERO ;
size_t remoteip, remotehost ;
char const *x ;
- uint32_t n, rhlen ;
+ uint32_t n ;
char progstr[14 + PID_FMT] = "tipideed: pid " ;
progstr[14 + pid_fmt(progstr + 14, getpid())] = 0 ;
PROG = progstr ;
@@ -397,11 +397,11 @@ int main (int argc, char const *const *argv, char const *const *envp)
if (!n) strerr_dief3x(102, "bad", " config value for ", "G:index_file") ;
g.indexn = n-1 ;
- x = tipidee_conf_get_responseheaders(&g.conf, "G:response_headers", &rhlen, &n) ;
+ x = tipidee_conf_get_responseheaders(&g.conf, "G:response_headers", &n, &g.rhdrn) ;
if (!x) strerr_diefu3sys(102, "get", " config value for ", "G:response_headers") ;
tipidee_response_header rhdr[n ? n : 1] ; /* should start a block but that's a lot of editing */
- if (!tipidee_response_header_preparebuiltin(rhdr, n, x, rhlen))
+ if (!tipidee_response_header_preparebuiltin(rhdr, g.rhdrn, x, n))
strerr_dief3x(102, "bad", " config value for ", "G:response_headers") ;
g.rhdr = rhdr ;
diff --git a/src/tipideed/trace.c b/src/tipideed/trace.c
index 122d269..d1aed09 100644
--- a/src/tipideed/trace.c
+++ b/src/tipideed/trace.c
@@ -19,7 +19,7 @@ int respond_trace (char const *buf, tipidee_rql const *rql, tipidee_headers cons
size_t cl = 0 ;
char fmt[SIZE_FMT] ;
tipidee_response_status(buffer_1, rql, 200, "OK") ;
- tipidee_response_header_common_put_g(buffer_1, 0) ;
+ tipidee_response_header_writeall_g(buffer_1, g.rhdr, g.rhdrn, 0) ;
buffer_putsnoflush(buffer_1, "Content-Type: message/http\r\nContent-Length: ") ;
cl += strlen(tipidee_method_tostr(rql->m)) + 1;
if (rql->uri.host) cl += 7 + rql->uri.https + strlen(rql->uri.host) ;