1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/protocol.cpp
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"
22 #include "wx/protocol/log.h"
25 #include "wx/module.h"
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 IMPLEMENT_CLASS(wxProtoInfo, wxObject)
39 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
40 const bool need_host1, wxClassInfo *info)
45 m_needhost = need_host1;
47 next = wxURL::ms_protocols;
48 wxURL::ms_protocols = this;
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
60 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
62 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
65 wxProtocol::wxProtocol()
70 m_lastError = wxPROTO_NOERR;
72 SetDefaultTimeout(60); // default timeout is 60 seconds
76 bool wxProtocol::Reconnect()
95 void wxProtocol::SetDefaultTimeout(wxUint32 Value)
97 m_uiDefaultTimeout = Value;
99 wxSocketBase::SetTimeout(Value); // sets it for this socket
103 wxProtocol::~wxProtocol()
108 // ----------------------------------------------------------------------------
109 // Read a line from socket
110 // ----------------------------------------------------------------------------
113 wxProtocolError wxProtocol::ReadLine(wxSocketBase *sock, wxString& result)
115 static const int LINE_BUF = 4095;
119 wxCharBuffer buf(LINE_BUF);
120 char *pBuf = buf.data();
121 while ( sock->WaitForRead() )
123 // peek at the socket to see if there is a CRLF
124 sock->Peek(pBuf, LINE_BUF);
126 size_t nRead = sock->LastCount();
127 if ( !nRead && sock->Error() )
128 return wxPROTO_NETERR;
130 // look for "\r\n" paying attention to a special case: "\r\n" could
131 // have been split by buffer boundary, so check also for \r at the end
132 // of the last chunk and \n at the beginning of this one
134 const char *eol = strchr(pBuf, '\n');
136 // if we found '\n', is there a '\r' as well?
141 // check for case of "\r\n" being split
142 if ( result.empty() || result.Last() != wxT('\r') )
144 // ignore the stray '\n'
147 //else: ok, got real EOL
149 // read just this '\n' and restart
152 else // '\n' in the middle of the buffer
154 // in any case, read everything up to and including '\n'
155 nRead = eol - pBuf + 1;
157 if ( eol[-1] != '\r' )
159 // as above, simply ignore stray '\n'
165 sock->Read(pBuf, nRead);
166 if ( sock->LastCount() != nRead )
167 return wxPROTO_NETERR;
170 result += wxString::FromAscii(pBuf);
174 // remove trailing "\r\n"
175 result.RemoveLast(2);
177 return wxPROTO_NOERR;
181 return wxPROTO_NETERR;
184 wxProtocolError wxProtocol::ReadLine(wxString& result)
186 return ReadLine(this, result);
189 #endif // wxUSE_SOCKETS
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
195 void wxProtocol::SetLog(wxProtocolLog *log)
201 void wxProtocol::LogRequest(const wxString& str)
204 m_log->LogRequest(str);
207 void wxProtocol::LogResponse(const wxString& str)
210 m_log->LogResponse(str);
213 void wxProtocolLog::DoLogString(const wxString& str)
215 wxUnusedVar(str); // unused if wxLogTrace() is disabled
216 wxLogTrace(m_traceMask, "%s", str);
219 #endif // wxUSE_PROTOCOL