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