about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2023-10-23 10:31:31 +0000
committerLaurent Bercot <ska@appnovation.com>2023-10-23 10:31:31 +0000
commit12891d0e8551e3d6bb7bf1429f936e04be4da8b5 (patch)
tree7e4ac4bb5dc895b1b7604c8c9f76cca470964162
parent941ff50d2183999d8f1cbd249b6a892f70091ded (diff)
downloadtipidee-12891d0e8551e3d6bb7bf1429f936e04be4da8b5.tar.gz
tipidee-12891d0e8551e3d6bb7bf1429f936e04be4da8b5.tar.xz
tipidee-12891d0e8551e3d6bb7bf1429f936e04be4da8b5.zip
Refactor tipidee-config code to accommodate for headers
Signed-off-by: Laurent Bercot <ska@appnovation.com>
-rw-r--r--doc/index.html5
-rw-r--r--package/deps.mak6
-rw-r--r--src/config/conftree.c70
-rw-r--r--src/config/defaults.c2
-rw-r--r--src/config/deps-exe/tipidee-config4
-rw-r--r--src/config/headers.c46
-rw-r--r--src/config/lexparse.c18
-rw-r--r--src/config/node.c (renamed from src/config/confnode.c)16
-rw-r--r--src/config/repo.c46
-rw-r--r--src/config/tipidee-config-internal.h59
10 files changed, 193 insertions, 79 deletions
diff --git a/doc/index.html b/doc/index.html
index bda2237..76971e9 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -99,9 +99,8 @@ similar sites that need an <em>intermediary</em> web server.
 
 <p>
  Because <em>h-t-t-p-d</em> is already pretty tedious to say out loud, and
-other web servers have a nasty habit of <em>adding</em> to it; it's much
-nicer to make it shorter. And yes, you can take that as an indication of what
-is going on with the code, too.
+other web servers have a nasty habit of <em>adding</em> to it; but I'd rather
+make it shorter. Just like the code.
 </p>
 
 <h2> Installation </h2>
diff --git a/package/deps.mak b/package/deps.mak
index 3b29af0..7b7ef4e 100644
--- a/package/deps.mak
+++ b/package/deps.mak
@@ -8,10 +8,12 @@ src/include/tipidee/response.h: src/include/tipidee/rql.h
 src/include/tipidee/rql.h: src/include/tipidee/method.h src/include/tipidee/uri.h
 src/include/tipidee/tipidee.h: src/include/tipidee/conf.h src/include/tipidee/config.h src/include/tipidee/headers.h src/include/tipidee/log.h src/include/tipidee/method.h src/include/tipidee/response.h src/include/tipidee/rql.h src/include/tipidee/uri.h src/include/tipidee/util.h
 src/tipideed/tipideed-internal.h: src/include/tipidee/tipidee.h
-src/config/confnode.o src/config/confnode.lo: src/config/confnode.c src/config/tipidee-config-internal.h
 src/config/conftree.o src/config/conftree.lo: src/config/conftree.c src/config/tipidee-config-internal.h
 src/config/defaults.o src/config/defaults.lo: src/config/defaults.c src/config/tipidee-config-internal.h src/include/tipidee/log.h
+src/config/headers.o src/config/headers.lo: src/config/headers.c src/config/tipidee-config-internal.h
 src/config/lexparse.o src/config/lexparse.lo: src/config/lexparse.c src/config/tipidee-config-internal.h src/include/tipidee/config.h src/include/tipidee/log.h
+src/config/node.o src/config/node.lo: src/config/node.c src/config/tipidee-config-internal.h
+src/config/repo.o src/config/repo.lo: src/config/repo.c src/config/tipidee-config-internal.h
 src/config/tipidee-config-preprocess.o src/config/tipidee-config-preprocess.lo: src/config/tipidee-config-preprocess.c
 src/config/tipidee-config.o src/config/tipidee-config.lo: src/config/tipidee-config.c src/config/tipidee-config-internal.h src/include/tipidee/config.h
 src/libtipidee/tipidee_conf_free.o src/libtipidee/tipidee_conf_free.lo: src/libtipidee/tipidee_conf_free.c src/include/tipidee/conf.h
@@ -56,7 +58,7 @@ src/tipideed/tipideed.o src/tipideed/tipideed.lo: src/tipideed/tipideed.c src/in
 src/tipideed/trace.o src/tipideed/trace.lo: src/tipideed/trace.c src/include/tipidee/log.h src/include/tipidee/method.h src/include/tipidee/response.h src/tipideed/tipideed-internal.h
 
 tipidee-config: EXTRA_LIBS := -lskarnet ${SPAWN_LIB}
