#cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.6.1 Author: Matwachich Script Function: Inspired from HTTP.au3 by Greg "Overload" Laabs #ce ---------------------------------------------------------------------------- #include-once #Region - ### Example ### Global $old = 0 $url = _Url_To_Host("http://files2.freedownloadmanager.org/lite/fdminst-lite-lngs.exe") For $i In $url ConsoleWrite($i & @CRLF) Next Do $ret = _InetGetX($url[0], $url[1], @DesktopDir & "\fdminst-lite-lngs.exe", -1, 0, "_callBack") ConsoleWrite($ret & " - " & @error & " - " & @extended & @CRLF) Until $ret = 1 And not @error Func _callBack($i, $total) Local $p = Round(($i/$total)*100) If $old <> $p Then ConsoleWrite("- " & $p & " %" & @CRLF) $old = $p EndFunc #EndRegion ### ; # FUNCTION # ================================================================================================ ; Name...........: _InetGetX ; Description....: Téléchager un fichier, fonction reprendre prise en charge ; ; Parameters.....: - $sHost = Le server (ex: "www.site.fr", "http://mon_site.hebergeur.com/") ; - $sRessource = Le fichier à télécharger (ex: "/dossier/fichier.exe", "dossier/fichier.txt") ; - $sFile = Le chemin du fichier local (destination) ; - $iStartByte = Le position (en octet) d'ou commencer/reprendre le téléchargement ; (DEFAULT: 0 = Depuis le début) ; Si = -1, le téléchargement reprend depuis la taille du fichier en local ; - $bForce = Forcer le téléchargement même si la taille du fichier ne peut être récupérer ; (DEFAULT: 0) ; (Si = 1, Aucune vérification ne sera faite à la fin du téléchargement) ; Voir Return values ; - $sCallBack = Fonction call-back de forme: ; Func _callBack($iDownloaded, $iTotalSize) ; (DEFAULT: "" => Aucun call-back) ; - $iTimeOut = Time-out (en secondes) PS: temps maximum sans recevoir de données du serveur ; (DEFAULT: 5) ; - $sAgent = User-Agent (Header HTTP) ; (DEFAULT: "AutoItMacro") ; ; Return values..: - $bForce = 0 ; - Ret = 0, @error = 1 => Echec inetgetsize ; - Ret = 1, @error = 2 => taille fichier local = Taille fichier distant (@extended = Taille) ; - Ret = 1, @error = 0 => Succes, @extended = Total télécharge (octets) ; (PS: Ici, @extended = FileGetSize("Fichier télécharger")) ; - Ret = 0, @error = 7 => Echec, @extended = Total télécharge (octets) ; - $bForce = 1 ; - Ret = -1, @error = 0 => La fonction s'est bien terminé, mais impossible de vérifier le téléchargement ; ; - Ret = 0: ; - @error = -x => Vérifiez le paramètre N° x ; - @error = 3 => Impossible de se connecter ; - @error = 4 => Impossible d'envoyer la requête HTTP ; - @error = 5 => Timed-out (@extended = Total télécharger (octets)) ; - @error = 6 => Impossible d'ouvrir le fichier destination ; ; Author.........: Matwachich, Inspired from HTTP.au3 by Greg "Overload" Laabs ; Remarks........: - La fonction reprend très bien un téléchargement (sans corruption du fichier) si on a ; killer le processus AutoIt.exe, aussi bien que si il y a un problème de connection ; - La meilleur méthode est de lancer cette fonction avec $iStartByte = -1, comme cela, ; Elle récupère la taille du fichier destination et continue automatiquement à ; partire de cette taille. ; Exemple: ; Voir entête de l'UDF ; =========================================================================================================== Func _InetGetX($sHost, $sRessource, $sFile, $iStartByte = 0, $bForce = 0, $sCallBack = "", $iTimeOut = 5, $sAgent = "AutoItMacro") ; Checking parameters If String($sHost) <> $sHost Or $sHost = "" Then Return SetError(-1, 0, 0) If String($sRessource) <> $sRessource Or $sRessource = "" Then Return SetError(-2, 0, 0) If String($sFile) <> $sFile Or $sFile = "" Then Return SetError(-3, 0, 0) If Number($iStartByte) <> $iStartByte Then Return SetError(-4, 0, 0) If Number($bForce) <> $bForce Then Return SetError(-5, 0, 0) If String($sCallBack) <> $sCallBack Then Return SetError(-6, 0, 0) If Number($iTimeOut) <> $iTimeOut Or $iTimeOut = 0 Then Return SetError(-7, 0, 0) If String($sAgent) <> $sAgent Or $sAgent = "" Then Return SetError(-8, 0, 0) $iTimeOut = $iTimeOut * 1000 If StringRight($sHost, 1) = "/" Then $sHost = StringTrimRight($sHost, 1) If StringLeft($sHost, 7) = "http://" Then $sHost = StringTrimLeft($sHost, 7) If StringLeft($sRessource, 1) <> "/" Then $sRessource = "/" & $sRessource Local $iFileSize = FileGetSize($sFile) If $iStartByte < 0 Then $iStartByte = $iFileSize ; Getting ressource size Local $iSize = 0x10 ; just random value If Not $bForce Then $iSize = InetGetSize("http://" & $sHost & "/" & $sRessource, 1) ConsoleWrite($iSize & @CRLF) If @error Then Return SetError(1, @error, 0) ; If Local FileSize = Remote FileSize Then cancel download If $iSize = $iFileSize Then Return SetError(2, $iFileSize, 1) EndIf ; Connecting to Server TCPStartup() Local $ip = TCPNameToIP($sHost) Local $socket = TCPConnect($ip, 80) If $socket <= 0 Then Return SetError(3, @error, 0) ; Could not connect XXX ; Creating command string Local $command = "GET " & $sRessource & " HTTP/1.1" & @CRLF $command &= "Host: " & $sHost & @CRLF $command &= "User-Agent: " & $sAgent & @CRLF If $iStartByte > 0 Then $command &= "Range: bytes=" & $iStartByte & "-" & @CRLF $command &= "Connection: close" & @CRLF & @CRLF ConsoleWrite($command & @CRLF) ; Sending command string Local $bytessent = TCPSend($socket, $command) If $bytessent = 0 Then ;FileClose($hFile) ;FileDelete($sFile) TCPCloseSocket($socket) TCPShutdown() Return SetError(4, @error, 0) ; Could not send request XXX EndIf ; Receiving and writing Local $hFile, $openFlag = 1, $bBinary = 0, $bStripLastCRLF = 0 Local $recv, $crlfPos, $split, $writing = 0, $iFirst = 1 Local $timer = TimerInit() ; Pour ne pas fausser la fonction call-back si c'est une reprise de téléchargement Local $iTotalDwn = $iStartByte While 1 $recv = TCPRecv($socket, 4096) ; On recoit un paquet de données If @error Then ExitLoop ; $socket n'est plus valide (déconnecté) If Not $recv Then ; Pas de données ; Test time-out If TimerDiff($timer) >= $iTimeOut Then FileClose($hFile) TCPCloseSocket($socket) TCPShutdown() Return SetError(5, $iTotalDwn, 0) ; Timed out XXX EndIf ContinueLoop ; => continuer la boucle EndIf $timer = TimerInit() ; Si des données sont reçues, on réinitialise le timer ; Si on recoit des données binaires, on les convertie en String If IsBinary($recv) Then $recv = BinaryToString($recv) ; Si c'est le premier paquet de données, on règle le flag d'ouverture du fichier ; et on met $bBinary à 1 If $iFirst Then $openFlag = 17 $bBinary = 1 EndIf EndIf ; Si c'est le premier paquet de données, on ouvre le fichier et on met $iFirst à 0 If $iFirst Then $hFile = FileOpen($sFile, $openFlag) If $hFile = -1 Then Return SetError(6, @error, 0) ; File creation failed XXX FileSetAttrib($sFile, "+H") $iFirst = 0 EndIf ; Si on est pas en train d'ecrire (CaD, on est au premier paquet), On passe les headers de la réponse HTTP: If Not $writing Then $split = StringSplit($recv, @CRLF, 1) If StringInStr($recv, 2) <> @CRLF Then $bStripLastCRLF = 1 ; Because the loop (*) inserts a @CRLF ; at the end of reconstructed $recv string $recv = "" For $i = 1 To $split[0] If $split[$i] = "" And Not $writing Then $writing = 1 ContinueLoop EndIf If $writing Then $recv &= $split[$i] & @CRLF ; Loop (*) EndIf Next If $bStripLastCRLF Then $recv = StringTrimRight($recv, 2) ; Trims the last @CRLF inserted by the loop (*) EndIf ; Ici, on ecrit les données dans le fichier If $writing Then If $bBinary Then $recv = Binary($recv) $iTotalDwn += BinaryLen($recv) Else $iTotalDwn += StringLen($recv) EndIf FileWrite($hFile, $recv) ; Appel de la fonction call-back If $sCallBack <> "" Then Call($sCallBack, $iTotalDwn, $iSize) EndIf Wend FileClose($hFile) FilesetAttrib($sFile, "-H") ; Verifications $iFileSize = FileGetSize($sFile) If Not $bForce Then If $iFileSize = $iSize Then Return SetError(0, $iTotalDwn, 1) Else return setError(7, $iTotalDwn, 0) ; Download failed XXX EndIf Else ; If $bForce Then we don't check file size Return SetError(0, $iTotalDwn, -1) EndIf EndFunc ; ############################################################## ; Permet de convertit un URL en $sHost et $sRessource utilisables par ; _InetGetX ; $ret[0] = $sHost ; $ret[1] = $sRessource Func _Url_To_Host($url) Local $ret[2] $url = StringReplace($url, 'http://', '') $ret[0] = StringLeft($url, StringInStr($url, '/') - 1) $ret[1] = StringTrimLeft($url, StringInStr($url, '/')) Return $ret EndFunc