AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Socket: 301 Moved Permanently (https://forums.alliedmods.net/showthread.php?t=264645)

HamletEagle 06-20-2015 07:35

Socket: 301 Moved Permanently
 
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 ?

Bugsy 06-20-2015 09:18

Re: Socket: 301 Moved Permanently
 
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.

klippy 06-20-2015 09:41

Re: Socket: 301 Moved Permanently
 
Quote:

Originally Posted by Bugsy (Post 2309831)
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)


HamletEagle 06-20-2015 10:07

Re: Socket: 301 Moved Permanently
 
Quote:

Originally Posted by Bugsy (Post 2309831)
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 (Post 2309837)
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 ?

klippy 06-20-2015 10:56

Re: Socket: 301 Moved Permanently
 
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.

Destro- 06-20-2015 11:52

Re: Socket: 301 Moved Permanently
 
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.

Shooting King 06-21-2015 13:57

Re: Socket: 301 Moved Permanently
 
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.


All times are GMT -4. The time now is 01:42.

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