/*
 * http_request.c: functions to get and process requests
 * 
 * All code contained herein is covered by the Copyright as distributed
 * in the README file in the main directory of the distribution of 
 * NCSA HTTPD.
 */

#include "httpd.h"
#include <setjmp.h>
#include "new.h"

char *remote_host = NULL;
char *remote_ip = NULL;
char *remote_name = NULL;
FILE *time_out_fd;
extern JMP_BUF jmpbuffer;
extern int getline_seen_cmd;
extern int getline_buffered_fd;

/* If RFC931 identity check is on, the remote user name */
char *remote_logname;
static void (*exit_callback)();

void send_fd_timed_out(int sigcode) {
    char errstr[MAX_STRING_LEN];

    fclose(time_out_fd);
    if(exit_callback) (*exit_callback)();
    if (sigcode != SIGPIPE) {
      sprintf(errstr,"httpd: send timed out for %s",remote_name);
    } else {
      sprintf(errstr,"httpd: send aborted for %s",remote_name);
    }
    log_error(errstr);
    log_transaction();
    if (in_headers_env) {
	free_env(in_headers_env);
	in_headers_env = NULL;
    }
    if (!standalone) {
   	fclose(stdin); 
    	fclose(stdout); 
	exit(0);
    } else {
      if (remote_host) {
	free(remote_host);
	remote_host = NULL;
      }	
/* Duh.  Free memory in bss */
/*      if (remote_ip) free(remote_ip); */
      if (remote_name) {
	free(remote_name);
	remote_name = NULL;
      }
#if defined(NeXT) || defined(__mc68000__)
      longjmp(jmpbuffer,1);
#else
      siglongjmp(jmpbuffer,1);
#endif
    }
}

/*
  We'll make it return the number of bytes sent
  so that we know if we need to send a body by default
*/
long send_fd(FILE *f, FILE *fd, void (*onexit)())
{
    char buf[IOBUFSIZE];
    long total_bytes_sent;
    register int n,o,w;

    time_out_fd = f;
    exit_callback = onexit;
    signal(SIGALRM,send_fd_timed_out);
    signal(SIGPIPE,send_fd_timed_out); 

    total_bytes_sent = 0;
    while (1) {
        alarm(timeout);
        if((n=fread(buf,sizeof(char),IOBUFSIZE,f)) < 1) {
            break;
        }
        o=0;
        if(bytes_sent != -1)
            bytes_sent += n;
        while(n) {
            w=fwrite(&buf[o],sizeof(char),n,fd);
            n-=w;
            o+=w;
	    total_bytes_sent += w;
        }
    }
    fflush(fd);
    alarm(0);
    signal(SIGALRM,SIG_IGN);
    signal(SIGPIPE,SIG_IGN);
    return total_bytes_sent;
}

void initialize_request() 
{
  /* reset security information to access config defaults */
  reset_security();
  reset_mime_vars();
  
  /* Initialize Error codes */ 
  ErrorStat = 0;
  status = 200;
  
  init_header_vars();
  reset_to_saved_aliases();
  exit_callback = NULL;
  as_requested[0] = '\0';
  
  /* All but HEAD send more than a header */
  header_only = 0;
  
  bytes_sent = -1;
  
  /* Initialize buffered getline */
  getline_buffered_fd = -1;
  getline_seen_cmd = 0;
  
}


void 
process_request(int in, FILE *out) 
{
  char url[HUGE_STRING_LEN];
  char args[HUGE_STRING_LEN];
  char method[HUGE_STRING_LEN];
  char as_requested[HUGE_STRING_LEN];
  char the_request[HUGE_STRING_LEN];

  get_remote_host(in);
  signal(SIGPIPE, send_fd_timed_out); 
  
  if(getline(as_requested, HUGE_STRING_LEN, in, timeout))
        return;
  if(!as_requested[0]) 
    return;
  
  strcpy(the_request, as_requested);
  
  getword(method, as_requested, ' ');
  getword(args, as_requested, ' ');
  getword(url, args, '?');
  unescape_url(url);
  die(REDIRECT, random_url(url), out);
}


static char *
random_url(url)
     char *url;
{
  static char ret[BUFSIZ];
  
  sprintf(buf, "http://%s", random_host());
  strcat(buf, url);
  return(buf);
}
      


static char *
random_host()
{
  static char **server_list = (char **) NULL;
  static int current_server = 0;
  static int num_servers;
  static int last_update = 0;
  
  if((time(0) - last_update) > serverlist_update)
    { 
      server_list = update_server_list(&num_servers);
      last_update = time(0);
    }
  
  if(current_server >= num_servers)
    current_server = 0;

  return(server_list[current_server++]);
}


char **
update_server_list(n)
     int *n;
{
  static char host[MAXHOSTNAMLEN][MAX_SERVERS];

  
}
