]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
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 | ||
fcc6dddd JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
2df7be7f | 20 | #pragma hdrstop |
fcc6dddd JS |
21 | #endif |
22 | ||
f4ada568 GL |
23 | #include "wx/protocol/protocol.h" |
24 | #include "wx/url.h" | |
3b4183d8 | 25 | #include "wx/module.h" |
f4ada568 | 26 | |
f61815af GL |
27 | #include <stdlib.h> |
28 | ||
f4ada568 GL |
29 | ///////////////////////////////////////////////////////////////// |
30 | // wxProtoInfo | |
31 | ///////////////////////////////////////////////////////////////// | |
32 | ||
33 | /* | |
34 | * -------------------------------------------------------------- | |
35 | * --------- wxProtoInfo CONSTRUCTOR ---------------------------- | |
36 | * -------------------------------------------------------------- | |
37 | */ | |
38 | ||
4846abaf | 39 | wxProtoInfo::wxProtoInfo(const wxChar *name, const wxChar *serv, |
f4ada568 GL |
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 | ||
8a4df159 | 54 | #if wxUSE_SOCKETS |
f4ada568 | 55 | IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxSocketClient) |
8a4df159 RR |
56 | #else |
57 | IMPLEMENT_ABSTRACT_CLASS(wxProtocol, wxObject) | |
58 | #endif | |
f4ada568 GL |
59 | |
60 | wxProtocol::wxProtocol() | |
8a4df159 | 61 | #if wxUSE_SOCKETS |
f4ada568 | 62 | : wxSocketClient() |
8a4df159 | 63 | #endif |
f4ada568 GL |
64 | { |
65 | } | |
66 | ||
8a4df159 | 67 | #if wxUSE_SOCKETS |
f4ada568 GL |
68 | bool wxProtocol::Reconnect() |
69 | { | |
70 | wxIPV4address addr; | |
71 | ||
72 | if (!GetPeer(addr)) { | |
73 | Close(); | |
74 | return FALSE; | |
75 | } | |
76 | if (!Close()) | |
77 | return FALSE; | |
78 | if (!Connect(addr)) | |
79 | return FALSE; | |
80 | return TRUE; | |
81 | } | |
82 | ||
83 | wxProtocolError GetLine(wxSocketBase *sock, wxString& result) { | |
84 | #define PROTO_BSIZE 2048 | |
85 | size_t avail, size; | |
86 | char tmp_buf[PROTO_BSIZE], tmp_str[PROTO_BSIZE]; | |
87 | char *ret; | |
88 | bool found; | |
89 | ||
90 | avail = sock->Read(tmp_buf, PROTO_BSIZE).LastCount(); | |
91 | if (sock->LastError() != 0 || avail == 0) | |
92 | return wxPROTO_NETERR; | |
93 | ||
94 | memcpy(tmp_str, tmp_buf, avail); | |
95 | ||
96 | // Not implemented on all systems | |
97 | // ret = (char *)memccpy(tmp_str, tmp_buf, '\n', avail); | |
98 | found = FALSE; | |
99 | for (ret=tmp_str;ret < (tmp_str+avail); ret++) | |
100 | if (*ret == '\n') { | |
101 | found = TRUE; | |
102 | break; | |
103 | } | |
104 | ||
105 | if (!found) | |
106 | return wxPROTO_PROTERR; | |
107 | *ret = 0; | |
108 | ||
109 | result = tmp_str; | |
110 | result = result.Left(result.Length()-1); | |
111 | ||
112 | size = ret-tmp_str+1; | |
5bb0807e | 113 | sock->Unread(&tmp_buf[size], avail-size); |
f4ada568 GL |
114 | return wxPROTO_NOERR; |
115 | #undef PROTO_BSIZE | |
116 | } | |
8a4df159 | 117 | #endif |
3b4183d8 GL |
118 | |
119 | // ---------------------------------------------------------------------- | |
120 | // Module | |
121 | // ---------------------------------------------------------------------- | |
122 | ||
123 | class wxProtocolModule: public wxModule { | |
124 | DECLARE_DYNAMIC_CLASS(wxProtocolModule) | |
125 | public: | |
126 | wxProtocolModule() {} | |
127 | bool OnInit(); | |
128 | void OnExit(); | |
129 | }; | |
130 | ||
131 | #if !USE_SHARED_LIBRARY | |
132 | IMPLEMENT_DYNAMIC_CLASS(wxProtocolModule, wxModule) | |
133 | #endif | |
134 | ||
135 | bool wxProtocolModule::OnInit() | |
136 | { | |
8a4df159 | 137 | #if wxUSE_SOCKETS |
f61815af GL |
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); | |
8a4df159 | 145 | #endif |
f61815af | 146 | |
3b4183d8 GL |
147 | return TRUE; |
148 | } | |
149 | ||
150 | void wxProtocolModule::OnExit() | |
151 | { | |
8a4df159 | 152 | #if wxUSE_SOCKETS |
f61815af GL |
153 | if (wxURL::g_proxy) |
154 | delete wxURL::g_proxy; | |
3b4183d8 | 155 | wxURL::g_proxy = NULL; |
8a4df159 | 156 | #endif |
3b4183d8 | 157 | } |
35a4dab7 | 158 |