Stupid bug: using LastError() instead of Error()
[wxWidgets.git] / src / common / protocol.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: protocol.cpp
3 // Purpose: Implement protocol base class
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 07/07/1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "protocol.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/protocol/protocol.h"
24 #include "wx/url.h"
25 #include "wx/module.h"
26
27 #include <stdlib.h>
28
29 /////////////////////////////////////////////////////////////////
30 // wxProtoInfo
31 /////////////////////////////////////////////////////////////////
32
33 /*
34 * --------------------------------------------------------------
35 * --------- wxProtoInfo CONSTRUCTOR ----------------------------
36 * --------------------------------------------------------------
37 */
38
39 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
40 const bool need_host1, wxClassInfo *info)
41 {
42 m_protoname = name;
43 m_servname = serv;
44 m_cinfo = info;
45 m_needhost = need_host1;
46 next = wxURL::g_protocols;
47 wxURL::g_protocols = this;
48 }
49
50 /////////////////////////////////////////////////////////////////
51 // wxProtocol ///////////////////////////////////////////////////
52 /////////////////////////////////////////////////////////////////
53
54 #if wxUSE_SOCKETS
55 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
56 #else
57 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject)
58 #endif
59
60 wxProtocol::wxProtocol()
61 #if wxUSE_SOCKETS
62 : wxSocketClient()
63 #endif
64 {
65 }
66
67 #if wxUSE_SOCKETS
68 bool wxProtocol::Reconnect()
69 {
70 wxIPV4address addr;
71
72 if (!GetPeer(addr))
73 {
74 Close();
75 return FALSE;
76 }
77 if (!Close())
78 return FALSE;
79 if (!Connect(addr))
80 return FALSE;
81
82 return TRUE;
83 }
84
85 wxProtocolError GetLine(wxSocketBase *sock, wxString& result) {
86 #define PROTO_BSIZE 2048
87 size_t avail, size;
88 char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
89 char *ret;
90 bool found;
91
92 avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
93 if (sock->Error() || avail == 0)
94 return wxPROTO_NETERR;
95
96 memcpy(tmp_str, tmp_buf, avail);
97
98 // Not implemented on all systems
99 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
100 found = FALSE;
101 for (ret=tmp_str;ret < (tmp_str+avail); ret++)
102 if (*ret == '\n') {
103 found = TRUE;
104 break;
105 }
106
107 if (!found)
108 return wxPROTO_PROTERR;
109 *ret = 0;
110
111 result = tmp_str;
112 result = result.Left(result.Length()-1);
113
114 size = ret-tmp_str+1;
115 sock->Unread(&tmp_buf[size], avail-size);
116 return wxPROTO_NOERR;
117 #undef PROTO_BSIZE
118 }
119 #endif
120
121 // ----------------------------------------------------------------------
122 // Module
123 // ----------------------------------------------------------------------
124
125 class wxProtocolModule: public wxModule {
126 DECLARE_DYNAMIC_CLASS(wxProtocolModule)
127 public:
128 wxProtocolModule() {}
129 bool OnInit();
130 void OnExit();
131 };
132
133 IMPLEMENT_DYNAMIC_CLASS(wxProtocolModule, wxModule)
134
135 bool wxProtocolModule::OnInit()
136 {
137 #if wxUSE_SOCKETS
138 char *env_http_prox;
139
140 wxURL::g_proxy = NULL;
141 // Initialize the proxy when HTTP_PROXY is defined
142 env_http_prox = getenv("HTTP_PROXY");
143 if (env_http_prox)
144 wxURL::SetDefaultProxy(env_http_prox);
145 #endif
146
147 return TRUE;
148 }
149
150 void wxProtocolModule::OnExit()
151 {
152 #if wxUSE_SOCKETS
153 if (wxURL::g_proxy)
154 delete wxURL::g_proxy;
155 wxURL::g_proxy = NULL;
156 #endif
157 }
158