Lua HTTP async client using libcurl (supports multi requests in parallel)

Overview

lua-async-http

lua-async-http rock, is a new lua rock written in C and based on libcurl. It allow us to make multiple http/https (with client certificate) calls in parallel (non-blocking) and wait for their responses.

In order to achieve such a behaviour, we've used the multi interface that libcurl has to offer.

Dependencies

The rock required the following system packages in order to compile:

  1. libcurl >= 7.34.0
  2. libcurl-devel >= 7.34.0

(TLS1.2 is supported since libcurl 7.34.0) QA env currently has libcurl 7.61.1-7.91.amzn1 installed by default.

Lua Usage

Important The following usage instructions are for the rock itself, and not for some lua based client.

Library require The library should be required by the caller module.

local async = require("lua_async_http")

Request Structure

The client has only one method avilable outside, the method name is: "request". The request method expect to get a bulked object of requests. For example:

{
	{request_object},
	{request_object},
	{request_object},
	...
}

Each request_object is a detailed request with the following optional/mandatory keys:

key value type mandatory
name The request key (no spaces!) string true
method GET | POST | PUT string true
post_params Post parameters, only if needed (for ex: a=true&b=val). post_params and data should not be configured at once! string false*
data The request body, in case of POST | PUT should transfer a body data. Example: {"data":true}. data and post_params should not be configured at once! string false*
url The request url string true
headers A collection of headers. Example: {{["Content-Type"] = "application/json"}, {["Custom-Header"] = "custom_value"}} collection true
cafile Path to CA PEM file string false
key Path to private key PEM file string false
certificate Path to public key PEM file string false
password The TLS private key password in case PEM has it string false
verify_peer Verify peer signature. (default: 1) bool(1|0) false
verify_host Verify host signature. (default: 0) bool(1|0) false
timeout The request timeout (in seconds. default= 8s) number false
debug Print to stdout for debugging bool(1|0) false

(* : cannot configure at the same time)

Requests example

local async = require("lua_async_http")
local res = async.request({
	{
		name = "key_1",
		url = "http://www.example.com/send",
		method = "POST",
		data = [[{"data": true}]],
		headers = {
			{["Content-Type"] =  "application/json"},
		},
		timeout = 2.5
	},
	{
		name = "key_2",
		url = "https://www.example.com/some_ssl/send",
		method = "GET",
		cafile = "/path/to/ca.pem",
		certificate = "/path/to/cert.pem",
		key = "/path/to/key.pem"
	}
})

Take a look more request examples.

Response

The response object is being returned based on the given request names. According the last request example, the response should return in the following structure:

{
	["key_1"] = {
		url = "http://www.example.com/send",
		response_status = "200",
		response_headers = {
			["content_type"] = "application/json"
		},
		response_body = "<HTML>...</HTML>",
		response_error = ""
	},
	["key_2"] = {
		url = "https://www.example.com/some_ssl/send",
		response_status = "0",
		response_headers = {},
		response_body = "",
		response_error = "Couldn't resolve host 'www.example.com'"
		
	}
}

So in order to get the values, we need to access the res variable (in that example) by the desired key.

print(res["key_1"].response_body)
-- prints: <HTML>...</HTML>

Response Types

key type
url string
response_status integer
response_headers table
response_body string
response_error string

Things to take into considerations

  1. A bulked request error may rarely fail, therefore it must be pcalled:
    local async = require("lua_async_http")
    local ok, res = pcall(function()
    	return async.request({
    		...
    	})
    end)
    
    if ok then
    	-- now accessing res object should be safe
    else
    	-- in case of a failure, res has the failure reason
    	print(string.format("failed due to: %s", res))
    end
    
  2. When using POST method, you must use only 1 delivery option (data or post_params).
  3. The SSL filenames can have other file extension names, but they content must based on PEM.
  4. In case of an error in a certain request, we returning libcurl official error message through "response_error" key.
  5. In response_headers, we are lowercasing the key names by default.
Owner
International Business Machines
International Business Machines
Async non-blocking multi-protocol networking library for C/C++

Fossa: Multi-Protocol Networking Library Note: As of September 21st 2015, Fossa project has been merged back into Mongoose project Fossa is a multi-pr

Cesanta Software 414 May 22, 2022
Tools to measure libcurl performance delta between versions

