home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW rman 1.3.4 / contrib / http-rman.c next >
Encoding:
C/C++ Source or Header  |  1995-11-08  |  4.9 KB  |  201 lines  |  [TEXT/KAHL]

  1. /* $Id: http-rman.c,v 1.2 1994/05/15 14:55:12 fredrik Exp $
  2.  *
  3.  * Name:
  4.  *    http-rman.c -- a rudimentary man-page HTTP server
  5.  *
  6.  * Description:
  7.  *    This is a minimal HTTP server using RosettaMan by T.A. Phelps
  8.  *    (phelps@cs.berkeley.edu) to produce hypertext renditions
  9.  *    of man-pages on the fly.
  10.  *
  11.  *    This server processes URLs with the following syntax:
  12.  *
  13.  *        [/man] ?<command> [ ?<section> ]
  14.  *
  15.  *    For URLs matching this format, it pipes the output of
  16.  *    man <section> <command> through rman and sends it to
  17.  *    the HTTP client. For other URLs, it returns the document
  18.  *    given as argv[1] (using cat(1). The leading /man is
  19.  *    optional, but is strongly recommended.
  20.  *
  21.  *    This server is shipped as two files, the http-rman.c
  22.  *    sources and the http-rman.html sample frontpage. I have
  23.  *    not included a Makefile; you can write your own or just
  24.  *    type [g]cc -o http-rman http-rman.c
  25.  *
  26.  * What do I need to run this:
  27.  *    If you don't have it, pick up RosettaMan by anonymous ftp
  28.  *    from ftp.cs.berkeley.edu: /ucb/people/phelps/tcl/rman.tar.Z
  29.  *
  30.  *    You'll also need an HTTP client such as NCSA Mosaic to talk
  31.  *    to this server. Mosaic is available by anonymous FTP from
  32.  *    ftp://ftp.ncsa.uiuc.edu/Mosaic 
  33.  *
  34.  *    Both RosettaMan (rman) and Mosaic are available from many
  35.  *    other sites. Try Archie, or check your local or national net
  36.  *    archive.
  37.  *
  38.  * How do I get it running:
  39.  *    First, compile the server (see above), and install it
  40.  *    somewhere.
  41.  *
  42.  *    The server runs under inetd(8). Add a service to
  43.  *    /etc/services, say:
  44.  *
  45.  *        http-rman 4080/tcp
  46.  *
  47.  *    If you're not about to install another HTTP server on your
  48.  *    machine, you may use the default HTTP port, 80, instead.
  49.  *
  50.  *    Then add an entry to /etc/inetd.conf, such as (on a single line):
  51.  *
  52.  *        http-rman stream tcp nowait root /usr/local/bin/http-rman
  53.  *            http-rman /usr/local/lib/rman/http-rman.html
  54.  *
  55.  *    Change /usr/local/bin and /usr/local/lib/rman to where you
  56.  *    installed the two files. In addition, you may wish to run
  57.  *    the server as something other than root...
  58.  *
  59.  *    Restart inetd(8) (use kill -HUP or kill it and start it again)
  60.  *    and try the following:
  61.  *
  62.  *    $ Mosaic http://localhost:4080
  63.  *
  64.  *    If you don't have Mosaic, try the following instead:
  65.  *
  66.  *    $ telnet localhost 4080
  67.  *    Trying 127.0.0.1...
  68.  *    Connected to localhost.
  69.  *    Escape character is '^]'.
  70.  *    GET /man?ls <return>
  71.  *    <return>
  72.  *    HTTP/1.0 200 OK
  73.  *    ...
  74.  *
  75.  * Portability:
  76.  *    You'll need an ANSI compiler (or an editor and some patience).
  77.  *    As it stands right now, this code has been successfully
  78.  *    compiled on OSF/1 AXP using cc, and on SunOS 4.1 using gcc.
  79.  *    Might need some tuning for other platforms.
  80.  *
  81.  * Legal Issues:
  82.  *    Check the external visibility of the http-rman service
  83.  *    you choose. This server gives a user access to ALL man-
  84.  *    pages on your machine. You may have installed copyrighted
  85.  *    software (your operating system, for example) with
  86.  *    man-pages that you are NOT allowed to make visible for
  87.  *    anyone out there...
  88.  *
  89.  * History:
  90.  *    94-04-30 fl: created
  91.  *    94-05-13 fl: stripped away everything but rman support
  92.  *
  93.  * Copyright (c) Fredrik Lundh 1994 (fredrik_lundh@ivab.se)
  94.  * All rights reserved.
  95.  */
  96.  
  97.  
  98. #include <stdio.h>        /* printf(), fflush(stdio) etc */
  99. #include <string.h>        /* strrchr(), strcmp() etc */
  100.  
  101.  
  102. static int
  103. http_error(int error)
  104. {
  105.     char *p;
  106.  
  107.     switch (error) {
  108.     case 400:
  109.     p = "Bad Request";
  110.     break;
  111.     case 404:
  112.     p = "Not Found";
  113.     break;
  114.     default:
  115.     p = "Error";
  116.     }
  117.     printf("HTTP/1.0 %d %s\r\n", error, p);
  118.     printf("MIME-version: 1.0\r\n");
  119.     printf("Content-Type: text/html\r\n\r\n");
  120.     printf("<head><title>%d %s</title></head>\r\n", error, p);
  121.     printf("<body><h1>%d %s</h1></body>\r\n", error, p);
  122.     return 0;
  123. }
  124.  
  125.  
  126. static int
  127. http_rman(char *url)
  128. {
  129.     char *pName;
  130.     char *pSection;
  131.     char buf[200];
  132.  
  133.     /* parse URL: should be /man?command[?section] */
  134.     pSection = strrchr(url, '?');
  135.     if (!pSection) {
  136.     return -1;
  137.     }
  138.     pName = pSection-1;
  139.     *pSection++ = '\0';
  140.  
  141.     pName = strrchr(url, '?');
  142.     if (!pName) {
  143.     pName = pSection;
  144.     pSection = "";
  145.     }
  146.     else
  147.     pName++;
  148.  
  149.     sprintf(buf, "man %s %s | rman -r \"man?%%s?%%s\" -n %s -f html",
  150.         pSection, pName, pName);
  151.  
  152.     return system(buf);
  153. }
  154.  
  155.  
  156. int
  157. main(int ac, char **av)
  158. {
  159.     char buf[200];
  160.     char url[200];
  161.     int status;
  162.     char *sFrontpage = "/usr/local/lib/rman/http-rman.html";
  163.  
  164. /* check arguments */
  165.  
  166.     if (ac > 1)
  167.     sFrontpage = av[1];
  168.  
  169. /* just read in one line from stdin and make sure it is a GET
  170. command */
  171.  
  172.     if (gets(buf)) {
  173.  
  174.     /* read rest of command (just for the sake of it) */
  175.     while (gets(url) && url[0] != '\r')
  176.         ;
  177.  
  178.     /* command should be GET <url> [HTTP/1.0] */
  179.     if (sscanf(buf, "GET %s", url) == 1) {
  180.  
  181.         status  = http_rman(url);
  182.  
  183.         if (status < 0) {
  184.         sprintf(buf, "cat %s", sFrontpage);
  185.         if (system(buf) == 0)
  186.             status = 0;
  187.         }
  188.  
  189.         if (status < 0)
  190.         http_error(404);
  191.  
  192.     } else
  193.  
  194.         http_error(400);
  195.     }
  196.  
  197.     fflush(stdout);
  198.  
  199.     exit(0);
  200. }
  201.