mIRC Home    About    Download    Register    News    Help

Print Thread
#251168 11/02/15 12:21 PM
Joined: Mar 2014
Posts: 42
S
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Mar 2014
Posts: 42
Hi, i'm trying to split my sockread into seperate files for header and body. Loki had something that worked but for my application it seemed kind of overly complicated so i came up with this
Code:
on *:sockread:checkName:{
  var %headerfile = $sockname $+ .header.txt
  var %datafile = $sockname $+ . $+ %id $+ .txt
  var %readname
  sockread -f %readname
  var %posname = $pos(%readname, $crlf $crlf)
  write %headerfile $mid(%readname, 1, %posname)
  write %datafile $mid(%readname, %posname)
  write test.txt %readname
}


As you see, my intentions are to find the position of the empty line and write everything before it to the header file, and everything after to the data file.

The problem is, this doesnt work. Im 100% sure i have used this before so i know it CAN work though. Ive been monitoring %posname and it never gets a value so i assume its something i did wrong with $pos but i cant figure out what. (for example using another substring that i can actually SEE is there in the server response doesnt work either)

Also i know that i AM getting a server response because test.txt gets populated just fine smirk

help would be appreciated, thanks in advance ^_^

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
There is good reason for everything in my sockread.

1. When you /sockread to a %var the $crlf is stripped out.
2. You need a persistent flag to tell you if you've completed the header.
3. If you're going to use HTTP/1.1 you need to support chunked transfer-encoding.

My script is more complicated in that it uses hash tables for easier passing of information between socket events and while loops to stay in the sockread as long as possible rather than letting it retrigger (this is more efficient), I also read to a &binvar to retrieve a bit-perfect copy of the content from the server - something reading to a %var will not do.

Joined: Jul 2006
Posts: 4,145
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,145
Read this http://en.wikichip.org/wiki/mirc/commands/sockread
Using sockread -f is wrong if you want to read line by line.
%id is not set in your code and just don't use $pos.

Set a bool value to 0, indicating you didn't read the header and set it to 1 when you read the header, you're done reading the header when you are reading line by line and the bool value is 0 and the variable's content is $null.
It's a good idea to use the socket mark as a bool value but here I'm going to use a global variable:

Code:
on *:sockopen:checkName:{
if ($sockerr) { echo -a socket error | return }
sockwrite -n checkName GET ...
sockwrite -n checName Host: ...
set %bool 0
}
on *:sockread:checkName:{
  var %headerfile = $sockname $+ .header.txt,%datafile = $sockname $+ .data.txt
  
  if (!%bool) {
    var %readname  
    sockread %readname
    if (!$sockbr) return
    if (%readname == $null) set %bool 1
    else write %headerfile %readname
  }
  else {
   ;I seperate the read of the header and the rest because you could be willing to read line by line after the header, but into a binvar, which I demonstrate here
   sockread -n &a
   if (!$sockbr) return
   bwrite %datafile -1 -1 &a
  }
}
and now that's a proper event.


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Mar 2014
Posts: 42
S
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Mar 2014
Posts: 42
Hmm welp i'll be using yours again then :P thanks again

Joined: Mar 2014
Posts: 42
S
Ameglian cow
OP Offline
Ameglian cow
S
Joined: Mar 2014
Posts: 42
Ooooh now that looks interesting as well, thanks for your time guys im sure i can fix the issue now!


Link Copied to Clipboard