]>
git.cameronkatri.com Git - mandoc.git/blob - cgi.c
1 /* $Id: cgi.c,v 1.8 2011/11/27 11:46:44 kristaps Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/param.h>
37 #include "apropos_db.h"
68 static int atou(const char *, unsigned *);
69 static void format(const char *);
70 static void html_print(const char *);
71 static int kval_decode(char *);
72 static void kval_parse(struct kval
**, size_t *, char *);
73 static void kval_free(struct kval
*, size_t);
74 static void pg_index(const struct manpaths
*,
75 const struct req
*, char *);
76 static void pg_search(const struct manpaths
*,
77 const struct req
*, char *);
78 static void pg_show(const struct manpaths
*,
79 const struct req
*, char *);
80 static void resp_bad(void);
81 static void resp_baddb(void);
82 static void resp_badexpr(const struct req
*);
83 static void resp_badmanual(void);
84 static void resp_begin_html(int, const char *);
85 static void resp_begin_http(int, const char *);
86 static void resp_end_html(void);
87 static void resp_index(const struct req
*);
88 static void resp_search(struct res
*, size_t, void *);
89 static void resp_searchform(const struct req
*);
91 static const char *progname
;
92 static const char *cache
;
93 static const char *host
;
95 static const char * const pages
[PAGE__MAX
] = {
96 "index", /* PAGE_INDEX */
97 "search", /* PAGE_SEARCH */
98 "show", /* PAGE_SHOW */
102 * This is just OpenBSD's strtol(3) suggestion.
103 * I use it instead of strtonum(3) for portability's sake.
106 atou(const char *buf
, unsigned *v
)
112 lval
= strtol(buf
, &ep
, 10);
113 if (buf
[0] == '\0' || *ep
!= '\0')
115 if ((errno
== ERANGE
&& (lval
== LONG_MAX
||
116 lval
== LONG_MIN
)) ||
117 (lval
> UINT_MAX
|| lval
< 0))
120 *v
= (unsigned int)lval
;
125 * Print a word, escaping HTML along the way.
126 * This will pass non-ASCII straight to output: be warned!
129 html_print(const char *p
)
137 switch ((c
= *p
++)) {
151 putchar((unsigned char)c
);
157 kval_free(struct kval
*p
, size_t sz
)
161 for (i
= 0; i
< (int)sz
; i
++) {
169 * Parse out key-value pairs from an HTTP request variable.
170 * This can be either a cookie or a POST/GET string, although man.cgi
171 * uses only GET for simplicity.
174 kval_parse(struct kval
**kv
, size_t *kvsz
, char *p
)
181 while (p
&& '\0' != *p
) {
188 if (NULL
!= (p
= strchr(p
, '='))) {
192 sz
= strcspn(p
, ";&");
200 sz
= strcspn(p
, ";&");
209 if ('\0' == *key
|| '\0' == *val
)
212 /* Just abort handling. */
214 if ( ! kval_decode(key
))
216 if ( ! kval_decode(val
))
219 if (*kvsz
+ 1 >= cur
) {
222 (*kv
, cur
* sizeof(struct kval
));
225 (*kv
)[(int)*kvsz
].key
= mandoc_strdup(key
);
226 (*kv
)[(int)*kvsz
].val
= mandoc_strdup(val
);
232 * HTTP-decode a string. The standard explanation is that this turns
233 * "%4e+foo" into "n foo" in the regular way. This is done in-place
234 * over the allocated string.
244 for ( ; '\0' != *p
; p
++) {
246 if ('\0' == (hex
[0] = *(p
+ 1)))
248 if ('\0' == (hex
[1] = *(p
+ 2)))
250 if (1 != sscanf(hex
, "%x", &c
))
256 memmove(p
+ 1, p
+ 3, strlen(p
+ 3) + 1);
258 *p
= '+' == *p
? ' ' : *p
;
266 resp_begin_http(int code
, const char *msg
)
270 printf("Status: %d %s\n", code
, msg
);
272 puts("Content-Type: text/html; charset=utf-8" "\n"
273 "Cache-Control: no-cache" "\n"
274 "Pragma: no-cache" "\n"
281 resp_begin_html(int code
, const char *msg
)
284 resp_begin_http(code
, msg
);
286 puts("<!DOCTYPE HTML PUBLIC " "\n"
287 " \"-//W3C//DTD HTML 4.01//EN\"" "\n"
288 " \"http://www.w3.org/TR/html4/strict.dtd\">" "\n"
291 " <TITLE>System Manpage Reference</TITLE>" "\n"
294 "<!-- Begin page content. //-->");
301 puts(" </BODY>\n</HTML>");
305 resp_searchform(const struct req
*req
)
308 const char *expr
, *sec
, *arch
;
310 expr
= sec
= arch
= "";
312 for (i
= 0; i
< (int)req
->fieldsz
; i
++)
313 if (0 == strcmp(req
->fields
[i
].key
, "expr"))
314 expr
= req
->fields
[i
].val
;
315 else if (0 == strcmp(req
->fields
[i
].key
, "sec"))
316 sec
= req
->fields
[i
].val
;
317 else if (0 == strcmp(req
->fields
[i
].key
, "arch"))
318 arch
= req
->fields
[i
].val
;
320 puts("<!-- Begin search form. //-->");
321 printf("<FORM ACTION=\"");
322 html_print(progname
);
323 printf("/search\" METHOD=\"get\">\n");
324 puts(" <FIELDSET>" "\n"
325 " <INPUT TYPE=\"submit\" VALUE=\"Search:\">");
326 printf(" Terms: <INPUT TYPE=\"text\" "
327 "SIZE=\"60\" NAME=\"expr\" VALUE=\"");
330 printf(" Section: <INPUT TYPE=\"text\" "
331 "SIZE=\"4\" NAME=\"sec\" VALUE=\"");
334 printf(" Arch: <INPUT TYPE=\"text\" "
335 "SIZE=\"8\" NAME=\"arch\" VALUE=\"");
338 puts(" </FIELDSET>\n</FORM>\n<!-- End search form. //-->");
342 resp_index(const struct req
*req
)
345 resp_begin_html(200, NULL
);
346 resp_searchform(req
);
354 resp_begin_html(404, "Not Found");
355 puts("<P>Requested manual not found.</P>");
360 resp_badexpr(const struct req
*req
)
363 resp_begin_html(200, NULL
);
364 resp_searchform(req
);
365 puts("<P>Your search didn't work.</P>");
372 resp_begin_html(500, "Internal Server Error");
373 puts("<P>Generic badness happened.</P>");
381 resp_begin_html(500, "Internal Server Error");
382 puts("<P>Your database is broken.</P>");
387 resp_search(struct res
*r
, size_t sz
, void *arg
)
393 * If we have just one result, then jump there now
396 puts("Status: 303 See Other");
397 printf("Location: http://%s%s/show/%u/%u.html\n",
399 r
[0].volume
, r
[0].rec
);
400 puts("Content-Type: text/html; charset=utf-8\n");
404 resp_begin_html(200, NULL
);
405 resp_searchform((const struct req
*)arg
);
408 puts("<P>No results found.</P>");
410 for (i
= 0; i
< (int)sz
; i
++) {
411 printf("<P><A HREF=\"");
412 html_print(progname
);
413 printf("/show/%u/%u.html\">", r
[i
].volume
, r
[i
].rec
);
414 html_print(r
[i
].title
);
416 html_print(r
[i
].cat
);
417 if (r
[i
].arch
&& '\0' != *r
[i
].arch
) {
419 html_print(r
[i
].arch
);
422 html_print(r
[i
].desc
);
431 pg_index(const struct manpaths
*ps
, const struct req
*req
, char *path
)
438 format(const char *file
)
447 if (-1 == (fd
= open(file
, O_RDONLY
, 0))) {
452 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
453 rc
= mparse_readfd(mp
, fd
, file
);
456 if (rc
>= MANDOCLEVEL_FATAL
) {
461 mparse_result(mp
, &mdoc
, &man
);
462 vp
= html_alloc(NULL
);
465 resp_begin_http(200, NULL
);
467 } else if (NULL
!= man
) {
468 resp_begin_http(200, NULL
);
478 pg_show(const struct manpaths
*ps
, const struct req
*req
, char *path
)
481 char file
[MAXPATHLEN
];
483 unsigned int vol
, rec
;
490 } else if (NULL
== (sub
= strrchr(path
, '/'))) {
496 if ( ! (atou(path
, &vol
) && atou(sub
, &rec
))) {
499 } else if (vol
>= (unsigned int)ps
->sz
) {
504 strlcpy(file
, ps
->paths
[vol
], MAXPATHLEN
);
505 strlcat(file
, "/mandoc.index", MAXPATHLEN
);
507 /* Open the index recno(3) database. */
509 db
= dbopen(file
, O_RDONLY
, 0, DB_RECNO
, NULL
);
518 if (0 != (rc
= (*db
->get
)(db
, &key
, &val
, 0))) {
519 rc
< 0 ? resp_baddb() : resp_badmanual();
524 /* Extra filename: the first nil-terminated entry. */
526 strlcpy(file
, ps
->paths
[vol
], MAXPATHLEN
);
527 strlcat(file
, "/", MAXPATHLEN
);
528 strlcat(file
, (char *)val
.data
, MAXPATHLEN
);
536 pg_search(const struct manpaths
*ps
, const struct req
*req
, char *path
)
540 const char *ep
, *start
;
550 memset(&opt
, 0, sizeof(struct opts
));
552 for (sz
= i
= 0; i
< (int)req
->fieldsz
; i
++)
553 if (0 == strcmp(req
->fields
[i
].key
, "expr"))
554 ep
= req
->fields
[i
].val
;
555 else if (0 == strcmp(req
->fields
[i
].key
, "sec"))
556 opt
.cat
= req
->fields
[i
].val
;
557 else if (0 == strcmp(req
->fields
[i
].key
, "arch"))
558 opt
.arch
= req
->fields
[i
].val
;
561 * Poor man's tokenisation.
562 * Just break apart by spaces.
563 * Yes, this is half-ass. But it works for now.
566 while (ep
&& isspace((unsigned char)*ep
))
569 while (ep
&& '\0' != *ep
) {
570 cp
= mandoc_realloc(cp
, (sz
+ 1) * sizeof(char *));
572 while ('\0' != *ep
&& ! isspace((unsigned char)*ep
))
574 cp
[sz
] = mandoc_malloc((ep
- start
) + 1);
575 memcpy(cp
[sz
], start
, ep
- start
);
576 cp
[sz
++][ep
- start
] = '\0';
577 while (isspace((unsigned char)*ep
))
584 * Pump down into apropos backend.
585 * The resp_search() function is called with the results.
588 if (NULL
!= (expr
= exprcomp(sz
, cp
, &tt
)))
590 (ps
->sz
, ps
->paths
, &opt
,
591 expr
, tt
, (void *)req
, resp_search
);
593 /* ...unless errors occured. */
600 for (i
= 0; i
< sz
; i
++)
612 char *p
, *path
, *subpath
;
613 struct manpaths paths
;
615 /* HTTP init: read and parse the query string. */
617 progname
= getenv("SCRIPT_NAME");
618 if (NULL
== progname
)
621 cache
= getenv("CACHE_DIR");
623 cache
= "/cache/man.cgi";
625 if (-1 == chdir(cache
)) {
627 return(EXIT_FAILURE
);
630 host
= getenv("HTTP_HOST");
634 memset(&req
, 0, sizeof(struct req
));
636 if (NULL
!= (p
= getenv("QUERY_STRING")))
637 kval_parse(&req
.fields
, &req
.fieldsz
, p
);
639 /* Resolve leading subpath component. */
641 subpath
= path
= NULL
;
642 req
.page
= PAGE__MAX
;
644 if (NULL
== (path
= getenv("PATH_INFO")) || '\0' == *path
)
645 req
.page
= PAGE_INDEX
;
647 if (NULL
!= path
&& '/' == *path
&& '\0' == *++path
)
648 req
.page
= PAGE_INDEX
;
650 /* Strip file suffix. */
652 if (NULL
!= path
&& NULL
!= (p
= strrchr(path
, '.')))
653 if (NULL
!= p
&& NULL
== strchr(p
, '/'))
656 /* Resolve subpath component. */
658 if (NULL
!= path
&& NULL
!= (subpath
= strchr(path
, '/')))
661 /* Map path into one we recognise. */
663 if (NULL
!= path
&& '\0' != *path
)
664 for (i
= 0; i
< (int)PAGE__MAX
; i
++)
665 if (0 == strcmp(pages
[i
], path
)) {
666 req
.page
= (enum page
)i
;
670 /* Initialise MANPATH. */
672 memset(&paths
, 0, sizeof(struct manpaths
));
673 manpath_manconf("etc/man.conf", &paths
);
679 pg_index(&paths
, &req
, subpath
);
682 pg_search(&paths
, &req
, subpath
);
685 pg_show(&paths
, &req
, subpath
);
691 manpath_free(&paths
);
692 kval_free(req
.fields
, req
.fieldsz
);
694 return(EXIT_SUCCESS
);