]>
git.cameronkatri.com Git - mandoc.git/blob - cgi.c
1 /* $Id: cgi.c,v 1.9 2011/12/04 22:52:50 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 catman(const char *);
70 static void format(const char *);
71 static void html_print(const char *);
72 static int kval_decode(char *);
73 static void kval_parse(struct kval
**, size_t *, char *);
74 static void kval_free(struct kval
*, size_t);
75 static void pg_index(const struct manpaths
*,
76 const struct req
*, char *);
77 static void pg_search(const struct manpaths
*,
78 const struct req
*, char *);
79 static void pg_show(const struct manpaths
*,
80 const struct req
*, char *);
81 static void resp_bad(void);
82 static void resp_baddb(void);
83 static void resp_badexpr(const struct req
*);
84 static void resp_badmanual(void);
85 static void resp_badpage(void);
86 static void resp_begin_html(int, const char *);
87 static void resp_begin_http(int, const char *);
88 static void resp_end_html(void);
89 static void resp_index(const struct req
*);
90 static void resp_search(struct res
*, size_t, void *);
91 static void resp_searchform(const struct req
*);
93 static const char *progname
;
94 static const char *cache
;
95 static const char *host
;
97 static const char * const pages
[PAGE__MAX
] = {
98 "index", /* PAGE_INDEX */
99 "search", /* PAGE_SEARCH */
100 "show", /* PAGE_SHOW */
104 * This is just OpenBSD's strtol(3) suggestion.
105 * I use it instead of strtonum(3) for portability's sake.
108 atou(const char *buf
, unsigned *v
)
114 lval
= strtol(buf
, &ep
, 10);
115 if (buf
[0] == '\0' || *ep
!= '\0')
117 if ((errno
== ERANGE
&& (lval
== LONG_MAX
||
118 lval
== LONG_MIN
)) ||
119 (lval
> UINT_MAX
|| lval
< 0))
122 *v
= (unsigned int)lval
;
127 * Print a word, escaping HTML along the way.
128 * This will pass non-ASCII straight to output: be warned!
131 html_print(const char *p
)
139 switch ((c
= *p
++)) {
153 putchar((unsigned char)c
);
159 kval_free(struct kval
*p
, size_t sz
)
163 for (i
= 0; i
< (int)sz
; i
++) {
171 * Parse out key-value pairs from an HTTP request variable.
172 * This can be either a cookie or a POST/GET string, although man.cgi
173 * uses only GET for simplicity.
176 kval_parse(struct kval
**kv
, size_t *kvsz
, char *p
)
183 while (p
&& '\0' != *p
) {
190 if (NULL
!= (p
= strchr(p
, '='))) {
194 sz
= strcspn(p
, ";&");
202 sz
= strcspn(p
, ";&");
211 if ('\0' == *key
|| '\0' == *val
)
214 /* Just abort handling. */
216 if ( ! kval_decode(key
))
218 if ( ! kval_decode(val
))
221 if (*kvsz
+ 1 >= cur
) {
224 (*kv
, cur
* sizeof(struct kval
));
227 (*kv
)[(int)*kvsz
].key
= mandoc_strdup(key
);
228 (*kv
)[(int)*kvsz
].val
= mandoc_strdup(val
);
234 * HTTP-decode a string. The standard explanation is that this turns
235 * "%4e+foo" into "n foo" in the regular way. This is done in-place
236 * over the allocated string.
246 for ( ; '\0' != *p
; p
++) {
248 if ('\0' == (hex
[0] = *(p
+ 1)))
250 if ('\0' == (hex
[1] = *(p
+ 2)))
252 if (1 != sscanf(hex
, "%x", &c
))
258 memmove(p
+ 1, p
+ 3, strlen(p
+ 3) + 1);
260 *p
= '+' == *p
? ' ' : *p
;
268 resp_begin_http(int code
, const char *msg
)
272 printf("Status: %d %s\n", code
, msg
);
274 puts("Content-Type: text/html; charset=utf-8" "\n"
275 "Cache-Control: no-cache" "\n"
276 "Pragma: no-cache" "\n"
283 resp_begin_html(int code
, const char *msg
)
286 resp_begin_http(code
, msg
);
288 puts("<!DOCTYPE HTML PUBLIC " "\n"
289 " \"-//W3C//DTD HTML 4.01//EN\"" "\n"
290 " \"http://www.w3.org/TR/html4/strict.dtd\">" "\n"
293 " <TITLE>System Manpage Reference</TITLE>" "\n"
296 "<!-- Begin page content. //-->");
303 puts(" </BODY>\n</HTML>");
307 resp_searchform(const struct req
*req
)
310 const char *expr
, *sec
, *arch
;
312 expr
= sec
= arch
= "";
314 for (i
= 0; i
< (int)req
->fieldsz
; i
++)
315 if (0 == strcmp(req
->fields
[i
].key
, "expr"))
316 expr
= req
->fields
[i
].val
;
317 else if (0 == strcmp(req
->fields
[i
].key
, "sec"))
318 sec
= req
->fields
[i
].val
;
319 else if (0 == strcmp(req
->fields
[i
].key
, "arch"))
320 arch
= req
->fields
[i
].val
;
322 puts("<!-- Begin search form. //-->");
323 printf("<FORM ACTION=\"");
324 html_print(progname
);
325 printf("/search.html\" METHOD=\"get\">\n");
326 puts(" <FIELDSET>" "\n"
327 " <INPUT TYPE=\"submit\" VALUE=\"Search:\">");
328 printf(" Terms: <INPUT TYPE=\"text\" "
329 "SIZE=\"60\" NAME=\"expr\" VALUE=\"");
332 printf(" Section: <INPUT TYPE=\"text\" "
333 "SIZE=\"4\" NAME=\"sec\" VALUE=\"");
336 printf(" Arch: <INPUT TYPE=\"text\" "
337 "SIZE=\"8\" NAME=\"arch\" VALUE=\"");
340 puts(" </FIELDSET>\n</FORM>\n<!-- End search form. //-->");
344 resp_index(const struct req
*req
)
347 resp_begin_html(200, NULL
);
348 resp_searchform(req
);
356 resp_begin_html(404, "Not Found");
357 puts("<P>Page not found.</P>");
365 resp_begin_html(404, "Not Found");
366 puts("<P>Requested manual not found.</P>");
371 resp_badexpr(const struct req
*req
)
374 resp_begin_html(200, NULL
);
375 resp_searchform(req
);
376 puts("<P>Your search didn't work.</P>");
383 resp_begin_html(500, "Internal Server Error");
384 puts("<P>Generic badness happened.</P>");
392 resp_begin_html(500, "Internal Server Error");
393 puts("<P>Your database is broken.</P>");
398 resp_search(struct res
*r
, size_t sz
, void *arg
)
404 * If we have just one result, then jump there now
407 puts("Status: 303 See Other");
408 printf("Location: http://%s%s/show/%u/%u.html\n",
410 r
[0].volume
, r
[0].rec
);
411 puts("Content-Type: text/html; charset=utf-8\n");
415 resp_begin_html(200, NULL
);
416 resp_searchform((const struct req
*)arg
);
419 puts("<P>No results found.</P>");
421 for (i
= 0; i
< (int)sz
; i
++) {
422 printf("<P><A HREF=\"");
423 html_print(progname
);
424 printf("/show/%u/%u.html\">", r
[i
].volume
, r
[i
].rec
);
425 html_print(r
[i
].title
);
427 html_print(r
[i
].cat
);
428 if (r
[i
].arch
&& '\0' != *r
[i
].arch
) {
430 html_print(r
[i
].arch
);
433 html_print(r
[i
].desc
);
442 pg_index(const struct manpaths
*ps
, const struct req
*req
, char *path
)
449 catman(const char *file
)
455 if (-1 == (fd
= open(file
, O_RDONLY
, 0))) {
460 resp_begin_http(200, NULL
);
462 while ((ssz
= read(fd
, buf
, BUFSIZ
)) > 0)
463 write(STDOUT_FILENO
, buf
, (size_t)ssz
);
472 format(const char *file
)
481 if (-1 == (fd
= open(file
, O_RDONLY
, 0))) {
486 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
487 rc
= mparse_readfd(mp
, fd
, file
);
490 if (rc
>= MANDOCLEVEL_FATAL
) {
495 mparse_result(mp
, &mdoc
, &man
);
496 vp
= html_alloc(NULL
);
499 resp_begin_http(200, NULL
);
501 } else if (NULL
!= man
) {
502 resp_begin_http(200, NULL
);
512 pg_show(const struct manpaths
*ps
, const struct req
*req
, char *path
)
515 char file
[MAXPATHLEN
];
518 unsigned int vol
, rec
;
525 } else if (NULL
== (sub
= strrchr(path
, '/'))) {
531 if ( ! (atou(path
, &vol
) && atou(sub
, &rec
))) {
534 } else if (vol
>= (unsigned int)ps
->sz
) {
539 strlcpy(file
, ps
->paths
[vol
], MAXPATHLEN
);
540 strlcat(file
, "/mandoc.index", MAXPATHLEN
);
542 /* Open the index recno(3) database. */
544 idx
= dbopen(file
, O_RDONLY
, 0, DB_RECNO
, NULL
);
553 if (0 != (rc
= (*idx
->get
)(idx
, &key
, &val
, 0))) {
554 rc
< 0 ? resp_baddb() : resp_badmanual();
558 cp
= (char *)val
.data
;
560 if (NULL
== (fn
= memchr(cp
, '\0', val
.size
)))
562 else if (++fn
- cp
>= (int)val
.size
)
564 else if (NULL
== memchr(fn
, '\0', val
.size
- (fn
- cp
)))
567 strlcpy(file
, ps
->paths
[vol
], MAXPATHLEN
);
568 strlcat(file
, "/", MAXPATHLEN
);
569 strlcat(file
, fn
, MAXPATHLEN
);
570 if (0 == strcmp(cp
, "cat"))
580 pg_search(const struct manpaths
*ps
, const struct req
*req
, char *path
)
584 const char *ep
, *start
;
594 memset(&opt
, 0, sizeof(struct opts
));
596 for (sz
= i
= 0; i
< (int)req
->fieldsz
; i
++)
597 if (0 == strcmp(req
->fields
[i
].key
, "expr"))
598 ep
= req
->fields
[i
].val
;
599 else if (0 == strcmp(req
->fields
[i
].key
, "sec"))
600 opt
.cat
= req
->fields
[i
].val
;
601 else if (0 == strcmp(req
->fields
[i
].key
, "arch"))
602 opt
.arch
= req
->fields
[i
].val
;
605 * Poor man's tokenisation.
606 * Just break apart by spaces.
607 * Yes, this is half-ass. But it works for now.
610 while (ep
&& isspace((unsigned char)*ep
))
613 while (ep
&& '\0' != *ep
) {
614 cp
= mandoc_realloc(cp
, (sz
+ 1) * sizeof(char *));
616 while ('\0' != *ep
&& ! isspace((unsigned char)*ep
))
618 cp
[sz
] = mandoc_malloc((ep
- start
) + 1);
619 memcpy(cp
[sz
], start
, ep
- start
);
620 cp
[sz
++][ep
- start
] = '\0';
621 while (isspace((unsigned char)*ep
))
628 * Pump down into apropos backend.
629 * The resp_search() function is called with the results.
632 if (NULL
!= (expr
= exprcomp(sz
, cp
, &tt
)))
634 (ps
->sz
, ps
->paths
, &opt
,
635 expr
, tt
, (void *)req
, resp_search
);
637 /* ...unless errors occured. */
644 for (i
= 0; i
< sz
; i
++)
656 char *p
, *path
, *subpath
;
657 struct manpaths paths
;
659 /* HTTP init: read and parse the query string. */
661 progname
= getenv("SCRIPT_NAME");
662 if (NULL
== progname
)
665 cache
= getenv("CACHE_DIR");
667 cache
= "/cache/man.cgi";
669 if (-1 == chdir(cache
)) {
671 return(EXIT_FAILURE
);
674 host
= getenv("HTTP_HOST");
678 memset(&req
, 0, sizeof(struct req
));
680 if (NULL
!= (p
= getenv("QUERY_STRING")))
681 kval_parse(&req
.fields
, &req
.fieldsz
, p
);
683 /* Resolve leading subpath component. */
685 subpath
= path
= NULL
;
686 req
.page
= PAGE__MAX
;
688 if (NULL
== (path
= getenv("PATH_INFO")) || '\0' == *path
)
689 req
.page
= PAGE_INDEX
;
691 if (NULL
!= path
&& '/' == *path
&& '\0' == *++path
)
692 req
.page
= PAGE_INDEX
;
694 /* Strip file suffix. */
696 if (NULL
!= path
&& NULL
!= (p
= strrchr(path
, '.')))
697 if (NULL
!= p
&& NULL
== strchr(p
, '/'))
700 /* Resolve subpath component. */
702 if (NULL
!= path
&& NULL
!= (subpath
= strchr(path
, '/')))
705 /* Map path into one we recognise. */
707 if (NULL
!= path
&& '\0' != *path
)
708 for (i
= 0; i
< (int)PAGE__MAX
; i
++)
709 if (0 == strcmp(pages
[i
], path
)) {
710 req
.page
= (enum page
)i
;
714 /* Initialise MANPATH. */
716 memset(&paths
, 0, sizeof(struct manpaths
));
717 manpath_manconf("etc/catman.conf", &paths
);
723 pg_index(&paths
, &req
, subpath
);
726 pg_search(&paths
, &req
, subpath
);
729 pg_show(&paths
, &req
, subpath
);
736 manpath_free(&paths
);
737 kval_free(req
.fields
, req
.fieldsz
);
739 return(EXIT_SUCCESS
);