Advertisement
polectron

Untitled

Aug 4th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.95 KB | None | 0 0
  1. module Win32
  2. def copymem(len)
  3. buf = "\0" * len
  4. Win32API.new("kernel32", "RtlMoveMemory", "ppl", "").call(buf, self, len)
  5. buf
  6. end
  7. end
  8.  
  9.  
  10.  
  11. # Extends the numeric class.
  12. class Numeric
  13. include Win32
  14. end
  15.  
  16.  
  17.  
  18. # Extends the string class.
  19. class String
  20. include Win32
  21. end
  22.  
  23.  
  24.  
  25. module Winsock
  26. DLL = "ws2_32"
  27. #-----------------------------------------------------------------------------
  28. # * Accept Connection
  29. #-----------------------------------------------------------------------------
  30. def self.accept(*args)
  31. Win32API.new(DLL, "accept", "ppl", "l").call(*args)
  32. end
  33. #-----------------------------------------------------------------------------
  34. # * Bind
  35. #-----------------------------------------------------------------------------
  36. def self.bind(*args)
  37. Win32API.new(DLL, "bind", "ppl", "l").call(*args)
  38. end
  39. #-----------------------------------------------------------------------------
  40. # * Close Socket
  41. #-----------------------------------------------------------------------------
  42. def self.closesocket(*args)
  43. Win32API.new(DLL, "closesocket", "p", "l").call(*args)
  44. end
  45. #-----------------------------------------------------------------------------
  46. # * Connect
  47. #-----------------------------------------------------------------------------
  48. def self.connect(*args)
  49. Win32API.new(DLL, "connect", "ppl", "l").call(*args)
  50. end
  51. #-----------------------------------------------------------------------------
  52. # * Get host (Using Adress)
  53. #-----------------------------------------------------------------------------
  54. def self.gethostbyaddr(*args)
  55. Win32API.new(DLL, "gethostbyaddr", "pll", "l").call(*args)
  56. end
  57. #-----------------------------------------------------------------------------
  58. # * Get host (Using Name)
  59. #-----------------------------------------------------------------------------
  60. def self.gethostbyname(*args)
  61. Win32API.new(DLL, "gethostbyname", "p", "l").call(*args)
  62. end
  63. #-----------------------------------------------------------------------------
  64. # * Get host's Name
  65. #-----------------------------------------------------------------------------
  66. def self.gethostname(*args)
  67. Win32API.new(DLL, "gethostname", "pl", "").call(*args)
  68. end
  69. #-----------------------------------------------------------------------------
  70. # * Get Server (Using Name)
  71. #-----------------------------------------------------------------------------
  72. def self.getservbyname(*args)
  73. Win32API.new(DLL, "getservbyname", "pp", "p").call(*args)
  74. end
  75. #-----------------------------------------------------------------------------
  76. # * Convert Host Long To Network Long
  77. #-----------------------------------------------------------------------------
  78. def self.htonl(*args)
  79. Win32API.new(DLL, "htonl", "l", "l").call(*args)
  80. end
  81. #-----------------------------------------------------------------------------
  82. # * Convert Host Short To Network Short
  83. #-----------------------------------------------------------------------------
  84. def self.htons(*args)
  85. Win32API.new(DLL, "htons", "l", "l").call(*args)
  86. end
  87. #-----------------------------------------------------------------------------
  88. # * Inet Adress
  89. #-----------------------------------------------------------------------------
  90. def self.inet_addr(*args)
  91. Win32API.new(DLL, "inet_addr", "p", "l").call(*args)
  92. end
  93. #-----------------------------------------------------------------------------
  94. # * Inet N To A
  95. #-----------------------------------------------------------------------------
  96. def self.inet_ntoa(*args)
  97. Win32API.new(DLL, "inet_ntoa", "l", "p").call(*args)
  98. end
  99. #-----------------------------------------------------------------------------
  100. # * Listen
  101. #-----------------------------------------------------------------------------
  102. def self.listen(*args)
  103. Win32API.new(DLL, "listen", "pl", "l").call(*args)
  104. end
  105. #-----------------------------------------------------------------------------
  106. # * Recieve
  107. #-----------------------------------------------------------------------------
  108. def self.recv(*args)
  109. Win32API.new(DLL, "recv", "ppll", "l").call(*args)
  110. end
  111. #-----------------------------------------------------------------------------
  112. # * Select
  113. #-----------------------------------------------------------------------------
  114. def self.select(*args)
  115. Win32API.new(DLL, "select", "lpppp", "l").call(*args)
  116. end
  117. #-----------------------------------------------------------------------------
  118. # * Send
  119. #-----------------------------------------------------------------------------
  120. def self.send(*args)
  121. Win32API.new(DLL, "send", "ppll", "l").call(*args)
  122. end
  123. #-----------------------------------------------------------------------------
  124. # * Set Socket Options
  125. #-----------------------------------------------------------------------------
  126. def self.setsockopt(*args)
  127. Win32API.new(DLL, "setsockopt", "pllpl", "l").call(*args)
  128. end
  129. #-----------------------------------------------------------------------------
  130. # * Shutdown
  131. #-----------------------------------------------------------------------------
  132. def self.shutdown(*args)
  133. Win32API.new(DLL, "shutdown", "pl", "l").call(*args)
  134. end
  135. #-----------------------------------------------------------------------------
  136. # * Socket
  137. #-----------------------------------------------------------------------------
  138. def self.socket(*args)
  139. Win32API.new(DLL, "socket", "lll", "l").call(*args)
  140. end
  141. #-----------------------------------------------------------------------------
  142. # * Get Last Error
  143. #-----------------------------------------------------------------------------
  144. def self.WSAGetLastError(*args)
  145. Win32API.new(DLL, "WSAGetLastError", "", "l").call(*args)
  146. end
  147. end
  148.  
  149.  
  150.  
  151. if !Object.const_defined?(:Socket) # for compatibility
  152.  
  153.  
  154.  
  155. #===============================================================================
  156. # ** Socket - Creates and manages sockets.
  157. #-------------------------------------------------------------------------------
  158. # Author Ruby
  159. # Version 1.8.1
  160. #===============================================================================
  161. class Socket
  162. #-----------------------------------------------------------------------------
  163. # * Constants
  164. #-----------------------------------------------------------------------------
  165. AF_UNSPEC = 0
  166. AF_UNIX = 1
  167. AF_INET = 2
  168. AF_IPX = 6
  169. AF_APPLETALK = 16
  170. PF_UNSPEC = 0
  171. PF_UNIX = 1
  172. PF_INET = 2
  173. PF_IPX = 6
  174. PF_APPLETALK = 16
  175. SOCK_STREAM = 1
  176. SOCK_DGRAM = 2
  177. SOCK_RAW = 3
  178. SOCK_RDM = 4
  179. SOCK_SEQPACKET = 5
  180. IPPROTO_IP = 0
  181. IPPROTO_ICMP = 1
  182. IPPROTO_IGMP = 2
  183. IPPROTO_GGP = 3
  184. IPPROTO_TCP = 6
  185. IPPROTO_PUP = 12
  186. IPPROTO_UDP = 17
  187. IPPROTO_IDP = 22
  188. IPPROTO_ND = 77
  189. IPPROTO_RAW = 255
  190. IPPROTO_MAX = 256
  191. SOL_SOCKET = 65535
  192. SO_DEBUG = 1
  193. SO_REUSEADDR = 4
  194. SO_KEEPALIVE = 8
  195. SO_DONTROUTE = 16
  196. SO_BROADCAST = 32
  197. SO_LINGER = 128
  198. SO_OOBINLINE = 256
  199. SO_RCVLOWAT = 4100
  200. SO_SNDTIMEO = 4101
  201. SO_RCVTIMEO = 4102
  202. SO_ERROR = 4103
  203. SO_TYPE = 4104
  204. SO_SNDBUF = 4097
  205. SO_RCVBUF = 4098
  206. SO_SNDLOWAT = 4099
  207. TCP_NODELAY = 1
  208. MSG_OOB = 1
  209. MSG_PEEK = 2
  210. MSG_DONTROUTE = 4
  211. IP_OPTIONS = 1
  212. IP_DEFAULT_MULTICAST_LOOP = 1
  213. IP_DEFAULT_MULTICAST_TTL = 1
  214. IP_MULTICAST_IF = 2
  215. IP_MULTICAST_TTL = 3
  216. IP_MULTICAST_LOOP = 4
  217. IP_ADD_MEMBERSHIP = 5
  218. IP_DROP_MEMBERSHIP = 6
  219. IP_TTL = 7
  220. IP_TOS = 8
  221. IP_MAX_MEMBERSHIPS = 20
  222. EAI_ADDRFAMILY = 1
  223. EAI_AGAIN = 2
  224. EAI_BADFLAGS = 3
  225. EAI_FAIL = 4
  226. EAI_FAMILY = 5
  227. EAI_MEMORY = 6
  228. EAI_NODATA = 7
  229. EAI_NONAME = 8
  230. EAI_SERVICE = 9
  231. EAI_SOCKTYPE = 10
  232. EAI_SYSTEM = 11
  233. EAI_BADHINTS = 12
  234. EAI_PROTOCOL = 13
  235. EAI_MAX = 14
  236. AI_PASSIVE = 1
  237. AI_CANONNAME = 2
  238. AI_NUMERICHOST = 4
  239. AI_MASK = 7
  240. AI_ALL = 256
  241. AI_V4MAPPED_CFG = 512
  242. AI_ADDRCONFIG = 1024
  243. AI_DEFAULT = 1536
  244. AI_V4MAPPED = 2048
  245. #--------------------------------------------------------------------------
  246. # * Returns the associated IP address for the given hostname.
  247. #--------------------------------------------------------------------------
  248. def self.getaddress(host)
  249. gethostbyname(host)[3].unpack("C4").join(".")
  250. end
  251. #--------------------------------------------------------------------------
  252. # * Returns the associated IP address for the given hostname.
  253. #--------------------------------------------------------------------------
  254. def self.getservice(serv)
  255. case serv
  256. when Numeric
  257. return serv
  258. when String
  259. return getservbyname(serv)
  260. else
  261. raise "Please use an integer or string for services."
  262. end
  263. end
  264. #--------------------------------------------------------------------------
  265. # * Returns information about the given hostname.
  266. #--------------------------------------------------------------------------
  267. def self.gethostbyname(name)
  268. raise SocketError::ENOASSOCHOST if (ptr = Winsock.gethostbyname(name)) == 0
  269. host = ptr.copymem(16).unpack("iissi")
  270. [host[0].copymem(64).split("\0")[0], [], host[2], host[4].copymem(4).unpack("l")[0].copymem(4)]
  271. end
  272. #--------------------------------------------------------------------------
  273. # * Returns the user's hostname.
  274. #--------------------------------------------------------------------------
  275. def self.gethostname
  276. buf = "\0" * 256
  277. Winsock.gethostname(buf, 256)
  278. buf.strip
  279. end
  280. #--------------------------------------------------------------------------
  281. # * Returns information about the given service.
  282. #--------------------------------------------------------------------------
  283. def self.getservbyname(name)
  284. case name
  285. when /echo/i
  286. return 7
  287. when /daytime/i
  288. return 13
  289. when /ftp/i
  290. return 21
  291. when /telnet/i
  292. return 23
  293. when /smtp/i
  294. return 25
  295. when /time/i
  296. return 37
  297. when /http/i
  298. return 80
  299. when /pop/i
  300. return 110
  301. else
  302. #Network.testing? != 0 ? (Network.testresult(true)) : (raise "Service not recognized.")
  303. #return if Network.testing? == 2
  304. end
  305. end
  306. #--------------------------------------------------------------------------
  307. # * Creates an INET-sockaddr struct.
  308. #--------------------------------------------------------------------------
  309. def self.sockaddr_in(port, host)
  310. begin
  311. [AF_INET, getservice(port)].pack("sn") + gethostbyname(host)[3] + [].pack("x8")
  312. rescue
  313. #Network.testing? != 0 ? (Network.testresult(true)): (nil)
  314. #return if Network.testing? == 2
  315. rescue Hangup
  316. #Network.testing? != 0 ? (Network.testresult(true)): (nil)
  317. #return if Network.testing? == 2
  318. end
  319. end
  320. #--------------------------------------------------------------------------
  321. # * Creates a new socket and connects it to the given host and port.
  322. #--------------------------------------------------------------------------
  323. def self.open(*args)
  324. socket = new(*args)
  325. if block_given?
  326. begin
  327. yield socket
  328. ensure
  329. socket.close
  330. end
  331. end
  332. nil
  333. end
  334. #--------------------------------------------------------------------------
  335. # * Creates a new socket.
  336. #--------------------------------------------------------------------------
  337. def initialize(domain, type, protocol)
  338. SocketError.check if (@fd = Winsock.socket(domain, type, protocol)) == -1
  339. @fd
  340. end
  341. #--------------------------------------------------------------------------
  342. # * Accepts incoming connections.
  343. #--------------------------------------------------------------------------
  344. def accept(flags = 0)
  345. buf = "\0" * 16
  346. SocketError.check if Winsock.accept(@fd, buf, flags) == -1
  347. buf
  348. end
  349. #--------------------------------------------------------------------------
  350. # * Binds a socket to the given sockaddr.
  351. #--------------------------------------------------------------------------
  352. def bind(sockaddr)
  353. SocketError.check if (ret = Winsock.bind(@fd, sockaddr, sockaddr.size)) == -1
  354. ret
  355. end
  356. #--------------------------------------------------------------------------
  357. # * Closes a socket.
  358. #--------------------------------------------------------------------------
  359. def close
  360. SocketError.check if (ret = Winsock.closesocket(@fd)) == -1
  361. ret
  362. end
  363. #--------------------------------------------------------------------------
  364. # * Connects a socket to the given sockaddr.
  365. #--------------------------------------------------------------------------
  366. def connect(sockaddr)
  367. #return if Network.testing? == 2
  368. SocketError.check if (ret = Winsock.connect(@fd, sockaddr, sockaddr.size)) == -1
  369. ret
  370. end
  371. #--------------------------------------------------------------------------
  372. # * Listens for incoming connections.
  373. #--------------------------------------------------------------------------
  374. def listen(backlog)
  375. SocketError.check if (ret = Winsock.listen(@fd, backlog)) == -1
  376. ret
  377. end
  378. #--------------------------------------------------------------------------
  379. # * Checks waiting data's status.
  380. #--------------------------------------------------------------------------
  381. def select(timeout) # timeout in seconds
  382. SocketError.check if (ret = Winsock.select(1, [1, @fd].pack("ll"), 0, 0, [timeout.to_i,
  383. (timeout * 1000000).to_i].pack("ll"))) == -1
  384. ret
  385. end
  386. #--------------------------------------------------------------------------
  387. # * Checks if data is waiting.
  388. #--------------------------------------------------------------------------
  389. def ready?
  390. not select(0) == 0
  391. end
  392. #--------------------------------------------------------------------------
  393. # * Reads data from socket.
  394. #--------------------------------------------------------------------------
  395. def read(len)
  396. buf = "\0" * len
  397. Win32API.new("msvcrt", "_read", "lpl", "l").call(@fd, buf, len)
  398. buf
  399. end
  400. #--------------------------------------------------------------------------
  401. # * Returns received data.
  402. #--------------------------------------------------------------------------
  403. def recv(len, flags = 0)
  404. retString=""
  405. remainLen=len
  406. while remainLen > 0
  407. buf = "\0" * remainLen
  408. retval=Winsock.recv(@fd, buf, buf.size, flags)
  409. SocketError.check if retval == -1
  410. # Note: Return value may not equal requested length
  411. remainLen-=retval
  412. retString+=buf[0,retval]
  413. end
  414. return retString
  415. end
  416. #--------------------------------------------------------------------------
  417. # * Sends data to a host.
  418. #--------------------------------------------------------------------------
  419. def send(data, flags = 0)
  420. SocketError.check if (ret = Winsock.send(@fd, data, data.size, flags)) == -1
  421. ret
  422. end
  423. #--------------------------------------------------------------------------
  424. # * Recieves file from a socket
  425. # size : file size
  426. # scene : update scene boolean
  427. #--------------------------------------------------------------------------
  428. def recv_file(size,scene=false,file="")
  429. data = []
  430. size.times do |i|
  431. if scene == true
  432. $scene.recv_update(size,i,file) if i%((size/1000)+1)== 0
  433. else
  434. Graphics.update if i%1024 == 0
  435. end
  436. data << recv(1)
  437. end
  438. return data
  439. end
  440.  
  441. def recvTimeout
  442. if select(10)==0
  443. raise Hangup.new("Timeout")
  444. end
  445. return recv(1)
  446. end
  447. #--------------------------------------------------------------------------
  448. # * Gets
  449. #--------------------------------------------------------------------------
  450. def gets
  451. # Create buffer
  452. message = ""
  453. # Loop Until "end of line"
  454. count=0
  455. while true
  456. x=select(0.05)
  457. if x==0
  458. count+=1
  459. Graphics.update if count%10==0
  460. raise Errno::ETIMEOUT if count>200
  461. next
  462. end
  463. ch = recv(1)
  464. break if ch == "\n"
  465. message += ch
  466. end
  467. # Return recieved data
  468. return message
  469. end
  470. #--------------------------------------------------------------------------
  471. # * Writes data to socket.
  472. #--------------------------------------------------------------------------
  473. def write(data)
  474. Win32API.new("msvcrt", "_write", "lpl", "l").call(@fd, data, 1)
  475. end
  476. end
  477.  
  478.  
  479.  
  480. #===============================================================================
  481. # ** TCPSocket - Creates and manages TCP sockets.
  482. #-------------------------------------------------------------------------------
  483. # Author Ruby
  484. # Version 1.8.1
  485. #===============================================================================
  486.  
  487. #-------------------------------------------------------------------------------
  488. # Begin SDK Enabled Check
  489. #-------------------------------------------------------------------------------
  490. class TCPSocket < Socket
  491. #--------------------------------------------------------------------------
  492. # * Creates a new socket and connects it to the given host and port.
  493. #--------------------------------------------------------------------------
  494. def self.open(*args)
  495. socket = new(*args)
  496. if block_given?
  497. begin
  498. yield socket
  499. ensure
  500. socket.close
  501. end
  502. end
  503. nil
  504. end
  505. #--------------------------------------------------------------------------
  506. # * Creates a new socket and connects it to the given host and port.
  507. #--------------------------------------------------------------------------
  508. def initialize(host, port)
  509. super(AF_INET, SOCK_STREAM, IPPROTO_TCP)
  510. connect(Socket.sockaddr_in(port, host))
  511. end
  512. end
  513.  
  514.  
  515.  
  516. #==============================================================================
  517. # ** SocketError
  518. #------------------------------------------------------------------------------
  519. # Default exception class for sockets.
  520. #==============================================================================
  521. class SocketError < StandardError
  522. ENOASSOCHOST = "getaddrinfo: no address associated with hostname."
  523.  
  524. def self.check
  525. errno = Winsock.WSAGetLastError
  526. #if not Network.testing? == 1
  527. raise Errno.const_get(Errno.constants.detect { |c| Errno.const_get(c).new.errno == errno })
  528. #else
  529. # errno != 0 ? (Network.testresult(true)) : (Network.testresult(false))
  530. #end
  531. end
  532. end
  533.  
  534.  
  535.  
  536. end # !Object.const_defined?(:Socket)
  537.  
  538.  
  539.  
  540. #############################
  541. #
  542. # HTTP utility functions
  543. #
  544. #############################
  545. def pbPostData(url, postdata, filename=nil, depth=0)
  546. userAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0"
  547. if url[/^https:\/\/([^\/]+)(.*)$/] || url[/^http:\/\/([^\/]+)(.*)$/]
  548. host=$1
  549. path=$2
  550. path="/" if path.length==0
  551. body = postdata.map {|key, value|
  552. keyString=key.to_s
  553. valueString=value.to_s
  554. keyString.gsub!(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
  555. valueString.gsub!(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) }
  556. next "#{keyString}=#{valueString}"
  557. }.join('&')
  558. request="POST #{path} HTTP/1.1\r\nUser-Agent: #{userAgent}\r\nPragma: no-cache\r\nHost: #{host}\r\nProxy-Connection: Close\r\n"
  559. request+="Content-Type: application/x-www-form-urlencoded\r\n"
  560. request+="Content-Length: #{body.length}\r\n"
  561. request+="\r\n"
  562. request+=body
  563. return pbHttpRequest(host, request, filename, depth)
  564. end
  565. return "Error on PostData"
  566. end
  567.  
  568. def pbTranslate(msg, lang="en")
  569. begin
  570. translateurl = "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e"
  571. translateurl = translateurl + "&tl=" + lang
  572. encodedmsg = ""
  573.  
  574. msg.split("").each do |i|
  575. if(i == " ")
  576. encodedmsg += "%20"
  577. else
  578. encodedmsg += i
  579. end
  580. end
  581. url = translateurl + "&q=" + encodedmsg
  582. userAgent="AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1"
  583. if url[/^https:\/\/([^\/]+)(.*)$/]
  584. host=$1
  585. path=$2
  586. path="/" if path.length==0
  587. request="GET #{path} HTTP/1.1\r\nUser-Agent: #{userAgent}\r\nPragma: no-cache\r\nHost: #{host}\r\nProxy-Connection: Close\r\n\r\n"
  588. return pbHttpRequest(host, request)
  589. end
  590. return "Error in pbTranslate"
  591. rescue Exception => e
  592. puts "Error on translation"
  593. puts e
  594. end
  595. end
  596.  
  597. def pbDownloadData(url, filename=nil, depth=0)
  598. userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"
  599. if depth>10
  600. raise "Redirection level too deep"
  601. end
  602. if url[/^https:\/\/([^\/]+)(.*)$/] || url[/^http:\/\/([^\/]+)(.*)$/]
  603. host=$1
  604. path=$2
  605. path="/" if path.length==0
  606. request="GET #{path} HTTP/1.1\r\nUser-Agent: #{userAgent}\r\nPragma: no-cache\r\nHost: #{host}\r\nProxy-Connection: Close\r\n\r\n"
  607. return pbHttpRequest(host, request, filename, depth)
  608. end
  609. return "Error on Download Data"
  610. end
  611.  
  612. def pbHttpRequest(host, request, filename=nil, depth=0)
  613. if depth>10
  614. raise "Redirection level too deep"
  615. end
  616. socket=::TCPSocket.new(host, 80)
  617. time=Time.now.to_i
  618. begin
  619. socket.send(request)
  620. result=socket.gets
  621. data=""
  622. # Get the HTTP result
  623. if result[/^HTTP\/1\.[01] (\d+).*/]
  624. errorcode=$1.to_i
  625. if errorcode>=400 && errorcode<500
  626. raise "HTTP Error #{errorcode}"
  627. end
  628. headers={}
  629. # Get the response headers
  630. while true
  631. result=socket.gets.sub(/\r$/,"")
  632. break if result==""
  633. if result[/^([^:]+):\s*(.*)/]
  634. headers[$1]=$2
  635. end
  636. end
  637. length=-1
  638. chunked=false
  639. if headers["Content-Length"]
  640. length=headers["Content-Length"].to_i
  641. end
  642. if headers["Transfer-Encoding"]=="chunked"
  643. chunked=true
  644. end
  645. if headers["Location"] && errorcode >= 300 && errorcode < 400
  646. socket.close rescue socket=nil
  647. return pbDownloadData(headers["Location"],filename,depth+1)
  648. end
  649. if chunked==true
  650. # Chunked content
  651. while true
  652. lengthline=socket.gets.sub(/\r$/,"")
  653. length=lengthline.to_i(16)
  654. break if length==0
  655. while Time.now.to_i-time>=5 || socket.select(10)==0
  656. time=Time.now.to_i
  657. Graphics.update
  658. end
  659. data+=socket.recv(length)
  660. socket.gets
  661. end
  662. elsif length==-1
  663. # No content length specified
  664. while true
  665. if socket.select(500)==0
  666. break
  667. else
  668. while Time.now.to_i-time>=5 || socket.select(10)==0
  669. time=Time.now.to_i
  670. Graphics.update
  671. end
  672. data+=socket.recv(1)
  673. end
  674. end
  675. else
  676. # Content length specified
  677. while length>0
  678. chunk=[length,4096].min
  679. while Time.now.to_i-time>=5 || socket.select(10)==0
  680. time=Time.now.to_i
  681. Graphics.update
  682. end
  683. data+=socket.recv(chunk)
  684. length-=chunk
  685. end
  686. end
  687. end
  688. if filename
  689. File.open(filename,"wb"){|f|
  690. f.write(data)
  691. }
  692. else
  693. return data
  694. end
  695. ensure
  696. socket.close rescue socket=nil
  697. end
  698. return "Error in pbHttpRequest"
  699. end
  700.  
  701. def pbDownloadToString(url)
  702. begin
  703. data=pbDownloadData(url)
  704. return data
  705. rescue Exception => msg
  706. return "Error in pbDownloadToString"
  707. end
  708. end
  709.  
  710. def pbDownloadToFile(url, file)
  711. begin
  712. pbDownloadData(url,file)
  713. rescue
  714. end
  715. end
  716.  
  717. def pbPostToString(url, postdata)
  718. begin
  719. data=pbPostData(url, postdata)
  720. return data
  721. rescue Exception => e
  722. puts e.message
  723. puts e.backtrace.inspect
  724. return ""
  725. end
  726. end
  727.  
  728. def pbPostToFile(url, postdata, file)
  729. begin
  730. pbPostData(url, postdata,file)
  731. rescue
  732. end
  733. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement