]> git.saurik.com Git - wxWidgets.git/blob - src/common/protocol.cpp
1f5fb7adfd5af6e2d230cdbe849ff984148bf5dc
[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 #if wxUSE_SOCKETS
24
25 #include "wx/protocol/protocol.h"
26 #include "wx/url.h"
27 #include "wx/module.h"
28
29 #include <stdlib.h>
30
31 /////////////////////////////////////////////////////////////////
32 // wxProtoInfo
33 /////////////////////////////////////////////////////////////////
34
35 /*
36 * --------------------------------------------------------------
37 * --------- wxProtoInfo CONSTRUCTOR ----------------------------
38 * --------------------------------------------------------------
39 */
40
41 wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv,
42 const bool need_host1, wxClassInfo *info)
43 {
44 m_protoname = name;
45 m_servname = serv;
46 m_cinfo = info;
47 m_needhost = need_host1;
48 next = wxURL::g_protocols;
49 wxURL::g_protocols = this;
50 }
51
52 /////////////////////////////////////////////////////////////////
53 // wxProtocol ///////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////
55
56 IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient)
57
58 wxProtocol::wxProtocol()
59 : wxSocketClient()
60 {
61 }
62
63 bool wxProtocol::Reconnect()
64 {
65 wxIPV4address addr;
66
67 if (!GetPeer(addr)) {
68 Close();
69 return FALSE;
70 }
71 if (!Close())
72 return FALSE;
73 if (!Connect(addr))
74 return FALSE;
75 return TRUE;
76 }
77
78 wxProtocolError GetLine(wxSocketBase *sock, wxString& result) {
79 #define PROTO_BSIZE 2048
80 size_t avail, size;
81 char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE];
82 char *ret;
83 bool found;
84
85 avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount();
86 if (sock->LastError() != 0 || avail == 0)
87 return wxPROTO_NETERR;
88
89 memcpy(tmp_str, tmp_buf, avail);
90
91 // Not implemented on all systems
92 // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail);
93 found = FALSE;
94 for (ret=tmp_str;ret < (tmp_str+avail); ret++)
95 if (*ret == '\n') {
96 found = TRUE;
97 break;
98 }
99
100 if (!found)
101 return wxPROTO_PROTERR;
102 *ret = 0;
103
104 result = tmp_str;
105 result = result.Left(result.Length()-1);
106
107 size = ret-tmp_str+1;
108 sock->CreatePushbackBefore(&tmp_buf[size], avail-size);
109 return wxPROTO_NOERR;
110 #undef PROTO_BSIZE
111 }
112
113 // ----------------------------------------------------------------------
114 // Module
115 // ----------------------------------------------------------------------
116
117 class wxProtocolModule: public wxModule {
118 DECLARE_DYNAMIC_CLASS(wxProtocolModule)
119 public:
120 wxProtocolModule() {}
121 bool OnInit();
122 void OnExit();
123 };
124
125 #if !USE_SHARED_LIBRARY
126 IMPLEMENT_DYNAMIC_CLASS(wxProtocolModule, wxModule)
127 #endif
128
129 bool wxProtocolModule::OnInit()
130 {
131 char *env_http_prox;
132
133 wxURL::g_proxy = NULL;
134 // Initialize the proxy when HTTP_PROXY is defined
135 env_http_prox = getenv("HTTP_PROXY");
136 if (env_http_prox)
137 wxURL::SetDefaultProxy(env_http_prox);
138
139 return TRUE;
140 }
141
142 void wxProtocolModule::OnExit()
143 {
144 if (wxURL::g_proxy)
145 delete wxURL::g_proxy;
146 wxURL::g_proxy = NULL;
147 }
148
149 #endif
150 // wxUSE_SOCKETS