Page 1 of 2

EatHTTP - Library

Posted: Sat Jul 29, 2006 8:00 pm
by ToastEater
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

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()
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:

Posted: Sat Jul 29, 2006 10:49 pm
by srod
ToastEater, the forum has mangled your code a little - to the point where it is full of errors. It's not your fault and it happens all the time. You will need to disable html in your profile and then reload the code.

Looking forward to trying this out! :)

Posted: Sat Jul 29, 2006 11:29 pm
by ToastEater
My code is buggy i know that, I need help fix the bugs..

Sorry for my bad english


regards

Posted: Sat Jul 29, 2006 11:50 pm
by srod
The first problem is that the code won't run! I suspect that this is a problem with the forum as we've all run into this - so it may not be your code.

For example, the following line:

Code: Select all

If *inp\max <inp> 1 And  easy\max > 1 
throws up all kinds of errors. Check whether you typed this line because the forum could well have messed it about.

Also, the variable 'easy' doesn't appear to be a structure variable.

Also, the line

Code: Select all

p("*",1+ status * 0.77,24)
is not recognised at all!

Sorry to be so down, but I'm just reporting what Purebasic is throwing.

:?

Posted: Sun Jul 30, 2006 12:34 am
by Dare
Hi ToastEater,

Would like to try your software above but at times the forum mangles lines with greater than and less than symbols (it thinks it is being smart with html).

Can you edit your original post.

1. Delete the existing code in the post, between the code] [/code BBcode tags.
2. Paste the real code into that place.
3. Make sure to check the "Disable HTML in this post" box (just below the text area).
4. Resubmit.


Thanks!

Posted: Sun Jul 30, 2006 12:58 am
by Fred
I have disabled HMTL in posts in the general forum configuration, may be it will solve this.. Please report any problems.

Posted: Sun Jul 30, 2006 1:03 am
by ToastEater
Sorry for double post didnt knew you could edit old post(some forums dont allow editing after xx minuts)

Posted: Mon Jul 31, 2006 4:50 pm
by ToastEater
Now 90looks since the last post, i have replaced the code so it work currectly now.


Regards

Posted: Mon Jul 31, 2006 6:03 pm
by oldBear
Works fine here.

Nice routine. Thanks for posting :)

bye for now

Posted: Mon Jul 31, 2006 6:53 pm
by Dr. Dri
your 'line' macro is useless, replace it by the #CRLF$ constant
besides it is brilliant

Dri :)

Posted: Mon Jul 31, 2006 7:12 pm
by Henrik
:D Really Cool
Thanks for shareing the source ToastEater 8)

Posted: Mon Jul 31, 2006 11:17 pm
by srod
Very nice. Works okay now! 8)

Thanks.

Posted: Tue Aug 01, 2006 12:38 am
by ToastEater
Thanks for the nice comments :D The code can be used like you want edit it etc. The reason i made NiceDownload() procedure is my english suck so hard explain.
Working on bug fix,Improvements.
Please post any suggestion


Regards :D

Posted: Tue Aug 01, 2006 8:00 am
by dige
Hmm, Download failed. We have a Proxy here, may be thats the problem?
How can I add the proxy configuration?

Posted: Tue Aug 01, 2006 9:48 am
by ToastEater
No tutorials about proxy, but playing with Ethereal and proxy, Trying fix it.
Thanks for mention :D