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

Diff for /mandoc/cgi.c between version 1.6 and 1.7

version 1.6, 2011/11/23 10:01:04 version 1.7, 2011/11/24 12:27:18
Line 63  struct req {
Line 63  struct req {
 };  };
   
 static  int              atou(const char *, unsigned *);  static  int              atou(const char *, unsigned *);
   static  void             format_insecure(const char *);
   static  void             format_secure(const char *);
 static  void             html_print(const char *);  static  void             html_print(const char *);
 static  int              kval_decode(char *);  static  int              kval_decode(char *);
 static  void             kval_parse(struct kval **, size_t *, char *);  static  void             kval_parse(struct kval **, size_t *, char *);
Line 73  static void   pg_search(const struct manpaths *,
Line 75  static void   pg_search(const struct manpaths *,
                                 const struct req *, char *);                                  const struct req *, char *);
 static  void             pg_show(const struct manpaths *,  static  void             pg_show(const struct manpaths *,
                                 const struct req *, char *);                                  const struct req *, char *);
   static  void             resp_bad(void);
 static  void             resp_baddb(void);  static  void             resp_baddb(void);
 static  void             resp_badexpr(const struct req *);  static  void             resp_badexpr(const struct req *);
 static  void             resp_badmanual(void);  static  void             resp_badmanual(void);
Line 83  static void   resp_index(const struct req *);
Line 86  static void   resp_index(const struct req *);
 static  void             resp_search(struct res *, size_t, void *);  static  void             resp_search(struct res *, size_t, void *);
 static  void             resp_searchform(const struct req *);  static  void             resp_searchform(const struct req *);
   
   static  int               insecure = 1;
 static  const char       *progname;  static  const char       *progname;
   static  const char       *cache;
 static  const char       *host;  static  const char       *host;
   
 static  const char * const pages[PAGE__MAX] = {  static  const char * const pages[PAGE__MAX] = {
Line 361  resp_badexpr(const struct req *req)
Line 366  resp_badexpr(const struct req *req)
 }  }
   
 static void  static void
   resp_bad(void)
   {
           resp_begin_html(500, "Internal Server Error");
           puts("<P>Generic badness happened.</P>");
           resp_end_html();
   }
   
   static void
 resp_baddb(void)  resp_baddb(void)
 {  {
   
Line 421  pg_index(const struct manpaths *ps, const struct req *
Line 434  pg_index(const struct manpaths *ps, const struct req *
 }  }
   
 static void  static void
 pg_show(const struct manpaths *ps, const struct req *req, char *path)  format_insecure(const char *file)
 {  {
         pid_t            pid;          pid_t            pid;
           char             cmd[MAXPATHLEN];
   
           strlcpy(cmd, "man=", MAXPATHLEN);
           strlcat(cmd, progname, MAXPATHLEN);
           strlcat(cmd, "/search?expr=%N&sec=%S", MAXPATHLEN);
   
           /* Get ready to call the child mandoc(1) process. */
   
           if (-1 == (pid = fork()))
                   exit(EXIT_FAILURE);
   
           if (pid > 0) {
                   waitpid(pid, NULL, 0);
                   return;
           }
   
           dup2(STDOUT_FILENO, STDERR_FILENO);
   
           puts("Content-Type: text/html; charset=utf-8\n");
   
           fflush(stdout);
   
           execlp("mandoc", "mandoc", "-T",
                   "html", "-O", cmd, file, (char *)NULL);
   }
   
   static void
   format_secure(const char *file)
   {
           char             buf[BUFSIZ];
           int              fd;
           ssize_t          ssz;
   
           if (-1 == (fd = open(file, O_RDONLY, 0))) {
                   resp_baddb();
                   return;
           }
   
           resp_begin_http(200, NULL);
   
           do {
                   ssz = read(fd, buf, BUFSIZ);
                   if (ssz > 0)
                           write(STDOUT_FILENO, buf, ssz);
           } while (ssz > 0);
   
           close(fd);
   }
   
   static void
   pg_show(const struct manpaths *ps, const struct req *req, char *path)
   {
         char            *sub;          char            *sub;
         char             file[MAXPATHLEN], cmd[MAXPATHLEN];          char             file[MAXPATHLEN];
         int              rc;          int              rc;
         unsigned int     vol, rec;          unsigned int     vol, rec;
         DB              *db;          DB              *db;
Line 470  pg_show(const struct manpaths *ps, const struct req *r
Line 535  pg_show(const struct manpaths *ps, const struct req *r
   
         /* Extra filename: the first nil-terminated entry. */          /* Extra filename: the first nil-terminated entry. */
   
           (*db->close)(db);
   
         strlcpy(file, ps->paths[vol], MAXPATHLEN);          strlcpy(file, ps->paths[vol], MAXPATHLEN);
         strlcat(file, "/", MAXPATHLEN);          strlcat(file, "/", MAXPATHLEN);
         strlcat(file, (char *)val.data, MAXPATHLEN);          strlcat(file, (char *)val.data, MAXPATHLEN);
   
         (*db->close)(db);          if ( ! insecure) {
                   strlcat(file, ".html", MAXPATHLEN);
         strlcpy(cmd, "man=", MAXPATHLEN);                  format_secure(file);
         strlcat(cmd, progname, MAXPATHLEN);          } else
         strlcat(cmd, "/search?expr=%N&sec=%S", MAXPATHLEN);                  format_insecure(file);
   
         /* Get ready to call the child mandoc(1) process. */  
   
         if (-1 == (pid = fork()))  
                 exit(EXIT_FAILURE);  
   
         if (pid > 0) {  
                 waitpid(pid, NULL, 0);  
                 return;  
         }  
   
         dup2(STDOUT_FILENO, STDERR_FILENO);  
   
         puts("Content-Type: text/html; charset=utf-8\n");  
   
         fflush(stdout);  
   
         execlp("mandoc", "mandoc", "-T",  
                 "html", "-O", cmd, file, (char *)NULL);  
 }  }
   
 static void  static void
Line 586  main(void)
Line 634  main(void)
         if (NULL == progname)          if (NULL == progname)
                 progname = "";                  progname = "";
   
           cache = getenv("CACHE_DIR");
           if (NULL == cache)
                   cache = "/cache/man.cgi";
   
           if (NULL == getenv("INSECURE")) {
                   insecure = 0;
                   if (-1 == chdir(cache)) {
                           resp_bad();
                           return(EXIT_FAILURE);
                   }
           }
   
         host = getenv("HTTP_HOST");          host = getenv("HTTP_HOST");
         if (NULL == host)          if (NULL == host)
                 host = "localhost";                  host = "localhost";
Line 629  main(void)
Line 689  main(void)
         /* Initialise MANPATH. */          /* Initialise MANPATH. */
   
         memset(&paths, 0, sizeof(struct manpaths));          memset(&paths, 0, sizeof(struct manpaths));
         manpath_parse(&paths, NULL, NULL);          if ( ! insecure)
                   manpath_manconf("etc/man.conf", &paths);
           else
                   manpath_parse(&paths, NULL, NULL);
   
         /* Route pages. */          /* Route pages. */
   

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

CVSweb