1212-- Implementation of Lua-cURL http://msva.github.io/lua-curl
1313--
1414
15+ local function clone (t , o )
16+ o = o or {}
17+ for k ,v in pairs (t ) do o [k ]= v end
18+ return o
19+ end
20+
1521local function wrap_function (k )
1622 return function (self , ...)
1723 local ok , err = self ._handle [k ](self ._handle , ... )
@@ -94,6 +100,80 @@ local function make_iterator(self, perform)
94100 end
95101end
96102
103+
104+ -- name = <string>/<stream>/<file>/<buffer>
105+ --
106+ -- <stream> = {
107+ -- stream = function/object
108+ -- length = ?number
109+ -- name = ?string
110+ -- type = ?string
111+ -- headers = ?table
112+ -- }
113+ --
114+ -- <file> = {
115+ -- file = string
116+ -- type = ?string
117+ -- name = ?string
118+ -- headers = ?table
119+ -- }
120+ --
121+ -- <buffer> = {
122+ -- data = string
123+ -- name = string
124+ -- type = ?string
125+ -- headers = ?table
126+ -- }
127+ --
128+ local function form_add_element (form , name , value )
129+ local vt = type (value )
130+ if vt == " string" then return form :add_content (name , value ) end
131+
132+ assert (type (name ) == " string" )
133+ assert (vt == " table" )
134+ assert ((value .name == nil ) or (type (value .name ) == ' string' ))
135+ assert ((value .type == nil ) or (type (value .type ) == ' string' ))
136+ assert ((value .headrs == nil ) or (type (value .type ) == ' string' ))
137+
138+ if value .stream then
139+ local vst = type (value .stream )
140+
141+ if vst == ' function' then
142+ assert (type (value .length ) == ' number' )
143+ local length = value .length
144+ return form :add_stream (name , value .name , value .type , value .headers , length , value .stream )
145+ end
146+
147+ if (vst == ' table' ) or (vst == ' userdata' ) then
148+ local length = value .length or assert (value .stream :length ())
149+ assert (type (length ) == ' number' )
150+ return form :add_stream (name , value .name , value .type , value .headers , length , value .stream )
151+ end
152+
153+ error (" Unsupported stream type: " .. vst )
154+ end
155+
156+ if value .file then
157+ assert (type (value .file ) == ' string' )
158+ return form :add_file (name , value .file , value .type , value .filename , value .headers )
159+ end
160+
161+ if value .data then
162+ assert (type (value .data ) == ' string' )
163+ assert (type (value .name ) == ' string' )
164+ return form :add_buffer (name , value .name , value .data , value .type , value .headers )
165+ end
166+ end
167+
168+ local function form_add (form , data )
169+ for k , v in pairs (data ) do
170+ local ok , err = form_add_element (form , k , v )
171+ if not ok then return nil , err end
172+ end
173+
174+ return form
175+ end
176+
97177local function class (ctor )
98178 local C = {}
99179 C .__index = function (self , k )
334414
335415local function Load_cURLv3 (cURL , curl )
336416
417+ ---- ---------------------------------------
418+ local Form = class (curl .form ) do
419+
420+ function Form :__init (opt )
421+ if opt then return self :add (opt ) end
422+ return self
423+ end
424+
425+ function Form :add (data )
426+ return form_add (self , data )
427+ end
428+
429+ end
430+ ---- ---------------------------------------
431+
337432---- ---------------------------------------
338433local Easy = class (curl .easy ) do
339434
@@ -352,6 +447,33 @@ function Easy:perform(opt)
352447 return perform (self )
353448end
354449
450+ local setopt_httppost = wrap_function (" setopt_httppost" )
451+ function Easy :setopt_httppost (form )
452+ return setopt_httppost (self , form :handle ())
453+ end
454+
455+ local setopt = wrap_function (" setopt" )
456+ function Easy :setopt (k , v )
457+ if type (k ) == ' table' then
458+ local t = k
459+
460+ local hpost = t .httppost or t [curl .OPT_HTTPPOST ]
461+ if hpost and hpost ._handle then
462+ t = clone (t )
463+ if t .httppost then t .httppost = hpost :handle () end
464+ if t [curl .OPT_HTTPPOST ] then t [curl .OPT_HTTPPOST ] = hpost :handle () end
465+ end
466+
467+ return setopt (self , t )
468+ end
469+
470+ if k == curl .OPT_HTTPPOST then
471+ return self :setopt_httppost (v )
472+ end
473+
474+ return setopt (self , k , v )
475+ end
476+
355477end
356478---- ---------------------------------------
357479
409531
410532setmetatable (cURL , {__index = curl })
411533
534+ function cURL .form (...) return Form :new (... ) end
535+
412536function cURL .easy (...) return Easy :new (... ) end
413537
414538function cURL .multi (...) return Multi :new (... ) end
0 commit comments