]> git.saurik.com Git - wxWidgets.git/blob - src/common/http.cpp
compilation fixes for !wxUSE_FONTMAP
[wxWidgets.git] / src / common / http.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: http.cpp
3 // Purpose: HTTP protocol
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: August 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "http.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_PROTOCOL_HTTP
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "wx/string.h"
28 #include "wx/tokenzr.h"
29 #include "wx/socket.h"
30 #include "wx/protocol/protocol.h"
31 #include "wx/url.h"
32 #include "wx/protocol/http.h"
33 #include "wx/sckstrm.h"
34
35 IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
36 IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), TRUE)
37
38 #define HTTP_BSIZE 2048
39
40 wxHTTP::wxHTTP()
41 : wxProtocol(),
42 m_headers(wxKEY_STRING)
43 {
44 m_addr = NULL;
45 m_read = FALSE;
46 m_proxy_mode = FALSE;
47
48 SetNotify(wxSOCKET_LOST_FLAG);
49 }
50
51 wxHTTP::~wxHTTP()
52 {
53 // wxString isn't a wxObject
54 wxNode *node = m_headers.First();
55 wxString *string;
56
57 while (node) {
58 string = (wxString *)node->Data();
59 delete string;
60 node = node->Next();
61 }
62
63 if (m_addr) {
64 delete m_addr;
65 m_addr = NULL;
66 }
67 }
68
69 wxString wxHTTP::GetContentType()
70 {
71 return GetHeader(wxT("Content-Type"));
72 }
73
74 void wxHTTP::SetProxyMode(bool on)
75 {
76 m_proxy_mode = on;
77 }
78
79 void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
80 {
81 if (m_read) {
82 m_headers.Clear();
83 m_read = FALSE;
84 }
85
86 wxNode *node = m_headers.Find(header);
87
88 if (!node)
89 m_headers.Append(header, (wxObject *)(new wxString(h_data)));
90 else {
91 wxString *str = (wxString *)node->Data();
92 (*str) = h_data;
93 }
94 }
95
96 wxString wxHTTP::GetHeader(const wxString& header)
97 {
98 wxNode *node;
99 wxString upper_header;
100
101 upper_header = header.Upper();
102
103 node = m_headers.Find(upper_header);
104 if (!node)
105 return wxEmptyString;
106
107 return *((wxString *)node->Data());
108 }
109
110 void wxHTTP::SendHeaders()
111 {
112 wxNode *head = m_headers.First();
113
114 while (head)
115 {
116 wxString *str = (wxString *)head->Data();
117
118 wxString buf;
119 buf.Printf(wxT("%s: %s\r\n"), head->GetKeyString(), str->GetData());
120
121 const wxWX2MBbuf cbuf = buf.mb_str();
122 Write(cbuf, strlen(cbuf));
123
124 head = head->Next();
125 }
126 }
127
128 bool wxHTTP::ParseHeaders()
129 {
130 wxString line;
131 wxStringTokenizer tokenzr;
132
133 m_headers.Clear();
134 m_read = TRUE;
135
136 #if defined(__VISAGECPP__)
137 // VA just can't stand while(1)
138 bool bOs2var = TRUE;
139 while(bOs2var) {
140 #else
141 while (1) {
142 #endif
143 m_perr = GetLine(this, line);
144 if (m_perr != wxPROTO_NOERR)
145 return FALSE;
146
147 if (line.Length() == 0)
148 break;
149
150 wxString left_str = line.BeforeFirst(':');
151 wxString *str = new wxString(line.AfterFirst(':').Strip(wxString::both));
152 left_str.MakeUpper();
153
154 m_headers.Append(left_str, (wxObject *) str);
155 }
156 return TRUE;
157 }
158
159 bool wxHTTP::Connect(const wxString& host)
160 {
161 wxIPV4address *addr;
162
163 if (m_addr) {
164 delete m_addr;
165 m_addr = NULL;
166 Close();
167 }
168
169 m_addr = addr = new wxIPV4address();
170
171 if (!addr->Hostname(host)) {
172 delete m_addr;
173 m_addr = NULL;
174 m_perr = wxPROTO_NETERR;
175 return FALSE;
176 }
177
178 if (!addr->Service(wxT("http")))
179 addr->Service(80);
180
181 SetHeader(wxT("Host"), host);
182
183 return TRUE;
184 }
185
186 bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
187 {
188 if (m_addr) {
189 delete m_addr;
190 Close();
191 }
192
193 m_addr = addr.Clone();
194
195 wxIPV4address *ipv4addr = wxDynamicCast(&addr, wxIPV4address);
196 if (ipv4addr)
197 SetHeader(wxT("Host"), ipv4addr->Hostname());
198
199 return TRUE;
200 }
201
202 bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
203 {
204 wxChar *tmp_buf;
205
206 switch (req) {
207 case wxHTTP_GET:
208 tmp_buf = wxT("GET");
209 break;
210 default:
211 return FALSE;
212 }
213
214 // If there is no User-Agent defined, define it.
215 if (GetHeader(wxT("User-Agent")).IsNull())
216 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
217
218 SaveState();
219 SetFlags(wxSOCKET_NONE);
220 Notify(FALSE);
221
222 wxString buf;
223 buf.Printf(wxT("%s %s HTTP/1.0\r\n"), tmp_buf, path.c_str());
224 const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf);
225 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
226 SendHeaders();
227 Write("\r\n", 2);
228
229 wxString tmp_str;
230 m_perr = GetLine(this, tmp_str);
231 if (m_perr != wxPROTO_NOERR) {
232 RestoreState();
233 return FALSE;
234 }
235
236 if (!tmp_str.Contains(wxT("HTTP/"))) {
237 // TODO: support HTTP v0.9 which can have no header.
238 // FIXME: tmp_str is not put back in the in-queue of the socket.
239 SetHeader(wxT("Content-Length"), wxT("-1"));
240 SetHeader(wxT("Content-Type"), wxT("none/none"));
241 RestoreState();
242 return TRUE;
243 }
244
245 wxStringTokenizer token(tmp_str,wxT(' '));
246 wxString tmp_str2;
247 bool ret_value;
248
249 token.NextToken();
250 tmp_str2 = token.NextToken();
251
252 switch (tmp_str2[0u]) {
253 case wxT('1'):
254 /* INFORMATION / SUCCESS */
255 break;
256 case wxT('2'):
257 /* SUCCESS */
258 break;
259 case wxT('3'):
260 /* REDIRECTION */
261 break;
262 default:
263 m_perr = wxPROTO_NOFILE;
264 RestoreState();
265 return FALSE;
266 }
267
268 ret_value = ParseHeaders();
269 RestoreState();
270 return ret_value;
271 }
272
273 class wxHTTPStream : public wxSocketInputStream
274 {
275 public:
276 wxHTTP *m_http;
277 size_t m_httpsize;
278 unsigned long m_read_bytes;
279
280 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
281 size_t GetSize() const { return m_httpsize; }
282 virtual ~wxHTTPStream(void) { m_http->Abort(); }
283
284 protected:
285 size_t OnSysRead(void *buffer, size_t bufsize);
286 };
287
288 size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
289 {
290 size_t ret;
291
292 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
293 return 0;
294
295 ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
296 m_read_bytes += ret;
297
298 return ret;
299 }
300
301 bool wxHTTP::Abort(void)
302 {
303 return wxSocketClient::Close();
304 }
305
306 wxInputStream *wxHTTP::GetInputStream(const wxString& path)
307 {
308 wxHTTPStream *inp_stream;
309
310 wxString new_path;
311
312 m_perr = wxPROTO_CONNERR;
313 if (!m_addr)
314 return NULL;
315
316 // We set m_connected back to FALSE so wxSocketBase will know what to do.
317 #ifdef __WXMAC__
318 wxSocketClient::Connect(*m_addr , FALSE );
319 wxSocketClient::WaitOnConnect(10);
320
321 if (!wxSocketClient::IsConnected())
322 return NULL;
323 #else
324 if (!wxProtocol::Connect(*m_addr))
325 return NULL;
326 #endif
327
328 if (!BuildRequest(path, wxHTTP_GET))
329 return NULL;
330
331 inp_stream = new wxHTTPStream(this);
332
333 if (!GetHeader(wxT("Content-Length")).IsEmpty())
334 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
335 else
336 inp_stream->m_httpsize = (size_t)-1;
337
338 inp_stream->m_read_bytes = 0;
339
340 Notify(FALSE);
341 SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
342
343 return inp_stream;
344 }
345
346 #endif // wxUSE_PROTOCOL_HTTP
347