
#include <netdb.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <signal.h>
#define SIZE sizeof(struct sockaddr_in)

void timeout(int nsig);

int ret,rt;
int allbytes;
time_t t,lastt;

int main() {
    int sockfd;
    int ret,retr,pplen;
    int len;
    char c,rc,i;
    struct hostent *ip;
    char *ptxt="Hello from client !!!";
    char pbuf[1024];
    char pbuffer[1024];
    char rbuffer[65535];
    char tmpbuf[1000];
    char tmptmp[1000];
    char psid[1000];
    char psid0[1000];

    char *ptmp;
    char *ppb,*ppb0;
    char e=(int)255;
    int flag=0,flag1=0;

    struct sockaddr_in server = {
                                    AF_INET,7010
                                };
    printf("%s\n%s\n%s\n%s\n",
           "Tiny browser ver 0.99",
           "Type a uri address of your site (without http) and add with port number",
           "For example type a www.google.com:80 for view a gogle page","Type Ctrl-C for exit");

    for(;;) {

        printf("%s","Enter server name :");
        if(fgets(rbuffer,sizeof(rbuffer),stdin)) {
	    rbuffer[strlen(rbuffer)-1]=0x0;
	    if((ip = gethostbyname(rbuffer))!= NULL){
		bcopy (ip->h_addr, &(server.sin_addr.s_addr), ip->h_length);
	    } else {
		printf("%s %s","Could resolv IP address from hostname",rbuffer);	    
		exit(1);
	    };
        };

        printf("%s","Enter server port :");
        if(fgets(pbuffer,sizeof(pbuffer),stdin)) {
            server.sin_port = htons(atoi(pbuffer));
        };

        if((sockfd= socket(AF_INET, SOCK_STREAM,0))==-1){
	    printf("%s\n","Could make a socket");
            exit(1);
	}

        if( connect(sockfd, (struct sockaddr *) &server, SIZE)==-1){
	    printf("%s\n","Could not connect to server");	
            exit(1);
	}

        memset(pbuffer,'0',1023);
        pbuffer[1024]=0x0;
        ppb=&pbuffer;

        len = sprintf(ppb,"GET http://%s/ HTTP/1.1\n",rbuffer);
        ppb+=len;
        len = sprintf(ppb,"Host: %s\n",rbuffer);
        ppb+=len;
        len = sprintf(ppb,"%s\n","User-Agent: TinyBrowser by SerjiOS");
        ppb+=len;
        len = sprintf(ppb,"%s\n","Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1");
        ppb+=len;
        len = sprintf(ppb,"%s\n","Accept-Encoding: gzip, deflate, compress;q=0.9");
        ppb+=len;
        len = sprintf(ppb,"%s\n","Accept-Charset: ISO-8859-1, utf-8;q=0.66, *;q=0.66");
        ppb+=len;
        len = sprintf(ppb,"%s\n","Keep-Alive: 300");
        ppb+=len;
        len = sprintf(ppb,"%s\n\n\n","Proxy-Connection: keep-alive");
        ppb+=len;

        printf("\n%s\n","------------------------------------------------");
        printf("%s",pbuffer);
        printf("%s\n","------------------------------------------------");
        i=0;

        signal(SIGALRM,timeout);
	alarm(7);

        ret = send(sockfd,pbuffer,strlen(pbuffer),0);

        flag=0;
        for(;!flag;) {
            memset(rbuffer,0,65535);
            if((ret=recv(sockfd,rbuffer,65535,0))>0) {
                printf("%s",rbuffer);
            };

            ppb=0;
            ppb = strstr(rbuffer,"</HTML>");
            if(ppb) {
                flag++;
            };
        };
        close(sockfd);
    };
    exit(0);

}

void timeout(int nsig) {

    printf("%s\n","TIMEOUT !!!");
    lastt = time(NULL);
    allbytes=0;
    exit(0);
};
