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