diff options
author | sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> | 2020-08-25 01:27:16 +0200 |
---|---|---|
committer | sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> | 2020-08-25 01:28:28 +0200 |
commit | 5036866dcdbc0d7521f17d820ca9ca0e54057417 (patch) | |
tree | 466fda0f83a84af6e28167edca0b42401330320a | |
parent | df7d83614b0f902772153852d29227cb79ada37b (diff) |
feat(rss,atom): use SERVER_{NAME,PORT} for external URL
-rw-r--r-- | TODO | 3 | ||||
-rw-r--r-- | cgiutil.c | 13 | ||||
-rw-r--r-- | cgiutil.h | 4 | ||||
-rw-r--r-- | config.example.h | 13 | ||||
-rw-r--r-- | doc/man/man1/sternenblog.cgi.1 | 25 | ||||
-rw-r--r-- | main.c | 69 |
6 files changed, 85 insertions, 42 deletions
diff --git a/TODO b/TODO index a2e1b3c..1470d43 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ introduce better configuration mechanism | id:1f8f1bd3a99e6f394da4fbb508f1355759c2eeec rethink logging / debug output | id:5734e2717f28d80ac65d3f2123a0f46c01d4a0c1 -if possible determine BLOG_SERVER_URL automatically | id:5ad1fabab42a5776ab1446d09659c7a99a104245 support markdown markup via lowdown | id:90c72d144f21d78d1b56790a927f6eea52df2a2c templating: give all functions context (index or not, SCRIPT_NAME, entry struct if applicable) | id:930c2e4f2c4c9ec3efef87b6ddd50a6ba6dd4903 add tests for timeutil | id:b6cd0665e5c376f52cf9702698330308aa722031 @@ -9,4 +8,4 @@ improve man3 situation | id:bf918930f6361c30f288c42894cbf9b0541c3340 use errno instead of returning HTTP status codes in make_index | id:bfa35c48dcedbecd3a951eba243c4840178289ed templating: unify single_entry and index_entry | id:e6dcb195d890bb0b5186716808d806101d81e4f6 sandboxing, at least chroot | id:f209aec893fe310c8276f64f6ff4a3208a2a4f28 -document timeutil and stringutil | id:fd05b97115848a8b22231a477b8252373bd87264 +document cgiutil, timeutil and stringutil | id:fd05b97115848a8b22231a477b8252373bd87264 diff --git a/cgiutil.c b/cgiutil.c index 135028d..7dc8114 100644 --- a/cgiutil.c +++ b/cgiutil.c @@ -115,3 +115,16 @@ int urlencode_realloc(char **input, int size) { return output_size; } + +char *server_url(bool https) { + char *server_name = getenv("SERVER_NAME"); + char *server_port = getenv("SERVER_PORT"); + + if(server_name == NULL || server_port == NULL) { + return NULL; + } + + char *proto = https ? "https://" : "http://"; + + return catn_alloc(4, proto, server_name, ":", server_port); +} diff --git a/cgiutil.h b/cgiutil.h index 4701e83..b90b895 100644 --- a/cgiutil.h +++ b/cgiutil.h @@ -3,6 +3,8 @@ * @brief Simple CGI/HTTP helper functions used by sternenblog. */ +#include <stdbool.h> + /*! * @brief Print a HTTP header * @@ -89,3 +91,5 @@ int http_errno(int err); * @return -1 on error, else size of buffer */ int urlencode_realloc(char **input, int size); + +char *server_url(bool https); diff --git a/config.example.h b/config.example.h index b089d21..372d266 100644 --- a/config.example.h +++ b/config.example.h @@ -18,14 +18,15 @@ #define BLOG_DIR "/srv/sternenblog/" /*! - * @brief Public URL of the webserver + * @brief Wether the server uses https for sternenblog * - * (External) URL the server that the script is running on is reachable via. - * This should be the base URL of your webserver without any trailing slashes - * or subpaths, as sternenblog will use SCRIPT_NAME and PATH_INFO to figure - * out the path from the root of the server. + * sternenblog will use `BLOG_USE_HTTPS` when building an external + * URL to pages: If it is `1`, it will use `https://` as prefix, otherwise + * `http://`. + * + * This is currently only relevant for the RSS and Atom feeds. */ -#define BLOG_SERVER_URL "http://localhost" +#define BLOG_USE_HTTPS 1 /*! * @brief Enable / Disable strict access check diff --git a/doc/man/man1/sternenblog.cgi.1 b/doc/man/man1/sternenblog.cgi.1 index d0af4e3..5cab28e 100644 --- a/doc/man/man1/sternenblog.cgi.1 +++ b/doc/man/man1/sternenblog.cgi.1 @@ -65,14 +65,18 @@ although the latter, i. e. an absolute path, is recommended. .Pp Default value is .Pa /srv/sternenblog/ . -.It Sy BLOG_SERVER_URL -Public URL of the root of the server, i. e. public address or hostname and used -protocol. -This is used to construct global URLs to the entries for the RSS and Atom -feeds. +.It Sy BLOG_USE_HTTPS +Wether the webserver is configured to serve +.Nm +via https or not. +This information will be used whe building external URLs to entries in the RSS +and Atom feeds to decide wether to prefix the URL with +.Ql https:// +or +.Ql http:// . .Pp Default value is -.Ql http://localhost . +.Ql 1 . .It Sy BLOG_STRICT_ACCESS If set to .Ql 1 @@ -254,6 +258,15 @@ Is expected to be the URL part after i. e. the path to a subpage of the script. This is interpreted as a path to an entry and must start with a leading .Ql / . +.It Ev SERVER_NAME +Expected to be the hostname or IP(v6) address of the server the request is +directed to or +.Nm +is running on. +.It Ev SERVER_PORT +Expected to be the port of the server the request is directed to or +.Nm +is running on. .El .Pp Most likely these will be set correctly by your favorite web server without diff --git a/main.c b/main.c index f5460ba..b9b6914 100644 --- a/main.c +++ b/main.c @@ -267,9 +267,6 @@ void blog_rss(char script_name[]) { struct xml_context ctx; new_xml_context(&ctx); - ctx.closing_slash = 1; - ctx.warn = stderr; // dev - xml_raw(&ctx, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); xml_open_tag_attrs(&ctx, "rss", 2, "version", "2.0", "xmlns:atom", "http://www.w3.org/2005/Atom"); xml_open_tag(&ctx, "channel"); @@ -284,9 +281,14 @@ void blog_rss(char script_name[]) { xml_close_cdata(&ctx); xml_close_tag(&ctx, "description"); - xml_open_tag(&ctx, "link"); - xml_escaped(&ctx, BLOG_SERVER_URL); - xml_close_tag(&ctx, "link"); + char *external_url = server_url(BLOG_USE_HTTPS); + + if(external_url != NULL) { + xml_open_tag(&ctx, "link"); + xml_escaped(&ctx, external_url); + xml_escaped(&ctx, script_name); + xml_close_tag(&ctx, "link"); + } if(count > 0) { time_t update_time = entries[0].time; @@ -299,13 +301,15 @@ void blog_rss(char script_name[]) { } } - char *rss_link = catn_alloc(3, BLOG_SERVER_URL, script_name, "/rss.xml"); - if(rss_link != NULL) { - xml_empty_tag(&ctx, "atom:link", 3, - "rel", "self", - "href", rss_link, - "type", "application/rss+xml"); - free(rss_link); + if(external_url != NULL) { + char *rss_link = catn_alloc(3, external_url, script_name, "/rss.xml"); + if(rss_link != NULL) { + xml_empty_tag(&ctx, "atom:link", 3, + "rel", "self", + "href", rss_link, + "type", "application/rss+xml"); + free(rss_link); + } } for(int i = 0; i < count; i++) { @@ -315,15 +319,17 @@ void blog_rss(char script_name[]) { xml_escaped(&ctx, entries[i].title); xml_close_tag(&ctx, "title"); - xml_open_tag(&ctx, "link"); - xml_escaped(&ctx, BLOG_SERVER_URL); - xml_escaped(&ctx, entries[i].link); - xml_close_tag(&ctx, "link"); + if(external_url != NULL) { + xml_open_tag(&ctx, "link"); + xml_escaped(&ctx, external_url); + xml_escaped(&ctx, entries[i].link); + xml_close_tag(&ctx, "link"); - xml_open_tag(&ctx, "guid"); - xml_escaped(&ctx, BLOG_SERVER_URL); - xml_escaped(&ctx, entries[i].link); - xml_close_tag(&ctx, "guid"); + xml_open_tag(&ctx, "guid"); + xml_escaped(&ctx, external_url); + xml_escaped(&ctx, entries[i].link); + xml_close_tag(&ctx, "guid"); + } if(entries[i].text_size > 0) { xml_open_tag(&ctx, "description"); @@ -349,6 +355,8 @@ void blog_rss(char script_name[]) { xml_close_all(&ctx); + free(external_url); + free_index(&entries, count); del_xml_context(&ctx); @@ -371,8 +379,9 @@ void blog_atom(char script_name[]) { struct xml_context ctx; new_xml_context(&ctx); - char *self_url = catn_alloc(3, BLOG_SERVER_URL, script_name, "/atom.xml"); - char *html_url = catn_alloc(2, BLOG_SERVER_URL, script_name); + char *external_url = server_url(BLOG_USE_HTTPS); + char *self_url = catn_alloc(3, external_url, script_name, "/atom.xml"); + char *html_url = catn_alloc(2, external_url, script_name); send_standard_headers(200, "application/atom+xml"); @@ -432,10 +441,12 @@ void blog_atom(char script_name[]) { if(entry_get_text(&entries[i]) != -1) { xml_open_tag(&ctx, "entry"); - xml_open_tag(&ctx, "id"); - xml_escaped(&ctx, BLOG_SERVER_URL); - xml_escaped(&ctx, entries[i].link); - xml_close_tag(&ctx, "id"); + if(external_url != NULL) { + xml_open_tag(&ctx, "id"); + xml_escaped(&ctx, external_url); + xml_escaped(&ctx, entries[i].link); + xml_close_tag(&ctx, "id"); + } xml_open_tag(&ctx, "title"); xml_escaped(&ctx, entries[i].title); @@ -448,7 +459,7 @@ void blog_atom(char script_name[]) { xml_close_tag(&ctx, "updated"); } - char *entry_url = catn_alloc(2, BLOG_SERVER_URL, entries[i].link); + char *entry_url = catn_alloc(2, external_url, entries[i].link); if(entry_url != NULL) { xml_empty_tag(&ctx, "link", 3, "rel", "alternate", "type", "text/html", "href", entry_url); free(entry_url); @@ -468,6 +479,8 @@ void blog_atom(char script_name[]) { xml_close_tag(&ctx, "feed"); + free(external_url); + free_index(&entries, count); del_xml_context(&ctx); } |