EatHTTP - Library

Share your advanced PureBasic knowledge/code with the community.
ToastEater
User
User
Posts: 49
Joined: Tue Jul 25, 2006 5:07 pm

EatHTTP - Library

Post 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:
Last edited by ToastEater on Sun Jul 30, 2006 1:56 pm, edited 3 times in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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! :)
I may look like a mule, but I'm not a complete ass.
ToastEater
User
User
Posts: 49
Joined: Tue Jul 25, 2006 5:07 pm

Post by ToastEater »

My code is buggy i know that, I need help fix the bugs..

Sorry for my bad english


regards
Sorry for my damn english
amour au PB et au traducteur de google d'ofcourse
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.

:?
I may look like a mule, but I'm not a complete ass.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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!
Dare2 cut down to size
Fred
Administrator
Administrator
Posts: 18252
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

I have disabled HMTL in posts in the general forum configuration, may be it will solve this.. Please report any problems.
ToastEater
User
User
Posts: 49
Joined: Tue Jul 25, 2006 5:07 pm

Post by ToastEater »

Sorry for double post didnt knew you could edit old post(some forums dont allow editing after xx minuts)
Sorry for my damn english
amour au PB et au traducteur de google d'ofcourse
ToastEater
User
User
Posts: 49
Joined: Tue Jul 25, 2006 5:07 pm

Post by ToastEater »

Now 90looks since the last post, i have replaced the code so it work currectly now.


Regards
Sorry for my damn english
amour au PB et au traducteur de google d'ofcourse
oldBear
Enthusiast
Enthusiast
Posts: 121
Joined: Tue Jul 05, 2005 2:42 pm
Location: berrypatch

Post by oldBear »

Works fine here.

Nice routine. Thanks for posting :)

bye for now
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

your 'line' macro is useless, replace it by the #CRLF$ constant
besides it is brilliant

Dri :)
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

:D Really Cool
Thanks for shareing the source ToastEater 8)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Very nice. Works okay now! 8)

Thanks.
I may look like a mule, but I'm not a complete ass.
ToastEater
User
User
Posts: 49
Joined: Tue Jul 25, 2006 5:07 pm

Post 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
Sorry for my damn english
amour au PB et au traducteur de google d'ofcourse
dige
Addict
Addict
Posts: 1412
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Hmm, Download failed. We have a Proxy here, may be thats the problem?
How can I add the proxy configuration?
ToastEater
User
User
Posts: 49
Joined: Tue Jul 25, 2006 5:07 pm

Post by ToastEater »

No tutorials about proxy, but playing with Ethereal and proxy, Trying fix it.
Thanks for mention :D
Sorry for my damn english
amour au PB et au traducteur de google d'ofcourse
Post Reply