'How to follow up redirects in c http socket programming
Is it possible to follow a redirect using TCP socket when trying to access a URL that is protected by Cloudflare or a web app that redirects to more than one route
this is my main.c program
connection(soc, "pastebin.com", 80);
char * request = "GET /raw/FLa321fb HTTP/1.1\r\nHost: pastebin.com\r\nLocation: /raw/FLa321fb\r\nConnection: close\r\n\r\n";
send_msg(soc, request); // soc is a socket descriptor
in requests.c I have the following functions
int send_msg(int soc, const char * msg)
{
if (send(soc, msg, strlen(msg),0) < 0)
return -1;
return 0;
}
int recv_msg(int soc)
{
size_t received=0;
char buff[MAX+1] = {};
while (1)
{
received = recv(soc, buff , MAX , 0 );
if (received <= 0 )
break;
else
{
printf("%s",buff);
}
sleep(1);
}
return 0;
}
when I compile and run the program I get the first request only which is the one that is sent to Cloudflare and then my program exits the pastebin link works fine in the web browser but not in my code
HTTP/1.1 301 Moved Permanently
Date: Thu, 28 Apr 2022 10:50:50 GMT
Transfer-Encoding: chunked
Connection: close
Cache-Control: max-age=3600
Expires: Thu, 28 Apr 2022 11:50:50 GMT
Location: https://pastebin.com/raw/FLa321fb
Server: cloudflare
CF-RAY: 70....-BRU
this is the response I get when I use any pastebin.com/raw/<paste-code-here> URL with my code
I tried to use the Location header but this does not seem to fix the issue, I also tried to change the User-Agent with a valid one same as if I was using firefox browser but still got the same issue
I did some research and found out that I need to make a proxy socket and/or use system hooks to be able to follow the redirection but I can't find useful info about this topic
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
