Raised This Month: $12 Target: $400
 3% 

Socket: 301 Moved Permanently


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-20-2015 , 07:35   Socket: 301 Moved Permanently
Reply With Quote #1

Hello friends.
I'm trying to get the content of a page. Since I don't know anything about sockets this was the moment to learn, so I read the two tutorials that we have around here and some info from wikipedia about http requests.

My code:
PHP Code:
#include <amxmodx>
#include <sockets>

new HandleSocket

public plugin_init()
{
    new const 
PageHost[] = "forums.alliedmods.net" 
    
new SocketError
    
    
do 
    {
        
HandleSocket socket_open(PageHost80SOCKET_TCPSocketError)
    }
    while(!
HandleSocket)
    
    switch(
SocketError
    {
        case 
0:
        {
            
log_amx("Connection was succesfully created")
        }
        case 
1
        { 
            
log_amx("Socket could not be created"
            return 
        } 
        case 
2
        { 
            
log_amx("Failed to connect to the host"
            return 
        } 
        case 
3
        { 
            
log_amx("Failed to connect to http port"
            return 
        } 
    }  
    
    new 
Request[528]
    
formatex(Requestcharsmax(Request), "GET %s HTTP/1.1^nHost:%s^r^n^r^n",  "/showthread.php?t=250244"PageHost)
    
socket_send(HandleSocketRequestcharsmax(Request))

    
set_task(1.0"WaitForResponse", .flags "a", .repeat 26)
    
}

public 
WaitForResponse()
{
    static 
Count
    
if(socket_change(HandleSocket, ._timeout 100000))
    {
        new 
Response[1024]
        
socket_recv(HandleSocketResponsecharsmax(Response))
        
server_print("Get: %s"Response)  
    }
    if(++
Count >= 25)
    {
        
socket_close(HandleSocket)
    }

Everything seems fine, but the response is always:
Code:
HTTP/1.1 301 Moved Permanently
Date: Sat, 20 Jun 2015 11:29:37 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d69feeb58b4361af03f2bc231df7914651434799777; expires=Sun, 19-Jun-16 11:29:37 GMT;
Code:
301 Moved Permanently
By searching about this, I found that it means that the current url contains a redirection to another url, it waits for the new url to be send but it doesn't. Strange...

Also, I don't think that the ssl encryption is the problem, I heard that encoding doesn't change the way in which query must be send.

My question is why ? What did I do wrong ?
__________________

Last edited by HamletEagle; 06-20-2015 at 07:44.
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-20-2015 , 09:18   Re: Socket: 301 Moved Permanently
Reply With Quote #2

I would just make one attempt to open the socket and then check response, instead of looping like that. The redirect URL may come in a separate connection but I'm not sure.
__________________
Bugsy is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-20-2015 , 09:41   Re: Socket: 301 Moved Permanently
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
I would just make one attempt to open the socket and then check response, instead of looping like that. The redirect URL may come in a separate connection but I'm not sure.
Return code 301 description (as I read on couple of sites) states that new URL should be sent in "Location:" header, and the response he attached doesn't contain one. :/

@HamletEagle,
I think you are missing one carriage return ('^r'). This may not be the actual problem, but I'm just pointing it out.
Code:
formatex(Request, charsmax(Request), "GET %s HTTP/1.1^r^nHost:%s^r^n^r^n",  "/showthread.php?t=250244", PageHost)

klippy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-20-2015 , 10:07   Re: Socket: 301 Moved Permanently
Reply With Quote #4

Quote:
Originally Posted by Bugsy View Post
I would just make one attempt to open the socket and then check response, instead of looping like that. The redirect URL may come in a separate connection but I'm not sure.
I think that if the server is busy or connection is slow the response can be delayed, this is why I used a repetitive task. But tried as you said, it's the same thing.

Code:
HTTP/1.1 301 Moved Permanently
Date: Sat, 20 Jun 2015 14:03:36 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=dda3b22fc4540110aa0badec54eafefd91434809016; expires=Sun, 19-Jun-16 14:03:36 GMT;
Quote:
Originally Posted by KliPPy View Post
Return code 301 description (as I read on couple of sites) states that new URL should be sent in "Location:" header, and the response he attached doesn't contain one. :/

@HamletEagle,
I think you are missing one carriage return ('^r'). This may not be the actual problem, but I'm just pointing it out.
Code:
formatex(Request, charsmax(Request), "GET %s HTTP/1.1^r^nHost:%s^r^n^r^n",  "/showthread.php?t=250244", PageHost)

Tried it, the same.

Also, I tested the plugin linked as an example in this tutorial https://forums.alliedmods.net/showthread.php?t=151401 for the sake of seeing if it works and it has the same problem. It may be something from alliedmods.net configuration ?

Bugsy, or anyone else, can you try to get a page content from AM and see if it works ?
__________________

Last edited by HamletEagle; 06-20-2015 at 11:05.
HamletEagle is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-20-2015 , 10:56   Re: Socket: 301 Moved Permanently
Reply With Quote #5

I just observed Firefox's Network Monitor to see what exactly happens when sending a request (loading a page).
It returns 301 because it tries to redirect you to the secure version (HTTPS) of the forum, instead of HTTP. Looks like you have to send a HTTPS request to retrieve the page. I don't know how it's done, but I believe there's a lot more to it than just a simple request, like with HTTP.

Last edited by klippy; 06-20-2015 at 10:56.
klippy is offline
Destro-
Veteran Member
Join Date: Jun 2010
Location: $me->location();
Old 06-20-2015 , 11:52   Re: Socket: 301 Moved Permanently
Reply With Quote #6

http://forums.alliedmods.net/
Code:
HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Location: https://forums.alliedmods.net/     ( MOVED TO HTTPS )
Connection HTTPS it is not something that can be achieved using only Sockets.
__________________

Last edited by Destro-; 06-20-2015 at 11:55.
Destro- is offline
Shooting King
RAAASENGAN
Join Date: Mar 2012
Location: India
Old 06-21-2015 , 13:57   Re: Socket: 301 Moved Permanently
Reply With Quote #7

Since you are just starting sockets, its better to stick to simple HTTP sites like for example www.example.com.

Working with HTTPS is quite trouble some. Running curl on forums.alliedmods.net (HTTP: 80) gave me follow console output of headers and the page in test.txt.

cmdLog


test.txt

From CmdLog you can see how a request is made by curl, the '>'s, (how you have to make with sockets) and what are the responses received by curl, the '<'s, and what its doing with that response.
__________________
As every time said, don't ever UNDERESTIMATE me.

Donate - Here
Shooting King is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:35.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode