1. --!parent
  2. --!encoding=utf-8
  3.  
  4. --****************************************************************************
  5. -- @file      LSW CORE LIB: CURL
  6. -- @author    Valentin Schmidt
  7. -- @version   0.2
  8. -- @requires  xtra("Curl")
  9. --****************************************************************************
  10.  
  11. property $
  12.  
  13. property _DetachClass
  14.  
  15. ----------------------------------------
  16. -- @constructor
  17. ----------------------------------------
  18. on new (me)
  19.    
  20.     $.loadXtras(me.libdir, ["Curl.x32"], ["CurlXtra.xtra"])
  21.    
  22.     $.import("detacher")
  23.     $.import("string")
  24.      
  25.     -- LOAD EXTENSIONS
  26.     $.inherit(me, $.include(me.libdir&"curlopt.ls"))
  27.     $.inherit(me, $.include(me.libdir&"curlinfo.ls"))
  28.     $.inherit(me, $.include(me.libdir&"curlform.ls"))
  29.  
  30.     -- LOAD "DETACH" HELPER CLASS
  31.     me._DetachClass = $.include(me.libdir&"detach.ls")
  32.  
  33.     return me
  34. end
  35.  
  36. ----------------------------------------
  37. -- FACTORY METHOD
  38. -- @return {instance} Curl handle (xtra instance)
  39. ----------------------------------------
  40. on init (me)
  41.     return xtra("Curl").new()
  42. end
  43.  
  44. ----------------------------------------
  45. -- Used for "detaching" xtra callbacks from original thread,
  46. -- so scripting errors in the callback's code don't fail silently
  47. -- @param {instance} ch - Curl handle
  48. -- @param {symbol} cbHandler - Callback handler
  49. -- @param {object} [cbTarget=_movie] - Callback target
  50. -- @param {bool} [passImmediately=false] - If specified, received data chunks are passed to callback immediately
  51. -- @return {integer} Curl multi error code (0 = no error)
  52. ----------------------------------------
  53. on execAsyncDetached (me, ch, cbHandler, cbTarget, passImmediately, customData)
  54.     if voidP(cbTarget) then cbTarget = _movie
  55.     obj = me._DetachClass.new(cbHandler, cbTarget, ch, customData)
  56.     return ch.execAsync(#_detach, obj, passImmediately)
  57. end
  58.  
  59. ----------------------------------------
  60. --
  61. ----------------------------------------
  62. on setHeaderCallbackDetached (me, ch, cbHandler, cbTarget)--, passImmediately, customData)
  63.     if voidP(cbTarget) then cbTarget = _movie
  64.     obj = me._DetachClass.new(cbHandler, cbTarget, ch)--, customData)
  65.     return ch.setHeaderCallback(#_detach, obj)
  66. end
  67.  
  68. ----------------------------------------
  69. --
  70. ----------------------------------------
  71. on setProgressCallbackDetached (me, ch, cbHandler, cbTarget, customData)
  72.     if voidP(cbTarget) then cbTarget = _movie
  73.     obj = me._DetachClass.new(cbHandler, cbTarget, ch, customData)
  74.     return ch.setProgressCallback(#_detachProgress, obj)
  75. end
  76.  
  77. ----------------------------------------
  78. -- Generates URL-encoded query string
  79. -- @param {propList} param_obj
  80. -- @return {string}
  81. ----------------------------------------
  82. on httpBuildQuery (me, param_obj)
  83.     query_str = ""
  84.     cnt = count(param_obj)
  85.     repeat with i = 1 to cnt
  86.         put param_obj.getPropAt(i)&"="&curl_escape(param_obj[i])&"&" after query_str
  87.     end repeat
  88.     delete the last char of query_str
  89.     return query_str
  90. end
  91.  
  92. ----------------------------------------
  93. -- Parse HTTP result headers
  94. -- @param {string} headerStr
  95. -- @return {propList}
  96. ----------------------------------------
  97. on parseHeaders (me, resultStr)
  98.     headerSep = $.CRLF&$.CRLF
  99.     lineSep = $.CRLF
  100.     parts = $.string.explode(headerSep, resultStr, 2)
  101.     headerStr = parts[1]
  102.     lines = $.string.explode(lineSep, headerStr)
  103.     statusLine = lines[1]
  104.     if not (statusLine contains ":") then
  105.         statusCode = integer(statusLine.word[2]) -- TODO
  106.         lines.deleteAt(1)
  107.     end if
  108.     headers = [:]
  109.     repeat with s in lines
  110.         if not (s contains ":") then next repeat
  111.         parts = $.string.explode(":", s, 2)
  112.         k = parts[1]
  113.         v = parts[2]
  114.         if v starts " " then delete char 1 of v
  115.         --headers[k] = v
  116.         headers.addProp(k, v)
  117.     end repeat
  118.     --return headers
  119.     return [#status:statusCode, #headers:headers]
  120. end
  121.  
[raw code]