-tipidee-config: src/config/tipidee-config.o src/config/confnode.o src/config/conftree.o src/config/defaults.o src/config/lexparse.o
+tipidee-config: src/config/tipidee-config.o src/config/node.o src/config/repo.o src/config/conftree.o src/config/headers.o src/config/defaults.o src/config/lexparse.o
 tipidee-config-preprocess: EXTRA_LIBS := -lskarnet
 tipidee-config-preprocess: src/config/tipidee-config-preprocess.o
 ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),)
diff --git a/src/config/conftree.c b/src/config/conftree.c
index 47bf4fb..d4b01ef 100644
--- a/src/config/conftree.c
+++ b/src/config/conftree.c
@@ -1,78 +1,56 @@
 /* ISC license. */
 
-#include <stdint.h>
-#include <string.h>
-#include <errno.h>
-
-#include <skalibs/gensetdyn.h>
+#include <skalibs/genalloc.h>
 #include <skalibs/avltree.h>
 #include <skalibs/cdbmake.h>
-#include <skalibs/strerr.h>
 
 #include "tipidee-config-internal.h"
 
-static void *confnode_dtok (uint32_t d, void *data)
-{
-  return g.storage.s + GENSETDYN_P(confnode, (gensetdyn *)data, d)->key ;
-}
+static repo conftree = \
+{ \
+  .ga = GENALLOC_ZERO, \
+  .tree = AVLTREE_INIT(8, 3, 8, &node_dtok, &node_cmp, &conftree.ga), \
+  .storage = &g.storage \
+} ;
 
-static int confnode_cmp (void const *a, void const *b, void *data)
+void confnode_start (node *node, char const *key, size_t filepos, uint32_t line)
 {
-  (void)data ;
-  return strcmp((char const *)a, (char const *)b) ;
+  return node_start(&g.storage, node, key, filepos, line) ;
 }
 
-struct nodestore_s
+void confnode_add (node *node, char const *s, size_t len)
 {
-  gensetdyn set ;
-  avltree tree ;
-} ;
-
-static struct nodestore_s nodestore = \
-{ \
-  .set = GENSETDYN_INIT(confnode, 8, 3, 8), \
-  .tree = AVLTREE_INIT(8, 3, 8, &confnode_dtok, &confnode_cmp, &nodestore.set) \
-} ;
+  return node_add(&g.storage, node, s, len) ;
+}
 
-confnode const *conftree_search (char const *key)
+node const *conftree_search (char const *key)
 {
-  uint32_t i ;
-  return avltree_search(&nodestore.tree, key, &i) ? GENSETDYN_P(confnode const, &nodestore.set, i) : 0 ;
+  return repo_search(&conftree, key) ;
 }
 
-void conftree_add (confnode const *node)
+void conftree_add (node const *node)
 {
-  uint32_t i ;
-  if (!gensetdyn_new(&nodestore.set, &i)) dienomem() ;
-  *GENSETDYN_P(confnode, &nodestore.set, i) = *node ;
-  if (!avltree_insert(&nodestore.tree, i)) dienomem() ;
+  return repo_add(&conftree, node) ;
 }
 
-void conftree_update (confnode const *node)
+void conftree_update (node const *node)
 {
-  uint32_t i ;
-  if (avltree_search(&nodestore.tree, g.storage.s + node->key, &i))
-  {
-    if (!avltree_delete(&nodestore.tree, g.storage.s + node->key)) dienomem() ;
-    *GENSETDYN_P(confnode, &nodestore.set, i) = *node ;
-    if (!avltree_insert(&nodestore.tree, i)) dienomem() ;
-  }
-  else return conftree_add(node) ;
+  return repo_update(&conftree, node) ;
 }
 
 static int confnode_write (uint32_t d, unsigned int h, void *data)
 {
-  confnode *node = GENSETDYN_P(confnode, &nodestore.set, d) ;
+  node *nod = genalloc_s(node, &conftree.ga) + d ;
   (void)h ;
-  if ((g.storage.s[node->key] & ~0x20) == 'A')
+  if ((conftree.storage->s[nod->key] & ~0x20) == 'A')
   {
-    g.storage.s[++node->data] |= '@' ;
-    node->datalen-- ;
+    conftree.storage->s[++nod->data] |= '@' ;
+    nod->datalen-- ;
   }
-  return cdbmake_add((cdbmaker *)data, g.storage.s + node->key, node->keylen, g.storage.s + node->data, node->datalen) ;
+  return cdbmake_add((cdbmaker *)data, conftree.storage->s + nod->key, nod->keylen, conftree.storage->s + nod->data, nod->datalen) ;
 }
 
 int conftree_write (cdbmaker *cm)
 {
-  return avltree_iter(&nodestore.tree, &confnode_write, cm) ;
+  return avltree_iter(&conftree.tree, &confnode_write, cm) ;
 }
