How does a browser know the size of a file?
How does a browser know the size of a file?
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?
			
			
									
									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
						PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: How does a browser know the size of a file?
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?
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
						PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: How does a browser know the size of a file?
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?
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?
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
						PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: How does a browser know the size of a file?
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):
...in hope that works for all use cases.
			
			
									
									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
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
						PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: How does a browser know the size of a file?
that works well.
			
			
									
									
						- NicTheQuick
- 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?
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.
						Re: How does a browser know the size of a file?
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
						PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD




