Hello all!
Note: The code requires at least PB 4.20 final!
I wrote this in order to be able to download files from redirected URLs.
// edit 2008-05-30:
Based on an idea by Trond, I wrote another version which I think is better.
// edit 2010-03-10: Corrected the link, which had changed due to the new phpBB version.

Code: Select all
; successfully tested on PureBasic 4.20 final
EnableExplicit
Procedure.s RealURL (url.s, recursionDepth.l=10)
; Very useful, because ReceiveHTTPFile() does
; not handle redirected URLs.
; in : URL
; out: - if not redirected: original address
; - if redirected: target address
Protected header.s, line.s, index.l
header = GetHTTPHeader(url)
index = 0
Repeat
index + 1
line = StringField(header, index, #LF$)
; Debug line
If FindString(line, "Location:", 1) = 1
If recursionDepth > 0 ; protection against endless loops
line = LTrim(Mid(line, 10))
If Right(line, 1) = #CR$
line = Left(line, Len(line)-1)
EndIf
url = RealURL(line, recursionDepth-1)
Else
url = "" ; error
EndIf
Break
EndIf
Until line = ""
ProcedureReturn url
EndProcedure
;-- Demo
InitNetwork()
Debug RealURL("http://forum.purebasic.com/")