1. --****************************************************************************
  2. -- Software: NET CLASS
  3. -- Version:  1.1
  4. -- Date:     2011-09-14
  5. -- Author:   Valentin Schmidt
  6. -- Contact:  fluxus@freenet.de
  7. -- License:  Freeware
  8. --
  9. -- Usage:
  10. --
  11. -- props = [:]
  12. -- props["url"] = member("URL").text  
  13. -- props["method"] = "post"
  14. -- props["cb_handler"] = #netCallback
  15. -- props["cb_target"] = me
  16. -- props["vars"] = ["foo":"bar"]
  17. -- pNet = script("NET").new(props)
  18. --
  19. --****************************************************************************
  20.  
  21. property pNetID
  22. property pProps
  23. property pTimeout
  24.  
  25. -- progressbar related
  26. property pPbProps
  27.  
  28. ----------------------------------------
  29. -- props: url, vars, method, timeout, serverosstring, cb_handler, cb_target, file
  30. -- progbar_props (optional): rate, ...
  31. ----------------------------------------
  32. on new (me, props, progbar_props)
  33.  
  34.   if voidP(props["vars"]) then props["vars"]=[:]
  35.   if voidP(props["method"]) then props["method"] ="get"
  36.   if voidP(props["serverosstring"]) then props["serverosstring"]="UNIX" -- "Win", "Mac"
  37.   if not voidP(props["timeout"]) then me.pTimeout = the milliseconds+props["timeout"]
  38.   if voidP(props["cb_target"]) then props["cb_target"]=_movie
  39.  
  40.   if not voidP(progbar_props) then
  41.     if voidP(progbar_props["rate"]) then progbar_props["rate"]=200 -- in ms
  42.     pPbProps = progbar_props
  43.     pPbProps["timer"] = the milliseconds
  44.     pPbProps["n"] = 0
  45.   end if
  46.  
  47.   me.pProps = props
  48.  
  49.   case (props["method"]) of
  50.     "post":
  51.       me.pNetID = postNetText(props["url"], props["vars"], props["serverosstring"])
  52.     "get":
  53.       me.pNetID = getNetText(props["url"], props["vars"], props["serverosstring"])
  54.     "preload":
  55.       me.pNetID = preloadNetThing(props["url"])
  56.     "download":
  57.       me.pNetID = downloadNetThing(props["url"], props["file"])
  58.      
  59.       -- D11.5+ only
  60.     "post_bytes":
  61.       me.pNetID = postNetBytearray(props["url"], props["vars"])
  62.     "get_bytes":
  63.       me.pNetID = getNetBytearray(props["url"], props["vars"], props["serverosstring"])
  64.   end case
  65.  
  66.   (the actorList).add(me)
  67.   return me
  68. end
  69.  
  70. ----------------------------------------
  71. -- HOOKS FOR PROGRESS BAR (overwrite, either directly or in child script!)
  72. ----------------------------------------
  73. on updateProgBar (me, prog)
  74.   --
  75. end
  76.  
  77. on stopProgBar (me)
  78.   --
  79. end
  80.  
  81. ----------------------------------------
  82. --
  83. ----------------------------------------
  84. on stepFrame me
  85.   progbar_flag = not voidP(pPbProps)
  86.  
  87.   if progbar_flag then
  88.     if (the milliseconds-pPbProps["timer"]>pPbProps["rate"]) then
  89.       if ["preload","download"].getpos(pProps["method"]) then
  90.         info = getStreamStatus(me.pNetID)
  91.         if info.bytesTotal>0 then
  92.           me.updateProgBar(float(info.bytesSoFar)/info.bytesTotal)
  93.         end if
  94.       else        
  95.         pPbProps["n"]=pPbProps["n"]+10
  96.         if pPbProps["n"]>100 then pPbProps["n"]=0
  97.         me.updateProgBar(pPbProps["n"]/100.0)
  98.       end if
  99.       pPbProps["timer"] = the milliseconds
  100.     end if
  101.   end if
  102.  
  103.   if netDone(me.pNetID) then
  104.     res = EMPTY
  105.     if ["post", "get"].getPos(me.pProps["method"]) then
  106.       res = netTextResult(me.pNetID)
  107.     else if ["post_bytes", "get_bytes"].getPos(me.pProps["method"]) then
  108.       res = netBytearrayResult(me.pNetID)
  109.     end if
  110.     err = netError(me.pNetID)
  111.     errStr = me.returnErrorString(err)
  112.    
  113.     (the actorList).deleteOne(me)
  114.    
  115.     if progbar_flag then me.stopProgBar()
  116.     if not voidP(me.pProps["cb_handler"]) then call(me.pProps["cb_handler"], me.pProps["cb_target"], res, err, errStr, me.pProps)
  117.    
  118.   else if not voidP(me.pTimeout) then
  119.     if the milliseconds>pTimeout then
  120.      
  121.       (the actorList).deleteOne(me)
  122.      
  123.       if progbar_flag then me.stopProgBar()
  124.       if not voidP(me.pProps["cb_handler"]) then call(me.pProps["cb_handler"], me.pProps["cb_target"], EMPTY, -1, "Timeout", me.pProps)
  125.      
  126.     end if
  127.   end if
  128. end
  129.  
  130. ----------------------------------------
  131. --Error codes http post
  132. ----------------------------------------
  133. on returnErrorString me, errorCode
  134.   if errorCode = "OK" then return("")
  135.   case errorCode of
  136.     "OK": return "OK"
  137.     -128 : return "User cancelled."
  138.     0    : return "Everything is OK, peachy, and generally acceptable."
  139.     1    : return "Memory error."
  140.     4    : return "Bad MOA Class. Network Xtras may be improperly installed."
  141.     5    : return "Bad MOA Interface. Network Xtras may be improperly installed."
  142.     6    : return "Bad URL."
  143.     20   : return "Internal error. Browser detected a network or internal error."
  144.     42   : return "Response Header Field too long to be handled by NetLingo."
  145.     900  : return "File to be written to is read only."
  146.     903  : return "Disk is full."
  147.     905  : return "Bad URL."
  148.     2018 : return "postNetText fail."
  149.     4146 : return "Connection could not be established with the remote host."
  150.     4149 : return "Data supplied by the server was in an unexpected format."
  151.     4150 : return "Unexpected early closing of connection."
  152.     4152 : return "Data returned is truncated."
  153.     4154 : return "Operation could not be completed due to timeout."
  154.     4155 : return "Not enough memory available to complete the transaction."
  155.     4156 : return "Protocol reply to request indicates an error in the reply."
  156.     4157 : return "Transaction failed to be authenticated."
  157.     4159 : return "Invalid URL."
  158.     4164 : return "Could not create a socket."
  159.     4165 : return "Requested Object could not be found (URL may be incorrect)."
  160.     4166 : return "Generic proxy failure."
  161.     4167 : return "Transfer was intentionally interrupted by client."
  162.     4168 : return "Failed network operation."
  163.     4240 : return "The network xtras weren't initialized properly."
  164.     4242 : return "Download stopped by netAbort(url)."
  165.     4836 : return "Download stopped for an unknown reason."
  166.     otherwise :
  167.       if errorCode > 4143 AND errorCode < 4164 then
  168.         return "Failed network operation:" && errorCode
  169.       else
  170.         return "Unknown network error:" && errorCode
  171.       end if
  172.   end case
  173. end
  174.  
[raw code]