InetgetSize Autoit

Just starting out? Need help? Post your questions and find answers here.
new2pb
New User
New User
Posts: 9
Joined: Tue Aug 03, 2010 4:59 pm

InetgetSize Autoit

Post by new2pb »

Hi all,

I been looking through the libraries of pure basic and only to find receivehttpfile, when I look into Autoit I saw this syntax called InetgetSize. I been wondering how come purebasic does not have a complete network library where I will be able to use such a function.
http://www.autoitscript.com/autoit3/doc ... etSize.htm

I tried these example:
http://www.purebasic.fr/english/posting ... 3&p=204028

but to my surprise it worked but not for authentication type of url ? Is there anyone out there have modified the example in a way to allow authentication and able to get the file size of a file ?
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: InetgetSize Autoit

Post by infratec »

Hi,

what kind of authentication?

For the basic method, you need only to add the following Auth$ to the packet header:

Code: Select all

Auth$ = "user:password"
enc$ = Space(40)
Base64Encoder(@Auth$, StringByteLength(Auth$), @enc$, 40)
Auth$ = "Authorization: Basic " + enc$
Bernd
new2pb
New User
New User
Posts: 9
Joined: Tue Aug 03, 2010 4:59 pm

Re: InetgetSize Autoit

Post by new2pb »

infratec wrote:Hi,

what kind of authentication?

For the basic method, you need only to add the following Auth$ to the packet header:

Code: Select all

Auth$ = "user:password"
enc$ = Space(40)
Base64Encoder(@Auth$, StringByteLength(Auth$), @enc$, 40)
Auth$ = "Authorization: Basic " + enc$
Bernd
Hi Bernd,

authentication such as http://xxxx:xxxx@blahblah.com/img.jpg, will it be possible to use the above method you provide.. do you mind elaborating more on how to implement this code into the example.
Procedure HttpFileSize(Url.s)
hInet = InternetOpen_("Downloader",0,0,0,0)
hURL = InternetOpenUrl_(hInet,Url,0,0,$80000000,0)

BufferLength = 2048
Buffer.s = Space(BufferLength)
BufferLengthWas = BufferLength

Domain.s = StringField(Url,3,"/")
hInetCon = InternetConnect_(hInet,Domain,80,#Null,#Null,3,0,0)
If hInetCon
hHttpOpenRequest = HttpOpenRequest_(hInetCon,"HEAD",ReplaceString(Url,"http://"+Domain+"/",""),#Null,#Null,0,$80000000,0)
If hHttpOpenRequest
iretval = HttpSendRequest_(hHttpOpenRequest,#Null,0,0,0)
If iretval
HttpQueryInfo_(hHttpOpenRequest,19,@Buffer,@BufferLength,0)
String.s = PeekS(@Buffer,BufferLength): BufferLength = BufferLengthWas
If Trim(String.s) = "200"
HttpQueryInfo_(hHttpOpenRequest,22,@Buffer,@BufferLength,0)
String.s = PeekS(@Buffer,BufferLength): BufferLength = BufferLengthWas
Debug String.s
If FindString(String.s,"Content-Length:",1)
i = FindString(String.s,"Content-Length:",1) + Len("Content-Length:")
String.s = Mid(String.s,i,Len(String.s)-i)
FileSize = Val(Trim(String.s))
EndIf
EndIf
EndIf
EndIf
EndIf

InternetCloseHandle_(hURL)
InternetCloseHandle_(hInetCon)
InternetCloseHandle_(hInet)
ProcedureReturn FileSize
EndProcedure

MessageRequester("",Str(HttpFileSize("http://acervo.gratis.com.br/arquivos/di ... iSetup.exe")))
thanks for replying.
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: InetgetSize Autoit

Post by infratec »

Hi,

I don't know how to implement it in your windows API code.
But...

Here is my PB only version:

Code: Select all

Procedure.q HttpFileSize(Url.s)
  
  FileSize = -1
  
  pos = FindString(Url, "://", 1)
  If pos
    Url = Mid(Url, pos + 3)
  EndIf
  
  Auth$ = ""
  pos = FindString(Url, "@", 1)
  If pos
    Auth$ = Left(Url, pos - 1)
    Size = Len(Auth$) * 2
    enc$ = Space(Size)
    Base64Encoder(@Auth$, StringByteLength(Auth$), @enc$, Size)
    Auth$ = "Authorization: Basic " + enc$
    Url = Mid(Url, pos + 1)
  EndIf
  
  File$ = ""
  pos = FindString(Url, "/", 1)
  If pos
    Host$ = Left(Url, pos - 1)
    File$ = Mid(Url, pos)
  EndIf
    
  ConnectionID = OpenNetworkConnection(Host$, 80)
  
  If ConnectionID
    
    String$ = "HEAD " + File$ + " HTTP/1.1" + #CRLF$
    String$ + "Host: " + Host$ + #CRLF$
    String$ + "Content-Type: application/x-www-form-urlencoded" + #CRLF$
    String$ + "Connection: keep-alive" + #CRLF$
    
    If Auth$ <> ""
      String$ + Auth$ + #CRLF$
    EndIf
    
    ; header is finished
    
    String$ + #CRLF$
    
    SendNetworkString(ConnectionID, String$)
    TimeOutCounter = 100
    Repeat
      If NetworkClientEvent(ConnectionID) = #PB_NetworkEvent_Data
        Break
      EndIf
      Delay(10)
      TimeOutCounter -1
    Until TimeOutCounter = 0
    
    If TimeOutCounter <> 0
      #Size = 100000
      *Buffer = AllocateMemory(#Size)
      
      If *Buffer
        String$ = ""      
        Repeat
          Laenge = ReceiveNetworkData(ConnectionID, *Buffer, #Size)
          If Laenge > 0
            String$ + PeekS(*Buffer, Laenge)
          EndIf
        Until Laenge <> #Size
        
        FreeMemory(*Buffer)
        
        StartPos = FindString(String$, "Content-Length:", 1)
        If StartPos
          StartPos + 16
          EndPos = FindString(String$, #CRLF$, StartPos)
          If EndPos
            FileSize = Val(Mid(String$, StartPos, EndPos - StartPos))
          EndIf
        EndIf
        
      Else
        Debug "HttpFileSize(): Was Not able To allocate Buffer"
      EndIf
    Else
      Debug "HttpFileSize(): Timeout"
    EndIf
    
    CloseNetworkConnection(ConnectionID)
    
  Else
    Debug "HttpFileSize(): Was not able to establish connection to " + Host$
  EndIf
  
  ProcedureReturn FileSize
  
EndProcedure



InitNetwork()

MessageRequester("HttpFileSize", Str(HttpFileSize("user:password@www.test.com/download/img.jpg")))
But since I don't know a location with user:password, I can not really test it :cry:

It works without authentication. :)

So please test it.

Bernd
Peyman
Enthusiast
Enthusiast
Posts: 203
Joined: Mon Dec 24, 2007 4:15 pm
Location: Iran

Re: InetgetSize Autoit

Post by Peyman »

i chaned Droopy's code to work well with username and password and with other protocol (ftp, http, https, ...) :

Code: Select all

#INTERNET_OPEN_TYPE_DIRECT      = 1

#INTERNET_INVALID_PORT_NUMBER   = 0           ; use the protocol-specific Default

#INTERNET_DEFAULT_FTP_PORT      = 21          ; Default For FTP servers
#INTERNET_DEFAULT_GOPHER_PORT   = 70          ;    "     " gopher "
#INTERNET_DEFAULT_HTTP_PORT     = 80          ;    "     " HTTP   "
#INTERNET_DEFAULT_HTTPS_PORT    = 443         ;    "     " HTTPS  "
#INTERNET_DEFAULT_SOCKS_PORT    = 1080        ; Default For SOCKS firewall servers.

#INTERNET_SERVICE_FTP           = 1
#INTERNET_SERVICE_GOPHER        = 2
#INTERNET_SERVICE_HTTP          = 3

#HTTP_QUERY_STATUS_CODE         = 19
#HTTP_QUERY_RAW_HEADERS_CRLF    = 22


Procedure.q HttpFileSize(Url.s)
  Protected.i hInet, hURL, hInetCon, hHttpOpenRequest, iretval, BufferLength, Port
  Protected Header.s, *Buffer
  
  hInet = InternetOpen_("PureBasic", #INTERNET_OPEN_TYPE_DIRECT, #Null, #Null, #Null)
  hURL  = InternetOpenUrl_(hInet, Url, #Null, #Null, $80000000, #Null)
 
  BufferLength = 2048
  *Buffer = AllocateMemory(BufferLength)
 
  Select LCase(GetURLPart(Url, #PB_URL_Protocol))
    Case "ftp"
      Port = #INTERNET_DEFAULT_FTP_PORT
    Case "gopher"
      Port = #INTERNET_DEFAULT_GOPHER_PORT
    Case "http"
      Port = #INTERNET_DEFAULT_HTTP_PORT
    Case "https"
      Port = #INTERNET_DEFAULT_HTTPS_PORT
    Default
      Port = #INTERNET_INVALID_PORT_NUMBER
  EndSelect
  
  If GetURLPart(Url, #PB_URL_Port) And Port <> Val(GetURLPart(Url, #PB_URL_Port))
    Port = Val(GetURLPart(Url, #PB_URL_Port))
  EndIf
      
  hInetCon = InternetConnect_(hInet, GetURLPart(Url, #PB_URL_Site), Port, GetURLPart(Url, #PB_URL_User), GetURLPart(Url, #PB_URL_Password), #INTERNET_SERVICE_HTTP, #Null, #Null)
  If hInetCon
    hHttpOpenRequest = HttpOpenRequest_(hInetCon, "HEAD", GetURLPart(Url, #PB_URL_Path), #Null, #Null, #Null, $80000000, #Null)
    If hHttpOpenRequest
      iretval = HttpSendRequest_(hHttpOpenRequest, #Null, #Null, #Null, #Null)
      If iretval
        HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_STATUS_CODE , *Buffer, @BufferLength, #Null)
        Header = PeekS(*Buffer, BufferLength)
        BufferLength = 2048
        If Trim(Header) = "200"
          HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_RAW_HEADERS_CRLF, *Buffer, @BufferLength, #Null)
          Header = PeekS(*Buffer, BufferLength)
          If FindString(Header, "Content-Length:", 1)
            i = FindString(Header, "Content-Length:", 1) + Len("Content-Length:")
            Header = Mid(Header, i, Len(Header) - i)
            FileSize = Val(Trim(Header))
          EndIf
        EndIf
      EndIf
    EndIf
  EndIf
  
  FreeMemory(*Buffer)
  InternetCloseHandle_(hInetCon)
  InternetCloseHandle_(hURL)
  InternetCloseHandle_(hInet)
  ProcedureReturn FileSize
EndProcedure

MessageRequester("",Str(HttpFileSize("http://purebasic.com/download/PureBasic_Demo.exe"))) 
test it with user and password.
Sorry for my bad english.
new2pb
New User
New User
Posts: 9
Joined: Tue Aug 03, 2010 4:59 pm

Re: InetgetSize Autoit

Post by new2pb »

Peyman wrote:i chaned Droopy's code to work well with username and password and with other protocol (ftp, http, https, ...) :

Code: Select all

#INTERNET_OPEN_TYPE_DIRECT      = 1

#INTERNET_INVALID_PORT_NUMBER   = 0           ; use the protocol-specific Default

#INTERNET_DEFAULT_FTP_PORT      = 21          ; Default For FTP servers
#INTERNET_DEFAULT_GOPHER_PORT   = 70          ;    "     " gopher "
#INTERNET_DEFAULT_HTTP_PORT     = 80          ;    "     " HTTP   "
#INTERNET_DEFAULT_HTTPS_PORT    = 443         ;    "     " HTTPS  "
#INTERNET_DEFAULT_SOCKS_PORT    = 1080        ; Default For SOCKS firewall servers.

#INTERNET_SERVICE_FTP           = 1
#INTERNET_SERVICE_GOPHER        = 2
#INTERNET_SERVICE_HTTP          = 3

#HTTP_QUERY_STATUS_CODE         = 19
#HTTP_QUERY_RAW_HEADERS_CRLF    = 22


Procedure.q HttpFileSize(Url.s)
  Protected.i hInet, hURL, hInetCon, hHttpOpenRequest, iretval, BufferLength, Port
  Protected Header.s, *Buffer
  
  hInet = InternetOpen_("PureBasic", #INTERNET_OPEN_TYPE_DIRECT, #Null, #Null, #Null)
  hURL  = InternetOpenUrl_(hInet, Url, #Null, #Null, $80000000, #Null)
 
  BufferLength = 2048
  *Buffer = AllocateMemory(BufferLength)
 
  Select LCase(GetURLPart(Url, #PB_URL_Protocol))
    Case "ftp"
      Port = #INTERNET_DEFAULT_FTP_PORT
    Case "gopher"
      Port = #INTERNET_DEFAULT_GOPHER_PORT
    Case "http"
      Port = #INTERNET_DEFAULT_HTTP_PORT
    Case "https"
      Port = #INTERNET_DEFAULT_HTTPS_PORT
    Default
      Port = #INTERNET_INVALID_PORT_NUMBER
  EndSelect
  
  If GetURLPart(Url, #PB_URL_Port) And Port <> Val(GetURLPart(Url, #PB_URL_Port))
    Port = Val(GetURLPart(Url, #PB_URL_Port))
  EndIf
      
  hInetCon = InternetConnect_(hInet, GetURLPart(Url, #PB_URL_Site), Port, GetURLPart(Url, #PB_URL_User), GetURLPart(Url, #PB_URL_Password), #INTERNET_SERVICE_HTTP, #Null, #Null)
  If hInetCon
    hHttpOpenRequest = HttpOpenRequest_(hInetCon, "HEAD", GetURLPart(Url, #PB_URL_Path), #Null, #Null, #Null, $80000000, #Null)
    If hHttpOpenRequest
      iretval = HttpSendRequest_(hHttpOpenRequest, #Null, #Null, #Null, #Null)
      If iretval
        HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_STATUS_CODE , *Buffer, @BufferLength, #Null)
        Header = PeekS(*Buffer, BufferLength)
        BufferLength = 2048
        If Trim(Header) = "200"
          HttpQueryInfo_(hHttpOpenRequest, #HTTP_QUERY_RAW_HEADERS_CRLF, *Buffer, @BufferLength, #Null)
          Header = PeekS(*Buffer, BufferLength)
          If FindString(Header, "Content-Length:", 1)
            i = FindString(Header, "Content-Length:", 1) + Len("Content-Length:")
            Header = Mid(Header, i, Len(Header) - i)
            FileSize = Val(Trim(Header))
          EndIf
        EndIf
      EndIf
    EndIf
  EndIf
  
  FreeMemory(*Buffer)
  InternetCloseHandle_(hInetCon)
  InternetCloseHandle_(hURL)
  InternetCloseHandle_(hInet)
  ProcedureReturn FileSize
EndProcedure

MessageRequester("",Str(HttpFileSize("http://purebasic.com/download/PureBasic_Demo.exe"))) 
test it with user and password.
Thanks alot Peyman for showing me win api and how it can be implement this works well with user and password.
infratec wrote:Hi,

I don't know how to implement it in your windows API code.
But...

Here is my PB only version:

Code: Select all

Procedure.q HttpFileSize(Url.s)
  
  FileSize = -1
  
  pos = FindString(Url, "://", 1)
  If pos
    Url = Mid(Url, pos + 3)
  EndIf
  
  Auth$ = ""
  pos = FindString(Url, "@", 1)
  If pos
    Auth$ = Left(Url, pos - 1)
    Size = Len(Auth$) * 2
    enc$ = Space(Size)
    Base64Encoder(@Auth$, StringByteLength(Auth$), @enc$, Size)
    Auth$ = "Authorization: Basic " + enc$
    Url = Mid(Url, pos + 1)
  EndIf
  
  File$ = ""
  pos = FindString(Url, "/", 1)
  If pos
    Host$ = Left(Url, pos - 1)
    File$ = Mid(Url, pos)
  EndIf
    
  ConnectionID = OpenNetworkConnection(Host$, 80)
  
  If ConnectionID
    
    String$ = "HEAD " + File$ + " HTTP/1.1" + #CRLF$
    String$ + "Host: " + Host$ + #CRLF$
    String$ + "Content-Type: application/x-www-form-urlencoded" + #CRLF$
    String$ + "Connection: keep-alive" + #CRLF$
    
    If Auth$ <> ""
      String$ + Auth$ + #CRLF$
    EndIf
    
    ; header is finished
    
    String$ + #CRLF$
    
    SendNetworkString(ConnectionID, String$)
    TimeOutCounter = 100
    Repeat
      If NetworkClientEvent(ConnectionID) = #PB_NetworkEvent_Data
        Break
      EndIf
      Delay(10)
      TimeOutCounter -1
    Until TimeOutCounter = 0
    
    If TimeOutCounter <> 0
      #Size = 100000
      *Buffer = AllocateMemory(#Size)
      
      If *Buffer
        String$ = ""      
        Repeat
          Laenge = ReceiveNetworkData(ConnectionID, *Buffer, #Size)
          If Laenge > 0
            String$ + PeekS(*Buffer, Laenge)
          EndIf
        Until Laenge <> #Size
        
        FreeMemory(*Buffer)
        
        StartPos = FindString(String$, "Content-Length:", 1)
        If StartPos
          StartPos + 16
          EndPos = FindString(String$, #CRLF$, StartPos)
          If EndPos
            FileSize = Val(Mid(String$, StartPos, EndPos - StartPos))
          EndIf
        EndIf
        
      Else
        Debug "HttpFileSize(): Was Not able To allocate Buffer"
      EndIf
    Else
      Debug "HttpFileSize(): Timeout"
    EndIf
    
    CloseNetworkConnection(ConnectionID)
    
  Else
    Debug "HttpFileSize(): Was not able to establish connection to " + Host$
  EndIf
  
  ProcedureReturn FileSize
  
EndProcedure



InitNetwork()

MessageRequester("HttpFileSize", Str(HttpFileSize("user:password@www.test.com/download/img.jpg")))
But since I don't know a location with user:password, I can not really test it :cry:

It works without authentication. :)

So please test it.

Bernd
Hey Bernd thanks alot for getting back to reply and yes your example above works as well, you have shown me a different version of beside win api that pure basic can be scripted to work.
Post Reply