/**		Syed Shahzad Ali		**/
/**		CS-742				**/	
/**		login:ssali			**/
/**		getpage				**/
/**		Project # 2			**/


#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, char *argv[]) 
{
int 	sock, rc, s, wc, port;
struct 	sockaddr_in svr;
struct 	hostent *hostptr;
char 	req[1024], response[1024];
char 	*http_ptr;			/* This pointer stores pointer after http word 			*/
char 	buff1[256];			/* This buffer stores FQDN/filename			        */
char 	buff2[256];			/* This buffer stores the file name e.g index.html		*/
char	buff3[256];			/* This buffer stores FQDN e.g www.yahoo.com			*/
int 	i=0;				/* counter variable to check till \0 appears 			*/	
char 	*pt;				/* This pointer stores pointer from end of the string for the   */
					/* occurance of /, to get file name				*/
char	*status_code="200 OK";
FILE	*fp;

        if(argc!=2)
        {
        perror("wrong arguments\n");
        perror("Usage: getpage http://www.yahoo.com/index.html");
        exit(0);
        }

	    /*******	String Parsing Code 		*****/	

  		/** Check for http word in the string **/
            if (strncmp("http://",argv[1],7)==0||strncmp("HTTP://",argv[1],7)==0)
            {
                printf("http is there\n");
                http_ptr=strchr(argv[1],'/');
                http_ptr=http_ptr+2;
                while(*http_ptr!='\0')
                {
                buff1[i++]=*http_ptr;
                http_ptr++;
                }
                buff1[i]='\0';
                //printf("Buffer1 is %s\n",buff1);
            }

            else
            {    
                //printf("NO http\n");
                printf("%s\n",argv[1]);
                strncpy(buff1,argv[1],255);  
                //printf("Buffer1 is %s\n",buff1); 
            }   
                i=0;     
                /** extract file name from the input line **/
                pt=strrchr(buff1,'/');
                    if(pt!=NULL)
                    {
                        pt=pt+1;
                        while(*pt!='\0')
                        {
                        buff2[i++]=*pt;
                        pt++;

    			}
                        buff2[i]='\0';
                        printf("File name is %s\n",buff2);
                    }
                    else
                       {
                       printf("No file specified\n");
                       exit (0);
                       }

                /** Extract FQDN from the input line **/
                i=0;
                while(buff1[i]!='/')
                {
                buff3[i++]=buff1[i];
                }
                buff3[i]='\0';
                printf("Fully Qualified Domain Name is %s\n",buff3);


	   /******** String Parsing Code Ends here ****/


	sock = socket(AF_INET, SOCK_STREAM, 0);
	if(sock<0)
	{ 
          perror("cant open socket\n"); 
	  exit(1);
	}

	/* Lookup server name */
	
	hostptr = gethostbyname(buff3);
	if(hostptr == 0) 
        {
        perror("Unknown server name");
        exit(1);
	}
	memcpy((char *)&svr.sin_addr,(char *)hostptr->h_addr,4);
	

	/* setup server address and port */
	
	svr.sin_family = AF_INET;
//	port = atoi('80');
	svr.sin_port = htons(80);

	s=connect(sock,(struct sockaddr *)&svr,sizeof(svr));
		if(s < 0) {
			   perror("cant connect\n"); 
			   exit(1); 
			  }

                                /* build http protocol request */
				
				strcpy(req,"GET /");
				strcat(req,buff2);
				strcat(req, " HTTP/1.0\r\n\r\n");
				wc = write(sock,req,strlen(req));
				if(wc < 0) 
                                { 
                                perror("cant write\n"); 
                                
				exit(1); 
                                }

				/* read the response on same connection 
		              	   and write it to standard output (file 1) */

				do {
				   rc = read(sock,response,512);
                                    
				   if(strncasecmp(response,status_code,strlen(status_code))==0)
				   {
				   printf("\n\n\n**** ERROR OPENING PAGE ****\n\n\n");	
				   write(1,response,rc);
				   exit (0);
				   }		
				   write(1,response,rc);
				   	if(rc < 0) 
                                   	{ 
                                  	perror("cant read\n"); 
                                   	exit(1); 
                                   	}
			           		
				   } 
				   while(rc > 0);
close(sock);
}

