home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / HTTPD / HTTPD_SO.TAR / httpd_1.3 / src / http_request.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-07  |  3.6 KB  |  167 lines

  1. /*
  2.  * http_request.c: functions to get and process requests
  3.  * 
  4.  * Rob McCool 3/21/93
  5.  * 
  6.  */
  7.  
  8.  
  9. #include "httpd.h"
  10.  
  11. int assbackwards;
  12. char *remote_host;
  13. char *remote_ip;
  14. char *remote_name;
  15.  
  16. /* If RFC931 identity check is on, the remote user name */
  17. char *remote_logname;
  18. static void (*exit_callback)();
  19.  
  20. void send_fd_timed_out() {
  21.     char errstr[MAX_STRING_LEN];
  22.  
  23.     if(exit_callback) (*exit_callback)();
  24.     sprintf(errstr,"httpd: send timed out for %s",remote_name);
  25.     log_error(errstr);
  26.     log_transaction();
  27.     fclose(stdin);
  28.     fclose(stdout);
  29.     exit(0);
  30. }
  31.  
  32. void send_fd(FILE *f, FILE *fd, void (*onexit)())
  33. {
  34.     char buf[IOBUFSIZE];
  35.     register int n,o,w;
  36.  
  37.     exit_callback = onexit;
  38.     signal(SIGALRM,send_fd_timed_out);
  39.     signal(SIGPIPE,send_fd_timed_out);
  40.  
  41.     while (1) {
  42.         alarm(timeout);
  43.         if((n=fread(buf,sizeof(char),IOBUFSIZE,f)) < 1) {
  44.             break;
  45.         }
  46.         o=0;
  47.         if(bytes_sent != -1)
  48.             bytes_sent += n;
  49.         while(n) {
  50.             w=fwrite(&buf[o],sizeof(char),n,fd);
  51.             n-=w;
  52.             o+=w;
  53.         }
  54.     }
  55.     fflush(fd);
  56. }
  57.  
  58. int find_script(char *method, char *name, char *args, int in, FILE *out) 
  59. {
  60.     int n=count_dirs(name),i;
  61.     char t[HUGE_STRING_LEN],ct_bak[MAX_STRING_LEN];
  62.     struct stat finfo;
  63.     
  64.     strcpy(ct_bak,content_type);
  65.     for(i=n;i;--i) {
  66.         make_dirstr(name,i,t);
  67.         probe_content_type(t);
  68.         if(!strcmp(content_type,CGI_MAGIC_TYPE)) {
  69.             char pa[HUGE_STRING_LEN];
  70.             int l=strlen(t);
  71.             
  72.             if(stat(t,&finfo) == -1)
  73.                 continue;
  74.             if(!(S_ISREG(finfo.st_mode)))
  75.                 return 0;
  76.             strcpy(pa,&name[l]);
  77.             name[l] = '\0';
  78.             strcpy(content_type,ct_bak);
  79.             send_cgi(method,name,pa,args,&finfo,in,out);
  80.             return 1;
  81.         }
  82.     }
  83.     return 0;
  84. }
  85.  
  86.  
  87. void process_request(int in, FILE *out) {
  88.     char m[HUGE_STRING_LEN];
  89.     char w[HUGE_STRING_LEN];
  90.     char l[HUGE_STRING_LEN];
  91.     char url[HUGE_STRING_LEN];
  92.     char args[HUGE_STRING_LEN];
  93.     int s,n;
  94.  
  95.     get_remote_host(in);
  96.     exit_callback = NULL;
  97.     signal(SIGPIPE,send_fd_timed_out);
  98.  
  99. #ifdef PEM_AUTH
  100.     doing_pem = -1;
  101.   handle_request:
  102. #endif
  103.     l[0] = '\0';
  104.     if(getline(l,HUGE_STRING_LEN,in,timeout))
  105.         return;
  106.     if(!l[0]) 
  107.         return;
  108.  
  109.     record_request(l);
  110.     getword(m,l,' ');
  111.     getword(args,l,' ');
  112.     getword(url,args,'?');
  113. /*    plustospace(url); */
  114.     unescape_url(url);
  115.     getword(w,l,'\0');
  116.     init_header_vars();
  117.     if(w[0] != '\0') {
  118.         assbackwards = 0;
  119.         get_mime_headers(in,out);
  120.     }
  121.     else
  122.         assbackwards = 1;
  123.         
  124.     if(!strcmp(m,"HEAD")) {
  125.         header_only=1;
  126.         process_get(in,out,m,url,args);
  127.     }
  128.     else if(!strcmp(m,"GET")) {
  129. #ifdef PEM_AUTH
  130.         if(!assbackwards) {
  131.             if(doing_pem == -1) {
  132.                 int s2;
  133.                 s2 = decrypt_request(in,url,&out);
  134.                 if(s2 != -1) {
  135.                     in = s2;
  136.                     content_type[0] = '\0';
  137.                     goto handle_request;
  138.                 }
  139.             }
  140.         }
  141. #endif
  142.         header_only=0;
  143.         process_get(in,out,m,url,args);
  144.     }
  145.     else if(!strcmp(m,"POST")) {
  146.         header_only = 0;
  147.         post_node(url,args,in,out);
  148.     }
  149.     else if(!strcmp(m,"PUT")) {
  150.         header_only = 0;
  151.         put_node(url,args,in,out);
  152.     }
  153.     else if(!strcmp(m,"DELETE")) {
  154.         header_only = 0;
  155.         delete_node(url,args,in,out);
  156.     }
  157.     else 
  158.         die(BAD_REQUEST,"Invalid or unsupported method.",out);
  159.  
  160. #ifdef PEM_AUTH
  161.     if(doing_pem != -1) {
  162.         close(in);
  163.         htexit(0,out);
  164.     }
  165. #endif
  166. }
  167.