How does a browser know the size of a file?

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

How does a browser know the size of a file?

Post 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?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
hoerbie
Enthusiast
Enthusiast
Posts: 119
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Re: How does a browser know the size of a file?

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: How does a browser know the size of a file?

Post 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).
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
hoerbie
Enthusiast
Enthusiast
Posts: 119
Joined: Fri Dec 06, 2013 11:57 am
Location: DE/BY/MUC

Re: How does a browser know the size of a file?

Post 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)
Nituvious
Addict
Addict
Posts: 999
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: How does a browser know the size of a file?

Post 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.
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: How does a browser know the size of a file?

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: How does a browser know the size of a file?

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How does a browser know the size of a file?

Post by idle »

that works well.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: How does a browser know the size of a file?

Post by NicTheQuick »

Keep in mind that there are also webservers that output dynamic content and may not know the content length in advance.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: How does a browser know the size of a file?

Post by jacdelad »

In this case it should return -1.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply