changeset 937:8e77b198735d

req: use 'feed' directly from the query nvlist This results in the feed format struct req member turning into a struct str pointer. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Thu, 07 Sep 2017 17:51:07 +0300
parents 6bfdf28a2f5d
children 99688032e2e9
files render.c req.c req.h
diffstat 3 files changed, 27 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/render.c	Thu Sep 07 17:38:02 2017 +0300
+++ b/render.c	Thu Sep 07 17:51:07 2017 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2016 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2013-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -64,7 +64,8 @@
 	struct str *raw;
 	char *out;
 
-	snprintf(path, sizeof(path), "templates/%s/%s.tmpl", req->fmt, tmpl);
+	snprintf(path, sizeof(path), "templates/%s/%s.tmpl", str_cstr(req->fmt),
+		 tmpl);
 
 	raw = file_cache_get_cb(path, revalidate_all_posts, NULL);
 	if (!raw)
--- a/req.c	Thu Sep 07 17:38:02 2017 +0300
+++ b/req.c	Thu Sep 07 17:51:07 2017 +0300
@@ -152,7 +152,7 @@
 	nvl_set_nvl(tmp, "headers", nvl_getref(scgi->request.headers));
 	nvl_set_nvl(tmp, "query", nvl_getref(scgi->request.query));
 	nvl_set_str(tmp, "body", STR_DUP(scgi->request.body));
-	nvl_set_str(tmp, "fmt", STR_DUP(req->fmt));
+	nvl_set_str(tmp, "fmt", str_getref(req->fmt));
 	nvl_set_int(tmp, "file-descriptor", scgi->fd);
 	nvl_set_int(tmp, "thread-id", (uint64_t) pthread_self());
 	nvl_set_nvl(logentry, "request", tmp);
@@ -222,6 +222,8 @@
 {
 	log_request(req);
 
+	str_putref(req->fmt);
+
 	vars_destroy(&req->vars);
 
 	set_session(0);
@@ -253,7 +255,6 @@
 	args->page = PAGE_INDEX;
 	args->cat = NULL;
 	args->tag = NULL;
-	args->feed = NULL;
 
 	uri = nvl_lookup_str(req->scgi->request.headers, SCGI_DOCUMENT_URI);
 	ASSERT(!IS_ERR(uri));
@@ -291,7 +292,7 @@
 		} else if (!strcmp(name, "tag")) {
 			cptr = &args->tag;
 		} else if (!strcmp(name, "feed")) {
-			cptr = &args->feed;
+			continue;
 		} else if (!strcmp(name, "comment")) {
 			continue;
 		} else if (!strcmp(name, "preview")) {
@@ -336,25 +337,34 @@
  */
 static bool switch_content_type(struct req *req)
 {
-	const char *fmt = req->args.feed;
+	struct str *fmt;
 	int page = req->args.page;
 
 	const char *content_type;
 	int index_stories;
 
+	fmt = nvl_lookup_str(req->scgi->request.query, "feed");
+	if (IS_ERR(fmt)) {
+		if (PTR_ERR(fmt) != -ENOENT)
+			return false; /* internal error */
+
+		fmt = NULL;
+	}
+
 	if (!fmt) {
 		/* no feed => OK, use html */
-		fmt = "html";
+		fmt = STATIC_STR("html");
 		content_type = "text/html";
 		index_stories = config.html_index_stories;
-	} else if (!strcmp(fmt, "atom")) {
+	} else if (!strcmp(str_cstr(fmt), "atom")) {
 		content_type = "application/atom+xml";
 		index_stories = config.feed_index_stories;
-	} else if (!strcmp(fmt, "rss2")) {
+	} else if (!strcmp(str_cstr(fmt), "rss2")) {
 		content_type = "application/rss+xml";
 		index_stories = config.feed_index_stories;
 	} else {
 		/* unsupported feed type */
+		str_putref(fmt);
 		return false;
 	}
 
@@ -365,13 +375,15 @@
 
 		/* for everything else, we have only HTML */
 		default:
-			if (strcmp(fmt, "html"))
+			if (strcmp(str_cstr(fmt), "html")) {
+				str_putref(fmt);
 				return false;
+			}
 			break;
 	}
 
 	/* let the template engine know */
-	req->fmt = fmt;
+	req->fmt = fmt; /* pass along the reference gotten from lookup */
 
 	/* let the client know */
 	req_head(req, "Content-Type", content_type);
@@ -444,7 +456,7 @@
 	req_head(req, "Content-Type", "text/html");
 
 	req->scgi->response.status = SCGI_STATUS_NOTFOUND;
-	req->fmt = "html";
+	req->fmt = STATIC_STR("html");
 
 	vars_scope_push(&req->vars);
 
@@ -463,7 +475,7 @@
 	req_head(req, "Location", url);
 
 	req->scgi->response.status = SCGI_STATUS_REDIRECT;
-	req->fmt = "html";
+	req->fmt = STATIC_STR("html");
 
 	vars_scope_push(&req->vars);
 
--- a/req.h	Thu Sep 07 17:38:02 2017 +0300
+++ b/req.h	Thu Sep 07 17:51:07 2017 +0300
@@ -46,7 +46,6 @@
 
 	const char *cat;
 	const char *tag;
-	const char *feed;
 };
 
 struct req {
@@ -58,7 +57,7 @@
 	/* state */
 	struct vars vars;
 
-	const char *fmt;	/* format (e.g., "html") */
+	struct str *fmt;	/* format (e.g., "html") */
 
 	struct {
 		int index_stories;