]> git.saurik.com Git - wxWidgets.git/blame - src/common/http.cpp
fixed SetWindowStyleFlag() to not remove WS_VISIBLE; also refresh the control automat...
[wxWidgets.git] / src / common / http.cpp
CommitLineData
f4ada568
GL
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
65571936 9// Licence: wxWindows licence
f4ada568
GL
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
2df7be7f 13 #pragma implementation "http.h"
f4ada568
GL
14#endif
15
fcc6dddd
JS
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
ce4169a4 20 #pragma hdrstop
fcc6dddd
JS
21#endif
22
a5d46b73 23#if wxUSE_PROTOCOL_HTTP
ce4169a4 24
f4ada568
GL
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"
fae05df5 31#include "wx/url.h"
f4ada568
GL
32#include "wx/protocol/http.h"
33#include "wx/sckstrm.h"
34
f4ada568 35IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
5d3e7b52 36IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), true)
f4ada568
GL
37
38#define HTTP_BSIZE 2048
39
40wxHTTP::wxHTTP()
df5168c4 41 : wxProtocol()
f4ada568
GL
42{
43 m_addr = NULL;
5d3e7b52
WS
44 m_read = false;
45 m_proxy_mode = false;
f9133b32 46 m_post_buf = wxEmptyString;
2e622163 47 m_http_response = 0;
f4ada568 48
fc4b32c2 49 SetNotify(wxSOCKET_LOST_FLAG);
f4ada568
GL
50}
51
52wxHTTP::~wxHTTP()
2fb203e6
VZ
53{
54 ClearHeaders();
55
56 delete m_addr;
57}
58
59void wxHTTP::ClearHeaders()
f4ada568 60{
df5168c4 61 m_headers.clear();
f4ada568
GL
62}
63
64wxString wxHTTP::GetContentType()
65{
223d09f6 66 return GetHeader(wxT("Content-Type"));
f4ada568
GL
67}
68
f61815af
GL
69void wxHTTP::SetProxyMode(bool on)
70{
71 m_proxy_mode = on;
72}
73
bdcade0a 74wxHTTP::wxHeaderIterator wxHTTP::FindHeader(const wxString& header)
71414756 75{
bdcade0a
MB
76 wxHeaderIterator it = m_headers.begin();
77 for ( wxHeaderIterator en = m_headers.end(); it != en; ++it )
78 {
79 if ( wxStricmp(it->first, header) == 0 )
80 break;
81 }
71414756 82
bdcade0a
MB
83 return it;
84}
85
86wxHTTP::wxHeaderConstIterator wxHTTP::FindHeader(const wxString& header) const
87{
88 wxHeaderConstIterator it = m_headers.begin();
89 for ( wxHeaderConstIterator en = m_headers.end(); it != en; ++it )
71414756
VZ
90 {
91 if ( wxStricmp(it->first, header) == 0 )
92 break;
93 }
94
95 return it;
96}
97
f4ada568
GL
98void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
99{
100 if (m_read) {
2fb203e6 101 ClearHeaders();
5d3e7b52 102 m_read = false;
f4ada568
GL
103 }
104
71414756 105 wxHeaderIterator it = FindHeader(header);
df5168c4 106 if (it != m_headers.end())
71414756 107 it->second = h_data;
df5168c4 108 else
71414756 109 m_headers[header] = h_data;
f4ada568
GL
110}
111
71414756 112wxString wxHTTP::GetHeader(const wxString& header) const
f4ada568 113{
bdcade0a 114 wxHeaderConstIterator it = FindHeader(header);
f4ada568 115
01482482 116 return it == m_headers.end() ? wxGetEmptyString() : it->second;
f4ada568
GL
117}
118
f9133b32
VZ
119void wxHTTP::SetPostBuffer(const wxString& post_buf)
120{
71414756 121 m_post_buf = post_buf;
f9133b32
VZ
122}
123
f4ada568
GL
124void wxHTTP::SendHeaders()
125{
df5168c4
MB
126 typedef wxStringToStringHashMap::iterator iterator;
127 wxString buf;
f4ada568 128
df5168c4 129 for (iterator it = m_headers.begin(), en = m_headers.end(); it != en; ++it )
53c6e7cc 130 {
df5168c4 131 buf.Printf(wxT("%s: %s\r\n"), it->first.c_str(), it->second.c_str());
53c6e7cc 132
fde7d98e 133 const wxWX2MBbuf cbuf = buf.mb_str();
4846abaf 134 Write(cbuf, strlen(cbuf));
f4ada568
GL
135 }
136}
137
138bool wxHTTP::ParseHeaders()
139{
140 wxString line;
a737331d 141 wxStringTokenizer tokenzr;
f4ada568 142
2fb203e6 143 ClearHeaders();
5d3e7b52 144 m_read = true;
f4ada568 145
2ce0a6e2
DW
146#if defined(__VISAGECPP__)
147// VA just can't stand while(1)
5d3e7b52 148 bool bOs2var = true;
71414756 149 while(bOs2var)
2ce0a6e2 150#else
71414756 151 while (1)
2ce0a6e2 152#endif
71414756 153 {
a324a7bc
GL
154 m_perr = GetLine(this, line);
155 if (m_perr != wxPROTO_NOERR)
5d3e7b52 156 return false;
f4ada568
GL
157
158 if (line.Length() == 0)
159 break;
160
02244615 161 wxString left_str = line.BeforeFirst(':');
df5168c4 162 m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
f4ada568 163 }
5d3e7b52 164 return true;
f4ada568
GL
165}
166
f9133b32 167bool wxHTTP::Connect(const wxString& host, unsigned short port)
f4ada568
GL
168{
169 wxIPV4address *addr;
170
f61815af 171 if (m_addr) {
f4ada568
GL
172 delete m_addr;
173 m_addr = NULL;
174 Close();
175 }
176
177 m_addr = addr = new wxIPV4address();
178
179 if (!addr->Hostname(host)) {
180 delete m_addr;
181 m_addr = NULL;
a324a7bc 182 m_perr = wxPROTO_NETERR;
5d3e7b52 183 return false;
f4ada568
GL
184 }
185
f9133b32
VZ
186 if ( port ) addr->Service(port);
187 else if (!addr->Service(wxT("http")))
f4ada568 188 addr->Service(80);
ce22d615 189
5b96a71a 190 SetHeader(wxT("Host"), host);
f4ada568 191
5d3e7b52 192 return true;
f4ada568
GL
193}
194
8a2c6ef8 195bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
f4ada568 196{
a324a7bc
GL
197 if (m_addr) {
198 delete m_addr;
a324a7bc
GL
199 Close();
200 }
f4ada568 201
acd15a3f
VZ
202 m_addr = addr.Clone();
203
5b96a71a
VS
204 wxIPV4address *ipv4addr = wxDynamicCast(&addr, wxIPV4address);
205 if (ipv4addr)
ce22d615 206 SetHeader(wxT("Host"), ipv4addr->OrigHostname());
5b96a71a 207
5d3e7b52 208 return true;
f4ada568
GL
209}
210
211bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
212{
295272bd 213 const wxChar *request;
7e1e0960 214
f4ada568
GL
215 switch (req) {
216 case wxHTTP_GET:
295272bd 217 request = wxT("GET");
f4ada568 218 break;
f9133b32 219 case wxHTTP_POST:
ed831501 220 request = wxT("POST");
b8bafcaa 221 if ( GetHeader( wxT("Content-Length") ).IsNull() )
06014e4f 222 SetHeader( wxT("Content-Length"), wxString::Format( wxT("%lu"), (unsigned long)m_post_buf.Len() ) );
f9133b32 223 break;
f4ada568 224 default:
5d3e7b52 225 return false;
f4ada568
GL
226 }
227
2e622163
VZ
228 m_http_response = 0;
229
02244615
VZ
230 // If there is no User-Agent defined, define it.
231 if (GetHeader(wxT("User-Agent")).IsNull())
77ffb593 232 SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
02244615 233
41895a05 234 SaveState();
1411f7eb 235#if wxUSE_THREADS
b8bafcaa 236 SetFlags( wxThread::IsMain() ? wxSOCKET_NONE : wxSOCKET_BLOCK );
1411f7eb
VZ
237#else
238 SetFlags( wxSOCKET_NONE );
239#endif
5d3e7b52 240 Notify(false);
41895a05 241
02244615 242 wxString buf;
295272bd 243 buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str());
b1ac3b56 244 const wxWX2MBbuf pathbuf = wxConvLocal.cWX2MB(buf);
e90c1d2a 245 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
f4ada568 246 SendHeaders();
df4b7d98 247 Write("\r\n", 2);
f4ada568 248
f9133b32 249 if ( req == wxHTTP_POST ) {
b8bafcaa 250 Write(m_post_buf.mbc_str(), m_post_buf.Len());
f9133b32
VZ
251 m_post_buf = wxEmptyString;
252 }
253
02244615 254 wxString tmp_str;
a324a7bc
GL
255 m_perr = GetLine(this, tmp_str);
256 if (m_perr != wxPROTO_NOERR) {
41895a05 257 RestoreState();
5d3e7b52 258 return false;
41895a05 259 }
f4ada568 260
223d09f6 261 if (!tmp_str.Contains(wxT("HTTP/"))) {
f4ada568 262 // TODO: support HTTP v0.9 which can have no header.
7e1e0960 263 // FIXME: tmp_str is not put back in the in-queue of the socket.
223d09f6
KB
264 SetHeader(wxT("Content-Length"), wxT("-1"));
265 SetHeader(wxT("Content-Type"), wxT("none/none"));
41895a05 266 RestoreState();
5d3e7b52 267 return true;
f4ada568
GL
268 }
269
223d09f6 270 wxStringTokenizer token(tmp_str,wxT(' '));
f4ada568 271 wxString tmp_str2;
41895a05 272 bool ret_value;
f4ada568
GL
273
274 token.NextToken();
275 tmp_str2 = token.NextToken();
276
2e622163
VZ
277 m_http_response = wxAtoi(tmp_str2);
278
02244615 279 switch (tmp_str2[0u]) {
223d09f6 280 case wxT('1'):
7e1e0960
GL
281 /* INFORMATION / SUCCESS */
282 break;
223d09f6 283 case wxT('2'):
7e1e0960
GL
284 /* SUCCESS */
285 break;
223d09f6 286 case wxT('3'):
7e1e0960 287 /* REDIRECTION */
f4ada568
GL
288 break;
289 default:
a324a7bc 290 m_perr = wxPROTO_NOFILE;
41895a05 291 RestoreState();
5d3e7b52 292 return false;
f4ada568
GL
293 }
294
41895a05
GL
295 ret_value = ParseHeaders();
296 RestoreState();
297 return ret_value;
f4ada568
GL
298}
299
fc4b32c2
GRG
300class wxHTTPStream : public wxSocketInputStream
301{
f4ada568
GL
302public:
303 wxHTTP *m_http;
9a1b2c28 304 size_t m_httpsize;
a324a7bc 305 unsigned long m_read_bytes;
9a1b2c28 306
f4ada568 307 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
f61815af 308 size_t GetSize() const { return m_httpsize; }
f4ada568 309 virtual ~wxHTTPStream(void) { m_http->Abort(); }
a324a7bc
GL
310
311protected:
312 size_t OnSysRead(void *buffer, size_t bufsize);
22f3361e
VZ
313
314 DECLARE_NO_COPY_CLASS(wxHTTPStream)
f4ada568
GL
315};
316
a324a7bc
GL
317size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
318{
32013d27
VZ
319 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
320 {
321 m_lasterror = wxSTREAM_EOF;
322 return 0;
323 }
a324a7bc 324
32013d27
VZ
325 size_t ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
326 m_read_bytes += ret;
a324a7bc 327
32013d27 328 return ret;
a324a7bc
GL
329}
330
f4ada568
GL
331bool wxHTTP::Abort(void)
332{
fc4b32c2 333 return wxSocketClient::Close();
f4ada568
GL
334}
335
336wxInputStream *wxHTTP::GetInputStream(const wxString& path)
337{
8f5bda17
JS
338 wxHTTPStream *inp_stream;
339
f61815af 340 wxString new_path;
f4ada568 341
f61815af
GL
342 m_perr = wxPROTO_CONNERR;
343 if (!m_addr)
f4ada568 344 return NULL;
f4ada568 345
5d3e7b52 346 // We set m_connected back to false so wxSocketBase will know what to do.
ad8b8498 347#ifdef __WXMAC__
5d3e7b52 348 wxSocketClient::Connect(*m_addr , false );
ad8b8498
SC
349 wxSocketClient::WaitOnConnect(10);
350
351 if (!wxSocketClient::IsConnected())
352 return NULL;
353#else
f4ada568
GL
354 if (!wxProtocol::Connect(*m_addr))
355 return NULL;
ad8b8498 356#endif
f4ada568 357
f9133b32 358 if (!BuildRequest(path, m_post_buf.IsEmpty() ? wxHTTP_GET : wxHTTP_POST))
f4ada568
GL
359 return NULL;
360
8f5bda17
JS
361 inp_stream = new wxHTTPStream(this);
362
223d09f6
KB
363 if (!GetHeader(wxT("Content-Length")).IsEmpty())
364 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
a324a7bc
GL
365 else
366 inp_stream->m_httpsize = (size_t)-1;
367
368 inp_stream->m_read_bytes = 0;
369
5d3e7b52 370 Notify(false);
fc4b32c2 371 SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
9a1b2c28 372
f4ada568
GL
373 return inp_stream;
374}
35a4dab7 375
a5d46b73
VZ
376#endif // wxUSE_PROTOCOL_HTTP
377