changeset 1065:6253a5651a47

static: make URI prefix configurable This changes the definition of the base-url config knob to be a list instead of a cons. The list should have one or two items: the scheme+host+port portion and the URI prefix portion. The migration is simple - just replace: (base-url . "http://example.com") With: (base-url "http://example.com") That is, the cons cell turned into a list. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Thu, 13 Aug 2020 11:22:19 -0400
parents 1e083700f98c
children 6323615ed193
files config.c config.h.in lisplint.c samples/full.lisp static.c
diffstat 5 files changed, 56 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/config.c	Sat Oct 24 08:24:22 2020 -0400
+++ b/config.c	Thu Aug 13 11:22:19 2020 -0400
@@ -32,6 +32,7 @@
 struct config config;
 
 static struct str exampledotcom = STR_STATIC_INITIALIZER("example.com");
+static struct str slash = STR_STATIC_INITIALIZER("/");
 
 static void config_load_u64(struct val *lv, const char *vname,
 			    uint64_t *ret, uint64_t def)
@@ -110,6 +111,43 @@
 		config.scgi_threads = tmp;
 }
 
+static void assemble_base_url(void)
+{
+	struct str *uri_prefix;
+	struct str *base_url;
+	struct val *va, *vb;
+
+	va = sexpr_nth(val_getref(config.base_url_parts), 1);
+	vb = sexpr_nth(val_getref(config.base_url_parts), 2);
+
+	/* we accept only strings */
+	if (va && (va->type != VT_STR)) {
+		val_putref(va);
+		va = NULL;
+	}
+
+	if (vb && (vb->type != VT_STR)) {
+		val_putref(vb);
+		vb = NULL;
+	}
+
+	/* we need *something* for the base URL */
+	va = va ? va : str_getref_val(&exampledotcom);
+
+	if (!vb) {
+		/* host only */
+		uri_prefix = str_getref(&slash);
+		base_url = val_cast_to_str(va);
+	} else {
+		/* host + prefix */
+		uri_prefix = val_getref_str(vb);
+		base_url = str_cat(2, va, vb);
+	}
+
+	config.base_url = base_url;
+	config.uri_prefix = uri_prefix;
+}
+
 int config_load(const char *fname)
 {
 	struct val *lv;
@@ -149,7 +187,7 @@
 	config_load_str(lv, CONFIG_DATA_DIR, &config.data_dir, DEFAULT_DATA_DIR);
 	config_load_str(lv, CONFIG_THEME_DIR, &config.theme_dir,
 			DEFAULT_THEME_DIR);
-	config_load_url(lv, CONFIG_BASE_URL, &config.base_url);
+	config_load_list(lv, CONFIG_BASE_URL, &config.base_url_parts);
 	config_load_url(lv, CONFIG_BUG_BASE_URL, &config.bug_base_url);
 	config_load_url(lv, CONFIG_WIKI_BASE_URL, &config.wiki_base_url);
 	config_load_url(lv, CONFIG_PHOTO_BASE_URL, &config.photo_base_url);
@@ -168,6 +206,9 @@
 
 	val_putref(lv);
 
+	/* convert the raw base url parts into something easy to use */
+	assemble_base_url();
+
 	DBG("config.scgi_host = %s", str_cstr(config.scgi_host));
 	DBG("config.scgi_port = %u", config.scgi_port);
 	DBG("config.scgi_threads = %d", config.scgi_threads);
@@ -180,6 +221,7 @@
 	DBG("config.data_dir = %s", str_cstr(config.data_dir));
 	DBG("config.theme_dir = %s", str_cstr(config.theme_dir));
 	DBG("config.base_url = %s", str_cstr(config.base_url));
+	DBG("config.uri_prefix = %s", str_cstr(config.uri_prefix));
 	DBG("config.wiki_base_url = %s", str_cstr(config.wiki_base_url));
 	DBG("config.bug_base_url = %s", str_cstr(config.bug_base_url));
 	DBG("config.photo_base_url = %s", str_cstr(config.photo_base_url));
--- a/config.h.in	Sat Oct 24 08:24:22 2020 -0400
+++ b/config.h.in	Thu Aug 13 11:22:19 2020 -0400
@@ -93,7 +93,9 @@
 	uint64_t comment_captcha_b;
 	struct str *data_dir;
 	struct str *theme_dir;
-	struct str *base_url;
+	struct val *base_url_parts; /* raw from config */
+	struct str *base_url; /* pre-processed, full url */
+	struct str *uri_prefix; /* pre-processed, prefix */
 	struct str *bug_base_url;
 	struct str *wiki_base_url;
 	struct str *photo_base_url;
--- a/lisplint.c	Sat Oct 24 08:24:22 2020 -0400
+++ b/lisplint.c	Thu Aug 13 11:22:19 2020 -0400
@@ -141,7 +141,7 @@
 	ret = check_type(fname, lv, CONFIG_COMMENT_CAPTCHA_B, VT_INT, false) && ret;
 	ret = check_type(fname, lv, CONFIG_DATA_DIR, VT_STR, false) && ret;
 	ret = check_type(fname, lv, CONFIG_THEME_DIR, VT_STR, false) && ret;
-	ret = check_type(fname, lv, CONFIG_BASE_URL, VT_STR, false) && ret;
+	ret = check_type(fname, lv, CONFIG_BASE_URL, VT_CONS, false) && ret;
 	ret = check_type(fname, lv, CONFIG_BUG_BASE_URL, VT_STR, false) && ret;
 	ret = check_type(fname, lv, CONFIG_WIKI_BASE_URL, VT_STR, false) && ret;
 	ret = check_type(fname, lv, CONFIG_PHOTO_BASE_URL, VT_STR, false) && ret;
--- a/samples/full.lisp	Sat Oct 24 08:24:22 2020 -0400
+++ b/samples/full.lisp	Thu Aug 13 11:22:19 2020 -0400
@@ -32,8 +32,12 @@
  ; The location of the theme directory.  Default is "./theme".
  (theme-dir . "/blahg/theme")
 
- ; The base URL for the blahg.  This is the URL for the index page.
- (base-url . "http://blahg.example.com")
+ ; The base URL for the blahg.  This is the URL for the index page.  It is
+ ; split into two parts: the scheme+host+port, and the path.  The actual URL
+ ; used is the concatenation of the two strings.  The path must contain a
+ ; leading and trailing slash.  If the path is omitted, it is assumed to be
+ ; "/".
+ (base-url "http://blahg.example.com" "/blahg/")
 
  ; The base URL for the \bug fmt3 command.
  (bug-base-url . "http://bugs.example.com")
--- a/static.c	Sat Oct 24 08:24:22 2020 -0400
+++ b/static.c	Thu Aug 13 11:22:19 2020 -0400
@@ -25,9 +25,6 @@
 #include "static.h"
 #include "utils.h"
 
-/* must include trailing '/' */
-#define URI_PREFIX	"/"
-
 struct uri_info {
 	const char *uri;
 	const char *content_type;	/* required for URI_STATIC */
@@ -53,11 +50,12 @@
 		return NULL;
 
 	/* all URIs must start with the proper prefix */
-	if (strncmp(URI_PREFIX, path, strlen(URI_PREFIX)))
+	if (strncmp(str_cstr(config.uri_prefix), path,
+		    str_len(config.uri_prefix)))
 		return NULL;
 
 	/* strip the prefix from the uri */
-	path += strlen(URI_PREFIX);
+	path += str_len(config.uri_prefix);
 
 	for (i = 0; i < ARRAY_LEN(safe_uris); i++) {
 		if (!strcmp(safe_uris[i].uri, path))