relative Tools to measure libcurl performance delta between versions build-many This script iterates over all the curl versions listed in the top of t

curl 11 Aug 30, 2021
Hobbyist Operating System targeting x86_64 systems. Includes userspace, Virtual File System, An InitFS (tarfs), Lua port, easy porting, a decent LibC and LibM, and a shell that supports: piping, file redirection, and more.

SynnixOS Epic Hobby OS targeting x86_64 CPUs, it includes some hacked together functionality for most essential OSs although, with interactivity via Q

RaidTheWeb 40 May 27, 2022
A dependency free, embeddable debugger for Lua in a single file (.lua or .c)

debugger.lua A simple, embedabble debugger for Lua 5.x, and LuaJIT 2.x. debugger.lua is a simple, single file, pure Lua debugger that is easy to integ

Scott Lembcke 571 Jun 28, 2022
The Lua development repository, as seen by the Lua team. Mirrored irregularly

The Lua development repository, as seen by the Lua team. Mirrored irregularly

Lua 5.7k Jul 4, 2022
You may learn how to make successful pull requests and get your first valid open source contribution by using this repository.

Your-First-Contribution You may learn how to make successful pull requests and get your first valid open source contribution by using this repository.

Veshraj Ghimire 24 Jun 16, 2022
null 239 Jul 6, 2022
Lua Client based on C++ for Polaris

Polaris Lua 中文文档 1. Project Introduction Based on the existing C++ sdk polaris-cpp project of Polaris, this project implements the Lua layer encapsula

Polarismesh 2 Apr 2, 2022
An Xposed module to detect or reject applist requests

Hide My Applist About this module Although "It is incorrect to detect specific app's installation", yet not every app using root provides random packa

null 818 Jun 26, 2022
Register for Hacktoberfest and make four pull requests (PRs) between October 1st-31st to grab free SWAGS

⭐️ Projects and Codes ⭐️ This is beginner friendly repo. We aim for beginners to start with their first contributions to open-source. If you are looki

Joel Sunny Varghese 8 Jun 13, 2022
Helping everyone to code and creating pull requests

Novice Coding It's an initiative to help create more awareness about Open Source and help introduce many more students to the benefits of FOSS. We wil

Saptarshi Sarkar 17 May 15, 2022
all valid pull requests accepted!!

Hacktoberfest_2021 Hacktober Fest Details Important Notice The repository has been marked as "Excluded Project" by Hacktoberfest Algorithm. I have no

Gautam Jain 23 Mar 21, 2022
Mirror only. Please do not send pull requests.

README - 08 March 2021 Welcome to the WebM VP8/VP9 Codec SDK! COMPILING THE APPLICATIONS/LIBRARIES: The build system used is similar to autotools.

WebM Project 731 Jun 24, 2022
The official Allegro 5 git repository. Pull requests welcome!

Welcome to Allegro! Allegro is a cross-platform library mainly aimed at video game and multimedia programming. It handles common, low-level tasks such

Allegro 1.4k Jun 25, 2022
Multi-dimensional dynamically distorted staggered multi-bandpass LV2 plugin

B.Angr A multi-dimensional dynamicly distorted staggered multi-bandpass LV2 plugin, for extreme soundmangling. Based on Airwindows XRegion. Key featur

null 18 Apr 17, 2022
Unicorn is a lightweight, multi-platform, multi-architecture CPU emulator framework, based on QEMU.

Unicorn Engine Unicorn is a lightweight, multi-platform, multi-architecture CPU emulator framework, based on QEMU. Unicorn offers some unparalleled fe

lazymio 1 Nov 7, 2021
Modified version of srlua for MSVC using version 5.4 of Lua

Modified version of srlua for MSVC using version 5.4 of Lua. Quote from the original README: This is a self-running Lua interpreter. It is meant to be

Augusto Goulart 2 Feb 24, 2022
Per function, Lua JIT using LLVM C++ toolchain

Lua Low Level Brief This is an alternative Lua (5.3.2) implementation that aims to archive better performance by generating native code with the help

Gabriel de Quadros Ligneul 10 Sep 4, 2021
Create simple visual sketches in Lua using SFML

Luna What is Luna? Luna is a simple game engine programmed in C++ and Lua which lets you create sketches and games in Lua Features Program in Lua, eas

XyronLabs 17 Jan 9, 2022