[BACK]Return to cgi.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/cgi.c between version 1.116 and 1.125

version 1.116, 2016/01/04 12:36:26 version 1.125, 2016/04/15 00:51:59
Line 1 
Line 1 
 /*      $Id$ */  /*      $Id$ */
 /*  /*
  * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>   * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@usta.de>   * Copyright (c) 2014, 2015, 2016 Ingo Schwarze <schwarze@usta.de>
  *   *
  * Permission to use, copy, modify, and distribute this software for any   * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above   * purpose with or without fee is hereby granted, provided that the above
Line 55  struct req {
Line 55  struct req {
         struct query      q;          struct query      q;
         char            **p; /* array of available manpaths */          char            **p; /* array of available manpaths */
         size_t            psz; /* number of available manpaths */          size_t            psz; /* number of available manpaths */
           int               isquery; /* QUERY_STRING used, not PATH_INFO */
 };  };
   
 static  void             catman(const struct req *, const char *);  static  void             catman(const struct req *, const char *);
Line 64  static void   html_putchar(char);
Line 65  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             http_parse(struct req *, const char *);
 static  void             pathgen(struct req *);  static  void             pathgen(struct req *);
   static  void             path_parse(struct req *req, const char *path);
 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 83  static int   validate_filename(const char *);
Line 85  static int   validate_filename(const char *);
 static  int              validate_manpath(const struct req *, const char *);  static  int              validate_manpath(const struct req *, const char *);
 static  int              validate_urifrag(const char *);  static  int              validate_urifrag(const char *);
   
 static  const char       *scriptname; /* CGI script name */  static  const char       *scriptname = SCRIPT_NAME;
   
 static  const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};  static  const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
 static  const char *const sec_numbers[] = {  static  const char *const sec_numbers[] = {
Line 106  static const int sec_MAX = sizeof(sec_names) / sizeof(
Line 108  static const int sec_MAX = sizeof(sec_names) / sizeof(
   
 static  const char *const arch_names[] = {  static  const char *const arch_names[] = {
     "amd64",       "alpha",       "armish",      "armv7",      "amd64",       "alpha",       "armish",      "armv7",
     "aviion",      "hppa",        "hppa64",      "i386",      "hppa",        "hppa64",      "i386",        "landisk",
     "ia64",        "landisk",     "loongson",    "luna88k",      "loongson",    "luna88k",     "macppc",      "mips64",
     "macppc",      "mips64",      "octeon",      "sgi",      "octeon",      "sgi",         "socppc",      "sparc",
     "socppc",      "solbourne",   "sparc",       "sparc64",      "sparc64",     "zaurus",
     "vax",         "zaurus",  
     "amiga",       "arc",         "arm32",       "atari",      "amiga",       "arc",         "arm32",       "atari",
     "beagle",      "cats",        "hp300",       "mac68k",      "aviion",      "beagle",      "cats",        "hp300",
     "mvme68k",     "mvme88k",     "mvmeppc",     "palm",      "ia64",        "mac68k",      "mvme68k",     "mvme88k",
     "pc532",       "pegasos",     "pmax",        "powerpc",      "mvmeppc",     "palm",        "pc532",       "pegasos",
     "sun3",        "wgrisc",      "x68k"      "pmax",        "powerpc",     "solbourne",   "sun3",
       "vax",         "wgrisc",      "x68k"
 };  };
 static  const int arch_MAX = sizeof(arch_names) / sizeof(char *);  static  const int arch_MAX = sizeof(arch_names) / sizeof(char *);
   
Line 187  http_parse(struct req *req, const char *qs)
Line 189  http_parse(struct req *req, const char *qs)
         char            *key, *val;          char            *key, *val;
         size_t           keysz, valsz;          size_t           keysz, valsz;
   
           req->isquery    = 1;
         req->q.manpath  = NULL;          req->q.manpath  = NULL;
         req->q.arch     = NULL;          req->q.arch     = NULL;
         req->q.sec      = NULL;          req->q.sec      = NULL;
Line 369  resp_searchform(const struct req *req)
Line 372  resp_searchform(const struct req *req)
   
         puts("<!-- Begin search form. //-->");          puts("<!-- Begin search form. //-->");
         printf("<DIV ID=\"mancgi\">\n"          printf("<DIV ID=\"mancgi\">\n"
                "<FORM ACTION=\"%s\" METHOD=\"get\">\n"                 "<FORM ACTION=\"/%s\" METHOD=\"get\">\n"
                "<FIELDSET>\n"                 "<FIELDSET>\n"
                "<LEGEND>Manual Page Search Parameters</LEGEND>\n",                 "<LEGEND>Manual Page Search Parameters</LEGEND>\n",
                scriptname);                 scriptname);
Line 506  pg_index(const struct req *req)
Line 509  pg_index(const struct req *req)
         resp_searchform(req);          resp_searchform(req);
         printf("<P>\n"          printf("<P>\n"
                "This web interface is documented in the\n"                 "This web interface is documented in the\n"
                "<A HREF=\"%s/mandoc/man8/man.cgi.8\">man.cgi</A>\n"                 "<A HREF=\"/%s%smandoc/man8/man.cgi.8\">man.cgi</A>\n"
                "manual, and the\n"                 "manual, and the\n"
                "<A HREF=\"%s/mandoc/man1/apropos.1\">apropos</A>\n"                 "<A HREF=\"/%s%smandoc/man1/apropos.1\">apropos</A>\n"
                "manual explains the query syntax.\n"                 "manual explains the query syntax.\n"
                "</P>\n",                 "</P>\n",
                scriptname, scriptname);                 scriptname, *scriptname == '\0' ? "" : "/",
                  scriptname, *scriptname == '\0' ? "" : "/");
         resp_end_html();          resp_end_html();
 }  }
   
Line 535  pg_error_badrequest(const char *msg)
Line 539  pg_error_badrequest(const char *msg)
              "<P>\n");               "<P>\n");
         puts(msg);          puts(msg);
         printf("Try again from the\n"          printf("Try again from the\n"
                "<A HREF=\"%s\">main page</A>.\n"                 "<A HREF=\"/%s\">main page</A>.\n"
                "</P>", scriptname);                 "</P>", scriptname);
         resp_end_html();          resp_end_html();
 }  }
Line 552  static void
Line 556  static void
 pg_searchres(const struct req *req, struct manpage *r, size_t sz)  pg_searchres(const struct req *req, struct manpage *r, size_t sz)
 {  {
         char            *arch, *archend;          char            *arch, *archend;
         size_t           i, iuse, isec;          const char      *sec;
           size_t           i, iuse;
         int              archprio, archpriouse;          int              archprio, archpriouse;
         int              prio, priouse;          int              prio, priouse;
         char             sec;  
   
         for (i = 0; i < sz; i++) {          for (i = 0; i < sz; i++) {
                 if (validate_filename(r[i].file))                  if (validate_filename(r[i].file))
Line 566  pg_searchres(const struct req *req, struct manpage *r,
Line 570  pg_searchres(const struct req *req, struct manpage *r,
                 return;                  return;
         }          }
   
         if (1 == sz) {          if (req->isquery && sz == 1) {
                 /*                  /*
                  * If we have just one result, then jump there now                   * If we have just one result, then jump there now
                  * without any delay.                   * without any delay.
                  */                   */
                 printf("Status: 303 See Other\r\n");                  printf("Status: 303 See Other\r\n");
                 printf("Location: http://%s%s/%s/%s",                  printf("Location: http://%s/%s%s%s/%s",
                     HTTP_HOST, scriptname, req->q.manpath, r[0].file);                      HTTP_HOST, scriptname,
                       *scriptname == '\0' ? "" : "/",
                       req->q.manpath, r[0].file);
                 printf("\r\n"                  printf("\r\n"
                      "Content-Type: text/html; charset=utf-8\r\n"                       "Content-Type: text/html; charset=utf-8\r\n"
                      "\r\n");                       "\r\n");
Line 582  pg_searchres(const struct req *req, struct manpage *r,
Line 588  pg_searchres(const struct req *req, struct manpage *r,
   
         resp_begin_html(200, NULL);          resp_begin_html(200, NULL);
         resp_searchform(req);          resp_searchform(req);
         puts("<DIV CLASS=\"results\">");  
         puts("<TABLE>");  
   
         for (i = 0; i < sz; i++) {          if (sz > 1) {
                 printf("<TR>\n"                  puts("<DIV CLASS=\"results\">");
                        "<TD CLASS=\"title\">\n"                  puts("<TABLE>");
                        "<A HREF=\"%s/%s/%s",  
                     scriptname, req->q.manpath, r[i].file);  
                 printf("\">");  
                 html_print(r[i].names);  
                 printf("</A>\n"  
                        "</TD>\n"  
                        "<TD CLASS=\"desc\">");  
                 html_print(r[i].output);  
                 puts("</TD>\n"  
                      "</TR>");  
         }  
   
         puts("</TABLE>\n"                  for (i = 0; i < sz; i++) {
              "</DIV>");                          printf("<TR>\n"
                                  "<TD CLASS=\"title\">\n"
                                  "<A HREF=\"/%s%s%s/%s",
                               scriptname, *scriptname == '\0' ? "" : "/",
                               req->q.manpath, r[i].file);
                           printf("\">");
                           html_print(r[i].names);
                           printf("</A>\n"
                                  "</TD>\n"
                                  "<TD CLASS=\"desc\">");
                           html_print(r[i].output);
                           puts("</TD>\n"
                                "</TR>");
                   }
   
                   puts("</TABLE>\n"
                        "</DIV>");
           }
   
         /*          /*
          * In man(1) mode, show one of the pages           * In man(1) mode, show one of the pages
          * even if more than one is found.           * even if more than one is found.
          */           */
   
         if (req->q.equal) {          if (req->q.equal || sz == 1) {
                 puts("<HR>");                  puts("<HR>");
                 iuse = 0;                  iuse = 0;
                 priouse = 10;                  priouse = 20;
                 archpriouse = 3;                  archpriouse = 3;
                 for (i = 0; i < sz; i++) {                  for (i = 0; i < sz; i++) {
                         isec = strcspn(r[i].file, "123456789");                          sec = r[i].file;
                         sec = r[i].file[isec];                          sec += strcspn(sec, "123456789");
                         if ('\0' == sec)                          if (sec[0] == '\0')
                                 continue;                                  continue;
                         prio = sec_prios[sec - '1'];                          prio = sec_prios[sec[0] - '1'];
                         if (NULL == req->q.arch) {                          if (sec[1] != '/')
                                   prio += 10;
                           if (req->q.arch == NULL) {
                                 archprio =                                  archprio =
                                     (NULL == (arch = strchr(                                      ((arch = strchr(sec + 1, '/'))
                                         r[i].file + isec, '/'))) ? 3 :                                          == NULL) ? 3 :
                                     (NULL == (archend = strchr(                                      ((archend = strchr(arch + 1, '/'))
                                         arch + 1, '/'))) ? 0 :                                          == NULL) ? 0 :
                                     strncmp(arch, "amd64/",                                      strncmp(arch, "amd64/",
                                         archend - arch) ? 2 : 1;                                          archend - arch) ? 2 : 1;
                                 if (archprio < archpriouse) {                                  if (archprio < archpriouse) {
Line 807  format(const struct req *req, const char *file)
Line 819  format(const struct req *req, const char *file)
         memset(&conf, 0, sizeof(conf));          memset(&conf, 0, sizeof(conf));
         conf.fragment = 1;          conf.fragment = 1;
         usepath = strcmp(req->q.manpath, req->p[0]);          usepath = strcmp(req->q.manpath, req->p[0]);
         mandoc_asprintf(&conf.man, "%s?query=%%N&sec=%%S%s%s%s%s",          mandoc_asprintf(&conf.man, "/%s%s%%N.%%S",
             scriptname,              usepath ? req->q.manpath : "", usepath ? "/" : "");
             req->q.arch ? "&arch="       : "",  
             req->q.arch ? req->q.arch    : "",  
             usepath     ? "&manpath="    : "",  
             usepath     ? req->q.manpath : "");  
   
         mparse_result(mp, &man, NULL);          mparse_result(mp, &man, NULL);
         if (man == NULL) {          if (man == NULL) {
Line 1005  main(void)
Line 1013  main(void)
                 return EXIT_FAILURE;                  return EXIT_FAILURE;
         }          }
   
         /* Scan our run-time environment. */  
   
         if (NULL == (scriptname = getenv("SCRIPT_NAME")))  
                 scriptname = "";  
   
         if ( ! validate_urifrag(scriptname)) {  
                 fprintf(stderr, "unsafe SCRIPT_NAME \"%s\"\n",  
                     scriptname);  
                 pg_error_internal();  
                 return EXIT_FAILURE;  
         }  
   
         /*          /*
          * First we change directory into the MAN_DIR so that           * First we change directory into the MAN_DIR so that
          * subsequent scanning for manpath directories is rooted           * subsequent scanning for manpath directories is rooted
Line 1031  main(void)
Line 1027  main(void)
         }          }
   
         memset(&req, 0, sizeof(struct req));          memset(&req, 0, sizeof(struct req));
           req.q.equal = 1;
         pathgen(&req);          pathgen(&req);
   
         /* Next parse out the query string. */          /* Parse the path info and the query string. */
   
         if (NULL != (querystring = getenv("QUERY_STRING")))          if ((path = getenv("PATH_INFO")) == NULL)
                   path = "";
           else if (*path == '/')
                   path++;
   
           if (*path != '\0') {
                   path_parse(&req, path);
                   if (access(path, F_OK) == -1)
                           path = "";
           } else if ((querystring = getenv("QUERY_STRING")) != NULL)
                 http_parse(&req, querystring);                  http_parse(&req, querystring);
   
           /* Validate parsed data and add defaults. */
   
         if (req.q.manpath == NULL)          if (req.q.manpath == NULL)
                 req.q.manpath = mandoc_strdup(req.p[0]);                  req.q.manpath = mandoc_strdup(req.p[0]);
         else if ( ! validate_manpath(&req, req.q.manpath)) {          else if ( ! validate_manpath(&req, req.q.manpath)) {
Line 1054  main(void)
Line 1062  main(void)
   
         /* Dispatch to the three different pages. */          /* Dispatch to the three different pages. */
   
         path = getenv("PATH_INFO");  
         if (NULL == path)  
                 path = "";  
         else if ('/' == *path)  
                 path++;  
   
         if ('\0' != *path)          if ('\0' != *path)
                 pg_show(&req, path);                  pg_show(&req, path);
         else if (NULL != req.q.query)          else if (NULL != req.q.query)
Line 1075  main(void)
Line 1077  main(void)
                 free(req.p[i]);                  free(req.p[i]);
         free(req.p);          free(req.p);
         return EXIT_SUCCESS;          return EXIT_SUCCESS;
   }
   
   /*
    * If PATH_INFO is not a file name, translate it to a query.
    */
   static void
   path_parse(struct req *req, const char *path)
   {
           int      dir_done;
   
           req->isquery = 0;
           req->q.equal = 1;
           req->q.manpath = mandoc_strdup(path);
   
           /* Mandatory manual page name. */
           if ((req->q.query = strrchr(req->q.manpath, '/')) == NULL) {
                   req->q.query = req->q.manpath;
                   req->q.manpath = NULL;
           } else
                   *req->q.query++ = '\0';
   
           /* Optional trailing section. */
           if ((req->q.sec = strrchr(req->q.query, '.')) != NULL) {
                   if(isdigit((unsigned char)req->q.sec[1])) {
                           *req->q.sec++ = '\0';
                           req->q.sec = mandoc_strdup(req->q.sec);
                   } else
                           req->q.sec = NULL;
           }
   
           /* Handle the case of name[.section] only. */
           if (req->q.manpath == NULL) {
                   req->q.arch = NULL;
                   return;
           }
           req->q.query = mandoc_strdup(req->q.query);
   
           /* Optional architecture. */
           dir_done = 0;
           for (;;) {
                   if ((req->q.arch = strrchr(req->q.manpath, '/')) == NULL)
                           break;
                   *req->q.arch++ = '\0';
                   if (dir_done || strncmp(req->q.arch, "man", 3)) {
                           req->q.arch = mandoc_strdup(req->q.arch);
                           break;
                   }
   
                   /* Optional directory name. */
                   req->q.arch += 3;
                   if (*req->q.arch != '\0') {
                           free(req->q.sec);
                           req->q.sec = mandoc_strdup(req->q.arch);
                   }
                   dir_done = 1;
           }
 }  }
   
 /*  /*

Legend:
Removed from v.1.116  
changed lines
  Added in v.1.125

CVSweb