bitetti

GDScript HTTP loader

Jun 19th, 2015
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.47 KB | None | 0 0
  1. #########################################################################
  2. # multi_loader.gd                                                       #
  3. #########################################################################
  4. #                        This file is part of:                          #
  5. #                         WILD WITCH PROJECT                            #
  6. #                     http://www.wildwitchproject/                      #
  7. #########################################################################
  8. #                             CC-BY-SA                                  #
  9. #   Wild Witch Project by Marcos Augusto Bitetti is licensed under a    #
  10. #   Creative Commons Attribution-ShareAlike 4.0 International License.  #
  11. #   Based on a work at http://www.wildwitchproject.com/.                #
  12. #   Permissions beyond the scope of this license may be available at    #
  13. #   http://www.wildwitchproject.com/p/direitos-da-obra.html.            #
  14. #########################################################################
  15.  
  16. # 'load_complete'
  17. # 'load_progress'
  18. # 'load_error'
  19.  
  20. var server = 'localhost'
  21. var protocol = 'HTTP'
  22. var port = 80
  23. var get_string = '/'
  24. var mime_type = 'none'
  25. var data = null
  26. var headers = [
  27.     "User-Agent: Custom Brownser/1.0 (Godot)",
  28.     "Accept: */*"
  29. ]
  30. var response_headers = null
  31. var response_code = 0
  32.    
  33. var _is_working = false
  34. var _http = HTTPClient.new()
  35. var _thread = Thread.new()
  36. var _is_connected = false
  37. var _old_server = ''
  38. var _redirection_level = 0
  39.  
  40. func _init():
  41.     add_user_signal('load_complete')
  42.     add_user_signal('load_progress')
  43.     add_user_signal('load_error')
  44.    
  45. func _parse_url(url):
  46.     var err = 0
  47.     var http = HTTPClient.new()
  48.     var tks = url.split('/',false)
  49.     protocol = 'HTTP'
  50.     if tks[0]=='https:':
  51.         self.protocol = 'HTTPS'
  52.     # remove protocol info
  53.     var i = 0
  54.     if tks[0]=='http:' or tks[0]=='https:' :
  55.         i = 1
  56.  
  57.     server = tks[i]
  58.     i = i + 1
  59.     if server.find(':')>-1:
  60.         var p1 = server.substr(0,server.find(':'))
  61.         var p2 = server.substr(server.find(':')+1,server.length())
  62.         server = p1
  63.         port = int(p2)
  64.  
  65.     get_string = ''
  66.     while i<tks.size():
  67.         get_string += '/' + tks[i]
  68.         i += 1
  69.  
  70. ###
  71. #
  72. static func _bg_load_data(s):
  73.     var err = 0
  74.     var rb = RawArray()
  75.     print(s.protocol,'://',s.server,':',s.port,'/',s.get_string)
  76.     if not s._is_connected or s._old_server!=s.server:
  77.         if s.protocol=='HTTP':
  78.             err = s._http.connect(s.server,s.port,false)
  79.         else:
  80.             print('sim sim')
  81.             err = s._http.connect(s.server,s.port,true)
  82.             print('salabim')
  83.     if err != OK:
  84.         s._is_working = false
  85.         print('err 00')
  86.         return err
  87.     print('quartman')
  88.     #while( s._http.get_status()==HTTPClient.STATUS_CONNECTING or s._http.get_status()==HTTPClient.STATUS_RESOLVING):
  89.     while s._http.get_status()!=HTTPClient.STATUS_CONNECTED:
  90.         print('po')
  91.         s._http.poll()
  92.         print('te')
  93.         OS.delay_msec(200)
  94.     print(s._http.get_status())
  95.     if s._http.get_status() != HTTPClient.STATUS_CONNECTED:
  96.         s._is_working = false
  97.         s._is_connected = false
  98.         print('err 1')
  99.         #return 1
  100.    
  101.     s._is_connected = true
  102.    
  103.     err = s._http.request(HTTPClient.METHOD_GET,s.get_string,s.headers)
  104.     if err != OK:
  105.         s._is_working = false
  106.         s._is_connected = false
  107.         print('err 2')
  108.         return err
  109.     print('bb')
  110.     while (s._http.get_status() == HTTPClient.STATUS_REQUESTING):
  111.         s._http.poll()
  112.         OS.delay_msec(200)
  113.    
  114.     assert( s._http.get_status() == HTTPClient.STATUS_BODY or s._http.get_status() == HTTPClient.STATUS_CONNECTED)
  115.     #   s._is_working = false
  116.     #   s._is_connected = false
  117.     #   print('err 3')
  118.     #   return err
  119.     print('0')
  120.     if s._http.has_response():
  121.         var headers = s._http.get_response_headers_as_dictionary()
  122.         s.response_headers = headers
  123.         print('a')
  124.         # ignore is chuncked
  125.         # s._http.is_response_chunked()
  126.        
  127.         if s._http.get_response_code() != HTTPClient.RESPONSE_MOVED_PERMANENTLY:
  128.             while( s._http.get_status()==HTTPClient.STATUS_BODY):
  129.                 s._http.poll()
  130.                 var chunk = s._http.read_response_body_chunk()
  131.                 if chunk.size()==0:
  132.                     # got nothing, wait for buffers to fill a bit
  133.                     OS.delay_usec(1000)
  134.                 else:
  135.                     rb = rb + chunk
  136.     #print('moved ',s._http.get_response_code() == HTTPClient.RESPONSE_MOVED_PERMANENTLY)
  137.     #print('moved')
  138.     #print(s._http.get_response_code())
  139.     print('b')
  140.     if s._http.get_response_code() == HTTPClient.RESPONSE_MOVED_PERMANENTLY:
  141.         print('BAIXAR ',s.response_headers.Location)
  142.         var nl = get_script().new()
  143.         nl._redirection_level = s._redirection_level + 1
  144.         if nl._redirection_level<2:
  145.             nl.connect('load_complete',s,'_secundary_load')
  146.             nl.load_data(s.response_headers.Location)
  147.             print('segundo')
  148.         else:
  149.             s.emit_signal('load_error',s)
  150.     else:
  151.         s.data = rb
  152.         print('c')
  153.         s.emit_signal('load_complete', s)
  154.  
  155. ###
  156. func _secundary_load(loader):
  157.     data = loader.data
  158.     emit_signal('load_complete',self)
  159.  
  160.  
  161. ####
  162. # return true if can load
  163. # return false if the object is already loading data
  164. ##
  165. func load_data(url):
  166.     if _is_working or _thread.is_active():
  167.         return false
  168.  
  169.     # define protocol
  170.     if url.find('http')==0:
  171.         _parse_url(url)
  172.         _is_working = true
  173.         _thread.start(self,"_bg_load_data",self)
  174.     else:
  175.         if url.find('file://')==0:
  176.             url = url.substr(7,url.length())
  177.         print(url)
  178.         var f = File.new()
  179.         var rb = RawArray()
  180.         if f.file_exists(url):
  181.             if f.open(url,File.READ)==OK:
  182.                 while not f.eof_reached():
  183.                     rb = rb + f.get_buffer(1024)
  184.                 data = rb
  185.                 f.close()
  186.                 emit_signal('load_complete',self)
  187.             else:
  188.                 emit_signal('load_error',self)
  189.         else:
  190.             emit_signal('load_error',self)
Advertisement
Add Comment
Please, Sign In to add comment