Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

luaSocket HTTP requests always respond with a redirect (301 or 302)

I use LuaForWindows (latest version) and I have read this and this answer and everything i could find in the mailinglist of lua-users.org. What ever I try (most) sites only respond with either 301 or 302. I have created an example batch script which downloads (some) of the OpenGL 2.1 Reference from their man pages.

@ECHO OFF

FOR /F "SKIP=5" %%# IN ( %~fs0 ) DO lua -l socket.http -e "print(socket.http.request('https://www.opengl.org/sdk/docs/man2/xhtml/%%#.xml'))"
GOTO:EOF

glAccum
glActiveTexture
glAlphaFunc
glAreTexturesResident
glArrayElement
glAttachShader
glBegin
glBeginQuery
glBindAttribLocation
glBindBuffer

the most important part is this:

print(require('socket.http').request('https://www.opengl.org/sdk/docs/man2/xhtml/glAccum.xml')) -- added glAccum so you can run it

This ALWAYS returns a 301. This also happens to me when downloading from other random pages. (I dont note them so I cant give a list, but i happened to find out some of them use cloudflare.)

If i write an equivalent downloader in Java using URL and openConnection() it wont redirect.

I already tried folowing the redirect manually (setting refferer and stuff) and using the 'generic' way. As most of the tips stated in other answers.

like image 690
tDwtp Avatar asked May 31 '16 20:05

tDwtp


People also ask

Should https redirect be 301 or 302?

For a permanent change that will rank for SEO, a 301 redirect is necessary and understood by search engines. 302 redirect should only be used if it is a temporary change, and they often get used because it's easier to create that instance than the permanent 301 redirect.

How does a 302 redirect work?

A 302 redirect does not pass the “juice,” or keep your domain authority to its new location. It simply redirects the user to the new location for you so they don't view a broken link, a 404 not found page, or an error page.


1 Answers

You are using socket.http, but try to access https URL. luasocket doesn't handle HTTPS protocol, so it sends a request to the default port 80 instead and gets a redirect to HTTPS link (same link); this goes for several times (as the URL doesn't really change), and in the end luasocket gives up producing the message.

The solution is to install luasec and to use ssl.https module to do the request.

like image 57
Paul Kulchenko Avatar answered Nov 15 '22 00:11

Paul Kulchenko