home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / linux / apache / contrib / modules / mod_sp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  5.1 KB  |  175 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54. /*
  55.  * mod_sp.c: server-push
  56.  *
  57.  * Sameer Parekh
  58.  */
  59.  
  60. #include "httpd.h"
  61. #include "http_config.h"
  62. #include "http_request.h"
  63. #include "http_core.h"
  64. #include "http_protocol.h"
  65. #include "http_main.h"
  66. #include "http_log.h"
  67. #include "util_script.h"
  68.  
  69. #define BOUNDARY "2387lcskdncluvw3doiqefdup9w47dwechsdkcsdfv"
  70.  
  71. /* Process the animation file */
  72. void do_animation(FILE *f, request_rec *r)
  73. {
  74.   int errstatus, time;
  75.   char buf[MAX_STRING_LEN];
  76.   char fname[MAX_STRING_LEN], ctype[MAX_STRING_LEN];
  77.   FILE *fp;
  78.   struct stat st;
  79.  
  80.   while(fgets(buf, MAX_STRING_LEN, f))
  81.     {
  82.       /* parse 'buf' */
  83.       if(sscanf(buf, "File: %s Type: %s", fname, ctype) == 2)
  84.     {
  85.       /* Get a file */
  86.  
  87.       /* Check permissions, etc. */
  88.       if(stat(fname, &st))
  89.         {
  90.           log_reason("can't stat", fname, r);
  91.           continue;
  92.         }
  93.       if(st.st_uid != r->finfo.st_uid)
  94.         {
  95.           log_reason("owners don't match", fname, r);
  96.           continue;
  97.         }
  98.       
  99.       fp = fopen(fname, "r");
  100.       if(fp == NULL)
  101.         {
  102.           log_reason("can't open", fname, r);
  103.           continue;
  104.         }
  105.       /* Send the file */
  106.       rprintf(r, "--%s\n", BOUNDARY);
  107.       rprintf(r, "Content-type: %s\n\n", ctype);
  108.  
  109.       send_fd (fp, r);
  110.       rputc('\n', r);
  111.       fclose(fp);
  112.     }
  113.       /* Sleep directive */
  114.       else if(sscanf(buf, "Sleep: %d", &time) == 1)
  115.     {
  116.       sleep(time);
  117.     }
  118.     }
  119.   rprintf(r, "--%s--\n", BOUNDARY);
  120. }
  121.  
  122. int sp_handler (request_rec *r)
  123. {
  124.     int errstatus, ret;
  125.     FILE *f;
  126.  
  127.     if (r->method_number != M_GET) return DECLINED;
  128.     if (r->finfo.st_mode == 0) {
  129.     log_reason("File does not exist", r->filename, r);
  130.     return NOT_FOUND;
  131.     }
  132.  
  133.     f = fopen (r->filename, "r");
  134.  
  135.     if (f == NULL) {
  136.         log_reason("file permissions deny server access", r->filename, r);
  137.         return FORBIDDEN;
  138.     }
  139.  
  140.     soft_timeout ("send", r);
  141.  
  142.     /* Set content-type */
  143.     r->content_type = (char *) palloc(r->pool, MAX_STRING_LEN);
  144.     sprintf(r->content_type, "multipart/x-mixed-replace;boundary=%s\n",
  145.       BOUNDARY);
  146.  
  147.     send_http_header (r);
  148.     if (!r->header_only) do_animation(f, r);
  149.     fclose(f);
  150.     return OK;
  151. }
  152.  
  153. handler_rec sp_handlers[] = {
  154. { "application/x-httpd-serverpush", sp_handler },
  155. { NULL }
  156. };
  157.  
  158. module sp_module = {
  159.    STANDARD_MODULE_STUFF,
  160.    NULL,            /* initializer */
  161.    NULL,            /* dir config creater */
  162.    NULL,            /* dir merger --- default is to override */
  163.    NULL,            /* server config */
  164.    NULL,            /* merge server config */
  165.    NULL,            /* command table */
  166.    sp_handlers,            /* handlers */
  167.    NULL,            /* filename translation */
  168.    NULL,            /* check_user_id */
  169.    NULL,            /* check auth */
  170.    NULL,            /* check access */
  171.    NULL,            /* type_checker */
  172.    NULL,            /* fixups */
  173.    NULL                /* logger */
  174. };
  175.