[Lua] 使用 lua-http 套件,連線 HTTPS 網站並取得回應內容

[Lua] 使用 lua-http 套件,連線 HTTPS 網站並取得回應內容

之前使用 LuaSec 套件來連線 HTTPS 網站

不過那時候只拿來看 HTTP 回應狀態 (status code),沒有需要看回應內容 (body)。

今天想要同樣用 LuaSec 來取回應內容,卻不知道要怎麼做…

 

查了一下,有人說可以用 lua-http 來做,

就來試試看吧~

 

1. 安裝 lua-http 套件

本來想說用  sudo luarocks install http 就能輕鬆安裝 lut-http,

結果跟安裝 LuaSec 時一樣,出現訊息說要指定 CRYPTO_DIR,

後來試了半天,還得再加上 OPENSSL_DIR,

並讓兩個都指向 Mac 的 /usr/local/opt/openssl 目錄。

即使如此,最後還是出現了錯誤:

$ sudo luarocks install http CRYPTO_DIR=/usr/local/opt/openssl OPENSSL_DIR=/usr/local/opt/openssl

Installing https://luarocks.org/http-0.4-0.all.rock
Missing dependencies for http 0.4-0:
   cqueues >= 20161214 (not installed)
   luaossl >= 20161208 (not installed)
   basexx >= 0.2.0 (not installed)
   lpeg_patterns >= 0.5 (not installed)
   binaryheap >= 0.3 (not installed)
   fifo (not installed)

......
env MACOSX_DEPLOYMENT_TARGET=10.5 gcc -O2 -std=gnu99 -fPIC -g -Wall -Wextra -Wno-missing-field-initializers -Wno-initializer-overrides -Wno-unused -Wno-dollar-in-identifier-extension -Wno-deprecated-declarations -O2 -fPIC -D_REENTRANT -D_THREAD_SAFE -D_GNU_SOURCE -I"/usr/local/opt/openssl/include" -I"/usr/local/opt/openssl/include" -DSOCKET_DEBUG -DHAVE_CONFIG_H -DDNS_RANDOM=arc4random -c -o /private/tmp/luarocks_cqueues-20200726.54-0-4536103/cqueues-rel-20200726/src/lib/socket.o /private/tmp/luarocks_cqueues-20200726.54-0-4536103/cqueues-rel-20200726/src/lib/socket.c
/private/tmp/luarocks_cqueues-20200726.54-0-4536103/cqueues-rel-20200726/src/lib/socket.c:498:10: error: thread-local storage is not supported for the current target
                static __thread char sslstr[256];
                       ^
1 error generated.
make: *** [/private/tmp/luarocks_cqueues-20200726.54-0-4536103/cqueues-rel-20200726/src/lib/socket.o] Error 1

Error: Failed installing dependency: https://luarocks.org/cqueues-20200726.54-0.src.rock - Build error: Failed building.

 

錯誤訊息是 thread-local storage is not supported for the current target,

又上網查了一下,似乎是這邊指定的 MACOSX_DEPLOYMENT_TARGET=10.5 太舊了,

導致 gcc 不支援 __thread 這個關鍵字

那要怎麼讓 MACOSX_DEPLOYMENT_TARGET 可以設成新一點的值,

像是我的 macOS Big Sur 是 11.6,如果可以設成 11.6,應該就沒問題了…

 

執行  luarocks config 看一下目前的配置,

發現它有個變數 CC,內容就是  env MACOSX_DEPLOYMENT_TARGET=10.5 gcc

嗯… 有可疑的味道… (屁屁偵探上身!):

$ luarocks config

......
variables = {
   AR = "ar",
   BUNZIP2 = "bunzip2",
   CC = "env MACOSX_DEPLOYMENT_TARGET=10.5 gcc",
   CFLAGS = "-O2 -fPIC",
   CHMOD = "chmod",
   CMAKE = "cmake",
......
}

 

又 Google 了一下,看看要怎麼改這個變數…

(話說沒有 Google 的話,我是不是沒辦法寫程式了呀?!)

後來看起來是加在 luarocks 的參數列裡面就行了,

可以看到 MACOSX_DEPLOYMENT_TARGET 的值確實被我們改變了,

而最終 lua-http 也裝成功了:

$ sudo luarocks install http CRYPTO_DIR=/usr/local/opt/openssl/  OPENSSL_DIR=/usr/local/opt/openssl/ CC="env MACOSX_DEPLOYMENT_TARGET=11.6 gcc"

......
env MACOSX_DEPLOYMENT_TARGET=11.6 gcc -O2 -std=gnu99 -fPIC -g -Wall -Wextra -Wno-missing-field-initializers -Wno-initializer-overrides -Wno-unused -Wno-dollar-in-identifier-extension -Wno-deprecated-declarations -O2 -fPIC -D_REENTRANT -D_THREAD_SAFE -D_GNU_SOURCE -I"/usr/local/opt/openssl/include" -I"/usr/local/opt/openssl/include" -DSOCKET_DEBUG -DHAVE_CONFIG_H -DDNS_RANDOM=arc4random -c -o /private/tmp/luarocks_cqueues-20200726.54-0-1215889/cqueues-rel-20200726/src/lib/socket.o /private/tmp/luarocks_cqueues-20200726.54-0-1215889/cqueues-rel-20200726/src/lib/socket.c
......
http 0.4-0 is now installed in /usr/local (license: MIT)

 

2. 使用 lua-http 套件

用 Lua 直譯器來玩一下 lua-http 套件,

首先匯入 http.request 模組,用它來讀取 http://example.com:

$ lua
Lua 5.4.1  Copyright (C) 1994-2020 Lua.org, PUC-Rio

> http_request = require "http.request"
> headers, stream = http_request.new_from_uri("http://example.com"):go()

 

看一下傳回來的 HTTP 回應標頭 (response header),

它看起來是一個 table,可以用 pl.pretty 來印出內容:

> headers
http.headers{12 headers}

> (require "pl.pretty")(headers)
{
  ["arg 1"] = {
    [":status"] = nil,
    age = nil,
    ["cache-control"] = nil,
    ["content-length"] = nil,
    ["content-type"] = nil,
    date = nil,
    etag = nil,
    expires = nil,
    ["last-modified"] = nil,
    server = nil,
    vary = nil,
    ["x-cache"] = nil
  }
}
true

 

也有提供內建函式  get() ,可以從這表格中直接取值,

像是我想要看 HTTP status code :

> headers:get(":status")
200

 

要取得回應內容 (body) 的話,可以呼叫  stream:get_body_as_string() ,

這會將 body 的部分以文字形式傳回:

> body = stream:get_body_as_string()
> body

<!doctype html>
<html>
<head>
    <title>Example Domain</title>
    ......
</head>

<body>
<div>
    <h1>Example Domain</h1>
    <p>This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.</p>
    <p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

 

這樣子,就可以用 lua-http 套件,

成功訪問一個 HTTP/HTTPS 網站,

並取得相關的 HTTP 回應標頭 (header) / 狀態碼 (status code) / 內容 (body) 囉~

 

參考資料:

Luarocks uses clang by default, how do I change it to GCC?

Doc to install lua-http on macos and lua 5.3

(本頁面已被瀏覽過 363 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料