]> git.saurik.com Git - wxWidgets.git/blame - src/common/http.cpp
font inheritance fixes (patch 983251)
[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)
223d09f6 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;
44 m_read = FALSE;
f61815af 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();
f4ada568
GL
102 m_read = FALSE;
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();
f4ada568
GL
144 m_read = TRUE;
145
2ce0a6e2
DW
146#if defined(__VISAGECPP__)
147// VA just can't stand while(1)
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)
f4ada568
GL
156 return FALSE;
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
GL
163 }
164 return TRUE;
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;
f4ada568
GL
183 return FALSE;
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
GL
191
192 return TRUE;
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
f4ada568
GL
208 return TRUE;
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");
f9133b32 221 break;
f4ada568
GL
222 default:
223 return FALSE;
224 }
225
2e622163
VZ
226 m_http_response = 0;
227
02244615
VZ
228 // If there is no User-Agent defined, define it.
229 if (GetHeader(wxT("User-Agent")).IsNull())
77ffb593 230 SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
02244615 231
41895a05 232 SaveState();
3f04dfd3 233 SetFlags(wxSOCKET_NONE);
41895a05 234 Notify(FALSE);
41895a05 235
02244615 236 wxString buf;
295272bd 237 buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str());
b1ac3b56 238 const wxWX2MBbuf pathbuf = wxConvLocal.cWX2MB(buf);
e90c1d2a 239 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
f4ada568 240 SendHeaders();
df4b7d98 241 Write("\r\n", 2);
f4ada568 242
f9133b32
VZ
243 if ( req == wxHTTP_POST ) {
244 Write(m_post_buf, m_post_buf.Len());
245 m_post_buf = wxEmptyString;
246 }
247
02244615 248 wxString tmp_str;
a324a7bc
GL
249 m_perr = GetLine(this, tmp_str);
250 if (m_perr != wxPROTO_NOERR) {
41895a05 251 RestoreState();
f4ada568 252 return FALSE;
41895a05 253 }
f4ada568 254
223d09f6 255 if (!tmp_str.Contains(wxT("HTTP/"))) {
f4ada568 256 // TODO: support HTTP v0.9 which can have no header.
7e1e0960 257 // FIXME: tmp_str is not put back in the in-queue of the socket.
223d09f6
KB
258 SetHeader(wxT("Content-Length"), wxT("-1"));
259 SetHeader(wxT("Content-Type"), wxT("none/none"));
41895a05 260 RestoreState();
f4ada568
GL
261 return TRUE;
262 }
263
223d09f6 264 wxStringTokenizer token(tmp_str,wxT(' '));
f4ada568 265 wxString tmp_str2;
41895a05 266 bool ret_value;
f4ada568
GL
267
268 token.NextToken();
269 tmp_str2 = token.NextToken();
270
2e622163
VZ
271 m_http_response = wxAtoi(tmp_str2);
272
02244615 273 switch (tmp_str2[0u]) {
223d09f6 274 case wxT('1'):
7e1e0960
GL
275 /* INFORMATION / SUCCESS */
276 break;
223d09f6 277 case wxT('2'):
7e1e0960
GL
278 /* SUCCESS */
279 break;
223d09f6 280 case wxT('3'):
7e1e0960 281 /* REDIRECTION */
f4ada568
GL
282 break;
283 default:
a324a7bc 284 m_perr = wxPROTO_NOFILE;
41895a05 285 RestoreState();
f4ada568
GL
286 return FALSE;
287 }
288
41895a05
GL
289 ret_value = ParseHeaders();
290 RestoreState();
291 return ret_value;
f4ada568
GL
292}
293
fc4b32c2
GRG
294class wxHTTPStream : public wxSocketInputStream
295{
f4ada568
GL
296public:
297 wxHTTP *m_http;
9a1b2c28 298 size_t m_httpsize;
a324a7bc 299 unsigned long m_read_bytes;
9a1b2c28 300
f4ada568 301 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
f61815af 302 size_t GetSize() const { return m_httpsize; }
f4ada568 303 virtual ~wxHTTPStream(void) { m_http->Abort(); }
a324a7bc
GL
304
305protected:
306 size_t OnSysRead(void *buffer, size_t bufsize);
22f3361e
VZ
307
308 DECLARE_NO_COPY_CLASS(wxHTTPStream)
f4ada568
GL
309};
310
a324a7bc
GL
311size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
312{
32013d27
VZ
313 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
314 {
315 m_lasterror = wxSTREAM_EOF;
316 return 0;
317 }
a324a7bc 318
32013d27
VZ
319 size_t ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
320 m_read_bytes += ret;
a324a7bc 321
32013d27 322 return ret;
a324a7bc
GL
323}
324
f4ada568
GL
325bool wxHTTP::Abort(void)
326{
fc4b32c2 327 return wxSocketClient::Close();
f4ada568
GL
328}
329
330wxInputStream *wxHTTP::GetInputStream(const wxString& path)
331{
8f5bda17
JS
332 wxHTTPStream *inp_stream;
333
f61815af 334 wxString new_path;
f4ada568 335
f61815af
GL
336 m_perr = wxPROTO_CONNERR;
337 if (!m_addr)
f4ada568 338 return NULL;
f4ada568 339
f61815af 340 // We set m_connected back to FALSE so wxSocketBase will know what to do.
ad8b8498
SC
341#ifdef __WXMAC__
342 wxSocketClient::Connect(*m_addr , FALSE );
343 wxSocketClient::WaitOnConnect(10);
344
345 if (!wxSocketClient::IsConnected())
346 return NULL;
347#else
f4ada568
GL
348 if (!wxProtocol::Connect(*m_addr))
349 return NULL;
ad8b8498 350#endif
f4ada568 351
f9133b32 352 if (!BuildRequest(path, m_post_buf.IsEmpty() ? wxHTTP_GET : wxHTTP_POST))
f4ada568
GL
353 return NULL;
354
8f5bda17
JS
355 inp_stream = new wxHTTPStream(this);
356
223d09f6
KB
357 if (!GetHeader(wxT("Content-Length")).IsEmpty())
358 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
a324a7bc
GL
359 else
360 inp_stream->m_httpsize = (size_t)-1;
361
362 inp_stream->m_read_bytes = 0;
363
364 Notify(FALSE);
fc4b32c2 365 SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
9a1b2c28 366
f4ada568
GL
367 return inp_stream;
368}
35a4dab7 369
a5d46b73
VZ
370#endif // wxUSE_PROTOCOL_HTTP
371