• gm_curly
    38 replies, posted
  • Avatar of haza55
  • gm_curly is a multi threaded, object orientated, callback based CURL implementation. It supports in its current form, HTTP, HTTPS and FTP(Get Only) [release] [highlight]Magic Definitions[/highlight] Errors(Not defined in Lua) - [url]http://curl.haxx.se/libcurl/c/libcurl-errors.html[/url] Options(Defined in Lua) - [url]http://curl.haxx.se/libcurl/c/curl_easy_setopt.html[/url] [/release] [release] [highlight]Functions[/highlight] [lua] -- Takes 0: -- -- Returns 1: -- Curly Object -- Callback none Curly( ) -- Takes 1: -- function( CurlyOBJ, errorCode, data ) -- Returns 0: -- -- Callback none Curly:SetCallback( function ) -- Takes 1: -- bool - false means the data parameter in the callback will always be a string. -- - true means the data parameter in the callback will be a BinRead object. -- Returns 0: -- -- Callback none Curly:SetBinaryMode( bool ) -- Takes 1: -- Url String -- Returns 1: -- Error Code - 0 means OK -- Callback none Curly:SetUrl( url ) -- Takes 0: -- -- Returns 1: -- Error Code - 0 means OK -- Callback is called when completed. Curly:Perform( ) -- Takes 1: -- Cookie String - "name1=content1; name2=content2;" etc. -- Returns 1: -- Error Code - 0 means OK -- Callback none Curly:SetCookies( cookies ) -- Takes 2: -- CURLOPT_* -- integer value -- Returns 1: -- Error Code - 0 means OK -- Callback none Curly:SetOptNumber( CURLOPT_*, value ) -- Takes 1: -- Username String -- Returns 1: -- Error Code - 0 means OK -- Callback none Curly:SetUsername( username ) -- Takes 1: -- Password String -- Returns 1: -- Error Code - 0 means OK -- Callback none Curly:SetPassword( password ) -- Takes 1: -- Data String - Can contain NULL Characters -- Returns 1: -- Error Code - 0 means OK -- Callback none Curly:SetPostFields( data ) -------------------------------------------------------------------- -- Takes 0: -- -- Returns 1: -- Returns data size. -- Callback none BinRead:GetSize( ) -- Takes 0: -- -- Returns 1: -- Returns the position of the next item to be read. -- Callback none BinRead:GetReadPosition( ) -- Takes 0: -- -- Returns 1: -- Returns the Read Position to 0 -- Callback none BinRead:Rewind( ) -- Takes 0: -- -- Returns 1: -- Reads 1byte, but does not change the Read Position -- Callback none BinRead:PeekByte( ) -- Takes 0: -- -- Returns 1: -- Reads 1bytes and returns it as a int. -- Callback none BinRead:ReadByte( ) -- Takes 0: -- -- Returns 1: -- Reads 8bytes and returns it as a double. -- Callback none BinRead:ReadDouble( ) -- Takes 0: -- -- Returns 1: -- Reads 4bytes and returns it as a int. -- Callback none BinRead:ReadInt( ) -- Takes 0: -- -- Returns 1: -- Reads 4bytes and returns it as a float. -- Callback none BinRead:ReadFloat( ) -- Takes 0: -- -- Returns 1: -- Reads a NULL terminated string. Read Position will be offset by string.len + 1 (the extra NULL char) -- Callback none BinRead:ReadString( ) [/lua] [/release] [release] [highlight]Basic HTTP connection example.[/highlight] [lua] require("curly") local curl = Curly() curl:SetUrl("http://www.google.com/") curl:SetOptNumber(CURLOPT_FOLLOWLOCATION, 1) curl:SetCallback(function(curlObj, errorCode, data) print(curlObj, "\n", errorCode,"\n") Msg(data) end) curl:Perform() [/lua] [/release] [release] [highlight]Basic HTTPS connection example.[/highlight] [lua] require("curly") local curl = Curly() curl:SetUrl("https://www.gmail.com/") curl:SetOptNumber(CURLOPT_FOLLOWLOCATION, 1) curl:SetOptNumber(CURLOPT_SSL_VERIFYPEER, 0) curl:SetOptNumber(CURLOPT_SSL_VERIFYHOST, 1) curl:SetCallback(function(curlObj, errorCode, data) print(curlObj, "\n", errorCode,"\n") Msg(data) end) curl:Perform() [/lua] [/release] [release] [highlight]Some notes.[/highlight] Only one operation can happen at once on an Curly object. Once you call Curly:Perform(), you must wait for the the Callback before performing another Curl Query. All the functions are non-blocking. [/release] [release] [highlight]Download[/highlight] [img]http://www.sourcemm.net/media/images/code.gif[/img] Source Code SVN Repo: [url]http://gmod-haza.googlecode.com/svn/trunk/gm_curly/[/url] [img]http://www.sourcemm.net/media/images/win32.gif[/img] Windows Binary: [url]http://gmod-haza.googlecode.com/svn/trunk/gm_curly/Release/gm_curly.dll[/url] [/release]
  • Hellyeah! Ive been waiting for this a long time! [editline]03:43PM[/editline] This is [URL="www.facepunch.com"]fucking incridible[/URL]
  • Actually no, its my file writing method that dosent accept \r or something [editline]05:10PM[/editline] Sadly gsub dosent seem to like strings anymore data:gsub("\r", "") produce "SK" what the [editline]05:12PM[/editline] Actually once again: no, its just IBaseFileSystem not liking anything today
  • Haza, nice module. Any chance you can do a libmemcache? You seem to be a capable person. Thanks.
  • We seriously need the ability to receive NULL's, in the worst case senario could you supply us with some bf_read style function and the ability to get the length so we could assemble the string ourselves for now? [editline]10:13PM[/editline] Oh nice 1500 posts!
  • Avatar of haza55
  • I'm going to add Curly:SetBinaryMode(true), so instead of receiving a string, you will get a bf_read like object. I will also do the same for oosocks.
  • Avatar of Gbps
  • [QUOTE=haza55;23553062]I'm going to add Curly:SetBinaryMode(true), so instead of receiving a string, you will get a bf_read like object. I will also do the same for oosocks.[/QUOTE] Make sure to add a few binary read and write functions too. It's kind of useless without it :S
  • Avatar of LuaStoned
  • [QUOTE=haza55;23558214]Added Binary Read Functionality.[/QUOTE] Cool stuff, now the serverbrowser/rcon/msn libs will finally get some functionallity :D €dit: Wanna give us an example?
  • Would it be possible to get the size of it so we can assemble the string with string.char?
  • Avatar of haza55
  • [QUOTE=Tobba;23559809]Would it be possible to get the size of it so we can assemble the string with string.char?[/QUOTE] [lua] Curly:SetBinaryMode(true) Curly:SetCallback(function(obj, erroCode, binread) local size = binread:GetSize() local str = "" for i=1, size do str = str .. string.char(binread:GetByte()) end end) [/lua]
  • Avatar of Grocel
  • [QUOTE=haza55;23559946][lua] Curly:SetBinaryMode(true) Curly:SetCallback(function(obj, erroCode, binread) local size = binread:GetSize() local str = "" for i=1, size do str = str .. string.char(binread:GetByte()) end end) [/lua][/QUOTE] Nice, will you also add this to gm_oosocks soon?
  • It almost works, it fails on me when i try to download a zip though (probbably a file.Write problem)
  • Obviously your wrong :v: BECUSE I CAN! Also would it be possible to decrypt it faster? Currently it lags like hell for a second while it decrypts that binread [editline]01:11PM[/editline] Apparantly it can only sometimes write NULL's what the shit(i can download a bz2 that contains nulls but not zips) [editline]01:14PM[/editline] Actually file.Write didnt like large files [editline]01:27PM[/editline] Could you make a function that reads a string until NULL, with this we could decode the BinRead without lagging everything to shit
  • Avatar of LuaStoned
  • Yeah, something like [lua]local out = {} for k,part in pairs(data:ReadStr()) do table.insert(out,part) end write(table.concat(out,"\0"))[/lua]
  • I got how that would work, it would be like [lua]local size = data:GetSize() local str = "" debug.sethook() while true do if data:GetReadPosition() > size then break end str = str..data:ReadString() end[/lua] [editline]01:35PM[/editline] Would be alot faster in most cases inless the file is made out of nulls [editline]01:39PM[/editline] Actually i got an idea: BinRead:ToTable() example: local str = "" for k, v in pairs(data:ToTable()) str = str..string.char(v) end
  • Avatar of LuaStoned
  • [QUOTE=Tobba;23561706]Actually i got an idea: BinRead:ToTable() example: local str = "" for k, v in pairs(data:ToTable()) str = str..string.char(v) end[/QUOTE] What no, string concatenation is extremely inefficient compared to table concat... [QUOTE=haza55;23561787]Ok, Added ReadString to BinRead[/QUOTE] Thanks.