Getting HTTP content size

Share your advanced PureBasic knowledge/code with the community.
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Getting HTTP content size

Post by deseven »

Get HTTP Content-Length value with 301/302 redirects.

Code: Select all

Procedure getHTTPSize(url.s,iteration.i = 1)
  Debug url
  Protected size.i,httpCode.i,header.s,headers.s,i.i
  If iteration >= 10 : ProcedureReturn : EndIf
  headers = GetHTTPHeader(url)
  httpCode = Val(StringField(StringField(headers,1,Chr(10)),2," "))
  Select httpCode
    Case 200:
      For i = 1 To CountString(headers,Chr(10))+1
        header = Trim(StringField(headers,i,Chr(10)),Chr(13))
        If FindString(header,"Content-Length",1,#PB_String_NoCase) = 1
          ProcedureReturn Val(StringField(header,2," "))
        EndIf
      Next
    Default:
      For i = 1 To CountString(headers,Chr(10))+1
        header = Trim(StringField(headers,i,Chr(10)),Chr(13))
        If FindString(header,"Location:",1,#PB_String_NoCase) = 1
          If FindString(StringField(header,2," "),"://") ; normal redirect
            ProcedureReturn getHTTPSize(StringField(header,2," "),iteration + 1)
          EndIf
          If Left(StringField(header,2," "),1) = "/" ; absolute redirect
            If GetURLPart(url,#PB_URL_Port)
              ProcedureReturn getHTTPSize(GetURLPart(url,#PB_URL_Protocol) + "://" + 
                                          GetURLPart(url,#PB_URL_Site) + ":" +
                                          GetURLPart(url,#PB_URL_Port) +
                                          StringField(header,2," "),iteration + 1)
            Else
              ProcedureReturn getHTTPSize(GetURLPart(url,#PB_URL_Protocol) + "://" + 
                                          GetURLPart(url,#PB_URL_Site) +
                                          StringField(header,2," "),iteration + 1)
            EndIf
          Else ; relative redirect
            ProcedureReturn getHTTPSize(url + StringField(header,2," "),iteration + 1)
          EndIf
        EndIf
      Next
      ProcedureReturn
  EndSelect
EndProcedure
Last edited by deseven on Thu May 13, 2021 10:22 am, edited 2 times in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Getting HTTP content size

Post by Kwai chang caine »

Works great
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Getting HTTP content size

Post by Shield »

Just bear in mind that this is unreliable as the Content-Length header is not always set.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: Getting HTTP content size

Post by deseven »

Shield wrote:Just bear in mind that this is unreliable as the Content-Length header is not always set.
It will return 0 if there is no Content-Length available. Which is, i believe, pretty reliable.
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Getting HTTP content size

Post by Shield »

I didn't question the reliability of your code, I am saying that one cannot rely
on a web server returning the correct content length (or return any length at all).
This may or may not be an issue depending on what this procedure is used for.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
deseven
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Jan 12, 2011 3:48 pm
Location: Serbia
Contact:

Re: Getting HTTP content size

Post by deseven »

Updated to also support absolute and relative redirects (not just full url redirects).
Post Reply