]> git.saurik.com Git - wxWidgets.git/blob - src/common/http.cpp
Watcom compilation fix
[wxWidgets.git] / src / common / http.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "http.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_PROTOCOL_HTTP
24
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"
31 #include "wx/url.h"
32 #include "wx/protocol/http.h"
33 #include "wx/sckstrm.h"
34
35 IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
36 IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), TRUE)
37
38 #define HTTP_BSIZE 2048
39
40 wxHTTP::wxHTTP()
41 : wxProtocol()
42 {
43 m_addr = NULL;
44 m_read = FALSE;
45 m_proxy_mode = FALSE;
46 m_post_buf = wxEmptyString;
47
48 SetNotify(wxSOCKET_LOST_FLAG);
49 }
50
51 wxHTTP::~wxHTTP()
52 {
53 ClearHeaders();
54
55 delete m_addr;
56 }
57
58 void wxHTTP::ClearHeaders()
59 {
60 m_headers.clear();
61 }
62
63 wxString wxHTTP::GetContentType()
64 {
65 return GetHeader(wxT("Content-Type"));
66 }
67
68 void wxHTTP::SetProxyMode(bool on)
69 {
70 m_proxy_mode = on;
71 }
72
73 wxHTTP::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
88 void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
89 {
90 if (m_read) {
91 ClearHeaders();
92 m_read = FALSE;
93 }
94
95 wxHeaderIterator it = FindHeader(header);
96 if (it != m_headers.end())
97 it->second = h_data;
98 else
99 m_headers[header] = h_data;
100 }
101
102 wxString wxHTTP::GetHeader(const wxString& header) const
103 {
104 wxHeaderIterator it = FindHeader(header);
105
106 return it == m_headers.end() ? wxGetEmptyString() : it->second;
107 }
108
109 void wxHTTP::SetPostBuffer(const wxString& post_buf)
110 {
111 m_post_buf = post_buf;
112 }
113
114 void wxHTTP::SendHeaders()
115 {
116 typedef wxStringToStringHashMap::iterator iterator;
117 wxString buf;
118
119 for (iterator it = m_headers.begin(), en = m_headers.end(); it != en; ++it )
120 {
121 buf.Printf(wxT("%s: %s\r\n"), it->first.c_str(), it->second.c_str());
122
123 const wxWX2MBbuf cbuf = buf.mb_str();
124 Write(cbuf, strlen(cbuf));
125 }
126 }
127
128 bool wxHTTP::ParseHeaders()
129 {
130 wxString line;
131 wxStringTokenizer tokenzr;
132
133 ClearHeaders();
134 m_read = TRUE;
135
136 #if defined(__VISAGECPP__)
137 // VA just can't stand while(1)
138 bool bOs2var = TRUE;
139 while(bOs2var)
140 #else
141 while (1)
142 #endif
143 {
144 m_perr = GetLine(this, line);
145 if (m_perr != wxPROTO_NOERR)
146 return FALSE;
147
148 if (line.Length() == 0)
149 break;
150
151 wxString left_str = line.BeforeFirst(':');
152 m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
153 }
154 return TRUE;
155 }
156
157 bool wxHTTP::Connect(const wxString& host, unsigned short port)
158 {
159 wxIPV4address *addr;
160
161 if (m_addr) {
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;
172 m_perr = wxPROTO_NETERR;
173 return FALSE;
174 }
175
176 if ( port ) addr->Service(port);
177 else if (!addr->Service(wxT("http")))
178 addr->Service(80);
179
180 SetHeader(wxT("Host"), host);
181
182 return TRUE;
183 }
184
185 bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
186 {
187 if (m_addr) {
188 delete m_addr;
189 Close();
190 }
191
192 m_addr = addr.Clone();
193
194 wxIPV4address *ipv4addr = wxDynamicCast(&addr, wxIPV4address);
195 if (ipv4addr)
196 SetHeader(wxT("Host"), ipv4addr->OrigHostname());
197
198 return TRUE;
199 }
200
201 bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
202 {
203 const wxChar *request;
204
205 switch (req) {
206 case wxHTTP_GET:
207 request = wxT("GET");
208 break;
209 case wxHTTP_POST:
210 request = wxT("POST");
211 break;
212 default:
213 return FALSE;
214 }
215
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
220 SaveState();
221 SetFlags(wxSOCKET_NONE);
222 Notify(FALSE);
223
224 wxString buf;
225 buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str());
226 const wxWX2MBbuf pathbuf = wxConvLocal.cWX2MB(buf);
227 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
228 SendHeaders();
229 Write("\r\n", 2);
230
231 if ( req == wxHTTP_POST ) {
232 Write(m_post_buf, m_post_buf.Len());
233 m_post_buf = wxEmptyString;
234 }
235
236 wxString tmp_str;
237 m_perr = GetLine(this, tmp_str);
238 if (m_perr != wxPROTO_NOERR) {
239 RestoreState();
240 return FALSE;
241 }
242
243 if (!tmp_str.Contains(wxT("HTTP/"))) {
244 // TODO: support HTTP v0.9 which can have no header.
245 // FIXME: tmp_str is not put back in the in-queue of the socket.
246 SetHeader(wxT("Content-Length"), wxT("-1"));
247 SetHeader(wxT("Content-Type"), wxT("none/none"));
248 RestoreState();
249 return TRUE;
250 }
251
252 wxStringTokenizer token(tmp_str,wxT(' '));
253 wxString tmp_str2;
254 bool ret_value;
255
256 token.NextToken();
257 tmp_str2 = token.NextToken();
258
259 switch (tmp_str2[0u]) {
260 case wxT('1'):
261 /* INFORMATION / SUCCESS */
262 break;
263 case wxT('2'):
264 /* SUCCESS */
265 break;
266 case wxT('3'):
267 /* REDIRECTION */
268 break;
269 default:
270 m_perr = wxPROTO_NOFILE;
271 RestoreState();
272 return FALSE;
273 }
274
275 ret_value = ParseHeaders();
276 RestoreState();
277 return ret_value;
278 }
279
280 class wxHTTPStream : public wxSocketInputStream
281 {
282 public:
283 wxHTTP *m_http;
284 size_t m_httpsize;
285 unsigned long m_read_bytes;
286
287 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
288 size_t GetSize() const { return m_httpsize; }
289 virtual ~wxHTTPStream(void) { m_http->Abort(); }
290
291 protected:
292 size_t OnSysRead(void *buffer, size_t bufsize);
293
294 DECLARE_NO_COPY_CLASS(wxHTTPStream)
295 };
296
297 size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
298 {
299 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
300 {
301 m_lasterror = wxSTREAM_EOF;
302 return 0;
303 }
304
305 size_t ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
306 m_read_bytes += ret;
307
308 return ret;
309 }
310
311 bool wxHTTP::Abort(void)
312 {
313 return wxSocketClient::Close();
314 }
315
316 wxInputStream *wxHTTP::GetInputStream(const wxString& path)
317 {
318 wxHTTPStream *inp_stream;
319
320 wxString new_path;
321
322 m_perr = wxPROTO_CONNERR;
323 if (!m_addr)
324 return NULL;
325
326 // We set m_connected back to FALSE so wxSocketBase will know what to do.
327 #ifdef __WXMAC__
328 wxSocketClient::Connect(*m_addr , FALSE );
329 wxSocketClient::WaitOnConnect(10);
330
331 if (!wxSocketClient::IsConnected())
332 return NULL;
333 #else
334 if (!wxProtocol::Connect(*m_addr))
335 return NULL;
336 #endif
337
338 if (!BuildRequest(path, m_post_buf.IsEmpty() ? wxHTTP_GET : wxHTTP_POST))
339 return NULL;
340
341 inp_stream = new wxHTTPStream(this);
342
343 if (!GetHeader(wxT("Content-Length")).IsEmpty())
344 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
345 else
346 inp_stream->m_httpsize = (size_t)-1;
347
348 inp_stream->m_read_bytes = 0;
349
350 Notify(FALSE);
351 SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
352
353 return inp_stream;
354 }
355
356 #endif // wxUSE_PROTOCOL_HTTP
357