Page 1 of 1

Getting HTTP content size

Posted: Sat Jun 03, 2017 1:32 pm
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

Re: Getting HTTP content size

Posted: Tue Jun 13, 2017 4:10 pm
by Kwai chang caine
Works great
Thanks for sharing 8)

Re: Getting HTTP content size

Posted: Tue Jun 13, 2017 4:18 pm
by Shield
Just bear in mind that this is unreliable as the Content-Length header is not always set.

Re: Getting HTTP content size

Posted: Tue Jun 13, 2017 8:35 pm
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.

Re: Getting HTTP content size

Posted: Wed Jun 14, 2017 2:21 pm
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.

Re: Getting HTTP content size

Posted: Thu May 13, 2021 10:22 am
by deseven
Updated to also support absolute and relative redirects (not just full url redirects).