Search code examples
httpsluagoogle-translateluasocket

Difference between firefox and luasocket with same headers


I'm trying to get 'https://translate.google.com' via luasocket.

Headers is based on HttpFox'es output;

My attempt to get content:

local r, c, h, fc = http.request { -- result (1 or nil on error), code (should be 200), headers, fancy code
    url = 'http://translate.google.com';
    method = 'GET';
    sink = sink;
    headers = {
        ['User-Agent'] = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0';
        ['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
        ['Host'] = 'translate.google.com';
        ['Accept-Encoding'] = 'gzip, deflate, br';
        ['Accept-Language'] = 'ru,ru-RU;q=0.8,en;q=0.5,en-US;q=0.3';
        ['DNT'] = '1';
        ['Upgrade-Insecure-Requests'] = '1';
        ['Connection'] = 'close';
    };
}

sink is valid ltn12 sink;

Result:

Code: HTTP/1.1 302 Found
Headers: {
  content-type : "text/html; charset=UTF-8"
  connection : "close"
  content-length : "226"
  x-xss-protection : "1; mode=block"
  p3p : "CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info.""
  server : "HTTP server (unknown)"
  cache-control : "private, max-age=0"
  content-language : "ru"
  date : "Tue, 03 Oct 2017 13:50:21 GMT"
  set-cookie : "NID=113=CO3BdznV6UYwcIZoIdF9F7dW1Cooi5ZVmNML0cQI6kA_TvfNig8xRQgS5E9dSKnIZxcfR3jxUbo3RA-7AEsqOCakciOo7Swtrcvz70Cmpm5M-_m1UQYTBBCg8VyNxzBW; expires=Wed, 04-Apr-2018 13:50:21 GMT; path=/; domain=.google.com; HttpOnly"
  x-content-type-options : "nosniff"
  expires : "Tue, 03 Oct 2017 13:50:21 GMT"
  location : "https://translate.google.com/"
}
Body: 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://translate.google.com/">here</A>.
</BODY></HTML>

What is wrong with my request? Why firefox got correct page and me not?


Solution

  • Code: HTTP/1.1 302 Found
    location : "https://translate.google.com/"
    

    It's a redirect to HTTPS location, but luasocket doesn't "know" how to deal with https, so it doesn't follow the redirect. If you install luasec and replace local http = require "socket.http" with local http = require "ssl.https", you should get back something like the following:

    1   200 table: 0x000274e0   HTTP/1.1 200 OK