home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / networking / amitcp / httpd.lha / httpd / http_request.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-18  |  4.8 KB  |  208 lines

  1. /*
  2.  * http_request.c: functions to get and process requests
  3.  * 
  4.  * Rob McCool 3/21/93
  5.  * 
  6.  * Include code by Charles Henrich
  7.  */
  8.  
  9.  
  10. #include "httpd.h"
  11.  
  12. #define OUTBUFSIZE 1024
  13.  
  14. int assbackwards;
  15. char *remote_host;
  16. char *remote_ip;
  17. char *remote_name;
  18.  
  19.  
  20. /*
  21.  * Thanks to Charles Henrich
  22.  */
  23. void process_include(FILE *f, FILE *fd, char *incstring, char *args)
  24. {
  25.     FILE *ifp;
  26.     char srcfile[HUGE_STRING_LEN];
  27.     char command[HUGE_STRING_LEN];
  28.     char errstr[MAX_STRING_LEN];
  29.     char cmd[10];
  30.  
  31.     if(sscanf(incstring,"%s \"%[^\"]", cmd, srcfile) != 2) {
  32.         sprintf(errstr,"the include string %s was invalid",incstring);
  33.         die(INCLUDE_ERROR,errstr,fd);
  34.     }                
  35.  
  36.     if(strncasecmp(cmd,"srv",3) != 0) {
  37.         fprintf(fd, "%s\r\n",cmd);
  38.         return;
  39.     }
  40.  
  41.     if(srcfile[0] != '|') {
  42.         if(num_includes > MAXINCLUDES) {
  43.             die(INCLUDE_ERROR,
  44.                 "the maximum number of includes has been exceeded",fd);
  45.         }
  46.  
  47.         num_includes++;
  48.  
  49.         if(translate_name(srcfile,fd) != 0)
  50.             die(INCLUDE_ERROR,"non-standard file include",fd);
  51.         ifp=fopen(srcfile,"r");
  52.  
  53.         if(ifp != NULL) {
  54.             send_fd(ifp, fd, "");
  55.             fclose(ifp);
  56.         }
  57.         else {
  58.             sprintf(errstr,"the include file %s was invalid",srcfile);
  59.             die(INCLUDE_ERROR,errstr,fd);
  60.         }
  61.     }
  62.     else {
  63. #ifndef NO_INCLUDE_COMMANDS
  64.         if(strncasecmp(cmd,"srvurl",6) == 0) {
  65.             sprintf(command,"%s '%s'",&srcfile[1],args);
  66.         }
  67.         else {
  68.             strcpy(command,&srcfile[1]);
  69.         }
  70.  
  71.         ifp=popen(command,"r");
  72.  
  73.         if(ifp != NULL) {
  74.             send_fd(ifp, fd, args);
  75.             pclose(ifp);
  76.         }
  77.         else {
  78.             sprintf(errstr,"the command %s is invalid",&srcfile[1]);
  79.             die(INCLUDE_ERROR,errstr,fd);
  80.         }
  81. #else
  82.     die(NOT_IMPLEMENTED, "Not in this version", fd );
  83. #endif
  84.     }
  85. }
  86.  
  87. void send_fd_timed_out() {
  88.     char errstr[MAX_STRING_LEN];
  89.  
  90.     sprintf(errstr,"httpd: send timed out for %s",remote_host);
  91.     log_error(errstr);
  92.     fclose(stdin);
  93.     fclose(stdout);
  94.     exit(0);
  95. }
  96.  
  97. void send_fd(FILE *f, FILE *fd, char *args)
  98. {
  99.     int num_chars=0;
  100.     char c;
  101.     struct stat finfo;
  102.  
  103. #ifndef AMIGA
  104.     signal(SIGALRM,send_fd_timed_out);
  105.     signal(SIGPIPE,send_fd_timed_out);
  106. #endif
  107.  
  108.     if((allow_options & OPT_INCLUDES) && is_content_type("text/html")) {
  109.         char *find="<inc";
  110.         char incstring[MAX_STRING_LEN];
  111.         int x,p;
  112.  
  113.         p=0;
  114.         while(1) {
  115.             alarm(timeout);
  116.             c = fgetc(f);
  117.             if(feof(f))
  118.                 return;
  119.             if(tolower(c) == find[p]) {
  120.                 if((++p) == 4) {
  121.                     x=0;
  122.                     c=fgetc(f); /* get the space instead of the c */
  123.                     while(c != '>') {
  124.                         incstring[x++] = c;
  125.                         c = fgetc(f);
  126.                         if(feof(f)) {
  127.                             incstring[x] = '\0';
  128.                             fputs("<inc",fd);
  129.                             fputs(incstring,fd);
  130.                             return;
  131.                         }
  132.                     }
  133.                     incstring[x] = '\0';
  134.                     process_include(f,fd,incstring,args);
  135.                     p=0;
  136.                 }
  137.             }
  138.             else {
  139.                 if(p) {
  140.                     for(x=0;x<p;x++)
  141.                         fputc(find[x],fd);
  142.                     p=0;
  143.                 }
  144.                 fputc(c,fd);
  145.             }
  146.         }
  147.     } else {
  148.         char buf[OUTBUFSIZE];
  149.         int n;
  150.  
  151.         while (1) {
  152.             alarm(timeout);
  153.             if((n=fread(buf,sizeof(char),OUTBUFSIZE,f)) < OUTBUFSIZE) {
  154.                 if(n) fwrite(buf,sizeof(char),n,fd);
  155.                 break;
  156.             }
  157.             fwrite(buf,sizeof(char),OUTBUFSIZE,fd);
  158.             fflush(fd);
  159.         }
  160.     }
  161.     fflush(fd);
  162. }
  163.  
  164. void process_request(int in, FILE *out) {
  165.     char m[HUGE_STRING_LEN];
  166.     char w[HUGE_STRING_LEN];
  167.     char l[HUGE_STRING_LEN];
  168.     char url[HUGE_STRING_LEN];
  169.     char args[HUGE_STRING_LEN];
  170.     int s,n;
  171.  
  172.     get_remote_host(in);
  173.     l[0] = '\0';
  174.     if(getline(l,HUGE_STRING_LEN,in,timeout))
  175.         return;
  176.     if(!l[0]) 
  177.         return;
  178.  
  179.     log_transaction(l);
  180.     getword(m,l,' ');
  181.     getword(args,l,' ');
  182.     getword(url,args,'?');
  183.     plustospace(url);
  184.     unescape_url(url);
  185.     getword(w,l,'\0');
  186.     if(w[0] != '\0') {
  187.         assbackwards = 0;
  188.         get_mime_headers(in);
  189.     }
  190.     else
  191.         assbackwards = 1;
  192.  
  193.     if(!strcmp(m,"HEAD")) {
  194.         header_only=1;
  195.         process_get(in,out,m,url,args);
  196.     }
  197.     else if(!strcmp(m,"GET")) {
  198.         header_only=0;
  199.         process_get(in,out,m,url,args);
  200.     }
  201.     else if(!strcmp(m,"POST")) {
  202.         header_only = 0;
  203.         get_node(url,args,in,out);
  204.     }
  205.     else 
  206.         die(BAD_REQUEST,"Invalid or unsupported method.",out);
  207. }
  208.