EatHTTP - Library
Posted: Sat Jul 29, 2006 8:00 pm
EatHTTP is a File Gather/Header gather, Im new to PB but got lot help By ppl here on the forum that i wanna say thanks for :)
Heres the lib
- EDIT i have edit the source code please post new comments
And for download a file simply with interface do this
The "NiceDownload" interface is kinda badly but made so you can make one yourself.
Please post comments about bugs, cheers and post about improvements - since im still new theres probely a couple of things could be made easier and better looking :)
Regardz :oops:
Heres the lib
- EDIT i have edit the source code please post new comments
Code: Select all
; -----------------------------------
; - EatHTTP - Lib for Purebasic4.0 -
; -----------------------------------
; - Author: ToastEater -
; - Thanks to: From PB -
; - NetMaestro and Srod And PBforum -
; - For help me with pointers and -
; - Winsock -
; -----------------------------------
InitNetwork()
Macro line
Chr(13) + Chr(10)
EndMacro
Structure GetFile
dlw.l
http.s
localfile.s
max.l
time.l
status.b
; Status for downloading
; 0 Initilate Download
; 1: Downloading
; 2: Finish :D - this what we want :D
; 3: Failed
EndStructure
Declare.s FindValue(find.s,Array hey.s(2))
Declare GetHeader(address.s,Array out.s(2),timeout.l = 5000)
Declare DownloadFile(*inp.GetFile)
; Returns of GetHeader
;
; -1 No host
; -2 No respons from server in the Timeout(Default 5seconds)
Procedure GetHeader(address.s,Array out.s(2),timeout.l = 5000)
host.s = StringField(address,1,"/")
wsc = OpenNetworkConnection(host,80)
If Not wsc
Print("No Host")
ProcedureReturn -1
EndIf
SendNetworkString(wsc,"GET /" + Mid(address,Len(host) + 2,100) + " HTTP/1.1" + line + "Host: " + host + line + line)
*buffer = AllocateMemory(2)
temper.s = ""
stime = ElapsedMilliseconds()
Repeat
event = NetworkClientEvent(wsc)
If event = #PB_NetworkEvent_Data
ReceiveNetworkData(wsc,*buffer,1)
temper = temper + PeekS(*buffer)
If Right(temper,4) = line + line
temper = Left(temper,Len(temper) - 4)
out(0,0) = StringField(temper,1,line)
out(0,1) = StringField(temper,2," ")
temper = Right(temper,Len(temper) - Len(out(0,0)))
Break
EndIf
ElseIf ElapsedMilliseconds() - stime > timeout ; TimeRan Out
Print("Failed...")
ProcedureReturn -2
EndIf
ForEver
For i = 1 To CountString(temper,line)
temp.s = StringField(temper,i,line)
out(i,0) = Mid(StringField(temp,1,":"),2,1000)
out(i,1) = Mid(temp,Len(out(i,0)) + 4,100)
Next
ProcedureReturn wsc
EndProcedure
Procedure DownloadFile(*inp.GetFile)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; MAKE HEADER ARRAY AND GET HEADER ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Dim header.s(10,1)
wsc = GetHeader(*inp\http,header())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;; MAKE SPACE FOR INFORMATION ;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
*buffer = AllocateMemory(2)
*inp\dlw = 0
*inp\max = Val(FindValue("Content-Length",header() ))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;; ERROR CHECKING ;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
If header(0,1) = "302" ; File Found ?
*inp\status = 0
ProcedureReturn
ElseIf *inp\max = 0 ; Bad header ?
*inp\status = 3
ProcedureReturn
ElseIf wsc = 0 ; Lost Connection ?
*inp\status = 3
ProcedureReturn
EndIf
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ss = CreateFile(0,*inp\localfile)
*inp\time = ElapsedMilliseconds()
*inp\status = 1
While 1
If *inp\max <= *inp\dlw
*inp\status = 2
Break
ElseIf NetworkClientEvent(wsc) = #PB_NetworkEvent_Data
ReceiveNetworkData(wsc,*buffer,1)
WriteData(0,*buffer,1)
*inp\dlw = *inp\dlw + 1
EndIf
Wend
CloseFile(0)
FreeMemory(ss)
FreeMemory(*buffer)
EndProcedure
Procedure p(text.s,x.l,y.l)
ConsoleLocate(x,y)
Print(text)
EndProcedure
Procedure.s FindValue(find.s,Array hey.s(2))
For i = 1 To 10
If LCase(find) = LCase(hey(i,0))
ProcedureReturn hey(i,1)
Break
EndIf
Next
ProcedureReturn "0"
EndProcedure
Procedure NiceDownload(file.s,localfile.s)
OpenConsole()
easy.GetFile
easy\status = 0 ; VERY IMPORTEN without this it may crash
easy\http = file
easy\localfile = localfile
EnableGraphicalConsole(0)
PrintN("")
PrintN("Started PureBasic4 EatHTTP library by ToastEater..")
PrintN("")
PrintN("Start downloading of " + easy\http + "...")
EnableGraphicalConsole(1)
CreateThread(@DownloadFile(),@easy)
p("[",0,24)
p("]",79,24)
status.d = 0
last.l
While easy\status = 0
Delay(50)
Wend
While 1
If easy\dlw > 1 And easy\max > 1
status = easy\dlw / easy\max * 100
EndIf
If last <> status ; Not Needed but reduce the flicking
p("*",1+ status * 0.77,24)
EndIf
ConsoleLocate(0,25)
Print(Str(status) + "% Downloaded: " + Str(easy\dlw) + "/" + Str(easy\max) + " KBs: " + Str( easy\dlw / (ElapsedMilliseconds() - easy\time) ))
If easy\status = 2
p("SUCCESS",0,7)
Break
ElseIf easy\status = 3
p("FAILED",0,7)
Break
EndIf
Delay(50)
Wend
EndProcedure
And for download a file simply with interface do this
Code: Select all
NiceDownload("download.nullsoft.com/winamp/client/winamp523_full_emusic-7plus.exe","winamp.exe")
Input()
Please post comments about bugs, cheers and post about improvements - since im still new theres probely a couple of things could be made easier and better looking :)
Regardz :oops: