Code archives/Networking/HTTP FileSize() and more

This code has been declared by its author to be Public Domain code.

Download source code

HTTP FileSize() and more by Yan
(Posted 21 years ago)
This was written in answer to a forum post.
I'm putting it here as it may be of use to others.
Print HTTPFileSize("http://www.google.co.uk/intl/en_uk/images/logo.gif") + " Bytes"

End


Function HTTPFileSize(url$)
	url$ = url$.Replace("http://", "")
	Local slashPos = url$.Find("/"), host$, file$
	
	If slashPos <> -1
		host$ = url$[..slashPos]
		file$ = url$[slashPos..]
	Else
		Return -1
	EndIf
	
	Local stream:TStream = OpenStream("tcp::" + host$)
	If Not stream Then Return -1
	
	stream.WriteLine "HEAD " + file$ + " HTTP/1.0"
	stream.WriteLine "Host: " + host$
	stream.WriteLine ""
	
	While Not Eof(stream)
		Local in$ = stream.ReadLine()

		If in$.Find("Content-Length:") <> -1
			stream.Close()
			Return Int(in$[in$.Find(":") + 1..].Trim())
		EndIf	
	Wend
	
	stream.Close()
	Return -1		
End Function

Comments

Yan
(Posted 21 years ago)
Meet more of the family...

Returns the HTTP header.
Print HTTPHeader("http://www.google.co.uk/intl/en_uk/images/logo.gif")

End


Function HTTPHeader$(url$, dropError=False)
	url$ = url$.Replace("http://", "")
	Local slashPos = url$.Find("/"), host$, file$, header$
	
	If slashPos <> -1
		host$ = url$[..slashPos]
		file$ = url$[slashPos..]
	Else
		Return
	EndIf
	
	Local stream:TStream = OpenStream("tcp::" + host$)
	If Not stream Then Return -1
	
	stream.WriteLine "HEAD " + file$ + " HTTP/1.1"
	stream.WriteLine "Host: " + host$
	stream.WriteLine "Connection: close"
	stream.WriteLine ""
	
	Local in$ = stream.ReadLine()	
	If dropError And (in$.Find("200") < 0)
		stream.Close()
		Return
	EndIf
	
	While in$
		header$ :+ in$ + "~r~n"
		
		in$ = stream.ReadLine()
	Wend
	
	stream.Close()
	
	Return header$
End Function


Download a file with a progress bar (not really useful for anything, more of an example).
Strict

Print HTTPGetFile("http://www.google.co.uk/intl/en_uk/images/logo.gif", "c:\google logo.gif")

End


Function HTTPGetFile(url$, filePath$)
	url$ = url$.Replace("http://", "")
	Local slashPos = url$.Find("/"), host$, file$
	
	If slashPos <> -1
		host$ = url$[..slashPos]
		file$ = url$[slashPos..]
	Else
		Return -1
	EndIf
	
	Local fileStream:TStream = OpenStream(filePath$, False, True)
	If Not(fileStream) Then Return -1
	
	Local TCPStream:TStream = OpenStream("tcp::" + host$)
	If Not(TCPStream)
		fileStream.Close()
		Return -1
	EndIf
	
	TCPStream.WriteLine "GET " + file$ + " HTTP/1.1"
	TCPStream.WriteLine "Host: " + host$
	TCPStream.WriteLine "Connection: close"
	TCPStream.WriteLine ""
	
	Local dataLength
	Local in$ = TCPStream.ReadLine()
	
	If in$.Find("200") < 0
		TCPStream.Close()
		fileStream.Close()
		Return -1
	EndIf
	
	While in$
		If in$.Find("Content-Length:") <> -1
			dataLength = Int(in$[in$.Find(":") + 1..].Trim())
		EndIf
		
		in$ = TCPStream.ReadLine()
	Wend
	
	Local byteCount
	While Not(TCPStream.Eof())
		'You'd probaly want to download more than a single byte at a time here
		fileStream.WriteByte(TCPStream.ReadByte())
		
		'You'd normaly update a progress bar here...
		Print "Done " + ((byteCount / Float(dataLength - 1)) * 100) + "%"
		
		'add number of bytes downloaded
		byteCount :+ 1
	Wend
	
	TCPStream.Close()
	fileStream.Close()
	
	If byteCount <> dataLength Then Return -1
End Function


Filax
(Posted 18 years ago)
Another version with progressive download :


Global FullName:String = "http://www.blabla.com/myvideo.mpg"

DownloadFile(FullName ,45) 

End

' ---------------------------
' Function to download a file
' ---------------------------
Function DownloadFile(Url:String , Size:Int )
	Local SourceFileSize:Int=HTTPFileSize(Url) 
	Print "File size : " + SourceFileSize
	
	If SourceFileSize< Size Then
		Size = SourceFileSize
	EndIf
	
	Url=Url.Replace("http://" , "http::") 
	
	
	Local RawBank:TBank
	
	Print "Opening file..."
	Local File:TStream = ReadStream(Url)
	
	Delay 2000
	
	If File Then
		
		Print "Downloading file..."
		Local Export:TStream= WriteStream("Tempo" + "." + "mpg") 
	
		Local BytesCounter:Int = 0
		Local FilesCounter:Int = 0

		Repeat
			
			If BytesCounter+Size >= SourceFileSize Then
				Size=SourceFileSize-BytesCounter
			EndIf
				
			RawBank = CreateBank(Size) 
			File.ReadBytes(RawBank.Buf(),Size)
			
			Export.WriteBytes(RawBank.buf() , BankSize(RawBank) )
						
						
			Local Percent:String = GetPercentage(0 , SourceFileSize , BytesCounter)	
			
			Print BankSize(RawBank)+" / "+Percent
			RawBank = Null
					
			
			BytesCounter=BytesCounter+Size
			FilesCounter = FilesCounter + 1
	
			If BytesCounter >= SourceFileSize Then
				Exit
			EndIf
			
			PollEvent
			
		Forever
	
		CloseFile(File) 
		Return True
	
	Else
	
		Return False
		
		
	EndIf
	
End Function

Function GetPercentage:Float(Range1:Float,Range2:Float,ActualValue:Float)
	Return  (ActualValue - Range1) / ((Range2 - Range1) / 100)
End Function




Function HTTPFileSize(url$)
	url$ = url$.Replace("http://", "")
	Local slashPos = url$.Find("/"), host$, file$
	
	If slashPos <> -1
		host$ = url$[..slashPos]
		file$ = url$[slashPos..]
	Else
		Return -1
	EndIf
	
	Local stream:TStream = OpenStream("tcp::" + host$)
	If Not stream Then Return -1
	
	stream.WriteLine "HEAD " + file$ + " HTTP/1.0"
	stream.WriteLine "Host: " + host$
	stream.WriteLine ""
	
	While Not Eof(stream)
		Local in$ = stream.ReadLine()

		If in$.Find("Content-Length:") <> -1
			stream.Close()
			Return Int(in$[in$.Find(":") + 1..].Trim())
		EndIf	
	Wend
	
	stream.Close()
	Return -1		
End Function


Code Archives Forum