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