diff options
author | sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> | 2021-04-15 01:17:01 +0200 |
---|---|---|
committer | sternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org> | 2021-04-15 01:19:29 +0200 |
commit | b97c3c15b11e1a761d77b37b0a811aa3090cc75a (patch) | |
tree | a67d0a532284ba6f491af8ceb558226e8ec3d2d3 | |
parent | 4962546af302635075869d170465d3c4dad2b551 (diff) |
feat(main): remove RSS feed functionality
This purpose is served with the atom feed already and allows us to simplify the routing logic a bit (this should be reduced further).
-rw-r--r-- | core.h | 13 | ||||
-rw-r--r-- | main.c | 203 | ||||
-rw-r--r-- | templates/simple.c | 11 |
3 files changed, 44 insertions, 183 deletions
diff --git a/core.h b/core.h index 8f317ec..81804fa 100644 --- a/core.h +++ b/core.h @@ -37,17 +37,22 @@ struct entry { }; /*! - * @brief Type of a HTML response + * @brief Supported response types * - * This enum describes the three types of HTML responses sternenblog can - * generate: a single entry, an index and an error page. + * This enum describes the types of responses sternenblog can generate: + * + * * a single entry + * * an index + * * an error page + * * a (atom) feed * * Used internally for routing and passed to the template for context. */ enum page_type { PAGE_TYPE_ENTRY, PAGE_TYPE_INDEX, - PAGE_TYPE_ERROR + PAGE_TYPE_ERROR, + PAGE_TYPE_FEED, }; #endif diff --git a/main.c b/main.c index d58ddc9..0c8378c 100644 --- a/main.c +++ b/main.c @@ -41,7 +41,7 @@ * Its usage and features are documented in xml.h. * * sternenblog uses xml.h internally for generating its - * rss feed and its default template. + * atom feed and its default template. * * @subsection cgi_doc CGI Helpers * @@ -55,7 +55,7 @@ * from index.h or gets a single one using `make_entry()` from entry.h * and serves the result to the user. * - * This happens either via a RSS feed generated using `blog_rss()` + * This happens either via an atom feed generated using `blog_atom()` * or as html pages which are generated by `blog_index()` and * `blog_entry()` calling the template functions defined in template.h. * @@ -97,20 +97,6 @@ #include "xml.h" /*! - * @brief Routing enum to differentiate feeds - * - * Used in routing to differentiate between - * - * * Feeds (RSS vs. Atom) - * * Feeds and non-feeds (feeds are a special type of `PAGE_TYPE_INDEX`) - */ -enum feed_type { - FEED_TYPE_NONE, - FEED_TYPE_RSS, - FEED_TYPE_ATOM -}; - -/*! * @brief Send terminated default header compound * * Sends `Status` and `Content-type` headers for the given @@ -120,22 +106,11 @@ enum feed_type { void send_standard_headers(int status, char content_type[]); /*! - * @brief Outputs the CGI response for the blog's RSS feed - * - * This function is called if `PATH_INFO` is `/rss.xml`. - * - * @see make_index - * @see blog_atom - */ -void blog_rss(char script_name[], struct entry *entries, int count); - -/*! * @brief Outputs the CGI response for the blog's Atom feed * * This function is called if `PATH_INFO` is `/atom.xml`. * * @see make_index - * @see blog_rss */ void blog_atom(char script_name[], struct entry *entries, int count); @@ -143,7 +118,6 @@ void blog_atom(char script_name[], struct entry *entries, int count); * @brief Implements routing of requests * * @see blog_index - * @see blog_rss * @see blog_entry */ int main(void) { @@ -151,7 +125,6 @@ int main(void) { char *script_name = getenv("SCRIPT_NAME"); enum page_type page_type; - enum feed_type is_feed = FEED_TYPE_NONE; struct entry *entries = NULL; int count = 0; @@ -167,12 +140,8 @@ int main(void) { setenv("PATH_INFO", "/", 1); page_type = PAGE_TYPE_INDEX; - } else if(strcmp(path_info, "/rss.xml") == 0) { - is_feed = FEED_TYPE_RSS; - page_type = PAGE_TYPE_INDEX; } else if(strcmp(path_info, "/atom.xml") == 0) { - page_type = PAGE_TYPE_INDEX; - is_feed = FEED_TYPE_ATOM; + page_type = PAGE_TYPE_FEED; } else { // single entry is just a special index entries = malloc(sizeof(struct entry)); @@ -190,18 +159,14 @@ int main(void) { } } - // confirm index is allocated if we are serving a feed - assert(is_feed == FEED_TYPE_NONE || page_type == PAGE_TYPE_INDEX); - // construct index for feeds and index page - if(page_type == PAGE_TYPE_INDEX) { + if(page_type == PAGE_TYPE_INDEX || page_type == PAGE_TYPE_FEED) { count = make_index(BLOG_DIR, script_name, 0, &entries); if(count < 0) { page_type = PAGE_TYPE_ERROR; status = 500; } else { - page_type = PAGE_TYPE_INDEX; status = 200; } } @@ -234,37 +199,39 @@ int main(void) { assert(page_type != PAGE_TYPE_ENTRY || data.entry != NULL); // render response - if(page_type == PAGE_TYPE_ERROR) { - send_standard_headers(status, "text/html"); - - template_header(data); - template_main(data); - template_footer(data); - } else if(is_feed == FEED_TYPE_NONE) { - // either PAGE_TYPE_INDEX or PAGE_TYPE_ENTRY - send_standard_headers(200, "text/html"); - - template_header(data); - - // confirm that PAGE_TYPE_ENTRY → count == 1 - assert(page_type != PAGE_TYPE_ENTRY || count == 1); - - for(int i = 0; i < count; i++) { - if(entries[i].text != NULL || entry_get_text(&entries[i]) != -1) { - data.entry = &entries[i]; - assert(data.entry != NULL); - - template_main(data); - - entry_unget_text(&entries[i]); + switch(page_type) { + case PAGE_TYPE_ERROR: + send_standard_headers(status, "text/html"); + + template_header(data); + template_main(data); + template_footer(data); + break; + case PAGE_TYPE_FEED: + blog_atom(script_name, entries, count); + break; + case PAGE_TYPE_INDEX: + case PAGE_TYPE_ENTRY: + send_standard_headers(200, "text/html"); + + template_header(data); + + // confirm that PAGE_TYPE_ENTRY → count == 1 + assert(page_type != PAGE_TYPE_ENTRY || count == 1); + + for(int i = 0; i < count; i++) { + if(entries[i].text != NULL || entry_get_text(&entries[i]) != -1) { + data.entry = &entries[i]; + assert(data.entry != NULL); + + template_main(data); + + entry_unget_text(&entries[i]); + } } - } - template_footer(data); - } else if(is_feed == FEED_TYPE_RSS) { - blog_rss(script_name, entries, count); - } else if(is_feed == FEED_TYPE_ATOM) { - blog_atom(script_name, entries, count); + template_footer(data); + break; } // clean up @@ -295,106 +262,6 @@ void send_standard_headers(int status, char content_type[]) { terminate_headers(); } -void blog_rss(char script_name[], struct entry *entries, int count) { - send_standard_headers(200, "application/rss+xml"); - - struct xml_context ctx; - new_xml_context(&ctx); - - 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"); - - xml_open_tag(&ctx, "title"); - xml_escaped(&ctx, BLOG_TITLE); - xml_close_tag(&ctx, "title"); - - xml_open_tag(&ctx, "description"); - xml_open_cdata(&ctx); - xml_raw(&ctx, BLOG_DESCRIPTION); - xml_close_cdata(&ctx); - xml_close_tag(&ctx, "description"); - - char *external_url = server_url(BLOG_USE_HTTPS); - - xml_open_tag(&ctx, "link"); - if(external_url != NULL) { - xml_escaped(&ctx, external_url); - } - xml_escaped(&ctx, script_name); - xml_escaped(&ctx, "/"); - xml_close_tag(&ctx, "link"); - - if(count > 0) { - time_t update_time = entries[0].time; - char strtime_update[MAX_TIMESTR_SIZE]; - - if(flocaltime(strtime_update, RSS_TIME_FORMAT, MAX_TIMESTR_SIZE, &update_time) > 0) { - xml_open_tag(&ctx, "lastBuildDate"); - xml_escaped(&ctx, strtime_update); - xml_close_tag(&ctx, "lastBuildDate"); - } - } - - 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++) { - if(entry_get_text(&entries[i]) != -1) { - xml_open_tag(&ctx, "item"); - xml_open_tag(&ctx, "title"); - xml_escaped(&ctx, entries[i].title); - xml_close_tag(&ctx, "title"); - - xml_open_tag(&ctx, "link"); - if(external_url != NULL) { - xml_escaped(&ctx, external_url); - } - xml_escaped(&ctx, entries[i].link); - xml_close_tag(&ctx, "link"); - - xml_open_tag(&ctx, "guid"); - if(external_url != NULL) { - 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"); - xml_open_cdata(&ctx); - xml_raw(&ctx, entries[i].text); - xml_close_cdata(&ctx); - xml_close_tag(&ctx, "description"); - } - - char strtime_entry[MAX_TIMESTR_SIZE]; - - if(flocaltime(strtime_entry, RSS_TIME_FORMAT, MAX_TIMESTR_SIZE, &entries[i].time) > 0) { - xml_open_tag(&ctx, "pubDate"); - xml_escaped(&ctx, strtime_entry); - xml_close_tag(&ctx, "pubDate"); - } - - xml_close_tag(&ctx, "item"); - - entry_unget_text(&entries[i]); - } - } - - xml_close_all(&ctx); - - free(external_url); - - del_xml_context(&ctx); -} - void blog_atom(char script_name[], struct entry *entries, int count) { struct xml_context ctx; new_xml_context(&ctx); diff --git a/templates/simple.c b/templates/simple.c index 27fe683..f1f8bdf 100644 --- a/templates/simple.c +++ b/templates/simple.c @@ -78,20 +78,9 @@ void template_footer(struct template_data data) { xml_open_tag(&ctx, "footer"); - char *rss_link = catn_alloc(2, data.script_name, "/rss.xml"); char *atom_link = catn_alloc(2, data.script_name, "/atom.xml"); - if(rss_link != NULL) { - xml_open_tag_attrs(&ctx, "a", 1, "href", rss_link); - xml_escaped(&ctx, "RSS Feed"); - xml_close_tag(&ctx, "a"); - - free(rss_link); - } - if(atom_link != NULL) { - xml_raw(&ctx, " • "); - xml_open_tag_attrs(&ctx, "a", 1, "href", atom_link); xml_escaped(&ctx, "Atom Feed"); xml_close_tag(&ctx, "a"); |