Page 1 of 1
How does a browser know the size of a file?
Posted: Sat May 20, 2023 6:45 pm
by jacdelad
Hello,
When I'm downloading a file with a browser it usually knows how big the file is, even if the link is just linked to a file without any script. How does the browser do that?
Re: How does a browser know the size of a file?
Posted: Sat May 20, 2023 7:13 pm
by hoerbie
When you click on the link to download the file, the web server sends a "Content-Length: xxxxxx" in the header with xxxxxx as the byte size of the real following data.
Re: How does a browser know the size of a file?
Posted: Sat May 20, 2023 9:27 pm
by jacdelad
Ah, thanks for the info. Is this somehow usable when using ReceiveHTTPFile/ReceiveHTTPMemory? For now I use an extra file which contains the sizes of the other files (which is an obvious extra step).
Re: How does a browser know the size of a file?
Posted: Sat May 20, 2023 9:52 pm
by hoerbie
Yes, please try for example
Code: Select all
hid = HTTPRequest(#PB_HTTP_Get, "http://yourserver/yourpath/yourfile.zip", "", #PB_HTTP_HeadersOnly)
Debug HTTPInfo(hid,#PB_HTTP_Headers)
Re: How does a browser know the size of a file?
Posted: Sun May 21, 2023 4:11 am
by Nituvious
the web server will reply with a Content-Length header which is the size of the file in bytes. It does this with every file, web page, download, etc.
Re: How does a browser know the size of a file?
Posted: Sun May 21, 2023 8:10 am
by jacdelad
Thanks, I'll try as soon as I'm healthy again (I'm currently sick). Maybe this would be a good addition to PureBasic, automatically getting the site of a file I'm downloading.
Re: How does a browser know the size of a file?
Posted: Tue May 30, 2023 8:58 am
by jacdelad
Ok, so here's a little helping hand. Tested on Windows, but should work on all platforms. Two versions, one using regex and one without regex (if the project doesn't use regex and it shall not be included):
Code: Select all
#HTTP_GetSize_UseRegEx = #True
CompilerIf Defined(HTTP_GetSize_UseRegEx,#PB_Constant)
Global HTTP_GetSize_RegEx=CreateRegularExpression(#PB_Any,"^Content-Length: (\d+)$",#PB_RegularExpression_AnyNewLine|#PB_RegularExpression_MultiLine|#PB_RegularExpression_NoCase)
CompilerEndIf
Procedure HTTPGetSize(URL$)
Protected hid,retval.q=-1,info$
hid = HTTPRequest(#PB_HTTP_Get, URL$, "", #PB_HTTP_HeadersOnly)
If hid
info$=HTTPInfo(hid,#PB_HTTP_Headers)
CompilerIf Defined(HTTP_GetSize_UseRegEx,#PB_Constant)
CompilerIf #HTTP_GetSize_UseRegEx=#True
If ExamineRegularExpression(HTTP_GetSize_RegEx,info$) And NextRegularExpressionMatch(HTTP_GetSize_RegEx)
retval=Val(RegularExpressionGroup(HTTP_GetSize_RegEx,1))
EndIf
CompilerElse
Protected mid
mid=FindString(info$,"Content-Length: ",1,#PB_String_NoCase)
If mid
retval=Val(StringField(StringField(Right(info$,Len(info$)-mid-15),1,#CR$),1,#LF$))
EndIf
CompilerEndIf
CompilerElse
Protected mid
mid=FindString(info$,"Content-Length: ",1,#PB_String_NoCase)
If mid
retval=Val(StringField(StringField(Right(info$,Len(info$)-mid-15),1,#CR$),1,#LF$))
EndIf
CompilerEndIf
FinishHTTP(hid)
EndIf
ProcedureReturn retval
EndProcedure
...in hope that works for all use cases.
Re: How does a browser know the size of a file?
Posted: Tue May 30, 2023 9:35 am
by idle
that works well.
Re: How does a browser know the size of a file?
Posted: Tue May 30, 2023 11:44 am
by NicTheQuick
Keep in mind that there are also webservers that output dynamic content and may not know the content length in advance.
Re: How does a browser know the size of a file?
Posted: Tue May 30, 2023 11:58 am
by jacdelad
In this case it should return -1.