* simpsock.h (SimpleSocket::invalidate): Declare new method.
* simpsock.cc (SimpleSocket::invalidate): Implement new method.
(SimpleSocket::SimpleSocket): Initialize buf to zero. Do not allocate
memory for buf in the constructor.
(SimpleSocket::~SimpleSocket): Use SimpleSocket::invalidate().
(SimpleSocket::printf): Use SimpleSocket::write() instead of send().
(SimpleSocket::write): Check object consistency - return -1 on error.
invalidate() the object on socket write error.
(SimpleSocket::fill): Check object consistency - return -1 on error.
invalidate() the object if socket read error is encountered and there
is no more data available in the internal read buffer.
Allocate memory for the internal read buffer.
(SimpleSocket::gets): Return zero (NULL pointer) if error is encountered
during fill() and no more data is available in the internal read buffer.
(SimpleSocket::read): Check object consistency - return -1 on error.
invalidate() the object if socket read error is encountered.
* nio-ftp.cc (NetIO_FTP:NetIO_FTP): Allow 125 as valid response code to
the RETR command (fix for MS IIS ftp server 5 - possibly others too).
(NetIO_FTP::ok): Check if the SimpleSocket object is ok().
(NetIO_FTP::read): Use NetIO_FTP::ok().
* nio-http.cc: Check for valid return value of SimpleSocket::gets().
(NetIO_HTTP::ok): Check if the SimpleSocket object is ok().
+2001-11-08 Pavel Tsekov <ptsekov@syntrex.com>
+
+ * simpsock.h (SimpleSocket::invalidate): Declare new method.
+ * simpsock.cc (SimpleSocket::invalidate): Implement new method.
+ (SimpleSocket::SimpleSocket): Initialize buf to zero. Do not allocate
+ memory for buf in the constructor.
+ (SimpleSocket::~SimpleSocket): Use SimpleSocket::invalidate().
+ (SimpleSocket::printf): Use SimpleSocket::write() instead of send().
+ (SimpleSocket::write): Check object consistency - return -1 on error.
+ invalidate() the object on socket write error.
+ (SimpleSocket::fill): Check object consistency - return -1 on error.
+ invalidate() the object if socket read error is encountered and there
+ is no more data available in the internal read buffer.
+ Allocate memory for the internal read buffer.
+ (SimpleSocket::gets): Return zero (NULL pointer) if error is encountered
+ during fill() and no more data is available in the internal read buffer.
+ (SimpleSocket::read): Check object consistency - return -1 on error.
+ invalidate() the object if socket read error is encountered.
+ * nio-ftp.cc (NetIO_FTP:NetIO_FTP): Allow 125 as valid response code to
+ the RETR command (fix for MS IIS ftp server 5 - possibly others too).
+ (NetIO_FTP::ok): Check if the SimpleSocket object is ok().
+ (NetIO_FTP::read): Use NetIO_FTP::ok().
+ * nio-http.cc: Check for valid return value of SimpleSocket::gets().
+ (NetIO_HTTP::ok): Check if the SimpleSocket object is ok().
+
2001-11-09 Robert Collins <rbtcollins@hotmail.com>
* simpsock.cc (cvsid): Avoid compiler warnings (not used, deprecated conversion).
{
do {
last_line = s->gets ();
- log (LOG_BABBLE, "ftp > %s", last_line);
+ log (LOG_BABBLE, "ftp > %s", last_line ? last_line : "error");
} while (last_line && (!isdigit (last_line[0]) || last_line[3] != ' '));
return atoi (last_line ?: "0");
}
cmd->printf ("RETR %s\r\n", path);
code = ftp_line (cmd);
- if (code != 150)
+ if (code != 150 && code != 125)
{
delete s;
s = 0;
int
NetIO_FTP::ok ()
{
- if (s)
+ if (s && s->ok ())
return 1;
return 0;
}
int
NetIO_FTP::read (char *buf, int nbytes)
{
- if (!s)
+ if (!ok ())
return 0;
return s->read (buf, nbytes);
}
char *l = s->gets ();
int code;
+ if (!l)
+ return;
sscanf (l, "%*s %d", &code);
if (code >= 300 && code < 400)
{
- do {
- l = s->gets ();
+ while ((l = s->gets ()) != 0) {
if (_strnicmp (l, "Location:", 9) == 0)
{
char *u = l + 9;
delete s;
goto retry_get;
}
- } while (*l);
+ }
}
if (code == 401) /* authorization required */
{
s = 0;
return;
}
- do {
- l = s->gets ();
+ while ((l = s->gets ()) != 0) {
if (_strnicmp (l, "Content-Length:", 15) == 0)
sscanf (l, "%*s %d", &file_size);
- } while (*l);
+ }
}
NetIO_HTTP::~NetIO_HTTP ()
int
NetIO_HTTP::ok ()
{
- if (s)
+ if (s && s->ok ())
return 1;
return 0;
}
}
s = INVALID_SOCKET;
- buf = (char *) malloc (SSBUFSZ + 3);
+ buf = 0;
putp = getp = 0;
int i1, i2, i3, i4;
SimpleSocket::~SimpleSocket ()
{
- if (s != INVALID_SOCKET)
- closesocket (s);
- s = INVALID_SOCKET;
- if (buf)
- free (buf);
- buf = 0;
+ invalidate ();
}
int
va_list args;
va_start (args, fmt);
vsprintf (buf, fmt, args);
- return send (s, buf, strlen (buf), 0);
+ return write (buf, strlen (buf));
}
int
SimpleSocket::write (const char *buf, int len)
{
- return send (s, buf, len, 0);
+ int rv;
+ if (!ok ())
+ return -1;
+ if ((rv = send (s, buf, len, 0)) == -1)
+ invalidate ();
+ return rv;
}
int
SimpleSocket::fill ()
{
+ if (!ok ())
+ return -1;
+
+ if (buf == 0)
+ buf = (char *) malloc (SSBUFSZ + 3);
if (putp == getp)
putp = getp = 0;
if (r > 0)
{
putp += r;
- return r;
}
- return 0;
+ else if (r < 0 && putp == getp)
+ {
+ invalidate();
+ }
+ return r;
}
char *
getp = 0;
}
if (putp == getp)
- fill();
+ if (fill () <= 0)
+ return 0;
// getp is zero, always, here, and putp is the count
char *nl;
while ((*nl == '\n' || *nl == '\r') && nl >= buf)
*nl-- = 0;
}
- else
+ else if (putp > getp)
{
getp = putp;
nl = buf + putp;
nl[1] = 0;
}
+ else
+ return 0;
return buf;
}
int
SimpleSocket::read (char *ubuf, int ulen)
{
+ if (!ok ())
+ return -1;
+
int n, rv=0;
if (putp > getp)
{
while (ulen > 0)
{
n = recv (s, ubuf, ulen, 0);
+ if (n < 0)
+ invalidate();
if (n <= 0)
- return rv;
+ return rv > 0 ? rv : n;
ubuf += n;
ulen -= n;
rv += n;
}
return rv;
}
+
+void
+SimpleSocket::invalidate (void)
+{
+ if (s != INVALID_SOCKET)
+ closesocket (s);
+ s = INVALID_SOCKET;
+ if (buf)
+ free (buf);
+ buf = 0;
+ getp = putp = 0;
+}
char *buf;
int putp, getp;
int fill ();
+ void invalidate (void);
public:
SimpleSocket (const char *hostname, int port);