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