mIRC Homepage
Posted By: DeSync socket downloads header - 25/12/10 06:26 PM
Hi,

I've made script which downloads text file via socket. And works fine. But I noticed that when it downloads file, writes this header at top of file, and then comes my script.

Code:
Server: Apache mod_fcgid/2.3.5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
Last-Modified: Sat, 25 Dec 2010 18:19:04 GMT
ETag: "67c8249-fb1-4984023469600"
Accept-Ranges: bytes
Content-Length: 4017
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/plain




How can I avoid this?
Posted By: 5618 Re: socket downloads header - 25/12/10 06:57 PM
Start writing to your file after receiving "$crlf $crlf" (the empty line between header and body).
Posted By: DeSync Re: socket downloads header - 25/12/10 07:44 PM
This is part of script which writing file:

Code:
while ($sock($sockname).rq > 0) {
  sockread &download_file
  .bwrite $qt($mircdir $+ filename.mrc) -1 -1 &download_file
}


Will I check it setting var when receive $crlf $crlf or which is the best way?
Posted By: Wims Re: socket downloads header - 25/12/10 08:29 PM
Code:
on *:sockread:name:{
if ($sock($sockname).mark) {
while ($sock($sockname).rq > 0) {
  sockread &download_file
  .bwrite $qt(filename.mrc) -1 -1 &download_file
}
}
else {
 var %a
 sockread %a
 if (!%a) sockmark $sockname 1
 }
}
Try that
Posted By: DeSync Re: socket downloads header - 25/12/10 08:47 PM
I have that before. I don't think that there's problem.
Posted By: Tomao Re: socket downloads header - 26/12/10 12:46 AM
The way you post your question is as though you want people to take an educated guess of what the issue is and expect them to solve it. When asking a question, especially for socket related matters, it's best to be as detailed as feasible, not just to post your code in a piecemeal fashion. Unless, of course, the website your socket connects to is a private one that you wish not for it to be disclosed.

Anyway, at the start of the socketmark, you may need to
Code:
/sockmark $sockname 0
the apply wims's else bit with it. This will avoid the headers being written.
Posted By: DeSync Re: socket downloads header - 26/12/10 02:34 AM
Also one more question.

I have file on server something.txt, and there are few lines:

name1
name2
name3
#EOF

And if I use sockwrite -n $sockname $str($crlf, 2) it will return all lines without last one #EOF, but if I use
sockwrite -n $sockname $crlf $crlf it will result a bunch of HTML tags and data from file also.

Is there a good explanation of using $crlf?

Regards
Posted By: Tomao Re: socket downloads header - 26/12/10 05:08 AM
I never used two crlfs but one if used as:
sockwrite -n $sockname $crlf
The only time I use double crlfs is after the Host: $+($sock($sockname).addr,$str($crlf,2))
I have no explanation for the answer to your question, as I don't know how exactly your file is set up on your server side.
Posted By: DeSync Re: socket downloads header - 26/12/10 09:38 AM
I told you, txt file with few names in list

--- start file ---
name1
name2
name3
#EOF
--- end file ---

When use sockmark 0 script won't work in any way.
Posted By: DeSync Re: socket downloads header - 26/12/10 12:46 PM
This is my file receive script:

Code:
on *:SOCKREAD:download_*: {
  if $sockerr > 0 {
    echo -s error
  }
  else {
    var %read
    if !$sock($sockname).mark {
      sockread %read
      if %read != $null {
        sockmark $sockname 1
      }
    }
    else {
      sockread %read
      if download_* iswm $sockname {
        while ($sock($sockname).rq > 0) {
          sockread &file_d
          .bwrite $qt($mircdir $+ $remove($sockname, download_)) -1 -1 &file_d
        }
      }
    }
  }
}
Posted By: Wims Re: socket downloads header - 26/12/10 02:28 PM
Quote:
I have that before. I don't think that there's problem.
That's the problem.

Quote:
And if I use sockwrite -n $sockname $str($crlf, 2) it will return all lines without last one #EOF, but if I use
sockwrite -n $sockname $crlf $crlf it will result a bunch of HTML tags and data from file also.
if you are missing a line, it mean you're not reading correctly, and indeed, both your code and mine are missing the -f switch on /sockread.

Quote:
Is there a good explanation of using $crlf?

The use of $crlf is completely a specification of the HTTP protocol, read the protocol...

