mIRC Homepage
Posted By: Knappen More socket help... - 17/04/03 10:41 AM
I need the socket to return the filesize of the file its going to download... How do i do that?

Thanks for any help!
Posted By: Nimue Re: More socket help... - 17/04/03 12:28 PM
Check the sockread for "Content-Length: NNN" NNN is the filesize.
Posted By: Knappen Re: More socket help... - 17/04/03 12:29 PM
Hu?
Posted By: Nimue Re: More socket help... - 17/04/03 12:50 PM
Code:
on *:SOCKREAD:sockname:{
  if $sockerr { return }
  ; Determine if you should be reading to &bvar (the file itself) or %var (the header)
  if whatever {
    var %read
    sockread %read
    if [color:green]Content-Length: [color:blue]*[/color] iswm %read[/color] { do stuff with the [color:blue]*[/color] }
    ..
  }
  else {
    sockread &var
    ..
  }
}
Posted By: Knappen Re: More socket help... - 17/04/03 01:05 PM
I dont understand it.... So.. Anyone else that can explain it for me? Thanks for your help anyway!
Posted By: Nimue Re: More socket help... - 17/04/03 04:04 PM
OK: When you request a file, the server will send some info to you, and then the file (if it exists).

Some thing like this, if the file you requested wasn't found.
HTTP/1.0 404 Not Found
Date: Thu, 17 Apr 2003 16:06:22 GMT
Server: Apache/2.0.40 (Red Hat Linux)
...
...


or this..

HTTP/1.0 200 OK
Date: Thu, 17 Apr 2003 15:57:00 GMT
Server: Apache/2.0.40 (Red Hat Linux)
Last-Modified: Fri, 16 Aug 2002 20:21:15 GMT
ETag: "23c04b-16806-f6b8e4c0"
Accept-Ranges: bytes
[color:blue]Content-Length: 92166

Content-Type: application/zip
<blank line>
..binary file data
..binary file data
...
..
etc.[/color]

This, "Content-Length: 92166", is the info you want. In this example the file was 92166 bytes.

So to get it you need to "/sockread %var" and parse %var to find the info.
e.g. if Content-Length: * iswm %var { set %file.size $gettok(%var,2,32) }
When "%var == $null" is when the actual file will begin to download, at that time you switch to "/sockread &bvar".
Posted By: Knappen Re: More socket help... - 17/04/03 04:21 PM
I cant get it to work..
I have this now:
Code:
 
on *:sockread:download:{
  sockread %size
  if (Content-Length: * iswm %size) { set %file.size $gettok(%size,2,32) }
  did -a update.now 2 %file.size
  :loop
  if ($sockerr &gt; 0) return
  sockread &amp;temp
  if (!$sockbr) return
  bwrite %version $+ .exe -1 &amp;temp
  did -ra update.now 4 $bytes($sock(download).rcvd,m3).suf
  did -ra update.now 6 $dur($sock(download).to)
  goto loop
}
 


What is wrong here? Thanks!
Posted By: Nimue Re: More socket help... - 17/04/03 04:51 PM
You have to loop for the header lines, and again for the file.
Code:
on *:sockread:download:{
  if $sockerr { echo -eat * Socket error: $sock(download).wserr $sock(download).wsmsg }
  while !$sock($sockname).mark {
    sockread %size
    if (Content-Length: * iswm %size) {
      set %file.size $gettok(%size,2,32)
      did -a update.now 2 %file.size
    }
    if !%size { sockmark $sockname $ticks | echo -sg $sock($sockname).mark }
  }
  while $sock($sockname) {
    sockread &amp;temp
    if (!$sockbr) return
    bwrite %version $+ .exe -1 &amp;temp
    did -ra update.now 4 $bytes($sock(download).rcvd,m3).suf
    did -ra update.now 6 $dur($sock(download).to)
    if $file(%version $+ .exe).size == %file.size {
      echo -eat * %version $+ .exe downloaded in $calc(($ticks - $sock($sockname).mark)*.001) secs
    }
  }
}
Posted By: Knappen Re: More socket help... - 17/04/03 05:07 PM
No! It still doesn`t work!

It keeps echoing out the
"if (!%size) { sockmark $sockname $ticks | echo -ag $sock($sockname).mark }".

Exept from that it`s still the same... And it downloads the file as well...
Posted By: Nimue Re: More socket help... - 17/04/03 05:14 PM
OOps well that line should have been
if !%size { sockmark $sockname $ticks }
the echo was for myself when I tested it, and it did work for me smile
Posted By: Knappen Re: More socket help... - 17/04/03 05:44 PM
Nothing changed... didn`t echo though... it downloads but it doesn`t put the file size in the dialog (did -a update.now 2 %file.size)
Posted By: Nimue Re: More socket help... - 17/04/03 06:06 PM
Check your dialog name & id numbers. the "/did " lines work here.
Posted By: Knappen Re: More socket help... - 17/04/03 06:12 PM
It`s correct.

dialog update.now {
text "File size:", 1, 40 20 60 30
text "", 2, 72 20 50 30
}
this is just the relevant part of the dialog.
Code:
  on *:sockread:download:{  
  if ($sockerr) { 
  echo -eat * Socket error: $sock(download).wserr $sock(download).wsmsg } 
  while (!$sock($sockname).mark) {   
    var %size
    sockread %size    
    if (Content-Length: * iswm %size) {   
      set %file.size $gettok(%size,2,32)   
      did -a update.now 2 %file.size 
    }  
 

This is just the relevant part of the algorithm...
As you see there is n`t anything wrong... Al the other functions in the dialog work properly, just not the filesize returner...

This is the sockopen command:
on *:sockopen:download:{
sockwrite -n $sockname GET /knappen/ $+ %version $+ .exe
}
Does this have anything to do with it?
Posted By: Nimue Re: More socket help... - 17/04/03 06:43 PM
Quote:
text "File size:", 1, 40 20 60 30
text "", 2, 72 20 50 30

Those overlap, perhaps you're hiding it.
Code:
ID1 (x40)|      |
ID2    (x72)|     |
Posted By: Knappen Re: More socket help... - 17/04/03 06:48 PM
No, that`s not the problem... It just wont show... I tried to echo it but it doesn`t do that either... I dont think it sets the var..
© mIRC Discussion Forums