/*
  httpproxy.c
  by Yusuke SHINYAMA (euske@cl.cs.titech.ac.jp)
*/

#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <netdb.h>

#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifdef SOCKS
#include <socks.h>
#endif

#define BUFFSIZE 1024

static int quiteof = 0;

/*
  data transfer
*/
static void doloop(int sock)
{
    fd_set fds;
    int stdin_ok = 1;
    FD_ZERO(&fds);
    while(stdin_ok) {
	FD_SET(sock, &fds);
	if (stdin_ok) FD_SET(STDIN_FILENO, &fds);
	if (0 < select(sock+1, &fds, NULL, NULL, NULL)) {
	    char buff[BUFFSIZE];
	    long len;
	    if (stdin_ok && FD_ISSET(STDIN_FILENO, &fds)) {
		len = read(STDIN_FILENO, buff, sizeof(buff));
#ifdef DEBUG
		fprintf(stderr, "stdin: len=%ld\n", len);
#endif
		if (0 < len) {
		    write(sock, buff, len);
		} else {
		    if (quiteof) exit(0);
		    stdin_ok = 0;
		}
	    }
	    if (FD_ISSET(sock, &fds)) {
		len = read(sock, buff, sizeof(buff));
#ifdef DEBUG
		fprintf(stderr, "sock: len=%ld\n", len);
#endif
		if (0 < len) {
		    write(STDOUT_FILENO, buff, len);
		} else {
		    exit(0);
		}
	    }
	}
    }
    exit(0);
}

/*
  message output in html
*/
static void die(char* s)
{
    printf("HTTP/1.0 300 ERROR\r\n\r\n");
    printf("<html><body>%s</body></html>\r\n", s);
    fprintf(stderr,"ERROR: %s\n", s);
    exit(1);
}


/*
  setup connection
*/
int main(int argc, char** argv)
{
    struct sockaddr_in sin1;
    struct in_addr host1;
    int port1, sock1;
    static char buff0[1024];
    static char hostname[1024];
    long len;

#ifdef SOCKS
    {
	char* progname;
	if ((progname = strrchr(argv[0], '/')) == NULL)
	    progname = argv[0];
	else 
	    progname++;
	SOCKSinit(progname);
    }
#endif

    {    
	char *hp = hostname, *p = buff0;
	int i;
	len = read(STDIN_FILENO, buff0, sizeof(buff0));
	i = len;
	if (i <= 0) exit(1);
	while(i-- && isalpha(*p)) p++;
	if (!i) die("bad request");
	while(i-- && isspace(*p)) p++;
	if (!i) die("bad request");
	if (strncmp("http://", p, 7)) die("bad request");
	p+=7;
	while(i-- && ' ' < *p && *p != '/' && *p != ':') *(hp++) = *(p++);
	*hp = '\0';
	port1 = 80;
	if (*p == ':') port1 = atoi(p+1);
	if (port1 != 80 && port1 < 1024) die("illegal port");
    }
    fprintf(stderr, "REQUEST: %s, %d\n", hostname, port1);

    /* lookup the host */
    if (!inet_aton(hostname, &host1)) {
	struct hostent *hent;
	hent = gethostbyname(hostname);
	if (hent == NULL) {
	    die("host not found");
	    exit(2);
	}
	memcpy(&host1, hent->h_addr_list[0], sizeof(host1));
    }
    
#ifdef DEBUG
    fprintf(stderr, "host=%lx, port=%d\n", *(unsigned long*)&host1, port1);
#endif

    /* connect */
    memset(&sin1, 0, sizeof(sin1));
    sin1.sin_family = AF_INET;
    sin1.sin_port = htons(port1);
    memcpy(&sin1.sin_addr.s_addr, &host1, sizeof(sin1.sin_addr.s_addr));

    if ((sock1 = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
	die("socket error");
	exit(4);
    }
    if (connect(sock1, (struct sockaddr*)&sin1, sizeof(sin1)) < 0) {
	die("connection refused");
	exit(5);
    }

    fprintf(stderr, "CONNECTED: %s, %d\n", hostname, port1);
    /* start data transfer */
    write(sock1, buff0, len);
    doloop(sock1);
    /*NOTREACHED*/
    return(0);
}
