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(PageHost, 80, SOCKET_TCP, SocketError) } 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 } }
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.
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 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.
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.
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.
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 ?
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.
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
PHP Code:
WIN>curl forums.alliedmods.net -L -v > test.txt
* Rebuilt URL to: forums.alliedmods.net/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0*
Trying 104.20.3.42...
* Connected to forums.alliedmods.net (104.20.3.42) port 80 (#0)
> GET / HTTP/1.1
> Host: forums.alliedmods.net
> User-Agent: curl/7.42.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Sun, 21 Jun 2015 17:43:21 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Set-Cookie: __cfduid=d302d713cdca76d28d26f565c7d6f28da1434908600; expires=Mon,
20-Jun-16 17:43:20 GMT; path=/; domain=.alliedmods.net; HttpOnly
< Location: https://forums.alliedmods.net/
< Server: cloudflare-nginx
< CF-RAY: 1fa183e42fd100c4-HKG
<
* Ignoring the response-body
{ [195 bytes data]
100 184 0 184 0 0 196 0 --:--:-- --:--:-- --:--:-- 199
* Connection #0 to host forums.alliedmods.net left intact
* Issue another request to this URL: 'https://forums.alliedmods.net/'
* Found bundle for host forums.alliedmods.net: 0x4b1d78
* Trying 104.20.3.42...
* Connected to forums.alliedmods.net (104.20.3.42) port 443 (#1)
* schannel: SSL/TLS connection with forums.alliedmods.net port 443 (step 1/3)
* schannel: checking server certificate revocation
* schannel: sending initial handshake data: sending 167 bytes...
* schannel: sent initial handshake data: sent 167 bytes
* schannel: SSL/TLS connection with forums.alliedmods.net port 443 (step 2/3)
* schannel: failed to receive handshake, need more data
* schannel: SSL/TLS connection with forums.alliedmods.net port 443 (step 2/3)
* schannel: encrypted data buffer: offset 3752 length 4096
* schannel: encrypted data length: 1030
* schannel: encrypted data buffer: offset 1030 length 4096
* schannel: received incomplete message, need more data
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0*
schannel: SSL/TLS connection with forums.alliedmods.net port 443 (step 2/3)
* schannel: encrypted data buffer: offset 2059 length 4096
* schannel: sending next handshake data: sending 166 bytes...
* schannel: SSL/TLS connection with forums.alliedmods.net port 443 (step 2/3)
* schannel: encrypted data buffer: offset 91 length 4096
* schannel: SSL/TLS handshake complete
* schannel: SSL/TLS connection with forums.alliedmods.net port 443 (step 3/3)
* schannel: incremented credential handle refcount = 1
* schannel: stored credential handle in session cache
> GET / HTTP/1.1
> Host: forums.alliedmods.net
> User-Agent: curl/7.42.1
> Accept: */*
>
0 0 0 0 0 0 0 0 --:--:-- 0:00:03 --:--:-- 0*
schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 0 length 16384
* schannel: encrypted data got 4165
* schannel: encrypted data buffer: offset 4165 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 4096 length 16384
* schannel: decrypted data buffer: offset 4096 length 16384
* schannel: decrypted data returned 4096
* schannel: decrypted data buffer: offset 0 length 16384
< HTTP/1.1 200 OK
< Server: cloudflare-nginx
< Date: Sun, 21 Jun 2015 17:43:23 GMT
< Content-Type: text/html; charset=ISO-8859-1
< Transfer-Encoding: chunked
< Connection: keep-alive
< Set-Cookie: __cfduid=d37bae4b36811cb5b5e01594e10d3bdce1434908602; expires=Mon,
20-Jun-16 17:43:22 GMT; path=/; domain=.alliedmods.net; HttpOnly
< X-Powered-By: PHP/5.5.13
< Cache-Control: private
< Pragma: private
< X-UA-Compatible: IE=7
< Set-Cookie: bblastvisit=1434908603; expires=Mon, 20-Jun-2016 17:43:23 GMT; Max
-Age=31536000; path=/; secure
< Set-Cookie: bblastactivity=0; expires=Mon, 20-Jun-2016 17:43:23 GMT; Max-Age=3
1536000; path=/; secure
< CF-RAY: 1fa183ee52ae123d-HKG
<
{ [3440 bytes data]
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 0 length 16384
* schannel: encrypted data got 8330
* schannel: encrypted data buffer: offset 8330 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 4096 length 16384
* schannel: encrypted data length: 4165
* schannel: encrypted data cached: offset 4165 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 8192 length 16384
* schannel: decrypted data buffer: offset 8192 length 16384
* schannel: decrypted data returned 8192
* schannel: decrypted data buffer: offset 0 length 16384
{ [8192 bytes data]
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 0 length 16384
* schannel: encrypted data got 1398
* schannel: encrypted data buffer: offset 1398 length 16384
* schannel: failed to decrypt data, need more data
100 11618 0 11618 0 0 2512 0 --:--:-- 0:00:04 --:--:-- 3872*
schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 1398 length 16384
* schannel: encrypted data got 1398
* schannel: encrypted data buffer: offset 2796 length 16384
* schannel: failed to decrypt data, need more data
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 2796 length 16384
* schannel: encrypted data got 3752
* schannel: encrypted data buffer: offset 6548 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 4096 length 16384
* schannel: encrypted data length: 2383
* schannel: encrypted data cached: offset 2383 length 16384
* schannel: decrypted data length: 10
* schannel: decrypted data added: 10
* schannel: decrypted data cached: offset 4106 length 16384
* schannel: encrypted data length: 2314
* schannel: encrypted data cached: offset 2314 length 16384
* schannel: failed to decrypt data, need more data
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 2314 length 16384
* schannel: encrypted data got 7504
* schannel: encrypted data buffer: offset 9818 length 16384
* schannel: decrypted data length: 2384
* schannel: decrypted data added: 2384
* schannel: decrypted data cached: offset 6490 length 16384
* schannel: encrypted data length: 7365
* schannel: encrypted data cached: offset 7365 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 10586 length 16384
* schannel: encrypted data length: 3200
* schannel: encrypted data cached: offset 3200 length 16384
* schannel: failed to decrypt data, need more data
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 3200 length 16384
* schannel: encrypted data got 7504
* schannel: encrypted data buffer: offset 10704 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 14682 length 16384
* schannel: encrypted data length: 6539
* schannel: encrypted data cached: offset 6539 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 18778 length 18778
* schannel: encrypted data length: 2374
* schannel: encrypted data cached: offset 2374 length 16384
* schannel: decrypted data buffer: offset 18778 length 18778
* schannel: decrypted data returned 16384
* schannel: decrypted data buffer: offset 2394 length 18778
{ [16384 bytes data]
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 2374 length 16384
* schannel: encrypted data got 7504
* schannel: encrypted data buffer: offset 9878 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 6490 length 18778
* schannel: encrypted data length: 5713
* schannel: encrypted data cached: offset 5713 length 16384
* schannel: decrypted data length: 10
* schannel: decrypted data added: 10
* schannel: decrypted data cached: offset 6500 length 18778
* schannel: encrypted data length: 5644
* schannel: encrypted data cached: offset 5644 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 10596 length 18778
* schannel: encrypted data length: 1479
* schannel: encrypted data cached: offset 1479 length 16384
* schannel: failed to decrypt data, need more data
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 1479 length 16384
* schannel: encrypted data got 3752
* schannel: encrypted data buffer: offset 5231 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 14692 length 18778
* schannel: encrypted data length: 1066
* schannel: encrypted data cached: offset 1066 length 16384
* schannel: decrypted data length: 8
* schannel: decrypted data added: 8
* schannel: decrypted data cached: offset 14700 length 18778
* schannel: encrypted data length: 997
* schannel: encrypted data cached: offset 997 length 16384
* schannel: failed to decrypt data, need more data
100 27979 0 27979 0 0 5087 0 --:--:-- 0:00:05 --:--:-- 7220*
schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 997 length 16384
* schannel: encrypted data got 4842
* schannel: encrypted data buffer: offset 5839 length 16384
* schannel: decrypted data length: 4096
* schannel: decrypted data added: 4096
* schannel: decrypted data cached: offset 18796 length 18796
* schannel: encrypted data length: 1674
* schannel: encrypted data cached: offset 1674 length 16384
* schannel: decrypted data buffer: offset 18796 length 18796
* schannel: decrypted data returned 16384
* schannel: decrypted data buffer: offset 2412 length 18796
{ [16384 bytes data]
* schannel: client wants to read 16384 bytes
* schannel: encrypted data buffer: offset 1674 length 16384
* schannel: encrypted data got -1
* schannel: encrypted data buffer: offset 1674 length 16384
* schannel: decrypted data length: 1545
* schannel: decrypted data added: 1545
* schannel: decrypted data cached: offset 3957 length 18796
* schannel: encrypted data length: 69
* schannel: encrypted data cached: offset 69 length 16384
* schannel: decrypted data length: 5
* schannel: decrypted data added: 5
* schannel: decrypted data cached: offset 3962 length 18796
* schannel: decrypted data buffer: offset 3962 length 18796
* schannel: decrypted data returned 3962
* schannel: decrypted data buffer: offset 0 length 18796
{ [3962 bytes data]
100 48302 0 48302 0 0 8707 0 --:--:-- 0:00:05 --:--:-- 12315
* Connection #1 to host forums.alliedmods.net left intact
test.txt
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<!-- no cache headers -->
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Cache-Control" content="no-cache" />
<!-- end no cache headers -->
<meta http-equiv="x-ua-compatible" content="IE=9" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="generator" content="vBulletin" />
<meta name="keywords" content="AMX Mod X,AMX Mod,Metamod,Half-Life Plugins,AMXX,AMX,Plugins,Counter-Strike,CSDM,Deathmatch,Servers,C++" />
<meta name="description" content="Half-Life/Counter-Strike Server-Side Modding (AMX Mod X, Metamod, and SourceMod), CSDM and SuperHero hosted" />
<tr>
<td class="tcat" colspan="5">Welcome to the AlliedModders forum.</td>
</tr>
<tr>
<td class="alt1" colspan="5">
If this is your first visit, be sure to check out the <a href="faq.php?"><strong>FAQ</strong></a> by clicking the link above.
You may have to <a href="register.php?" rel="nofollow"><strong>register</strong></a> before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
</td>
</tr>
<a href="showthread.php?goto=newpost&t=264788" style="white-space:nowrap" title="Go to first unread post in thread 'Make counter server 24/7'"><strong>Make counter server 24/7</strong></a></span>
</div>
<div style="white-space:nowrap">
by <a href="member.php?find=lastposter&f=131" rel="nofollow">fysiks</a>
</div>
<div align="right" style="white-space:nowrap">
Today <span class="time">13:05</span>
<a href="showthread.php?p=2310313#post2310313"><img class="inlineimg" src="images/buttons/lastpost.gif" alt="Go to last post" border="0" /></a>
</div>
</div>
</td>
<a href="showthread.php?goto=newpost&t=264605" style="white-space:nowrap" title="Go to first unread post in thread '[TF2] Connecting a particle to two players'"><strong>[TF2] Connecting a particle...</strong></a></span>
</div>
<div style="white-space:nowrap">
by <a href="member.php?find=lastposter&f=107" rel="nofollow">Chdata</a>
</div>
<div align="right" style="white-space:nowrap">
Today <span class="time">12:14</span>
<a href="showthread.php?p=2310300#post2310300"><img class="inlineimg" src="images/buttons/lastpost.gif" alt="Go to last post" border="0" /></a>
</div>
</div>
</td>
<a href="showthread.php?goto=newpost&t=264797" style="white-space:nowrap" title="Go to first unread post in thread 'admins.cfg to MySQL admins'"><strong>admins.cfg to MySQL admins</strong></a></span>
</div>
<div style="white-space:nowrap">
by <a href="member.php?find=lastposter&f=112" rel="nofollow">Pelipoika</a>
</div>
<div align="right" style="white-space:nowrap">
Today <span class="time">11:52</span>
<a href="showthread.php?p=2310293#post2310293"><img class="inlineimg" src="images/buttons/lastpost.gif" alt="Go to last post" border="0" /></a>
</div>
</div>
</td>
<a href="showthread.php?goto=newpost&t=138742" style="white-space:nowrap" title="Go to first unread post in thread 'New Game Type Idea'"><strong>New Game Type Idea</strong></a></span>
</div>
<div style="white-space:nowrap">
by <a href="member.php?find=lastposter&f=70" rel="nofollow">Jelle</a>
</div>
<div align="right" style="white-space:nowrap">
09-22-2010 <span class="time">02:08</span>
<a href="showthread.php?p=1305484#post1305484"><img class="inlineimg" src="images/buttons/lastpost.gif" alt="Go to last post" border="0" /></a>
</div>
</div>
</td>
<div class="smallfont" align="center">
<!-- Do not remove or your scheduled tasks will cease to function -->
<!-- Do not remove or your scheduled tasks will cease to function -->
</div>
</div>
</form>
<script type="text/javascript">
<!--
// Main vBulletin Javascript Initialization
vBulletin_init();
//-->
</script>
</body>
</html>
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.