Code : Tout sélectionner
;{/ Librairie HTTP
#HTTP_Ok = 1
#HTTP_Error = 0
#HTTP_ProxyData_Needed = -2
#HTTP_Authentication_Needed =-3
#HTTP_Invalid_URL = -4
#HTTP_FileNotFound = -5
#HTTP_FileMoved = -6
#HTTP_TimeOut = -7
#HTTP_UnknowFileSize = -8
#HTTP_UnknowDate = -9
#HTTP_Downloading = 2
#HTTP_DownloadEnd = 3
#HTTP_Action_Get2File = 2
#HTTP_Action_Get2Mem = 3
#HTTP_Action_GetInfo = 4
Procedure HTTP_Init();Start HTTP library
If InitNetwork()
Global InBuffer.l,Last_Error.l
InBuffer=10240
NewList http_data.http()
If AddElement(http_data()) : EndIf
;-URL-Commands
;{Day,Month
Dim wkday$(6)
Dim weekday$(6)
Dim Month$(12)
wkday$(1)="MON"
wkday$(2)="TUE"
wkday$(3)="WED"
wkday$(4)="THU"
wkday$(5)="FRI"
wkday$(6)="SAT"
wkday$(0)="SUN"
weekday$(1)="MONDAY"
weekday$(2)="TUESDAY"
weekday$(3)="WEDNESDAY"
weekday$(4)="THURSDAY"
weekday$(5)="FRIDAY"
weekday$(6)="SATURDAY"
weekday$(0)="SUNDAY"
Month$(1)="JAN"
Month$(2)="FEB"
Month$(3)="MAR"
Month$(4)="APR"
Month$(5)="MAY"
Month$(6)="JUN"
Month$(7)="JUL"
Month$(8)="AUG"
Month$(9)="SEP"
Month$(10)="OCT"
Month$(11)="NOV"
Month$(12)="DEC"
;}
ProcedureReturn #HTTP_Ok
Else
ProcedureReturn #HTTP_Error
EndIf
EndProcedure
Procedure.s HTTP_LastError_Description();Textual error description
Select Last_Error
Case #HTTP_Invalid_URL
text.s="L'URL est invalide."
Case #HTTP_ProxyData_Needed
text.s="Proxy authentication needed"
Case #HTTP_Authentication_Needed
text.s="Vous devez vous identifier."
Case #HTTP_FileNotFound
text.s="Fichier introuvable."
Case #HTTP_FileMoved
text.s="Fichier déplacé."
Case #HTTP_TimeOut
text.s="Délai de connexion expiré."
Default
text.s="Une erreur est survenue."
EndSelect
ProcedureReturn text.s
EndProcedure
Procedure HTTP_LastError_ID();Last error return code
ProcedureReturn Last_Error
EndProcedure
;///// internal procedures
Procedure int_AnalyseDate(TestTime$)
;Thank to GPI for this code :P
TestTime$=ReplaceString(Trim(UCase(TestTime$))," "," ")
day=0:Month$="":year=0:Time$=""
For i=0 To 6
If Left(TestTime$,4)=wkday$(i)+"," ;{"rfc1123-Date"
day=Val(StringField(TestTime$,2," "))
Month$=StringField(TestTime$,3," ")
year=Val(StringField(TestTime$,4," "))
Time$=StringField(TestTime$,5," ")
Break
;}
ElseIf Left(TestTime$,Len(weekday$(i))+1)=weekday$(i)+"," ;{"rfc850-Date"
SubTime$=StringField(TestTime$,2," ")
day=Val(StringField(SubTime$,1,"-"))
Month$=StringField(SubTime$,2,"-")
year=Val(StringField(SubTime$,3,"-"))
If year>80:year+1900:Else:year+2000:EndIf
Time$=StringField(TestTime$,3," ")
Break
;}
ElseIf Left(TestTime$,4)=wkday$(i)+" " ;{"asctime-Date"
day=Val(StringField(TestTime$,3," "))
Month$=StringField(TestTime$,2," ")
year=Val(StringField(TestTime$,5," "))
Time$=StringField(TestTime$,4," ")
Break
;}
EndIf
Next
For i=1 To 12
If Month$(i)=Month$ : month=i:Break : EndIf
Next
Date=ParseDate("%hh:%ii:%ss",Time$)
Hour=Hour(Date)
Min=Minute(Date)
Sec=Second(Date)
ProcedureReturn Date(year,month,day,Hour,Min,Sec)
EndProcedure
Procedure int_Parse_URL(ConnectionID.l,Url.s)
Url=LCase(Url)
If FindString(Url,"http://",0)=#True
Url=RemoveString(Url,"http://")
EndIf
Url=ReplaceString(Url,"\","/",1)
server.s=StringField(Url,1,"/")
If server<>""
http_data()\server=StringField(Url,1,"/")
out.s=""
For a=2 To CountString(Url,"/")
out.s+"/"+StringField(Url,a,"/")
Next
out.s+"/"
If out<>"/"
http_data()\path=out
Else
http_data()\path=""
EndIf
file.s=StringField(Url,CountString(Url,"/")+1,"/")
If file<>""
http_data()\file=file
Else
ProcedureReturn #HTTP_Invalid_URL
EndIf
ProcedureReturn #HTTP_Ok
Else
ProcedureReturn #HTTP_Invalid_URL
EndIf
EndProcedure
Procedure int_Action(in_data.l)
command.s=PeekS(in_data)
ConnectionID.l=Val(StringField(command,1,"|"))
Action.l=Val(StringField(command,2,"|"))
If SelectElement(http_data(),ConnectionID)=0
ProcedureReturn #HTTP_Error
EndIf
If http_data()\proxy<>""
conc$=http_data()\puser+":"+http_data()\ppass
OutputBuffer = AllocateMemory(Len(conc$))
Base64Encoder(conc$,Len(conc$),OutputBuffer,OutputBuffer*2)
penc$=PeekS(OutputBuffer)
internetID = OpenNetworkConnection(proxyserver$, Val(proxyport$))
Else
internetID = OpenNetworkConnection(server$, Val(port$))
EndIf
If http_data()\wwwpass<>""
conc$=http_data()\wwwuser +":"+http_data()\wwwpass
OutputBuffer = AllocateMemory(Len(conc$))
Base64Encoder(conc$,Len(conc$),OutputBuffer,OutputBuffer*2)
wenc$=PeekS(OutputBuffer)
EndIf
header=#False
download=#False
file_size=0
resend:
If http_data()\proxy<>""
internetID = OpenNetworkConnection(http_data()\proxy, http_data()\pport)
Else
internetID = OpenNetworkConnection(http_data()\server,http_data()\url_port)
EndIf
If internetID
;{ /// File Information
com$="HEAD "+http_data()\path+http_data()\file+" HTTP/1.1"+Chr(13)+Chr(10)
com$+"Accept: */*"+Chr(13)+Chr(10)
com$+"Host: "+http_data()\server+Chr(13)+Chr(10)
com$+"User-Agent: PureDownload 1.0"+Chr(13)+Chr(10)
If http_data()\proxy<>""
com$+"Proxy-Authorization: Basic "+enc$+Chr(13)+Chr(10)
EndIf
If http_data()\wwwuser<>""
com$+"Authorization: Basic "+wenc$+Chr(13)+Chr(10)
EndIf
If cookie$<>""
com$+"Cookie: "+cookie$+Chr(13)+Chr(10)
EndIf
If location$<>""
com$+"Location: "+location$+Chr(13)+Chr(10)
EndIf
com$+Chr(13)+Chr(10)
res = SendNetworkData(internetID,@com$,Len(com$))
If res < 0
CloseNetworkConnection(internetID)
Last_Error=#HTTP_Error
ProcedureReturn #HTTP_Error
EndIf
Repeat
index.l=ListIndex(http_data())
If index<0
ProcedureReturn
EndIf
Result = NetworkClientEvent(internetID)
If Result=2 ;/// Raw data received
Content$ = Space(14500)
ReceiveNetworkData(internetID,@Content$,14500)
Content$=Trim(LCase(Content$))
;/// File not found handle
If FindString(Content$,"404",1)
CloseNetworkConnection(internetID)
Last_Error=#HTTP_FileNotFound
ProcedureReturn #HTTP_FileNotFound
EndIf
;/// File moved
If FindString(Content$,"301",1)
CloseNetworkConnection(internetID)
Last_Error=#HTTP_FileMoved
ProcedureReturn #HTTP_FileMoved
EndIf
;/// www authorization required
If FindString(Content$,"401",1)
CloseNetworkConnection(internetID)
Last_Error=#HTTP_Authentication_Needed
ProcedureReturn #HTTP_Authentication_Needed
EndIf
;/// File found handle but redirect
If FindString(Content$,"302",1)
location$=""
loc=FindString(Content$,"location: ",1)
If loc>0
temploc.s=Mid(Content$,loc+10,Len(Content$))
For a=1 To Len(temploc)
tcok.s=Mid(temploc,a,1)
If tcok<>Chr(13)
location$+tcok
Else
Break 1
EndIf
Next
EndIf
EndIf
;/// Site sends cookie authentication
cok=FindString(Content$,"set-cookie: ",1)
cookie$=""
If cok>0
tempcok.s=Mid(Content$,cok+12,Len(Content$))
For a=1 To Len(tempcok)
tcok.s=Mid(tempcok,a,1)
If tcok<>";"
cookie$+tcok
Else
Break 1
EndIf
Next
Goto resend
EndIf
;/// File found handle
If FindString(Content$,"200",1)
pos=FindString(Content$,"content-length:" , 1)
If pos
pos=FindString(Content$," " , pos+15)
file_size=Val(Mid(Content$,pos+1,Len(Content$)))
http_data()\file_size=file_size
Else
CloseNetworkConnection(internetID)
Last_Error=#HTTP_UnknowFileSize
ProcedureReturn #HTTP_UnknowFileSize
EndIf
pos=FindString(LCase(Content$),"modified: " , 1)
pos1=FindString(Content$,Chr(13)+Chr(10),pos)
If pos
Date.s=Mid(Content$,pos+10,(pos1-pos-10))
http_data()\file_date=int_AnalyseDate(Date.s)
Else
CloseNetworkConnection(internetID)
Last_Error=#HTTP_UnknowDate
ProcedureReturn #HTTP_UnknowDate
EndIf
Else
CloseNetworkConnection(internetID)
Last_Error=#HTTP_FileNotFound
ProcedureReturn #HTTP_FileNotFound
EndIf
header=#True
EndIf
Until header=#True
;}
If Action=#HTTP_Action_Get2File
;{ /// File Download
com$="GET "+http_data()\path+http_data()\file+" HTTP/1.1"+Chr(13)+Chr(10)
com$+"Accept: */*"+Chr(13)+Chr(10)
com$+"Host: "+http_data()\server+Chr(13)+Chr(10)
com$+"User-Agent: PureDownload 1.0"+Chr(13)+Chr(10)
If http_data()\proxy<>""
com$+"Proxy-Authorization: Basic "+enc$+Chr(13)+Chr(10)
EndIf
If http_data()\wwwuser<>""
com$+"Authorization: Basic "+wenc$+Chr(13)+Chr(10)
EndIf
If cookie$<>""
com$+"Cookie: "+cookie$+Chr(13)+Chr(10)
EndIf
If location$<>""
com$+"Location: "+location$+Chr(13)+Chr(10)
EndIf
com$+Chr(13)+Chr(10)
res = SendNetworkData(internetID,@com$,Len(com$))
If res < 0
CloseNetworkConnection(internetID)
Last_Error=#HTTP_Error
ProcedureReturn #HTTP_Error
EndIf
http_data()\file_progress=0
If CreateFile(0,http_data()\file)
incoming_buffer=AllocateMemory(http_data()\in_buffer)
time.l=ElapsedMilliseconds()
Repeat
Delay(1)
index.l=ListIndex(http_data())
If index<0
ProcedureReturn
EndIf
Result = NetworkClientEvent(internetID)
If Result=0 ;/// time out counter
now=ElapsedMilliseconds()
If now-time > 2000
Last_Error=#HTTP_TimeOut
ProcedureReturn #HTTP_TimeOut
EndIf
ElseIf Result=2 ;/// Raw data received
size=ReceiveNetworkData(internetID,incoming_buffer,http_data()\in_buffer)
offset.l=FindString(PeekS(incoming_buffer),Chr(13)+Chr(10)+Chr(13)+Chr(10),1)
If offset>0
offset+3
EndIf
http_data()\file_progress+size-offset
If size>0
WriteData(incoming_buffer+offset,size-offset)
If http_data()\file_size=http_data()\file_progress
file=#True
EndIf
time=ElapsedMilliseconds()
EndIf
EndIf
Until file=#True
CloseFile(0)
FreeMemory(incoming_buffer)
CloseNetworkConnection(internetID)
ProcedureReturn #HTTP_DownloadEnd
EndIf
;}
EndIf
If Action=#HTTP_Action_Get2Mem
;{ /// Memory Download
com$="GET "+http_data()\path+http_data()\file+" HTTP/1.1"+Chr(13)+Chr(10)
com$+"Accept: */*"+Chr(13)+Chr(10)
com$+"Host: "+http_data()\server+Chr(13)+Chr(10)
com$+"User-Agent: PureDownload 1.0"+Chr(13)+Chr(10)
If http_data()\proxy<>""
com$+"Proxy-Authorization: Basic "+enc$+Chr(13)+Chr(10)
EndIf
If http_data()\wwwuser<>""
com$+"Authorization: Basic "+wenc$+Chr(13)+Chr(10)
EndIf
If cookie$<>""
com$+"Cookie: "+cookie$+Chr(13)+Chr(10)
EndIf
If location$<>""
com$+"Location: "+location$+Chr(13)+Chr(10)
EndIf
com$+Chr(13)+Chr(10)
res = SendNetworkData(internetID,@com$,Len(com$))
If res < 0
CloseNetworkConnection(internetID)
Last_Error=#HTTP_Error
ProcedureReturn #HTTP_Error
EndIf
http_data()\file_progress=0
If http_data()\membuffer
incoming_buffer=AllocateMemory(http_data()\in_buffer)
time.l=ElapsedMilliseconds()
Repeat
Delay(1)
index.l=ListIndex(http_data())
If index<0
ProcedureReturn
EndIf
Result = NetworkClientEvent(internetID)
If Result=0 ;/// time out counter
now=ElapsedMilliseconds()
If now-time > 2000
Last_Error=#HTTP_TimeOut
ProcedureReturn #HTTP_TimeOut
EndIf
ElseIf Result=2 ;/// Raw data received
size=ReceiveNetworkData(internetID,incoming_buffer,http_data()\in_buffer)
offset.l=FindString(PeekS(incoming_buffer),Chr(13)+Chr(10)+Chr(13)+Chr(10),1)
If offset>0
offset+3
EndIf
If size>0
CopyMemory(incoming_buffer+offset,http_data()\membuffer+http_data()\file_progress,size-offset)
If http_data()\file_size=http_data()\file_progress
file=#True
EndIf
time=ElapsedMilliseconds()
EndIf
http_data()\file_progress+size-offset
EndIf
Until file=#True
FreeMemory(incoming_buffer)
EndIf
CloseNetworkConnection(internetID)
ProcedureReturn #HTTP_DownloadEnd
;}
EndIf
CloseNetworkConnection(internetID)
ProcedureReturn #HTTP_Ok
EndIf
EndProcedure
;///// External procedures
Procedure HTTP_New_Connection(Url.s,Port.l) ;Create a new HTTP connection, if file exists returns ConnectionID
If Port=0
Port=80
EndIf
If AddElement(http_data())
Result = int_Parse_URL(ListIndex(http_data()),Url)
If Result<>#HTTP_Ok
DeleteElement(http_data())
Last_Error=#HTTP_Invalid_URL
ProcedureReturn #HTTP_Error
EndIf
index.l=ListIndex(http_data())
http_data()\url_port=Port
http_data()\in_buffer=InBuffer
line.s=Str(index)+"|0"
size=int_Action(@line)
If size<>#HTTP_Ok
ProcedureReturn HTTP_LastError_ID()
EndIf
ProcedureReturn index
Else
ProcedureReturn #HTTP_Error
EndIf
EndProcedure
Procedure HTTP_Delete_Connection(ConnectionID.l);Delete specified connection
If SelectElement(http_data(),ConnectionID)
DeleteElement(http_data())
ProcedureReturn #HTTP_Ok
Else
ProcedureReturn #HTTP_Error
EndIf
EndProcedure
Procedure HTTP_Set_InBuffer(Buffer_size.l) ;Set Global incoming data buffer size
InBuffer=Buffer_size
EndProcedure
Procedure HTTP_Get_InBuffer();Get Global incoming data buffer size
ProcedureReturn InBuffer
EndProcedure
Procedure HTTP_File_Size(ConnectionID.l);Get file size
If SelectElement(http_data(),ConnectionID)
ProcedureReturn http_data()\file_size
Else
ProcedureReturn #HTTP_Error
EndIf
EndProcedure
Procedure HTTP_Download_ToFile(ConnectionID.l ,Filename.s);Download HTTP data to file
http_data()\file_progress=0
If SelectElement(http_data(),ConnectionID)
http_data()\savefile=Filename
ThreadID = CreateThread(@int_Action(),Str(ConnectionID)+"|"+Str(#HTTP_Action_Get2File))
If ThreadID
ProcedureReturn ThreadID
Else
ProcedureReturn #HTTP_Error
EndIf
Else
ProcedureReturn #HTTP_Error
EndIf
EndProcedure
Procedure HTTP_Get_Progress(ConnectionID.l);Returns downloaded size in bytes
If SelectElement(http_data(),ConnectionID)
ProcedureReturn http_data()\file_progress
Else
ProcedureReturn #HTTP_Error
EndIf
EndProcedure
Procedure HTTP_Stop();Stops HTTP library
ClearList(http_data())
EndProcedure
;}
#PROJECT_NAME = "Update PCC 2005"
;- Window Constants
;
Enumeration
#Window_1
EndEnumeration
Enumeration
#Frame3D_0
#ProgressBar_1
#Text_0
#Text_1
#Text_2
#Text_3
#Text_4
#Button_1
EndEnumeration
Procedure Open_Window_1()
If OpenWindow(#Window_1, 545, 215, 317, 153, #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered , #PROJECT_NAME)
If CreateGadgetList(WindowID())
Frame3DGadget(#Frame3D_0, 10, 0, 300, 125, "")
ProgressBarGadget(#ProgressBar_1, 20, 50, 280, 20, 0, 100)
TextGadget(#Text_0, 20, 30, 170, 20, "Version de la mise à jour :")
TextGadget(#Text_1, 20, 80, 140, 20, "Etat du téléchargement : ")
TextGadget(#Text_2, 200, 80, 90, 20, "")
TextGadget(#Text_3, 20, 100, 110, 20, "Vitesse de transfert :")
TextGadget(#Text_4, 200, 100, 90, 20, "")
ButtonGadget(#Button_1, 120, 130, 80, 20, "Annuler")
EndIf
EndIf
EndProcedure
Procedure.s GetWebFilePart(lastUpdate.s)
lastUpdate = ReplaceString(lastUpdate, "http://www." , "c:\")
lastUpdate = ReplaceString(lastUpdate, "/" , "\")
ProcedureReturn GetFilePart(lastUpdate)
EndProcedure
Procedure SetTransfertRate(gadget, nbOctets.f, rate)
unite$ = "o/s"
nbOctets = nbOctets * (1000 / rate)
If nbOctets > 1000
nbOctets = nbOctets / 100
nbOctets = nbOctets / 10.0
unite$ = "Ko/s"
If nbOctets > 1000
nbOctets = nbOctets / 100
nbOctets = nbOctets / 10.0
unite$ = "Mo/s"
EndIf
EndIf
SetGadgetText(gadget, Str(nbOctets) + " " + unite$)
EndProcedure
Procedure SetTransfertState(gadget, actuel, total)
SetGadgetText(gadget, Str(actuel) + "/" + Str(total))
EndProcedure
; *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*
; fichier à télécharger
url$ = "http://www.trendmicro.com/ftp/products/pattern/lpt637.zip"
; Téléchargement du fichier
; -------------------------
If HTTP_Init()
Result= HTTP_New_Connection(url$,80)
If Result>0 ;All error are negative numbers
size.l=HTTP_File_Size(Result)
Open_Window_1() ; Création de la fenêtre
; Fonction pour Dl le fichier
ThreadID = HTTP_Download_ToFile(Result,GetWebFilePart(url$))
If ThreadID
current = 0
rate = 500
; Boucle
Repeat
EventID.l = WindowEvent()
If EventID = #PB_Event_CloseWindow ; If the user has pressed on the close button
KillThread(ThreadID)
End
EndIf
If EventID = #PB_Event_Gadget
If EventGadgetID() = #Button_1
KillThread(ThreadID)
End
EndIf
EndIf
Tempo+1
If Tempo=rate
; Actualisation de la fenêtre
exSize = current
current.l=HTTP_Get_Progress(Result)
SetTransfertRate(#Text_4, current - exSize, rate)
SetTransfertState(#Text_2, current, size)
SetGadgetState(#ProgressBar_1, (current*100)/size)
SetWindowTitle(#Window_1, #PROJECT_NAME + " - " + Str((current*100)/size) + "%")
Tempo=0
EndIf
; On attend (pour ne pas oqp tout l'UC)
Delay(1)
Until size=current
Else
MessageRequester("Erreur", HTTP_LastError_Description(),#MB_ICONERROR)
End
EndIf
Else
MessageRequester("Erreur",HTTP_LastError_Description(),#MB_ICONERROR )
End
EndIf
EndIf
En fait tu ne gère pas les évènements suffisament rapidement ( rate = 500 = 500ms ! ) ce qui génère un défaut de raffraichessement de la fenêtre