1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implement protocol base class
4 // Author: Guilhem Lavaux
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/protocol/protocol.h"
23 #include "wx/module.h"
27 /////////////////////////////////////////////////////////////////
29 /////////////////////////////////////////////////////////////////
32 * --------------------------------------------------------------
33 * --------- wxProtoInfo CONSTRUCTOR ----------------------------
34 * --------------------------------------------------------------
37 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
38 const bool need_host1, wxClassInfo *info)
43 m_needhost = need_host1;
45 next = wxURL::ms_protocols;
46 wxURL::ms_protocols = this;
52 /////////////////////////////////////////////////////////////////
53 // wxProtocol ///////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////
57 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
59 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
62 wxProtocol::wxProtocol()
70 bool wxProtocol::Reconnect()
89 // ----------------------------------------------------------------------------
90 // Read a line from socket
91 // ----------------------------------------------------------------------------
94 wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result)
96 static const int LINE_BUF = 4095;
100 wxCharBuffer buf(LINE_BUF);
101 char *pBuf = buf.data();
102 while ( sock->WaitForRead() )
104 // peek at the socket to see if there is a CRLF
105 sock->Peek(pBuf, LINE_BUF);
107 size_t nRead = sock->LastCount();
108 if ( !nRead && sock->Error() )
109 return wxPROTO_NETERR;
111 // look for "\r\n" paying attention to a special case: "\r\n" could
112 // have been split by buffer boundary, so check also for \r at the end
113 // of the last chunk and \n at the beginning of this one
115 const char *eol = strchr(pBuf, '\n');
117 // if we found '\n', is there a '\r' as well?
122 // check for case of "\r\n" being split
123 if ( result.empty() || result.Last() != _T('\r') )
125 // ignore the stray '\n'
128 //else: ok, got real EOL
130 // read just this '\n' and restart
133 else // '\n' in the middle of the buffer
135 // in any case, read everything up to and including '\n'
136 nRead = eol - pBuf + 1;
138 if ( eol[-1] != '\r' )
140 // as above, simply ignore stray '\n'
146 sock->Read(pBuf, nRead);
147 if ( sock->LastCount() != nRead )
148 return wxPROTO_NETERR;
151 result += wxString::FromAscii(pBuf);
155 // remove trailing "\r\n"
156 result.RemoveLast(2);
158 return wxPROTO_NOERR;
162 return wxPROTO_NETERR;
165 wxProtocolError wxProtocol::ReadLine(wxString& result)
167 return ReadLine(this, result);
170 // old function which only chops '\n' and not '\r\n'
171 wxProtocolError GetLine(wxSocketBase *sock, wxString& result)
173 #define PROTO_BSIZE 2048
175 char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
179 avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
180 if (sock->Error() || avail == 0)
181 return wxPROTO_NETERR;
183 memcpy(tmp_str, tmp_buf, avail);
185 // Not implemented on all systems
186 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
188 for (ret=tmp_str;ret < (tmp_str+avail); ret++)
196 return wxPROTO_PROTERR;
200 result = wxString::FromAscii( tmp_str );
201 result = result.Left(result.Length()-1);
203 size = ret-tmp_str+1;
204 sock->Unread(&tmp_buf[size], avail-size);
206 return wxPROTO_NOERR;
209 #endif // wxUSE_SOCKETS
211 #endif // wxUSE_PROTOCOL