version 1.127, 2016/04/15 15:13:07 |
version 1.129, 2016/04/15 21:14:51 |
|
|
#include <sys/time.h> |
#include <sys/time.h> |
|
|
#include <ctype.h> |
#include <ctype.h> |
|
#include <err.h> |
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <limits.h> |
#include <limits.h> |
|
|
int isquery; /* QUERY_STRING used, not PATH_INFO */ |
int isquery; /* QUERY_STRING used, not PATH_INFO */ |
}; |
}; |
|
|
static void catman(const struct req *, const char *); |
|
static void format(const struct req *, const char *); |
|
static void html_print(const char *); |
static void html_print(const char *); |
static void html_putchar(char); |
static void html_putchar(char); |
static int http_decode(char *); |
static int http_decode(char *); |
static void http_parse(struct req *, const char *); |
static void parse_manpath_conf(struct req *); |
static void pathgen(struct req *); |
static void parse_path_info(struct req *req, const char *path); |
static void path_parse(struct req *req, const char *path); |
static void parse_query_string(struct req *, const char *); |
static void pg_error_badrequest(const char *); |
static void pg_error_badrequest(const char *); |
static void pg_error_internal(void); |
static void pg_error_internal(void); |
static void pg_index(const struct req *); |
static void pg_index(const struct req *); |
Line 76 static void pg_searchres(const struct req *, |
|
Line 75 static void pg_searchres(const struct req *, |
|
static void pg_show(struct req *, const char *); |
static void pg_show(struct req *, const char *); |
static void resp_begin_html(int, const char *); |
static void resp_begin_html(int, const char *); |
static void resp_begin_http(int, const char *); |
static void resp_begin_http(int, const char *); |
|
static void resp_catman(const struct req *, const char *); |
static void resp_copy(const char *); |
static void resp_copy(const char *); |
static void resp_end_html(void); |
static void resp_end_html(void); |
|
static void resp_format(const struct req *, const char *); |
static void resp_searchform(const struct req *); |
static void resp_searchform(const struct req *); |
static void resp_show(const struct req *, const char *); |
static void resp_show(const struct req *, const char *); |
static void set_query_attr(char **, char **); |
static void set_query_attr(char **, char **); |
Line 184 set_query_attr(char **attr, char **val) |
|
Line 185 set_query_attr(char **attr, char **val) |
|
* and store the values into the query structure. |
* and store the values into the query structure. |
*/ |
*/ |
static void |
static void |
http_parse(struct req *req, const char *qs) |
parse_query_string(struct req *req, const char *qs) |
{ |
{ |
char *key, *val; |
char *key, *val; |
size_t keysz, valsz; |
size_t keysz, valsz; |
Line 564 pg_searchres(const struct req *req, struct manpage *r, |
|
Line 565 pg_searchres(const struct req *req, struct manpage *r, |
|
for (i = 0; i < sz; i++) { |
for (i = 0; i < sz; i++) { |
if (validate_filename(r[i].file)) |
if (validate_filename(r[i].file)) |
continue; |
continue; |
fprintf(stderr, "invalid filename %s in %s database\n", |
warnx("invalid filename %s in %s database", |
r[i].file, req->q.manpath); |
r[i].file, req->q.manpath); |
pg_error_internal(); |
pg_error_internal(); |
return; |
return; |
Line 660 pg_searchres(const struct req *req, struct manpage *r, |
|
Line 661 pg_searchres(const struct req *req, struct manpage *r, |
|
} |
} |
|
|
static void |
static void |
catman(const struct req *req, const char *file) |
resp_catman(const struct req *req, const char *file) |
{ |
{ |
FILE *f; |
FILE *f; |
char *p; |
char *p; |
Line 797 catman(const struct req *req, const char *file) |
|
Line 798 catman(const struct req *req, const char *file) |
|
} |
} |
|
|
static void |
static void |
format(const struct req *req, const char *file) |
resp_format(const struct req *req, const char *file) |
{ |
{ |
struct manoutput conf; |
struct manoutput conf; |
struct mparse *mp; |
struct mparse *mp; |
Line 824 format(const struct req *req, const char *file) |
|
Line 825 format(const struct req *req, const char *file) |
|
|
|
mparse_result(mp, &man, NULL); |
mparse_result(mp, &man, NULL); |
if (man == NULL) { |
if (man == NULL) { |
fprintf(stderr, "fatal mandoc error: %s/%s\n", |
warnx("fatal mandoc error: %s/%s", req->q.manpath, file); |
req->q.manpath, file); |
|
pg_error_internal(); |
pg_error_internal(); |
mparse_free(mp); |
mparse_free(mp); |
mchars_free(); |
mchars_free(); |
Line 856 resp_show(const struct req *req, const char *file) |
|
Line 856 resp_show(const struct req *req, const char *file) |
|
file += 2; |
file += 2; |
|
|
if ('c' == *file) |
if ('c' == *file) |
catman(req, file); |
resp_catman(req, file); |
else |
else |
format(req, file); |
resp_format(req, file); |
} |
} |
|
|
static void |
static void |
Line 889 pg_show(struct req *req, const char *fullpath) |
|
Line 889 pg_show(struct req *req, const char *fullpath) |
|
*/ |
*/ |
|
|
if (chdir(manpath) == -1) { |
if (chdir(manpath) == -1) { |
fprintf(stderr, "chdir %s: %s\n", |
warn("chdir %s", manpath); |
manpath, strerror(errno)); |
|
pg_error_internal(); |
pg_error_internal(); |
free(manpath); |
free(manpath); |
return; |
return; |
Line 931 pg_search(const struct req *req) |
|
Line 930 pg_search(const struct req *req) |
|
* relative to the manpath root. |
* relative to the manpath root. |
*/ |
*/ |
|
|
if (-1 == (chdir(req->q.manpath))) { |
if (chdir(req->q.manpath) == -1) { |
fprintf(stderr, "chdir %s: %s\n", |
warn("chdir %s", req->q.manpath); |
req->q.manpath, strerror(errno)); |
|
pg_error_internal(); |
pg_error_internal(); |
return; |
return; |
} |
} |
|
|
itimer.it_interval.tv_sec = 2; |
itimer.it_interval.tv_sec = 2; |
itimer.it_interval.tv_usec = 0; |
itimer.it_interval.tv_usec = 0; |
if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) { |
if (setitimer(ITIMER_VIRTUAL, &itimer, NULL) == -1) { |
fprintf(stderr, "setitimer: %s\n", strerror(errno)); |
warn("setitimer"); |
pg_error_internal(); |
pg_error_internal(); |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
} |
} |
|
|
* relative to the same position. |
* relative to the same position. |
*/ |
*/ |
|
|
if (-1 == chdir(MAN_DIR)) { |
if (chdir(MAN_DIR) == -1) { |
fprintf(stderr, "MAN_DIR: %s: %s\n", |
warn("MAN_DIR: %s", MAN_DIR); |
MAN_DIR, strerror(errno)); |
|
pg_error_internal(); |
pg_error_internal(); |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
} |
} |
|
|
memset(&req, 0, sizeof(struct req)); |
memset(&req, 0, sizeof(struct req)); |
req.q.equal = 1; |
req.q.equal = 1; |
pathgen(&req); |
parse_manpath_conf(&req); |
|
|
/* Parse the path info and the query string. */ |
/* Parse the path info and the query string. */ |
|
|
|
|
path++; |
path++; |
|
|
if (*path != '\0') { |
if (*path != '\0') { |
path_parse(&req, path); |
parse_path_info(&req, path); |
if (access(path, F_OK) == -1) |
if (access(path, F_OK) == -1) |
path = ""; |
path = ""; |
} else if ((querystring = getenv("QUERY_STRING")) != NULL) |
} else if ((querystring = getenv("QUERY_STRING")) != NULL) |
http_parse(&req, querystring); |
parse_query_string(&req, querystring); |
|
|
/* Validate parsed data and add defaults. */ |
/* Validate parsed data and add defaults. */ |
|
|
|
|
* If PATH_INFO is not a file name, translate it to a query. |
* If PATH_INFO is not a file name, translate it to a query. |
*/ |
*/ |
static void |
static void |
path_parse(struct req *req, const char *path) |
parse_path_info(struct req *req, const char *path) |
{ |
{ |
char *dir; |
char *dir; |
|
|
Line 1135 path_parse(struct req *req, const char *path) |
|
Line 1132 path_parse(struct req *req, const char *path) |
|
* Scan for indexable paths. |
* Scan for indexable paths. |
*/ |
*/ |
static void |
static void |
pathgen(struct req *req) |
parse_manpath_conf(struct req *req) |
{ |
{ |
FILE *fp; |
FILE *fp; |
char *dp; |
char *dp; |
size_t dpsz; |
size_t dpsz; |
ssize_t len; |
ssize_t len; |
|
|
if (NULL == (fp = fopen("manpath.conf", "r"))) { |
if ((fp = fopen("manpath.conf", "r")) == NULL) { |
fprintf(stderr, "%s/manpath.conf: %s\n", |
warn("%s/manpath.conf", MAN_DIR); |
MAN_DIR, strerror(errno)); |
|
pg_error_internal(); |
pg_error_internal(); |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
} |
} |
Line 1158 pathgen(struct req *req) |
|
Line 1154 pathgen(struct req *req) |
|
req->p = mandoc_realloc(req->p, |
req->p = mandoc_realloc(req->p, |
(req->psz + 1) * sizeof(char *)); |
(req->psz + 1) * sizeof(char *)); |
if ( ! validate_urifrag(dp)) { |
if ( ! validate_urifrag(dp)) { |
fprintf(stderr, "%s/manpath.conf contains " |
warnx("%s/manpath.conf contains " |
"unsafe path \"%s\"\n", MAN_DIR, dp); |
"unsafe path \"%s\"", MAN_DIR, dp); |
pg_error_internal(); |
pg_error_internal(); |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
} |
} |
if (NULL != strchr(dp, '/')) { |
if (strchr(dp, '/') != NULL) { |
fprintf(stderr, "%s/manpath.conf contains " |
warnx("%s/manpath.conf contains " |
"path with slash \"%s\"\n", MAN_DIR, dp); |
"path with slash \"%s\"", MAN_DIR, dp); |
pg_error_internal(); |
pg_error_internal(); |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
} |
} |
Line 1175 pathgen(struct req *req) |
|
Line 1171 pathgen(struct req *req) |
|
} |
} |
free(dp); |
free(dp); |
|
|
if ( req->p == NULL ) { |
if (req->p == NULL) { |
fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR); |
warnx("%s/manpath.conf is empty", MAN_DIR); |
pg_error_internal(); |
pg_error_internal(); |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
} |
} |