Luasocket custom headers, 404 turns to 301

可紊 提交于 2019-12-02 06:18:21

问题


My previous question was about fetching page title in lua using the socket.http module. The question lies here. Previously, youtube pages led me to a 404 error page. Based on MattJ's help, I put up custom HOST header for the request. This is what I did and what was the result:

Code

header = { host= "youtube.com" }
local result,b,c,h = http.request{ url = "http://www.youtube.com/watch?v=_eT40eV7OiI", headers = header }
print ( result, b, c, h )
for k,v in pairs(c) do print(k,v) end

Result

1   301 table: 0047D430 HTTP/1.1 301 Moved Permanently
x-content-type-options  nosniff
content-length  0
expires Tue, 27 Apr 1971 19:44:06 EST
cache-control   no-cache
connection  close
location    http://www.youtube.com/watch?v=_eT40eV7OiI
content-type    text/html; charset=utf-8
date    Sat, 28 Apr 2012 04:26:21 GMT
server  wiseguy/0.6.11

As far as I was able to understand from this, the error is basically because of X-Content-Type-Options valued nosniff. Reading its documentation, I got to know that the only defined value, "nosniff", prevents Internet Explorer from MIME-sniffing a response away from the declared content-type.


Please help me so that I can use custom proxy and fetch the youtube(and some other sites, as mentioned in the previous question) title from their body. Here is the complete LUA file I currently have:

local http = require "socket.http"
http.PROXY="http://<proxy address here>:8080"
header = { host= "youtube.com" }
local result,b,c,h = http.request{ url = "http://www.youtube.com/watch?v=_eT40eV7OiI", headers = header }
print ( result, b, c, h )
for k,v in pairs(c) do print(k,v) end

回答1:


I believe this line should be changed:

 header = { host= "youtube.com" }

To:

 header = { host= "www.youtube.com" }

After that, works for me.




回答2:


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

Answered here by Paul Kulchenko!

Example:

-- luasec version 0.4.2
require("ssl")
require("https")
-- ssl.https.request(...)


来源:https://stackoverflow.com/questions/10360632/luasocket-custom-headers-404-turns-to-301

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!