mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Dec 2004
Posts: 81
N
Babel fish
OP Offline
Babel fish
N
Joined: Dec 2004
Posts: 81
I'm having problems stripping the header from the actual file. The sockread event writes the HTTP header information to the file aswell. Any ideas?

Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Why dont u just NOT write it to the file?

Or am i missing something in what you mean?

Joined: Dec 2004
Posts: 81
N
Babel fish
OP Offline
Babel fish
N
Joined: Dec 2004
Posts: 81
Well when I request the file from the webserver it automatically writes the header information.

Code:
on *:sockopen:blah:{
  if ($sockerr) return
  sockwrite -n $sockname GET /download.asp?id=9855 HTTP/1.1
  sockwrite -n $sockname Accept: */*
  sockwrite -n $sockname Accept-Language: en-us
  sockwrite -n $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
  sockwrite -n $sockname Host: $sock($sockname).ip
  sockwrite -n $sockname Connection: Close
  sockwrite -n $sockname $crlf
}
on *:sockread:blah:{
  if ($sockerr) return
  :read
  sockread %tmp
  ;  sockread &bvar
  if (!$sockbr) return
  if (!%tmp) %tmp = -
  ;  if ($bfind(&bvar,1,Content-Type:).text) { %chk = 1 }
  ; if (%chk) { bwrite mirc616.exe -1 &bvar }
  ;  write temp.txt %tmp
  echo -s %tmp
  goto read
}
on *:sockclose:blah:echo -s * closed socket

Joined: Dec 2002
Posts: 15
I
idk Offline
Pikka bird
Offline
Pikka bird
I
Joined: Dec 2002
Posts: 15
If you dont want it to write to a text file, just remove the write temp.txt %tmp line, im not exactly sure what you are wanting.

Joined: Dec 2002
Posts: 21
T
Ameglian cow
Offline
Ameglian cow
T
Joined: Dec 2002
Posts: 21
I believe he wants to write and save the file to disk, but not the headers returned from the server as well. If this is the case, look for a single $crlf on a line all by itself. Or, probably better yet, look for a two consecitive $crlf togethor. The header is seperated from the body by a blank line. Once you find that first blank line, set a variable like %haveHeader = $true, so you know to write any other blank lines.

Joined: Dec 2004
Posts: 81
N
Babel fish
OP Offline
Babel fish
N
Joined: Dec 2004
Posts: 81
I'm still having problems with this. I've tried several different methods and the file still isnt being written correctly. Any examples?

Joined: Dec 2004
Posts: 81
N
Babel fish
OP Offline
Babel fish
N
Joined: Dec 2004
Posts: 81
This is what I have so far:

Code:
on *:sockread:blah:{
  if ($sockerr) return
  :read
  sockread -fn &a
  if (!$sockbr) return
  if (!$bvar(&a,1,2).text) {
    echo -s YES
    if (!%headerend) { set %headerend $true }
  }
  if (%headerend) { fwrite -b a &a }
  goto read
}


It skips a couple of the first lines needed, any suggestions?

Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Code:
on *:sockread:blah:{
  if ($sockerr) return
  while (!%headerend) {
    sockread %header.data
    ECHO -st -- %header.data -- %lookingfor.second.crlf -- 
    if (!$sockbr) return
    if (!%header.data) {
      if (!%lookingfor.second.crlf) { set -s %lookingfor.second.crlf $true }
      else                          { set -s %headerend $true | unset -s %lookingfor.second.crlf }
    }
    else { unset -s %lookingfor.second.crlf }
    ;^ [edit] added that line just now [edit]
  }
  :read
  sockread -fn &a
  if (!$sockbr) return
  fwrite -b a &a
  goto read
}


OK since I dont actually know what data comes before the start of the file, it makes it a bit hard, but someone said there were TWO consecutive $CRLF then the file data, so Im using that, if that isnt true then, well the above wont work.
Now what did i do?
It comes in and if %headerend isnt $true, then were in the header still, so just do a text read into variable, at this point i echo it to the status window for debugging sakes, since if it dont wokr u might like to show us what the header consists off. OK next exit if we didnt have nothing to read (i havent dont stuff all with sockets so im going to assume this value includes the $crlf 2 bytes if a blank line is read., next if the dataread turns out to be $null just the $crlf, then we go into the IF , if were not yet looking for the second crlf then set it so we are now, and if were already looking for the second $crlf then we just found it, so set that the header just ended, cleanup the second crlf flag, and the code exits the while loop into the binvar retrivial of the file.

[edit]
I just added a line to it that if the header data wasnt a $crlf then it resets the %lookingfor.second.crlf flag, as were not looking for JUST the second crlf, but the second consecutive crlf
[edit]

PS: remeber to reset the %headerend flag when initiating the send I would suggest also reseting the %lookingfor.second.crlf flag also, just to be sure.
(also take the -s out of the sets and the ECHO -st once its all a go go)

Hope i was able to help.

Last edited by DaveC; 25/01/05 09:48 AM.
Joined: Aug 2003
Posts: 1,831
I
Hoopy frood
Offline
Hoopy frood
I
Joined: Aug 2003
Posts: 1,831
Here's an example that downloads the latest servers.ini to a file called "atest'ini" in your mirc dir.
Code:
alias getfiletest sockopen atest www.mirc.co.uk 80
on *:sockopen:atest:{
  write -c atest.ini
  sockwrite -n atest GET /servers.ini HTTP/1.0
  sockwrite -n atest Host: www.mirc.co.uk
  sockwrite -n atest Connection: close
  sockwrite atest $str($crlf,2)
}
on *:sockread:atest:{
  if !$sock(atest).mark {
    var %a
    sockread %a
    ; Just dropping the file headers - this info contains filesize, filetype, modified date, etc
    ; To show progress or such you'll need to parse this info to compare against later 

    ; when headers are finished, mark the socket.
    if !%a { sockmark atest 1 }
  }
  else {
    ; pretty obvious, just read/write data
    sockread -f &a
    bwrite atest.ini -1 -1 &a
  }
}
on *:sockclose:atest:run atest.ini

Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Using /sockmark <name> [text] was a nice touch, I never incountered it before, I like how you can wildcard to set several of them , good for multi socket scripts I guess.

Joined: Dec 2004
Posts: 81
N
Babel fish
OP Offline
Babel fish
N
Joined: Dec 2004
Posts: 81
Thank you everyone who has helped, greatly appreciated.

Joined: Dec 2002
Posts: 21
T
Ameglian cow
Offline
Ameglian cow
T
Joined: Dec 2002
Posts: 21
Very nice code indeed. Never thought to use the sockmark in that fashion. I usually use for when reading huge files for some reason or another, I don't recall right off hand.

Couple of quick notes:
Code:
sockwrite -n atest Host: www.mirc.co.uk


Host is not a valid request header in HTTP 1.0. It will most likely just be ignored and as such is not needed in this request. It was added in HTTP 1.1 for name based virtual hosting.

Code:
sockwrite atest $str($crlf,2)


Really unneeded. You already have a newline added after the previous line (the -n switch). Easier just to add a blank sockwrite line:

Code:
sockwrite -n atest


Link Copied to Clipboard