]> git.saurik.com Git - wxWidgets.git/blob - src/common/http.cpp
Rewind the input stream after failing to load image from it.
[wxWidgets.git] / src / common / http.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/http.cpp
3 // Purpose: HTTP protocol
4 // Author: Guilhem Lavaux
5 // Modified by: Simo Virokannas (authentication, Dec 2005)
6 // Created: August 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_PROTOCOL_HTTP
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #ifndef WX_PRECOMP
25 #include "wx/string.h"
26 #include "wx/app.h"
27 #endif
28
29 #include "wx/tokenzr.h"
30 #include "wx/socket.h"
31 #include "wx/protocol/protocol.h"
32 #include "wx/url.h"
33 #include "wx/protocol/http.h"
34 #include "wx/sckstrm.h"
35 #include "wx/thread.h"
36
37
38 // ----------------------------------------------------------------------------
39 // wxHTTP
40 // ----------------------------------------------------------------------------
41
42 IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
43 IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), true)
44
45 wxHTTP::wxHTTP()
46 : wxProtocol()
47 {
48 m_addr = NULL;
49 m_read = false;
50 m_proxy_mode = false;
51 m_post_buf = wxEmptyString;
52 m_http_response = 0;
53
54 SetNotify(wxSOCKET_LOST_FLAG);
55 }
56
57 wxHTTP::~wxHTTP()
58 {
59 ClearHeaders();
60
61 delete m_addr;
62 }
63
64 void wxHTTP::ClearHeaders()
65 {
66 m_headers.clear();
67 }
68
69 void wxHTTP::ClearCookies()
70 {
71 m_cookies.clear();
72 }
73
74 wxString wxHTTP::GetContentType() const
75 {
76 return GetHeader(wxT("Content-Type"));
77 }
78
79 void wxHTTP::SetProxyMode(bool on)
80 {
81 m_proxy_mode = on;
82 }
83
84 wxHTTP::wxHeaderIterator wxHTTP::FindHeader(const wxString& header)
85 {
86 wxHeaderIterator it = m_headers.begin();
87 for ( wxHeaderIterator en = m_headers.end(); it != en; ++it )
88 {
89 if ( header.CmpNoCase(it->first) == 0 )
90 break;
91 }
92
93 return it;
94 }
95
96 wxHTTP::wxHeaderConstIterator wxHTTP::FindHeader(const wxString& header) const
97 {
98 wxHeaderConstIterator it = m_headers.begin();
99 for ( wxHeaderConstIterator en = m_headers.end(); it != en; ++it )
100 {
101 if ( header.CmpNoCase(it->first) == 0 )
102 break;
103 }
104
105 return it;
106 }
107
108 wxHTTP::wxCookieIterator wxHTTP::FindCookie(const wxString& cookie)
109 {
110 wxCookieIterator it = m_cookies.begin();
111 for ( wxCookieIterator en = m_cookies.end(); it != en; ++it )
112 {
113 if ( cookie.CmpNoCase(it->first) == 0 )
114 break;
115 }
116
117 return it;
118 }
119
120 wxHTTP::wxCookieConstIterator wxHTTP::FindCookie(const wxString& cookie) const
121 {
122 wxCookieConstIterator it = m_cookies.begin();
123 for ( wxCookieConstIterator en = m_cookies.end(); it != en; ++it )
124 {
125 if ( cookie.CmpNoCase(it->first) == 0 )
126 break;
127 }
128
129 return it;
130 }
131
132 void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
133 {
134 if (m_read) {
135 ClearHeaders();
136 m_read = false;
137 }
138
139 wxHeaderIterator it = FindHeader(header);
140 if (it != m_headers.end())
141 it->second = h_data;
142 else
143 m_headers[header] = h_data;
144 }
145
146 wxString wxHTTP::GetHeader(const wxString& header) const
147 {
148 wxHeaderConstIterator it = FindHeader(header);
149
150 return it == m_headers.end() ? wxGetEmptyString() : it->second;
151 }
152
153 wxString wxHTTP::GetCookie(const wxString& cookie) const
154 {
155 wxCookieConstIterator it = FindCookie(cookie);
156
157 return it == m_cookies.end() ? wxGetEmptyString() : it->second;
158 }
159
160 wxString wxHTTP::GenerateAuthString(const wxString& user, const wxString& pass) const
161 {
162 // TODO: Use wxBase64Encode() now that we have it instead of reproducing it
163
164 static const char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
165
166 wxString buf;
167 wxString toencode;
168
169 buf.Printf(wxT("Basic "));
170
171 toencode.Printf(wxT("%s:%s"),user.c_str(),pass.c_str());
172
173 size_t len = toencode.length();
174 const wxChar *from = toencode.c_str();
175 while (len >= 3) { // encode full blocks first
176 buf << wxString::Format(wxT("%c%c"), base64[(from[0] >> 2) & 0x3f], base64[((from[0] << 4) & 0x30) | ((from[1] >> 4) & 0xf)]);
177 buf << wxString::Format(wxT("%c%c"), base64[((from[1] << 2) & 0x3c) | ((from[2] >> 6) & 0x3)], base64[from[2] & 0x3f]);
178 from += 3;
179 len -= 3;
180 }
181 if (len > 0) { // pad the remaining characters
182 buf << wxString::Format(wxT("%c"), base64[(from[0] >> 2) & 0x3f]);
183 if (len == 1) {
184 buf << wxString::Format(wxT("%c="), base64[(from[0] << 4) & 0x30]);
185 } else {
186 buf << wxString::Format(wxT("%c%c"), base64[((from[0] << 4) & 0x30) | ((from[1] >> 4) & 0xf)], base64[(from[1] << 2) & 0x3c]);
187 }
188 buf << wxT("=");
189 }
190
191 return buf;
192 }
193
194 void wxHTTP::SetPostBuffer(const wxString& post_buf)
195 {
196 m_post_buf = post_buf;
197 }
198
199 void wxHTTP::SendHeaders()
200 {
201 typedef wxStringToStringHashMap::iterator iterator;
202 wxString buf;
203
204 for (iterator it = m_headers.begin(), en = m_headers.end(); it != en; ++it )
205 {
206 buf.Printf(wxT("%s: %s\r\n"), it->first.c_str(), it->second.c_str());
207
208 const wxWX2MBbuf cbuf = buf.mb_str();
209 Write(cbuf, strlen(cbuf));
210 }
211 }
212
213 bool wxHTTP::ParseHeaders()
214 {
215 wxString line;
216 wxStringTokenizer tokenzr;
217
218 ClearHeaders();
219 ClearCookies();
220 m_read = true;
221
222 for ( ;; )
223 {
224 m_lastError = ReadLine(this, line);
225 if (m_lastError != wxPROTO_NOERR)
226 return false;
227
228 if (line.length() == 0)
229 break;
230
231 wxString left_str = line.BeforeFirst(':');
232 if(!left_str.CmpNoCase("Set-Cookie"))
233 {
234 wxString cookieName = line.AfterFirst(':').Strip(wxString::both).BeforeFirst('=');
235 wxString cookieValue = line.AfterFirst(':').Strip(wxString::both).AfterFirst('=').BeforeFirst(';');
236 m_cookies[cookieName] = cookieValue;
237
238 // For compatibility
239 m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
240 }
241 else
242 {
243 m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
244 }
245 }
246 return true;
247 }
248
249 bool wxHTTP::Connect(const wxString& host, unsigned short port)
250 {
251 wxIPV4address *addr;
252
253 if (m_addr) {
254 wxDELETE(m_addr);
255 Close();
256 }
257
258 m_addr = addr = new wxIPV4address();
259
260 if (!addr->Hostname(host)) {
261 wxDELETE(m_addr);
262 m_lastError = wxPROTO_NETERR;
263 return false;
264 }
265
266 if ( port )
267 addr->Service(port);
268 else if (!addr->Service(wxT("http")))
269 addr->Service(80);
270
271 wxString hostHdr = host;
272 if ( port && port != 80 )
273 hostHdr << wxT(":") << port;
274 SetHeader(wxT("Host"), hostHdr);
275
276 m_lastError = wxPROTO_NOERR;
277 return true;
278 }
279
280 bool wxHTTP::Connect(const wxSockAddress& addr, bool WXUNUSED(wait))
281 {
282 if (m_addr) {
283 delete m_addr;
284 Close();
285 }
286
287 m_addr = addr.Clone();
288
289 wxIPV4address *ipv4addr = wxDynamicCast(&addr, wxIPV4address);
290 if ( ipv4addr )
291 {
292 wxString hostHdr = ipv4addr->OrigHostname();
293 unsigned short port = ipv4addr->Service();
294 if ( port && port != 80 )
295 hostHdr << wxT(":") << port;
296 SetHeader(wxT("Host"), hostHdr);
297 }
298
299 m_lastError = wxPROTO_NOERR;
300 return true;
301 }
302
303 bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
304 {
305 const wxChar *request;
306
307 switch (req)
308 {
309 case wxHTTP_GET:
310 request = wxT("GET");
311 break;
312
313 case wxHTTP_POST:
314 request = wxT("POST");
315 if ( GetHeader( wxT("Content-Length") ).IsNull() )
316 SetHeader( wxT("Content-Length"), wxString::Format( wxT("%lu"), (unsigned long)m_post_buf.Len() ) );
317 break;
318
319 default:
320 return false;
321 }
322
323 m_http_response = 0;
324
325 // If there is no User-Agent defined, define it.
326 if (GetHeader(wxT("User-Agent")).IsNull())
327 SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
328
329 // Send authentication information
330 if (!m_username.empty() || !m_password.empty()) {
331 SetHeader(wxT("Authorization"), GenerateAuthString(m_username, m_password));
332 }
333
334 SaveState();
335
336 // we may use non blocking sockets only if we can dispatch events from them
337 SetFlags( wxIsMainThread() && wxApp::IsMainLoopRunning() ? wxSOCKET_NONE
338 : wxSOCKET_BLOCK );
339 Notify(false);
340
341 wxString buf;
342 buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str());
343 const wxWX2MBbuf pathbuf = buf.mb_str();
344 Write(pathbuf, strlen(pathbuf));
345 SendHeaders();
346 Write("\r\n", 2);
347
348 if ( req == wxHTTP_POST ) {
349 // Post data can be arbitrary binary data when the "binary" content
350 // transfer encoding is used so don't assume it's ASCII only or
351 // NUL-terminated.
352 {
353 const wxScopedCharBuffer buf(m_post_buf.To8BitData());
354 Write(buf, buf.length());
355 } // delete the buffer before modifying the string it points to, it
356 // wouldn't really be a problem here even if we didn't do this
357 // because we won't use this buffer again but this will avoid any
358 // nasty surprises in the future if this code changes
359
360 m_post_buf = wxEmptyString;
361 }
362
363 wxString tmp_str;
364 m_lastError = ReadLine(this, tmp_str);
365 if (m_lastError != wxPROTO_NOERR) {
366 RestoreState();
367 return false;
368 }
369
370 if (!tmp_str.Contains(wxT("HTTP/"))) {
371 // TODO: support HTTP v0.9 which can have no header.
372 // FIXME: tmp_str is not put back in the in-queue of the socket.
373 m_lastError = wxPROTO_NOERR;
374 SetHeader(wxT("Content-Length"), wxT("-1"));
375 SetHeader(wxT("Content-Type"), wxT("none/none"));
376 RestoreState();
377 return true;
378 }
379
380 wxStringTokenizer token(tmp_str,wxT(' '));
381 wxString tmp_str2;
382 bool ret_value;
383
384 token.NextToken();
385 tmp_str2 = token.NextToken();
386
387 m_http_response = wxAtoi(tmp_str2);
388
389 switch ( tmp_str2[0u].GetValue() )
390 {
391 case wxT('1'):
392 /* INFORMATION / SUCCESS */
393 break;
394
395 case wxT('2'):
396 /* SUCCESS */
397 break;
398
399 case wxT('3'):
400 /* REDIRECTION */
401 break;
402
403 default:
404 m_lastError = wxPROTO_NOFILE;
405 RestoreState();
406 return false;
407 }
408
409 m_lastError = wxPROTO_NOERR;
410 ret_value = ParseHeaders();
411 RestoreState();
412 return ret_value;
413 }
414
415 bool wxHTTP::Abort(void)
416 {
417 return wxSocketClient::Close();
418 }
419
420 // ----------------------------------------------------------------------------
421 // wxHTTPStream and wxHTTP::GetInputStream
422 // ----------------------------------------------------------------------------
423
424 class wxHTTPStream : public wxSocketInputStream
425 {
426 public:
427 wxHTTP *m_http;
428 size_t m_httpsize;
429 unsigned long m_read_bytes;
430
431 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
432 size_t GetSize() const { return m_httpsize; }
433 virtual ~wxHTTPStream(void) { m_http->Abort(); }
434
435 protected:
436 size_t OnSysRead(void *buffer, size_t bufsize);
437
438 wxDECLARE_NO_COPY_CLASS(wxHTTPStream);
439 };
440
441 size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
442 {
443 if (m_read_bytes >= m_httpsize)
444 {
445 m_lasterror = wxSTREAM_EOF;
446 return 0;
447 }
448
449 size_t ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
450 m_read_bytes += ret;
451
452 if (m_httpsize==(size_t)-1 && m_lasterror == wxSTREAM_READ_ERROR )
453 {
454 // if m_httpsize is (size_t) -1 this means read until connection closed
455 // which is equivalent to getting a READ_ERROR, for clients however this
456 // must be translated into EOF, as it is the expected way of signalling
457 // end end of the content
458 m_lasterror = wxSTREAM_EOF;
459 }
460
461 return ret;
462 }
463
464 wxInputStream *wxHTTP::GetInputStream(const wxString& path)
465 {
466 wxHTTPStream *inp_stream;
467
468 wxString new_path;
469
470 m_lastError = wxPROTO_CONNERR; // all following returns share this type of error
471 if (!m_addr)
472 return NULL;
473
474 // We set m_connected back to false so wxSocketBase will know what to do.
475 #ifdef __WXMAC__
476 wxSocketClient::Connect(*m_addr , false );
477 wxSocketClient::WaitOnConnect(10);
478
479 if (!wxSocketClient::IsConnected())
480 return NULL;
481 #else
482 if (!wxProtocol::Connect(*m_addr))
483 return NULL;
484 #endif
485
486 if (!BuildRequest(path, m_post_buf.empty() ? wxHTTP_GET : wxHTTP_POST))
487 return NULL;
488
489 inp_stream = new wxHTTPStream(this);
490
491 if (!GetHeader(wxT("Content-Length")).empty())
492 inp_stream->m_httpsize = wxAtoi(GetHeader(wxT("Content-Length")));
493 else
494 inp_stream->m_httpsize = (size_t)-1;
495
496 inp_stream->m_read_bytes = 0;
497
498 Notify(false);
499 SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
500
501 // no error; reset m_lastError
502 m_lastError = wxPROTO_NOERR;
503 return inp_stream;
504 }
505
506 #endif // wxUSE_PROTOCOL_HTTP