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