Simplify code of cURL module.

This commit is contained in:
Alexey Melnichuk 2014-09-10 12:55:37 +05:00
parent c2f8fc99f9
commit e0dfc3a490

View File

@ -32,21 +32,20 @@ local function wrap_setopt_flags(k, flags)
end end
end end
------------------------------------------- local function class(ctor)
local Easy = {} do local C = {}
C.__index = function(self, k)
Easy.__index = function(self, k) local fn = C[k]
local fn = Easy[k]
if not fn and self._handle[k] then if not fn and self._handle[k] then
fn = wrap_function(k) fn = wrap_function(k)
self[k] = fn self[k] = fn
end end
return fn return fn
end end
function Easy:new() function C:new()
local h, err = curl.easy() local h, err = ctor()
if not h then return nil, err end if not h then return nil, err end
local o = setmetatable({ local o = setmetatable({
@ -54,11 +53,17 @@ function Easy:new()
}, self) }, self)
return o return o
end
function C:handle()
return self._handle
end
return C
end end
function Easy:handle() -------------------------------------------
return self._handle local Easy = class(curl.easy) do
end
local perform = wrap_function("perform") local perform = wrap_function("perform")
local setopt_share = wrap_function("setopt_share") local setopt_share = wrap_function("setopt_share")
@ -196,33 +201,7 @@ end
------------------------------------------- -------------------------------------------
------------------------------------------- -------------------------------------------
local Multi = {} do local Multi = class(curl.multi) do
Multi.__index = function(self, k)
local fn = Multi[k]
if not fn and self._handle[k] then
fn = wrap_function(k)
self[k] = fn
end
return fn
end
function Multi:new()
local h, err = curl.multi()
if not h then return nil, err end
local o = setmetatable({
_handle = h;
_easy = {};
}, self)
return o
end
function Multi:handle()
return self._handle
end
local perform = wrap_function("perform") local perform = wrap_function("perform")
local add_handle = wrap_function("add_handle") local add_handle = wrap_function("add_handle")
@ -309,32 +288,7 @@ end
------------------------------------------- -------------------------------------------
------------------------------------------- -------------------------------------------
local Share = {} do local Share = class(curl.share) do
Share.__index = function(self, k)
local fn = Share[k]
if not fn and self._handle[k] then
fn = wrap_function(k)
self[k] = fn
end
return fn
end
function Share:new()
local h, err = curl.share()
if not h then return nil, err end
local o = setmetatable({
_handle = h
}, self)
return o
end
function Share:handle()
return self._handle
end
Share.setopt_share = wrap_setopt_flags("share", { Share.setopt_share = wrap_setopt_flags("share", {
[ "COOKIE" ] = curl.LOCK_DATA_COOKIE; [ "COOKIE" ] = curl.LOCK_DATA_COOKIE;