From 7820a26ea52032d0330456ae02d4044b357144de Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Wed, 15 Mar 2017 10:17:29 +0000 Subject: It's annoying that people keep writing URIs including redundant parts like "/OpenBSD-current/manN/". To discourage that, let man.cgi(8) redirect search form results to nice, concise URIs. --- cgi.c | 28 +++++++++++++++++++++++++--- man.cgi.3 | 24 +++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/cgi.c b/cgi.c index af56da27..05a861d4 100644 --- a/cgi.c +++ b/cgi.c @@ -1,4 +1,4 @@ -/* $Id: cgi.c,v 1.148 2017/02/22 16:20:01 schwarze Exp $ */ +/* $Id: cgi.c,v 1.149 2017/03/15 10:17:29 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2014, 2015, 2016, 2017 Ingo Schwarze @@ -76,6 +76,7 @@ static void pg_error_badrequest(const char *); static void pg_error_internal(void); static void pg_index(const struct req *); static void pg_noresult(const struct req *, const char *); +static void pg_redirect(const struct req *, const char *); static void pg_search(const struct req *); static void pg_searchres(const struct req *, struct manpage *, size_t); @@ -539,6 +540,23 @@ pg_error_internal(void) resp_end_html(); } +static void +pg_redirect(const struct req *req, const char *name) +{ + printf("Status: 303 See Other\r\n"); + printf("Location: http://%s/", HTTP_HOST); + if (*scriptname != '\0') + printf("%s/", scriptname); + if (strcmp(req->q.manpath, req->p[0])) + printf("%s/", req->q.manpath); + if (req->q.arch != NULL) + printf("%s/", req->q.arch); + printf("%s", name); + if (req->q.sec != NULL) + printf(".%s", req->q.sec); + printf("\r\nContent-Type: text/html; charset=utf-8\r\n\r\n"); +} + static void pg_searchres(const struct req *req, struct manpage *r, size_t sz) { @@ -956,9 +974,13 @@ pg_search(const struct req *req) } } - if (0 == mansearch(&search, &paths, argc, argv, &res, &ressz)) + res = NULL; + ressz = 0; + if (req->isquery && req->q.equal && argc == 1) + pg_redirect(req, argv[0]); + else if (mansearch(&search, &paths, argc, argv, &res, &ressz) == 0) pg_noresult(req, "You entered an invalid query."); - else if (0 == ressz) + else if (ressz == 0) pg_noresult(req, "No results found."); else pg_searchres(req, res, ressz); diff --git a/man.cgi.3 b/man.cgi.3 index e7c1d2fc..472d4119 100644 --- a/man.cgi.3 +++ b/man.cgi.3 @@ -1,4 +1,4 @@ -.\" $Id: man.cgi.3,v 1.2 2016/07/07 19:19:01 schwarze Exp $ +.\" $Id: man.cgi.3,v 1.3 2017/03/15 10:17:29 schwarze Exp $ .\" .\" Copyright (c) 2016 Ingo Schwarze .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: July 7 2016 $ +.Dd $Mdocdate: March 15 2017 $ .Dt MAN.CGI 3 .Os .Sh NAME @@ -126,12 +126,30 @@ contains a search query in short format or when is empty and a .Ev QUERY_STRING is provided. -It changes into the manpath and calls +If possible, requests using +.Ev QUERY_STRING +are redirected to URIs using +.Ev PATH_INFO +by calling +.Fn pg_redirect . +Otherwise, it changes into the manpath and calls .Xr mansearch 3 . Depending on the result, it calls either .Fn pg_noresult or .Fn pg_searchres . +.It Ft void Fn pg_redirect "const struct req *req" "const char *name" +This function is special in so far as it does not print an HTML page, +but only an HTTP 303 response with a Location: of the form: +.Sm off +.No http:// +.Ar host Ns / +.Op Ar scriptname Ns / +.Op Ar manpath Ns / +.Op Ar arch Ns / +.Fa name +.Op Pf . Ar sec +.Sm on .It Ft void Fn pg_noresult "const struct req *req" "const char *msg" This function calls .Fn resp_begin_html , -- cgit v1.2.3-56-ge451