Code:
on *:sockread:download_*:{
if ($sock($sockname).mark) {
while ($sock($sockname).rq > 0) {
  sockread -f &download_file
  .bwrite $qt(filename.mrc) -1 -1 &download_file
}
}
else {
 var %a
 sockread %a
 if (%a == $null) sockmark $sockname 1
 }
}


@Tomao: the "/sockmark $sockname 0" isn't needed
Posted By: DeSync Re: socket downloads header - 26/12/10 04:32 PM
Nothing new, just same with -f
Posted By: Wims Re: socket downloads header - 26/12/10 04:38 PM
Please post the whole code
Posted By: DeSync Re: socket downloads header - 26/12/10 06:14 PM
Originally Posted By: DeSync
This is my file receive script:

Code:
on *:SOCKREAD:download_*: {
  if $sockerr > 0 {
    echo -s error
  }
  else {
    var %read
    if !$sock($sockname).mark {
      sockread %read
      if %read != $null {
        sockmark $sockname 1
      }
    }
    else {
      sockread %read
      if download_* iswm $sockname {
        while ($sock($sockname).rq > 0) {
          sockread &file_d
          .bwrite $qt($mircdir $+ $remove($sockname, download_)) -1 -1 &file_d
        }
      }
    }
  }
}


Now just with
Code:
sockread -f &file_d


This is additional:

Code:
alias download_file {
  if $sock($1) {
    sockclose $1
  }
  sockopen $+(download_, $1) HOST-HERE 80
}
on *:SOCKOPEN:download_*: {
  if $sockerr > 0 {
    echo -s Error.
  }
  else {
    sockwrite -n $sockname GET $+(/files/, $remove($sockname, download_)) HTTP/1.0
    sockwrite -n $sockname Connection: Keep-Alive
    sockwrite -n $sockname Host: HOST
    sockwrite -n $sockname $crlf $crlf
  }
}

Posted By: Wims Re: socket downloads header - 26/12/10 06:57 PM
I though you were using my code, now try with :
Code:
on *:SOCKREAD:download_*: {
  if ($sockerr > 0) echo -s error
  elseif (!$sock($sockname).mark) {
    var %read
    sockread %read
    if (%read == $null) sockmark $sockname 1
  }
  else {
    while ($sock($sockname).rq > 0) {
      sockread &file_d
      .bwrite $qt($remove($sockname, download_)) -1 -1 &file_d
    }
  }
}

alias download_file {
  sockclose $1
  sockopen $+(download_, $1) HOST-HERE 80
}

on *:SOCKOPEN:download_*: {
  if ($sockerr > 0) echo -s Error.
  else {
    sockwrite -n $sockname GET $+(/files/, $remove($sockname, download_)) HTTP/1.0
    sockwrite -n $sockname Connection: Keep-Alive
    sockwrite -n $sockname Host: HOST
    sockwrite -n $sockname
  }
}

Posted By: DeSync Re: socket downloads header - 26/12/10 07:30 PM
Sorry, but I don't have difference. Code is different, but doing same thing, just on other way smirk

Bot codes, opens socket, send header, check if socket is marked, if not, marking it, and storing file on same way.

Can you explain what's point? Or I can't understand it.
Posted By: Wims Re: socket downloads header - 26/12/10 08:04 PM
When headers are sent, you will get a $null value, you need to sockmark at that time, you're sockmark'ing when you get the first header, since you're using "if (%read != $null)"

Does the code I gave work ?
If not, what is not working ?
Posted By: DeSync Re: socket downloads header - 26/12/10 09:10 PM
Well, thanks. When I change to if %read == $null won't download header, but anyway adds this at bottom of file:

Code:
<head>
<style type="text/css">
* { margin: 0; padding: 0; }

html { height:100%; }

body {
  text-align: left;
  width: 100%;
  height: 100%;
  font-size: 62.5%;
  font-family: Helvetica, arial, sans-serif;
  color: #000;
  background: #fff;
  margin: 0;
  border: 0;
padding: 0; }
</style>
</head>
<body>
<iframe src="http://searchportal.information.com/?a_id=47368&domainname=" width="100%" height="100%" frameborder="0"></iframe>
</body>
Posted By: mazdaa Re: socket downloads header - 11/01/11 02:02 PM
Thanks for the info..........
© mIRC Discussion Forums