lua-resty-ffi-req

command module
v0.0.0-...-4b09b43 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

README

lua-resty-ffi-req

The openresty http client library, supports http1, http2 and http3.

It encapsulates golang req library.

Background

Check this blog for more detail:

http://luajit.io/posts/http3-client-for-openresty

HTTP2/HTTP3 protocol is popular now, but OpenResty does not support them.

req is an intresesing golang library, which provides an unique API for all HTTP versions.

Highlights:

  • automatically detect the server side and select the optimal HTTP version for requests, you can also force the protocol if you want
  • Easy Download and Upload (form-data, file)

Why not encapsulate it so that we could reuse it in openresty?

lua-resty-ffi provides an efficient and generic API to do hybrid programming in openresty with mainstream languages (Go, Python, Java, Rust, Nodejs).

lua-resty-ffi-req = lua-resty-ffi + req

Synopsis

local req = require("resty.ffi.req")
local client, err = req:new_client()
local ok, res = client:request{
    url = "http://httpbin.org/anything?foo=bar",
    body = "hello",
    args = {
        foo1 = "foo1",
        foo2 = 2,
        foo3 = false,
        foo4 = 2.2,
    },
}
assert(ok)
ngx.say(inspect(res))
ngx.say(inspect(cjson.decode(res.body)))

local ok, res = client:request{
    method = req.HTTP_POST,
    url = "http://httpbin.org/anything",
    body = coroutine.wrap(function()
        coroutine.yield("hello")
    end),
    body_reader = true,
}
assert(ok)
ngx.say(inspect(res))

local body = {}
for chunk in res.body_reader do
    table.insert(body, chunk)
end
ngx.say(inspect(cjson.decode(table.concat(body, ""))))

Demo

# install lua-resty-ffi
# https://github.com/kingluo/lua-resty-ffi#install-lua-resty-ffi-via-luarocks
# set `OR_SRC` to your openresty source path
luarocks config variables.OR_SRC /tmp/tmp.Z2UhJbO1Si/openresty-1.21.4.1
luarocks install lua-resty-ffi

cd /opt
git clone https://github.com/kingluo/lua-resty-ffi-req
cd /opt/lua-resty-ffi-req
make

cd /opt/lua-resty-ffi-req/demo

# run nginx
LD_LIBRARY_PATH=/opt/lua-resty-ffi-req:/usr/local/lib/lua/5.1 \
nginx -p $PWD -c nginx.conf

# in another terminal, run demo
curl localhost:20000/demo/get

Documentation

Overview

Copyright (c) 2023, Jinhua Luo (kingluo) luajit.io@gmail.com All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL