]> git.saurik.com Git - wxWidgets.git/blob - src/common/http.cpp
added default ctor for wxDirDialog
[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 license
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_SOCKETS
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 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
37 IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), TRUE)
38 #endif
39
40 #define HTTP_BSIZE 2048
41
42 wxHTTP::wxHTTP()
43 : wxProtocol(),
44 m_headers(wxKEY_STRING)
45 {
46 m_addr = NULL;
47 m_read = FALSE;
48 m_proxy_mode = FALSE;
49
50 SetNotify(GSOCK_LOST_FLAG);
51 }
52
53 wxHTTP::~wxHTTP()
54 {
55 // wxString isn't a wxObject
56 wxNode *node = m_headers.First();
57 wxString *string;
58
59 while (node) {
60 string = (wxString *)node->Data();
61 delete string;
62 node = node->Next();
63 }
64 }
65
66 wxString wxHTTP::GetContentType()
67 {
68 return GetHeader(wxT("Content-Type"));
69 }
70
71 void wxHTTP::SetProxyMode(bool on)
72 {
73 m_proxy_mode = on;
74 }
75
76 void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
77 {
78 if (m_read) {
79 m_headers.Clear();
80 m_read = FALSE;
81 }
82
83 wxNode *node = m_headers.Find(header);
84
85 if (!node)
86 m_headers.Append(header, (wxObject *)(new wxString(h_data)));
87 else {
88 wxString *str = (wxString *)node->Data();
89 (*str) = h_data;
90 }
91 }
92
93 wxString wxHTTP::GetHeader(const wxString& header)
94 {
95 wxNode *node;
96 wxString upper_header;
97
98 upper_header = header.Upper();
99
100 node = m_headers.Find(upper_header);
101 if (!node)
102 return wxEmptyString;
103
104 return *((wxString *)node->Data());
105 }
106
107 void wxHTTP::SendHeaders()
108 {
109 wxNode *head = m_headers.First();
110
111 while (head)
112 {
113 wxString *str = (wxString *)head->Data();
114
115 wxString buf;
116 buf.Printf(wxT("%s: %s\n\r"), head->GetKeyString(), str->GetData());
117
118 const wxWX2MBbuf cbuf = buf.mb_str();
119 Write(cbuf, strlen(cbuf));
120
121 head = head->Next();
122 }
123 }
124
125 bool wxHTTP::ParseHeaders()
126 {
127 wxString line;
128 wxStringTokenizer tokenzr;
129
130 m_headers.Clear();
131 m_read = TRUE;
132
133 #if defined(__VISAGECPP__)
134 // VA just can't stand while(1)
135 bool bOs2var = TRUE;
136 while(bOs2var) {
137 #else
138 while (1) {
139 #endif
140 m_perr = GetLine(this, line);
141 if (m_perr != wxPROTO_NOERR)
142 return FALSE;
143
144 if (line.Length() == 0)
145 break;
146
147 tokenzr.SetString(line, " :\t\n\r");
148 if (!tokenzr.HasMoreTokens())
149 return FALSE;
150
151 wxString left_str = tokenzr.GetNextToken();
152 wxString *str = new wxString(tokenzr.GetNextToken());
153
154 left_str.MakeUpper();
155
156 m_headers.Append(left_str, (wxObject *) str);
157 }
158 return TRUE;
159 }
160
161 bool wxHTTP::Connect(const wxString& host)
162 {
163 wxIPV4address *addr;
164
165 if (m_addr) {
166 delete m_addr;
167 m_addr = NULL;
168 Close();
169 }
170
171 m_addr = addr = new wxIPV4address();
172
173 if (!addr->Hostname(host)) {
174 delete m_addr;
175 m_addr = NULL;
176 m_perr = wxPROTO_NETERR;
177 return FALSE;
178 }
179
180 if (!addr->Service(wxT("http")))
181 addr->Service(80);
182
183 return TRUE;
184 }
185
186 bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
187 {
188 if (m_addr) {
189 delete m_addr;
190 m_addr = NULL;
191 Close();
192 }
193
194 m_addr = (wxSockAddress *) addr.Clone();
195 return TRUE;
196 }
197
198 bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
199 {
200 wxChar *tmp_buf;
201 wxChar buf[200]; // 200 is arbitrary.
202 wxString tmp_str = path;
203
204 // If there is no User-Agent defined, define it.
205 if (GetHeader(wxT("User-Agent")).IsNull())
206 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
207
208 switch (req) {
209 case wxHTTP_GET:
210 tmp_buf = wxT("GET");
211 break;
212 default:
213 return FALSE;
214 }
215
216 SaveState();
217 SetFlags(NONE);
218 Notify(FALSE);
219
220 wxSprintf(buf, wxT("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData());
221 const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf);
222 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
223 SendHeaders();
224 Write("\n\r", 2);
225
226 m_perr = GetLine(this, tmp_str);
227 if (m_perr != wxPROTO_NOERR) {
228 RestoreState();
229 return FALSE;
230 }
231
232 if (!tmp_str.Contains(wxT("HTTP/"))) {
233 // TODO: support HTTP v0.9 which can have no header.
234 // FIXME: tmp_str is not put back in the in-queue of the socket.
235 SetHeader(wxT("Content-Length"), wxT("-1"));
236 SetHeader(wxT("Content-Type"), wxT("none/none"));
237 RestoreState();
238 return TRUE;
239 }
240
241 wxStringTokenizer token(tmp_str,wxT(' '));
242 wxString tmp_str2;
243 bool ret_value;
244
245 token.NextToken();
246 tmp_str2 = token.NextToken();
247
248 switch (tmp_str2[(unsigned int) 0]) {
249 case wxT('1'):
250 /* INFORMATION / SUCCESS */
251 break;
252 case wxT('2'):
253 /* SUCCESS */
254 break;
255 case wxT('3'):
256 /* REDIRECTION */
257 break;
258 default:
259 m_perr = wxPROTO_NOFILE;
260 RestoreState();
261 return FALSE;
262 }
263
264 ret_value = ParseHeaders();
265 RestoreState();
266 return ret_value;
267 }
268
269 class wxHTTPStream : public wxSocketInputStream {
270 public:
271 wxHTTP *m_http;
272 size_t m_httpsize;
273 unsigned long m_read_bytes;
274
275 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
276 size_t GetSize() const { return m_httpsize; }
277 virtual ~wxHTTPStream(void) { m_http->Abort(); }
278
279 protected:
280 size_t OnSysRead(void *buffer, size_t bufsize);
281 };
282
283 size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
284 {
285 size_t ret;
286
287 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
288 return 0;
289
290 ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
291 m_read_bytes += ret;
292
293 return ret;
294 }
295
296 bool wxHTTP::Abort(void)
297 {
298 bool ret;
299
300 ret = wxSocketClient::Close();
301
302 return ret;
303 }
304
305 wxInputStream *wxHTTP::GetInputStream(const wxString& path)
306 {
307 wxHTTPStream *inp_stream = new wxHTTPStream(this);
308 wxString new_path;
309
310 m_perr = wxPROTO_CONNERR;
311 if (!m_addr)
312 return NULL;
313
314 // We set m_connected back to FALSE so wxSocketBase will know what to do.
315 if (!wxProtocol::Connect(*m_addr))
316 return NULL;
317
318 if (!BuildRequest(path, wxHTTP_GET))
319 return NULL;
320
321 if (!GetHeader(wxT("Content-Length")).IsEmpty())
322 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
323 else
324 inp_stream->m_httpsize = (size_t)-1;
325
326 inp_stream->m_read_bytes = 0;
327
328 Notify(FALSE);
329 SetFlags(SPEED | WAITALL);
330
331 return inp_stream;
332 }
333
334 #endif
335 // wxUSE_SOCKETS