home *** CD-ROM | disk | FTP | other *** search
- /*
- * http_request.c: functions to get and process requests
- *
- * Rob McCool 3/21/93
- *
- * Include code by Charles Henrich
- */
-
-
- #include "httpd.h"
-
- #define OUTBUFSIZE 1024
-
- int assbackwards;
- char *remote_host;
- char *remote_ip;
- char *remote_name;
-
-
- /*
- * Thanks to Charles Henrich
- */
- void process_include(FILE *f, FILE *fd, char *incstring, char *args)
- {
- FILE *ifp;
- char srcfile[HUGE_STRING_LEN];
- char command[HUGE_STRING_LEN];
- char errstr[MAX_STRING_LEN];
- char cmd[10];
-
- if(sscanf(incstring,"%s \"%[^\"]", cmd, srcfile) != 2) {
- sprintf(errstr,"the include string %s was invalid",incstring);
- die(INCLUDE_ERROR,errstr,fd);
- }
-
- if(strncasecmp(cmd,"srv",3) != 0) {
- fprintf(fd, "%s\r\n",cmd);
- return;
- }
-
- if(srcfile[0] != '|') {
- if(num_includes > MAXINCLUDES) {
- die(INCLUDE_ERROR,
- "the maximum number of includes has been exceeded",fd);
- }
-
- num_includes++;
-
- if(translate_name(srcfile,fd) != 0)
- die(INCLUDE_ERROR,"non-standard file include",fd);
- ifp=fopen(srcfile,"r");
-
- if(ifp != NULL) {
- send_fd(ifp, fd, "");
- fclose(ifp);
- }
- else {
- sprintf(errstr,"the include file %s was invalid",srcfile);
- die(INCLUDE_ERROR,errstr,fd);
- }
- }
- else {
- #ifndef NO_INCLUDE_COMMANDS
- if(strncasecmp(cmd,"srvurl",6) == 0) {
- sprintf(command,"%s '%s'",&srcfile[1],args);
- }
- else {
- strcpy(command,&srcfile[1]);
- }
-
- ifp=popen(command,"r");
-
- if(ifp != NULL) {
- send_fd(ifp, fd, args);
- pclose(ifp);
- }
- else {
- sprintf(errstr,"the command %s is invalid",&srcfile[1]);
- die(INCLUDE_ERROR,errstr,fd);
- }
- #else
- die(NOT_IMPLEMENTED, "Not in this version", fd );
- #endif
- }
- }
-
- void send_fd_timed_out() {
- char errstr[MAX_STRING_LEN];
-
- sprintf(errstr,"httpd: send timed out for %s",remote_host);
- log_error(errstr);
- fclose(stdin);
- fclose(stdout);
- exit(0);
- }
-
- void send_fd(FILE *f, FILE *fd, char *args)
- {
- int num_chars=0;
- char c;
- struct stat finfo;
-
- #ifndef AMIGA
- signal(SIGALRM,send_fd_timed_out);
- signal(SIGPIPE,send_fd_timed_out);
- #endif
-
- if((allow_options & OPT_INCLUDES) && is_content_type("text/html")) {
- char *find="<inc";
- char incstring[MAX_STRING_LEN];
- int x,p;
-
- p=0;
- while(1) {
- alarm(timeout);
- c = fgetc(f);
- if(feof(f))
- return;
- if(tolower(c) == find[p]) {
- if((++p) == 4) {
- x=0;
- c=fgetc(f); /* get the space instead of the c */
- while(c != '>') {
- incstring[x++] = c;
- c = fgetc(f);
- if(feof(f)) {
- incstring[x] = '\0';
- fputs("<inc",fd);
- fputs(incstring,fd);
- return;
- }
- }
- incstring[x] = '\0';
- process_include(f,fd,incstring,args);
- p=0;
- }
- }
- else {
- if(p) {
- for(x=0;x<p;x++)
- fputc(find[x],fd);
- p=0;
- }
- fputc(c,fd);
- }
- }
- } else {
- char buf[OUTBUFSIZE];
- int n;
-
- while (1) {
- alarm(timeout);
- if((n=fread(buf,sizeof(char),OUTBUFSIZE,f)) < OUTBUFSIZE) {
- if(n) fwrite(buf,sizeof(char),n,fd);
- break;
- }
- fwrite(buf,sizeof(char),OUTBUFSIZE,fd);
- fflush(fd);
- }
- }
- fflush(fd);
- }
-
- void process_request(int in, FILE *out) {
- char m[HUGE_STRING_LEN];
- char w[HUGE_STRING_LEN];
- char l[HUGE_STRING_LEN];
- char url[HUGE_STRING_LEN];
- char args[HUGE_STRING_LEN];
- int s,n;
-
- get_remote_host(in);
- l[0] = '\0';
- if(getline(l,HUGE_STRING_LEN,in,timeout))
- return;
- if(!l[0])
- return;
-
- log_transaction(l);
- getword(m,l,' ');
- getword(args,l,' ');
- getword(url,args,'?');
- plustospace(url);
- unescape_url(url);
- getword(w,l,'\0');
- if(w[0] != '\0') {
- assbackwards = 0;
- get_mime_headers(in);
- }
- else
- assbackwards = 1;
-
- if(!strcmp(m,"HEAD")) {
- header_only=1;
- process_get(in,out,m,url,args);
- }
- else if(!strcmp(m,"GET")) {
- header_only=0;
- process_get(in,out,m,url,args);
- }
- else if(!strcmp(m,"POST")) {
- header_only = 0;
- get_node(url,args,in,out);
- }
- else
- die(BAD_REQUEST,"Invalid or unsupported method.",out);
- }
-