diff --git a/src/config/defaults.c b/src/config/defaults.c
index 69881fb..3db2ad2 100644
--- a/src/config/defaults.c
+++ b/src/config/defaults.c
@@ -103,7 +103,7 @@ void conf_defaults (void)
   {
     if (!conftree_search(p->key))
     {
-      confnode node ;
+      node node ;
       confnode_start(&node, p->key, 0, 0) ;
       confnode_add(&node, p->value, p->vlen) ;
       conftree_add(&node) ;
diff --git a/src/config/deps-exe/tipidee-config b/src/config/deps-exe/tipidee-config
index 85a9645..55cf760 100644
--- a/src/config/deps-exe/tipidee-config
+++ b/src/config/deps-exe/tipidee-config
@@ -1,5 +1,7 @@
-confnode.o
+node.o
+repo.o
 conftree.o
+headers.o
 defaults.o
 lexparse.o
 -lskarnet
diff --git a/src/config/headers.c b/src/config/headers.c
new file mode 100644
index 0000000..7bbb573
--- /dev/null
+++ b/src/config/headers.c
@@ -0,0 +1,46 @@
+/* ISC license. */
+
+#include <skalibs/genalloc.h>
+#include <skalibs/avltree.h>
+
+#include "tipidee-config-internal.h"
+
+static stralloc headers_storage = GENALLOC_ZERO ;
+
+static repo headers = \
+{ \
+  .ga = GENALLOC_ZERO, \
+  .tree = AVLTREE_INIT(8, 3, 8, &node_dtok, &node_cmp, &headers.ga), \
+  .storage = &headers_storage \
+} ;
+
+void header_start (node *node, char const *key, size_t filepos, uint32_t line)
+{
+  return node_start(&headers_storage, node, key, filepos, line) ;
+}
+
+void header_add (node *node, char const *s, size_t len)
+{
+  return node_add(&headers_storage, node, s, len) ;
+}
+
+node const *headers_search (char const *key)
+{
+  return repo_search(&headers, key) ;
+}
+
+void headers_add (node const *node)
+{
+  return repo_add(&headers, node) ;
+}
+
+static int header_write (uint32_t d, unsigned int h, void *data)
+{
+  return 1 ;
+}
+
+int headers_write (void)
+{
+  if (!avltree_iter(&headers.tree, &header_write, 0)) return 0 ;
+  return 1 ;
+}
diff --git a/src/config/lexparse.c b/src/config/lexparse.c
index 88a9157..5fb9b71 100644
--- a/src/config/lexparse.c
+++ b/src/config/lexparse.c
@@ -73,7 +73,7 @@ static int keycmp (void const *a, void const *b)
 
 static void check_unique (char const *key, mdt const *md)
 {
-  confnode const *node = conftree_search(key) ;
+  node const *node = conftree_search(key) ;
   if (node)
   {
     char fmt[UINT32_FMT] ;
@@ -84,7 +84,7 @@ static void check_unique (char const *key, mdt const *md)
 
 static void add_unique (char const *key, char const *value, size_t valuelen, mdt const *md)
 {
-  confnode node ;
+  node node ;
   check_unique(key, md) ;
   confnode_start(&node, key, md->filepos, md->line) ;
   confnode_add(&node, value, valuelen) ;
@@ -123,7 +123,7 @@ static inline void parse_global (char const *s, size_t const *word, size_t n, md
     }
     case 1 : /* argv */
     {
-      confnode node ;
+      node node ;
       check_unique(gl->key, md) ;
       confnode_start(&node, gl->key, md->filepos, md->line) ;
       for (size_t i = 1 ; i < n ; i++)
@@ -210,7 +210,7 @@ static inline void parse_redirect (char const *s, size_t const *word, size_t n,
   if (strncmp(s + word[2], "http://", 7) && strncmp(s + word[2], "https://", 8))
     strerr_dief5x(1, "redirection target must be a full http:// or https:// target", " in file ", g.storage.s + md->filepos, " line ", md->linefmt) ;
   {
-    confnode node ;
+    node node ;
     size_t urlen = strlen(s + word[0]) ;
     char key[3 + domainlen + urlen] ;
     if (s[word[0] + urlen - 1] == '/') { key[0] = 'r' ; urlen-- ; } else key[0] = 'R' ;
@@ -240,7 +240,7 @@ static void parse_bitattr (char const *s, size_t const *word, size_t n, char con
   if (s[*word] != '/')
     strerr_dief6x(1, "resource", " must start with /", " in file ", g.storage.s + md->filepos, " line ", md->linefmt) ;
   {
-    confnode const *oldnode ;
+    node const *oldnode ;
     size_t arglen = strlen(s + *word) ;
     char key[3 + domainlen + arglen] ;
     if (s[*word + arglen - 1] == '/') { key[0] = 'a' ; arglen-- ; } else key[0] = 'A' ;
@@ -264,7 +264,7 @@ static void parse_bitattr (char const *s, size_t const *word, size_t n, char con
       }
     else
     {
-      confnode node ;
+      node node ;
       char val[3] = { mask, set ? mask : 0, 0 } ;
       confnode_start(&node, key, md->filepos, md->line) ;
       confnode_add(&node, val, 3) ;
@@ -282,7 +282,7 @@ static inline void parse_filetype (char const *s, size_t const *word, size_t n,
   if (s[word[0]] != '/')
     strerr_dief6x(1, "resource", " must start with /", " in file ", g.storage.s + md->filepos, " line ", md->linefmt) ;
   {
-    confnode const *oldnode ;
+    node const *oldnode ;
     size_t arglen = strlen(s + word[0]) ;
     char key[3 + domainlen + arglen] ;
     if (s[word[0] + arglen - 1] == '/') { key[0] = 'a' ; arglen-- ; } else key[0] = 'A' ;
@@ -302,7 +302,7 @@ static inline void parse_filetype (char const *s, size_t const *word, size_t n,
       
       else
       {
-        confnode node ;
+        node node ;
         char val[2] = { g.storage.s[oldnode->data] | 0x80, g.storage.s[oldnode->data + 1] } ;
         confnode_start(&node, key, md->filepos, md->line) ;
         confnode_add(&node, val, 2) ;
@@ -312,7 +312,7 @@ static inline void parse_filetype (char const *s, size_t const *word, size_t n,
     }
     else
     {
-      confnode node ;
+      node node ;
       char val[2] = { 0x80, 0x00 } ;
       confnode_start(&node, key, md->filepos, md->line) ;
       confnode_add(&node, val, 2) ;
diff --git a/src/config/confnode.c b/src/config/node.c
index abcf962..6f3fd5a 100644
--- a/src/config/confnode.c
+++ b/src/config/node.c
@@ -11,24 +11,24 @@
 #define diestorage() strerr_diefu2x(100, "add node to configuration tree", ": too much data")
 #define diefilepos() strerr_diefu2x(100, "add node to configuration tree", ": file too large")
 
-void confnode_start (confnode *node, char const *key, size_t filepos, uint32_t line)
+void node_start (stralloc *storage, node *node, char const *key, size_t filepos, uint32_t line)
 {
   size_t l = strlen(key) ;
-  size_t k = g.storage.len ;
-  if (!stralloc_catb(&g.storage, key, l + 1)) dienomem() ;
-  if (g.storage.len >= UINT32_MAX) diestorage() ;
+  size_t k = storage->len ;
+  if (!stralloc_catb(storage, key, l + 1)) dienomem() ;
+  if (storage->len >= UINT32_MAX) diestorage() ;
   if (filepos > UINT32_MAX) diefilepos() ;
   node->key = k ;
   node->keylen = l ;
-  node->data = g.storage.len ;
+  node->data = storage->len ;
   node->datalen = 0 ;
   node->filepos = filepos ;
   node->line = line ;
 }
 
-void confnode_add (confnode *node, char const *s, size_t len)
+void node_add (stralloc *storage, node *node, char const *s, size_t len)
 {
-  if (!stralloc_catb(&g.storage, s, len)) dienomem() ;
-  if (g.storage.len >= UINT32_MAX) diestorage() ;
+  if (!stralloc_catb(storage, s, len)) dienomem() ;
+  if (storage->len >= UINT32_MAX) diestorage() ;
   node->datalen += len ;
 }
diff --git a/src/config/repo.c b/src/config/repo.c
new file mode 100644
index 0000000..a3ba390
--- /dev/null
+++ b/src/config/repo.c
@@ -0,0 +1,46 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <string.h>
+
+#include <skalibs/genalloc.h>
+#include <skalibs/avltree.h>
+
+#include "tipidee-config-internal.h"
+
+void *node_dtok (uint32_t d, void *data)
+{
+  repo *r = data ;
+  return r->storage->s + genalloc_s(node, &r->ga)[d].key ;
+}
+
+int node_cmp (void const *a, void const *b, void *data)
+{
+  (void)data ;
+  return strcmp((char const *)a, (char const *)b) ;
+}
+
+node const *repo_search (repo const *r, char const *key)
+{
+  uint32_t i ;
+  return avltree_search(&r->tree, key, &i) ? genalloc_s(node const, &r->ga) + i : 0 ;
+}
+
+void repo_add (repo *r, node const *node)
+{
+  uint32_t i = genalloc_len(node, &r->ga) ;
+  if (!genalloc_append(node, &r->ga, node)) dienomem() ;
+  if (!avltree_insert(&r->tree, i)) dienomem() ;
+}
+
+void repo_update (repo *r, node const *nod)
+{
+  uint32_t i ;
+  if (avltree_search(&r->tree, r->storage->s + nod->key, &i))
+  {
+    if (!avltree_delete(&r->tree, r->storage->s + nod->key)) dienomem() ;
+    genalloc_s(node, &r->ga)[i] = *nod ;
+    if (!avltree_insert(&r->tree, i)) dienomem() ;
+  }
+  else repo_add(r, nod) ;
+}
diff --git a/src/config/tipidee-config-internal.h b/src/config/tipidee-config-internal.h
index 7ffee8a..190ac45 100644
--- a/src/config/tipidee-config-internal.h
+++ b/src/config/tipidee-config-internal.h
@@ -7,13 +7,16 @@
 #include <string.h>
 
 #include <skalibs/buffer.h>
+#include <skalibs/strerr.h>
 #include <skalibs/stralloc.h>
+#include <skalibs/genalloc.h>
 #include <skalibs/cdbmake.h>
+#include <skalibs/avltree.h>
 
 #define dienomem() strerr_diefu1sys(111, "stralloc_catb")
 
-typedef struct confnode_s confnode, *confnode_ref ;
-struct confnode_s
+typedef struct node_s node, *node_ref ;
+struct node_s
 {
   uint32_t key ;
   uint32_t keylen ;
@@ -22,7 +25,16 @@ struct confnode_s
   uint32_t filepos ;
   uint32_t line ;
 } ;
-#define CONFNODE_ZERO { .key = 0, .keylen = 0, .data = 0, .datalen = 0 }
+#define NODE_ZERO { .key = 0, .keylen = 0, .data = 0, .datalen = 0 }
+
+typedef struct repo_s repo, *repo_ref ;
+struct repo_s
+{
+  genalloc ga ;
+  avltree tree ;
+  stralloc *storage ;
+} ;
+#define REPO_ZERO { .ga = GENALLOC_ZERO, .tree = AVLTREE_ZERO, .storage = 0 }
 
 struct global_s
 {
@@ -33,20 +45,42 @@ struct global_s
 extern struct global_s g ;
 
 
- /* confnode */
+ /* node */
+
+extern void node_start (stralloc *, node *, char const *, size_t, uint32_t) ;
+extern void node_add (stralloc *, node *, char const *, size_t) ;
 
-extern void confnode_start (confnode *, char const *, size_t, uint32_t) ;
-extern void confnode_add (confnode *, char const *, size_t) ;
+
+ /* repo */
+
+extern void *node_dtok (uint32_t, void *) ;
+extern int node_cmp (void const *, void const *, void *) ;
+extern node const *repo_search (repo const *, char const *) ;
+extern void repo_add (repo *, node const *) ;
+extern void repo_update (repo *, node const *) ;
 
 
  /* conftree */
 
-extern confnode const *conftree_search (char const *) ;
-extern void conftree_add (confnode const *) ;
-extern void conftree_update (confnode const *) ;
+extern void confnode_start (node *, char const *, size_t, uint32_t) ;
+extern void confnode_add (node *, char const *, size_t) ;
+
+extern node const *conftree_search (char const *) ;
+extern void conftree_add (node const *) ;
+extern void conftree_update (node const *) ;
 extern int conftree_write (cdbmaker *) ;
 
 
+ /* headers */
+
+extern void header_start (node *, char const *, size_t, uint32_t) ;
+extern void header_add (node *, char const *, size_t) ;
+
+extern node const *headers_search (char const *) ;
+extern void headers_add (node const *) ;
+extern int headers_write (void) ;
+
+
  /* lexparse */
 
 extern void conf_lexparse (buffer *, char const *) ;
@@ -56,4 +90,11 @@ extern void conf_lexparse (buffer *, char const *) ;
 
 extern void conf_defaults (void) ;
 
+
+ /* headers */
+
+extern node const *headers_search (char const *) ;
+extern void headers_add (node const *) ;
+extern void headers_finish (void) ;
+
 #endif