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: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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?
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
hoerbie
Enthusiast
Enthusiast
Posts: 137
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: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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).
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
hoerbie
Enthusiast
Enthusiast
Posts: 137
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: 1030
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: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
jacdelad
Addict
Addict
Posts: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
idle
Always Here
Always Here
Posts: 6025
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: 1527
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: 2029
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

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

Post by jacdelad »

In this case it should return -1.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply