ReceiveHTTPFile - Redirect support

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

ReceiveHTTPFile - Redirect support

Post by oryaaaaa »

Result = ReceiveHTTPFile(URL$, Filename$)

example
http://nr3.coolverse.jp/_userdata/downl ... =nr3_beta2

Download Access counter, Redirect then send file. I want to use linux version.

Could you support redirect mode?
boop64
User
User
Posts: 14
Joined: Tue Nov 18, 2003 12:42 pm
Contact:

Re: ReceiveHTTPFile - Redirect support

Post by boop64 »

oryaaaaa wrote:Result = ReceiveHTTPFile(URL$, Filename$)

example
http://nr3.coolverse.jp/_userdata/downl ... =nr3_beta2

Download Access counter, Redirect then send file. I want to use linux version.

Could you support redirect mode?
Hi oryaaaaa,

You can implement this your self using the tools that are at your disposal. Since this method uses nothing but the built in functions it should be cross platform.

Code: Select all

InitNetwork()

CreateRegularExpression(0, "(302|301)")
CreateRegularExpression(1, "http://.*$")
CreateRegularExpression(2, "http://.*/")
CreateRegularExpression(3, "200")

inputurl$ = ProgramParameter(0)

 Procedure.s Parse302(url$)
    Header$ = GetHTTPHeader(url$)
    Index = 0
    Index+1
    Line$ = StringField(Header$, Index, #LF$)
    If MatchRegularExpression(0, Line$)
      Debug "302 FOUND!... Parsing."
      Repeat 
      Index+1
      Line$ = StringField(Header$, Index, #LF$)
      Until MatchRegularExpression(1, Line$)
      Dim Result$(0)
      ExtractRegularExpression(1, Line$, Result$())
      Debug Result$(0)
    Else
      Debug url$
      ProcedureReturn url$ 
    EndIf 
  ProcedureReturn Result$(0)

EndProcedure

Repeat  
    inputurl$ = Parse302(inputurl$)
    Header$ = GetHTTPHeader(inputurl$)
    Index = 0
    Index+1
    Line$ = StringField(Header$, Index, #LF$)
Until MatchRegularExpression(3, Line$)
      Debug "Attempting to download file."
      Dim Result$(0)
      filename$ = GetCurrentDirectory() + ReplaceRegularExpression(2, inputurl$, "")
      filename$ = Left(filename$, Len(filename$)-1)
      Debug filename$
      PrintN("Downloading file: " + filename$)
      ReceiveHTTPFile(inputurl$, filename$)
      PrintN("Done!")

This is a rough idea of what could be done that I threw together. Basically you could inspect the headers of the URL you are wanting to process and determine if it's a redirect "302" or "301" then just parse out the information as needed.

This code when compiled takes one param via the commanline, the url. Best to encapsulate the url with quotes as some of the characters in URLs don't make bash very happy.

Anyway sorry for the rough code I have been away from PB for a while and I'm starting to pick it back up.

Hope it helps.

Boop64
Post Reply