summary refs log tree commit diff
path: root/search.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--search.c39
1 files changed, 21 insertions, 18 deletions
diff --git a/search.c b/search.c
index c284732..67ce79c 100644
--- a/search.c
+++ b/search.c
@@ -44,15 +44,15 @@ const char *SearchQuery = SQL(
 	LIMIT :offset, :limit;
 );
 
-static char *offsetURL(struct Scope scope, int64_t offset) {
-	const char *networkKey = (scope.network ? Keys[Network].name : NULL);
-	const char *contextKey = (scope.context ? Keys[Context].name : NULL);
+static char *offsetURL(
+	const char *network, const char *context, const char *query, int64_t offset
+) {
 	char *url = khttp_urlpartx(
 		NULL, NULL, Pages[Search],
-		Keys[Query].name, KATTRX_STRING, scope.query,
+		Keys[Query].name, KATTRX_STRING, query,
 		Keys[Offset].name, KATTRX_INT, offset,
-		networkKey, KATTRX_STRING, scope.network,
-		contextKey, KATTRX_STRING, scope.context,
+		(network ? Keys[Network].name : NULL), KATTRX_STRING, network,
+		(context ? Keys[Context].name : NULL), KATTRX_STRING, context,
 		NULL
 	);
 	if (!url) err(EX_OSERR, "khttp_urlpartx");
@@ -60,12 +60,15 @@ static char *offsetURL(struct Scope scope, int64_t offset) {
 }
 
 enum kcgi_err searchPage(struct kreq *req) {
-	struct Scope scope = pageScope(req);
-	if (!scope.query) return httpFail(req, KHTTP_400);
-	if (scope.context && !scope.network) return httpFail(req, KHTTP_400);
-
+	if (!req->fieldmap[Query]) return httpFail(req, KHTTP_400);
+	const char *query = req->fieldmap[Query]->parsed.s;
+	const char *network = NULL;
+	const char *context = NULL;
 	int64_t offset = 0;
+	if (req->fieldmap[Network]) network = req->fieldmap[Network]->parsed.s;
+	if (req->fieldmap[Context]) context = req->fieldmap[Context]->parsed.s;
 	if (req->fieldmap[Offset]) offset = req->fieldmap[Offset]->parsed.i;
+	if (context && !network) return httpFail(req, KHTTP_400);
 
 	enum kcgi_err error = 0
 		|| httpHead(req, KHTTP_200, KMIME_TEXT_HTML)
@@ -75,14 +78,14 @@ enum kcgi_err searchPage(struct kreq *req) {
 	struct khtmlreq html;
 	error = error
 		|| khtml_open(&html, req, 0)
-		|| htmlHead(&html, scope.query)
-		|| htmlNav(&html, scope)
+		|| htmlHead(&html, query)
+		|| htmlNav(&html, req)
 		|| khtml_elem(&html, KELEM_TABLE);
 	if (error) return error;
 
 	if (offset) {
 		int64_t prev = offset - eventsLimit;
-		char *href = offsetURL(scope, (prev > 0 ? prev : 0));
+		char *href = offsetURL(network, context, query, (prev > 0 ? prev : 0));
 		error = 0
 			|| khtml_attr(&html, KELEM_TR, KATTR_CLASS, "page", KATTR__MAX)
 			|| khtml_attr(&html, KELEM_TH, KATTR_COLSPAN, "5", KATTR__MAX)
@@ -95,9 +98,9 @@ enum kcgi_err searchPage(struct kreq *req) {
 
 	sqlite3_reset(stmt.search);
 	dbBindText(stmt.search, ":highlight", "\26");
-	dbBindText(stmt.search, ":network", scope.network);
-	dbBindText(stmt.search, ":context", scope.context);
-	dbBindText(stmt.search, ":query", scope.query);
+	dbBindText(stmt.search, ":network", network);
+	dbBindText(stmt.search, ":context", context);
+	dbBindText(stmt.search, ":query", query);
 	dbBindInt(stmt.search, ":public", contextsPublic);
 	dbBindInt(stmt.search, ":limit", eventsLimit);
 	dbBindInt(stmt.search, ":offset", offset);
@@ -117,7 +120,7 @@ enum kcgi_err searchPage(struct kreq *req) {
 		event.host = sqlite3_column_text(stmt.search, i++);
 		event.target = sqlite3_column_text(stmt.search, i++);
 		event.message = sqlite3_column_text(stmt.search, i++);
-		error = htmlEvent(&html, scope, event);
+		error = htmlEvent(&html, req, &event);
 		if (error) return error;
 	}
 	if (result != SQLITE_DONE) {
@@ -130,7 +133,7 @@ enum kcgi_err searchPage(struct kreq *req) {
 	}
 
 	if (rows == eventsLimit) {
-		char *href = offsetURL(scope, offset + eventsLimit);
+		char *href = offsetURL(network, context, query, offset + eventsLimit);
 		error = 0
 			|| khtml_attr(&html, KELEM_TR, KATTR_CLASS, "page", KATTR__MAX)
 			|| khtml_attr(&html, KELEM_TH, KATTR_COLSPAN, "5", KATTR__MAX)