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