]>
Commit | Line | Data |
---|---|---|
f4ada568 | 1 | ///////////////////////////////////////////////////////////////////////////// |
670f9935 | 2 | // Name: src/common/http.cpp |
f4ada568 GL |
3 | // Purpose: HTTP protocol |
4 | // Author: Guilhem Lavaux | |
dbccd1c2 | 5 | // Modified by: Simo Virokannas (authentication, Dec 2005) |
f4ada568 GL |
6 | // Created: August 1997 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1997, 1998 Guilhem Lavaux | |
65571936 | 9 | // Licence: wxWindows licence |
f4ada568 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
fcc6dddd JS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
670f9935 | 16 | #pragma hdrstop |
fcc6dddd JS |
17 | #endif |
18 | ||
a5d46b73 | 19 | #if wxUSE_PROTOCOL_HTTP |
ce4169a4 | 20 | |
f4ada568 GL |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
a8d628fc DS |
23 | |
24 | #ifndef WX_PRECOMP | |
670f9935 WS |
25 | #include "wx/string.h" |
26 | #include "wx/app.h" | |
a8d628fc DS |
27 | #endif |
28 | ||
f4ada568 GL |
29 | #include "wx/tokenzr.h" |
30 | #include "wx/socket.h" | |
31 | #include "wx/protocol/protocol.h" | |
fae05df5 | 32 | #include "wx/url.h" |
f4ada568 GL |
33 | #include "wx/protocol/http.h" |
34 | #include "wx/sckstrm.h" | |
204abcd4 | 35 | #include "wx/thread.h" |
f4ada568 | 36 | |
730b772b FM |
37 | |
38 | // ---------------------------------------------------------------------------- | |
39 | // wxHTTP | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
f4ada568 | 42 | IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol) |
5d3e7b52 | 43 | IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), true) |
f4ada568 | 44 | |
f4ada568 | 45 | wxHTTP::wxHTTP() |
df5168c4 | 46 | : wxProtocol() |
f4ada568 | 47 | { |
48f7ffbe WS |
48 | m_addr = NULL; |
49 | m_read = false; | |
50 | m_proxy_mode = false; | |
51 | m_post_buf = wxEmptyString; | |
52 | m_http_response = 0; | |
f4ada568 | 53 | |
48f7ffbe | 54 | SetNotify(wxSOCKET_LOST_FLAG); |
f4ada568 GL |
55 | } |
56 | ||
57 | wxHTTP::~wxHTTP() | |
2fb203e6 VZ |
58 | { |
59 | ClearHeaders(); | |
60 | ||
61 | delete m_addr; | |
62 | } | |
63 | ||
64 | void wxHTTP::ClearHeaders() | |
f4ada568 | 65 | { |
730b772b | 66 | m_headers.clear(); |
f4ada568 GL |
67 | } |
68 | ||
906cb3f1 JS |
69 | void wxHTTP::ClearCookies() |
70 | { | |
71 | m_cookies.clear(); | |
72 | } | |
73 | ||
730b772b | 74 | wxString wxHTTP::GetContentType() const |
f4ada568 | 75 | { |
48f7ffbe | 76 | return GetHeader(wxT("Content-Type")); |
f4ada568 GL |
77 | } |
78 | ||
f61815af GL |
79 | void wxHTTP::SetProxyMode(bool on) |
80 | { | |
48f7ffbe | 81 | m_proxy_mode = on; |
f61815af GL |
82 | } |
83 | ||
bdcade0a | 84 | wxHTTP::wxHeaderIterator wxHTTP::FindHeader(const wxString& header) |
71414756 | 85 | { |
bdcade0a MB |
86 | wxHeaderIterator it = m_headers.begin(); |
87 | for ( wxHeaderIterator en = m_headers.end(); it != en; ++it ) | |
88 | { | |
86501081 | 89 | if ( header.CmpNoCase(it->first) == 0 ) |
bdcade0a MB |
90 | break; |
91 | } | |
71414756 | 92 | |
bdcade0a MB |
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 ) | |
71414756 | 100 | { |
86501081 | 101 | if ( header.CmpNoCase(it->first) == 0 ) |
71414756 VZ |
102 | break; |
103 | } | |
104 | ||
105 | return it; | |
106 | } | |
107 | ||
906cb3f1 JS |
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 | ||
f4ada568 GL |
132 | void wxHTTP::SetHeader(const wxString& header, const wxString& h_data) |
133 | { | |
48f7ffbe WS |
134 | if (m_read) { |
135 | ClearHeaders(); | |
136 | m_read = false; | |
137 | } | |
f4ada568 | 138 | |
48f7ffbe WS |
139 | wxHeaderIterator it = FindHeader(header); |
140 | if (it != m_headers.end()) | |
141 | it->second = h_data; | |
142 | else | |
143 | m_headers[header] = h_data; | |
f4ada568 GL |
144 | } |
145 | ||
71414756 | 146 | wxString wxHTTP::GetHeader(const wxString& header) const |
f4ada568 | 147 | { |
bdcade0a | 148 | wxHeaderConstIterator it = FindHeader(header); |
f4ada568 | 149 | |
01482482 | 150 | return it == m_headers.end() ? wxGetEmptyString() : it->second; |
f4ada568 GL |
151 | } |
152 | ||
906cb3f1 JS |
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 | ||
dbccd1c2 JS |
160 | wxString wxHTTP::GenerateAuthString(const wxString& user, const wxString& pass) const |
161 | { | |
162 | static const char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
163 | ||
164 | wxString buf; | |
165 | wxString toencode; | |
166 | ||
167 | buf.Printf(wxT("Basic ")); | |
168 | ||
169 | toencode.Printf(wxT("%s:%s"),user.c_str(),pass.c_str()); | |
170 | ||
670f9935 | 171 | size_t len = toencode.length(); |
dbccd1c2 JS |
172 | const wxChar *from = toencode.c_str(); |
173 | while (len >= 3) { // encode full blocks first | |
174 | buf << wxString::Format(wxT("%c%c"), base64[(from[0] >> 2) & 0x3f], base64[((from[0] << 4) & 0x30) | ((from[1] >> 4) & 0xf)]); | |
175 | buf << wxString::Format(wxT("%c%c"), base64[((from[1] << 2) & 0x3c) | ((from[2] >> 6) & 0x3)], base64[from[2] & 0x3f]); | |
176 | from += 3; | |
177 | len -= 3; | |
178 | } | |
179 | if (len > 0) { // pad the remaining characters | |
180 | buf << wxString::Format(wxT("%c"), base64[(from[0] >> 2) & 0x3f]); | |
181 | if (len == 1) { | |
182 | buf << wxString::Format(wxT("%c="), base64[(from[0] << 4) & 0x30]); | |
183 | } else { | |
886f61ca | 184 | buf << wxString::Format(wxT("%c%c"), base64[((from[0] << 4) & 0x30) | ((from[1] >> 4) & 0xf)], base64[(from[1] << 2) & 0x3c]); |
dbccd1c2 | 185 | } |
2523e9b7 | 186 | buf << wxT("="); |
dbccd1c2 JS |
187 | } |
188 | ||
189 | return buf; | |
190 | } | |
191 | ||
f9133b32 VZ |
192 | void wxHTTP::SetPostBuffer(const wxString& post_buf) |
193 | { | |
71414756 | 194 | m_post_buf = post_buf; |
f9133b32 VZ |
195 | } |
196 | ||
f4ada568 GL |
197 | void wxHTTP::SendHeaders() |
198 | { | |
48f7ffbe WS |
199 | typedef wxStringToStringHashMap::iterator iterator; |
200 | wxString buf; | |
f4ada568 | 201 | |
48f7ffbe WS |
202 | for (iterator it = m_headers.begin(), en = m_headers.end(); it != en; ++it ) |
203 | { | |
204 | buf.Printf(wxT("%s: %s\r\n"), it->first.c_str(), it->second.c_str()); | |
53c6e7cc | 205 | |
48f7ffbe WS |
206 | const wxWX2MBbuf cbuf = buf.mb_str(); |
207 | Write(cbuf, strlen(cbuf)); | |
208 | } | |
f4ada568 GL |
209 | } |
210 | ||
211 | bool wxHTTP::ParseHeaders() | |
212 | { | |
48f7ffbe WS |
213 | wxString line; |
214 | wxStringTokenizer tokenzr; | |
f4ada568 | 215 | |
48f7ffbe | 216 | ClearHeaders(); |
906cb3f1 | 217 | ClearCookies(); |
48f7ffbe | 218 | m_read = true; |
f4ada568 | 219 | |
a09f8377 | 220 | for ( ;; ) |
48f7ffbe | 221 | { |
730b772b FM |
222 | m_lastError = ReadLine(this, line); |
223 | if (m_lastError != wxPROTO_NOERR) | |
48f7ffbe WS |
224 | return false; |
225 | ||
670f9935 | 226 | if (line.length() == 0) |
48f7ffbe WS |
227 | break; |
228 | ||
229 | wxString left_str = line.BeforeFirst(':'); | |
906cb3f1 JS |
230 | if(!left_str.CmpNoCase("Set-Cookie")) |
231 | { | |
232 | wxString cookieName = line.AfterFirst(':').Strip(wxString::both).BeforeFirst('='); | |
233 | wxString cookieValue = line.AfterFirst(':').Strip(wxString::both).AfterFirst('=').BeforeFirst(';'); | |
234 | m_cookies[cookieName] = cookieValue; | |
235 | ||
236 | // For compatibility | |
237 | m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both); | |
238 | } | |
239 | else | |
240 | { | |
241 | m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both); | |
242 | } | |
48f7ffbe WS |
243 | } |
244 | return true; | |
f4ada568 GL |
245 | } |
246 | ||
f9133b32 | 247 | bool wxHTTP::Connect(const wxString& host, unsigned short port) |
f4ada568 | 248 | { |
48f7ffbe | 249 | wxIPV4address *addr; |
f4ada568 | 250 | |
48f7ffbe WS |
251 | if (m_addr) { |
252 | delete m_addr; | |
253 | m_addr = NULL; | |
254 | Close(); | |
255 | } | |
f4ada568 | 256 | |
48f7ffbe | 257 | m_addr = addr = new wxIPV4address(); |
f4ada568 | 258 | |
48f7ffbe WS |
259 | if (!addr->Hostname(host)) { |
260 | delete m_addr; | |
261 | m_addr = NULL; | |
730b772b | 262 | m_lastError = wxPROTO_NETERR; |
48f7ffbe WS |
263 | return false; |
264 | } | |
f4ada568 | 265 | |
48f7ffbe WS |
266 | if ( port ) |
267 | addr->Service(port); | |
268 | else if (!addr->Service(wxT("http"))) | |
269 | addr->Service(80); | |
ce22d615 | 270 | |
1c7a6772 VZ |
271 | wxString hostHdr = host; |
272 | if ( port && port != 80 ) | |
273 | hostHdr << wxT(":") << port; | |
274 | SetHeader(wxT("Host"), hostHdr); | |
f4ada568 | 275 | |
730b772b | 276 | m_lastError = wxPROTO_NOERR; |
48f7ffbe | 277 | return true; |
f4ada568 GL |
278 | } |
279 | ||
ddc7f0c9 | 280 | bool wxHTTP::Connect(const wxSockAddress& addr, bool WXUNUSED(wait)) |
f4ada568 | 281 | { |
48f7ffbe WS |
282 | if (m_addr) { |
283 | delete m_addr; | |
284 | Close(); | |
285 | } | |
f4ada568 | 286 | |
48f7ffbe | 287 | m_addr = addr.Clone(); |
acd15a3f | 288 | |
48f7ffbe | 289 | wxIPV4address *ipv4addr = wxDynamicCast(&addr, wxIPV4address); |
1c7a6772 VZ |
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 | } | |
5b96a71a | 298 | |
730b772b | 299 | m_lastError = wxPROTO_NOERR; |
48f7ffbe | 300 | return true; |
f4ada568 GL |
301 | } |
302 | ||
303 | bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req) | |
304 | { | |
48f7ffbe | 305 | const wxChar *request; |
f9133b32 | 306 | |
48f7ffbe WS |
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 | ||
dbccd1c2 | 329 | // Send authentication information |
670f9935 | 330 | if (!m_username.empty() || !m_password.empty()) { |
dbccd1c2 JS |
331 | SetHeader(wxT("Authorization"), GenerateAuthString(m_username, m_password)); |
332 | } | |
333 | ||
48f7ffbe WS |
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()); | |
86501081 VS |
343 | const wxWX2MBbuf pathbuf = buf.mb_str(); |
344 | Write(pathbuf, strlen(pathbuf)); | |
48f7ffbe WS |
345 | SendHeaders(); |
346 | Write("\r\n", 2); | |
347 | ||
348 | if ( req == wxHTTP_POST ) { | |
349 | Write(m_post_buf.mbc_str(), m_post_buf.Len()); | |
350 | m_post_buf = wxEmptyString; | |
351 | } | |
352 | ||
353 | wxString tmp_str; | |
730b772b FM |
354 | m_lastError = ReadLine(this, tmp_str); |
355 | if (m_lastError != wxPROTO_NOERR) { | |
48f7ffbe WS |
356 | RestoreState(); |
357 | return false; | |
358 | } | |
359 | ||
360 | if (!tmp_str.Contains(wxT("HTTP/"))) { | |
361 | // TODO: support HTTP v0.9 which can have no header. | |
362 | // FIXME: tmp_str is not put back in the in-queue of the socket. | |
730b772b | 363 | m_lastError = wxPROTO_NOERR; |
48f7ffbe WS |
364 | SetHeader(wxT("Content-Length"), wxT("-1")); |
365 | SetHeader(wxT("Content-Type"), wxT("none/none")); | |
366 | RestoreState(); | |
367 | return true; | |
368 | } | |
f4ada568 | 369 | |
48f7ffbe WS |
370 | wxStringTokenizer token(tmp_str,wxT(' ')); |
371 | wxString tmp_str2; | |
372 | bool ret_value; | |
373 | ||
374 | token.NextToken(); | |
375 | tmp_str2 = token.NextToken(); | |
376 | ||
377 | m_http_response = wxAtoi(tmp_str2); | |
378 | ||
c9f78968 | 379 | switch ( tmp_str2[0u].GetValue() ) |
48f7ffbe WS |
380 | { |
381 | case wxT('1'): | |
382 | /* INFORMATION / SUCCESS */ | |
383 | break; | |
384 | ||
385 | case wxT('2'): | |
386 | /* SUCCESS */ | |
387 | break; | |
388 | ||
389 | case wxT('3'): | |
390 | /* REDIRECTION */ | |
391 | break; | |
392 | ||
393 | default: | |
730b772b | 394 | m_lastError = wxPROTO_NOFILE; |
48f7ffbe WS |
395 | RestoreState(); |
396 | return false; | |
397 | } | |
398 | ||
730b772b | 399 | m_lastError = wxPROTO_NOERR; |
48f7ffbe WS |
400 | ret_value = ParseHeaders(); |
401 | RestoreState(); | |
402 | return ret_value; | |
f4ada568 GL |
403 | } |
404 | ||
730b772b FM |
405 | bool wxHTTP::Abort(void) |
406 | { | |
407 | return wxSocketClient::Close(); | |
408 | } | |
409 | ||
410 | // ---------------------------------------------------------------------------- | |
411 | // wxHTTPStream and wxHTTP::GetInputStream | |
412 | // ---------------------------------------------------------------------------- | |
413 | ||
fc4b32c2 GRG |
414 | class wxHTTPStream : public wxSocketInputStream |
415 | { | |
f4ada568 | 416 | public: |
48f7ffbe WS |
417 | wxHTTP *m_http; |
418 | size_t m_httpsize; | |
419 | unsigned long m_read_bytes; | |
9a1b2c28 | 420 | |
48f7ffbe WS |
421 | wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {} |
422 | size_t GetSize() const { return m_httpsize; } | |
423 | virtual ~wxHTTPStream(void) { m_http->Abort(); } | |
a324a7bc GL |
424 | |
425 | protected: | |
48f7ffbe | 426 | size_t OnSysRead(void *buffer, size_t bufsize); |
22f3361e | 427 | |
c0c133e1 | 428 | wxDECLARE_NO_COPY_CLASS(wxHTTPStream); |
f4ada568 GL |
429 | }; |
430 | ||
a324a7bc GL |
431 | size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize) |
432 | { | |
ccc3a25d | 433 | if (m_read_bytes >= m_httpsize) |
32013d27 VZ |
434 | { |
435 | m_lasterror = wxSTREAM_EOF; | |
436 | return 0; | |
437 | } | |
a324a7bc | 438 | |
32013d27 VZ |
439 | size_t ret = wxSocketInputStream::OnSysRead(buffer, bufsize); |
440 | m_read_bytes += ret; | |
a324a7bc | 441 | |
8b6b8e21 SC |
442 | if (m_httpsize==(size_t)-1 && m_lasterror == wxSTREAM_READ_ERROR ) |
443 | { | |
444 | // if m_httpsize is (size_t) -1 this means read until connection closed | |
445 | // which is equivalent to getting a READ_ERROR, for clients however this | |
446 | // must be translated into EOF, as it is the expected way of signalling | |
447 | // end end of the content | |
730b772b | 448 | m_lasterror = wxSTREAM_EOF; |
8b6b8e21 SC |
449 | } |
450 | ||
32013d27 | 451 | return ret; |
a324a7bc GL |
452 | } |
453 | ||
f4ada568 GL |
454 | wxInputStream *wxHTTP::GetInputStream(const wxString& path) |
455 | { | |
48f7ffbe | 456 | wxHTTPStream *inp_stream; |
8f5bda17 | 457 | |
48f7ffbe | 458 | wxString new_path; |
f4ada568 | 459 | |
730b772b | 460 | m_lastError = wxPROTO_CONNERR; // all following returns share this type of error |
48f7ffbe WS |
461 | if (!m_addr) |
462 | return NULL; | |
f4ada568 | 463 | |
48f7ffbe | 464 | // We set m_connected back to false so wxSocketBase will know what to do. |
ad8b8498 | 465 | #ifdef __WXMAC__ |
48f7ffbe WS |
466 | wxSocketClient::Connect(*m_addr , false ); |
467 | wxSocketClient::WaitOnConnect(10); | |
ad8b8498 SC |
468 | |
469 | if (!wxSocketClient::IsConnected()) | |
470 | return NULL; | |
471 | #else | |
48f7ffbe WS |
472 | if (!wxProtocol::Connect(*m_addr)) |
473 | return NULL; | |
ad8b8498 | 474 | #endif |
f4ada568 | 475 | |
48f7ffbe WS |
476 | if (!BuildRequest(path, m_post_buf.empty() ? wxHTTP_GET : wxHTTP_POST)) |
477 | return NULL; | |
f4ada568 | 478 | |
48f7ffbe | 479 | inp_stream = new wxHTTPStream(this); |
8f5bda17 | 480 | |
48f7ffbe | 481 | if (!GetHeader(wxT("Content-Length")).empty()) |
86501081 | 482 | inp_stream->m_httpsize = wxAtoi(GetHeader(wxT("Content-Length"))); |
48f7ffbe WS |
483 | else |
484 | inp_stream->m_httpsize = (size_t)-1; | |
a324a7bc | 485 | |
48f7ffbe | 486 | inp_stream->m_read_bytes = 0; |
a324a7bc | 487 | |
48f7ffbe WS |
488 | Notify(false); |
489 | SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL); | |
9a1b2c28 | 490 | |
730b772b FM |
491 | // no error; reset m_lastError |
492 | m_lastError = wxPROTO_NOERR; | |
48f7ffbe | 493 | return inp_stream; |
f4ada568 | 494 | } |
35a4dab7 | 495 | |
a5d46b73 | 496 | #endif // wxUSE_PROTOCOL_HTTP |