On Sat, Aug 9, 2014 at 11:30 PM, Zoom.Quiet <zoom...@gmail.com> wrote:
> 2014-08-09 1:33 GMT+08:00 Yichun Zhang (agentzh) <age...@gmail.com>:
> ...
>> 不要在 OpenResty 上使用 luacurl,因为必会阻塞 nginx worker 进程。
>> 可以考虑下面这个基于 cosocket 的 lua-resty-http 库:
>>
>> https://github.com/pintsized/lua-resty-http
>
> 这就是和俺想写的库重名的完成版哪...
> 不过, 看起来简单,用起来的确有门槛,
> 俺当前的需求很简单:
> - 调用 goo.gl 接口生成短址
> - 参考文档:
> https://developers.google.com/url-shortener/v1/getting_started
>
> 对应的 curl 行为是:
> curl https://www.googleapis.com/urlshortener/v1/url \
> -H 'Content-Type: application/json' \
> -d '{"longUrl": "http://www.google.com/"}'
>
> 返回将是:
> {
> "kind": "urlshortener#url",
> "id": "http://goo.gl/fbsS",
> "longUrl": "http://www.google.com/"
> }
>
> 俺的测试代码为:
>
> DATA = "{'longUrl'='" .. URI .. "'}"
> SURI = "https://www.googleapis.com/urlshortener/v1/url"
> -- For simple singleshot requests, use the URI interface.
> local http = require "resty.http"
> local httpc = http.new()
> local res, err = httpc:request_uri(SURI, {
> method = "POST",
> body = DATA,
> headers = {
> ["Content-Type"] = "application/json",
> }
> })
>
> ngx.status = res.status
> for k,v in pairs(res.headers) do
> ngx.say(k, "\t", v)
> end
>
> ngx.say(res.body)
>
> 从本地发起请求后,返回的内容为:
> curl: (52) Empty reply from server
>
>
> 在 日志中的记录为:
>
> 2014/08/09 11:16:55 [error] 27878#0: *1 lua entry thread aborted:
> runtime error: /opt/srv/openresty/nginx/conf/lua/gl.lua:40: attempt to
> index local 'res' (a nil value)
> stack traceback:
> coroutine 0:
> /opt/srv/openresty/nginx/conf/lua/gl.lua: in function
> </opt/srv/openresty/nginx/conf/lua/gl.lua:1>, client: 113.76.18.20,
> server: localhost, request: "POST /=/gl HTTP/1.1", host:
> "23.239.0.15:10080"
>
> 这里俺还专门看了一下源代码 http.lua:
>
> function _M.request_uri(self, uri, params)
> if not params then params = {} end
> ...
>
> return res, nil
> end
>
>
> 这儿 res 返回 nil 的情况非常多,
> 俺只有先打印 err 结果用
> ngx.log(ngx.INFO, "httpc:err\t", err)
> 发现...
>
> httpc:err no resolver defined to resolve "www.googleapis.com"
>
> 这个点解哪,,,
> http.lua 代码中,也没有对应的地方...
>
>
不甘心, 搜索了一下,发现,这一模块用的人很少
同期能用的只有 https://github.com/liseen/lua-resty-http
根据示例,快速迁移了一下,结果输出有类似的错误:
sock connected failed no resolver defined to resolve "www.googleapis.com"
这种信息,太宽泛,俺应该怎么想呢?
--
人生苦短, Pythonic! 冗余不做,日子甭过!备份不做,十恶不赦!
KM keep growing environment culture which promoting organization be learnning!
俺: http://zoomquiet.io
许: http://creativecommons.org/licenses/by-sa/2.5/cn/