i ve decieded to ask this question after reading HTTP 1.1 usage. i don't get why i cannot access even www.google.com . its giving 3xx to 4xx(nearly all errors) error everytime. i ve tried every combination of these GET HOST CONNECT thingys in HTTP/1.1 but still not able to understand it. why my connections rejected/throwed/assumed as unsafe ?
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#define TRUE 1
#define FALSE 0
#define INT_MAX_VAL 100000000
#define MILL 1000000
#define DEF_PORT "80"
typedef enum compOpts
{
higher,
lower,
hEqual,
lEqual,
equal,
nEqual
} compOpts_t;
void checkErr(char *title ,int status ,int trueVal ,compOpts_t compType ,char* (*errFunc)(int errCode));
int main()
{
int status,
servSockFD,
byteCount = 0 ;
struct addrinfo hints ,*servinfo;
struct sockaddr_in *servSock;
char *ip4,
*message = "GET www.google.com HTTP/1.1\r\n\r\n",
*htmlCode;
/**initialization*/
memset(&hints ,0 ,sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
ip4 = (char *)malloc(sizeof(char)*INET_ADDRSTRLEN);
htmlCode = (char *)malloc(sizeof(char)*INT_MAX_VAL);
/**getting server info's*/
status = getaddrinfo("www.google.com" ,DEF_PORT ,&hints ,&servinfo);
checkErr("serv info" ,status ,0 ,equal ,gai_strerror);
/**Checking ipAddress*/
servSock = (struct sockaddr_in *)servinfo->ai_addr;
status = inet_ntop(AF_INET ,&servSock->sin_addr ,ip4,INET_ADDRSTRLEN);
checkErr("ntop" ,status ,0 ,nEqual ,gai_strerror);
printf("IPv4 : %s\n",ip4);
/**creating socket*/
servSockFD = socket(servinfo->ai_family ,servinfo->ai_socktype ,servinfo->ai_protocol);
if (servSockFD == -1)
{
status = errno;
checkErr("socket creation" ,status ,status ,nEqual ,strerror);
}
/**connecting through socket*/
status = connect(servSockFD ,servSock ,servinfo->ai_addrlen);
if(status == -1)
{
status = errno;
checkErr("connection" ,status ,status ,nEqual ,strerror);
}
/**sending through socket*/
do{
byteCount = send(servSockFD ,message ,strlen(message) ,0);
if(byteCount==-1)
{
status = errno;
checkErr("send" ,status ,status ,nEqual ,strerror);
break;
}
}while(byteCount!=strlen(message));
/**recieving html code from socket*/
byteCount = recv(servSockFD ,htmlCode ,INT_MAX_VAL ,0);
if(byteCount == -1)
{
status = errno;
checkErr("recv" ,status ,status ,nEqual ,strerror);
}
printf("%s",htmlCode);
free(ip4);
freeaddrinfo(servinfo);
return 0;
}
void checkErr(char *title ,int status ,int trueVal ,compOpts_t compType ,char* (*errFunc)(int errCode))
{
int res=TRUE;
switch (compType)
{
case hEqual:
if(status != trueVal)
res =FALSE;
case higher:
if(res == TRUE && status<trueVal)
res=FALSE;
break;
case lEqual:
if(status!=trueVal)
res=FALSE;
case lower:
if(status>trueVal)
res=FALSE;
break;
case equal:
if(status!=trueVal)
res=FALSE;
break;
case nEqual:
if(status==trueVal)
res=FALSE;
break;
default:
printf("Error : Unknown comparision type.\n");
break;
}
if(res==TRUE)
printf("%s : succeeded.\n" ,title);
else
printf("%s : failed.\nError Message : %s\n" ,title,errFunc(status));
}
actually i want to get html code of any site using HTTP/1.1 .Guide me , where is wrong thing , what should i do ? i ve stuck at this code for 6 hours :S
NOTE: I will add if i can find its answer anywhere.
You're misunderstanding the spec.
The first line of the request payload should contain either an absolute URI or a path; not a hostname.
The domain name goes in the
Host:
header.For more information, see the spec.