Added wxInternetFilesystemModule to fs_inet.cpp
[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, _T("http"), _T("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(_T("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(_T("%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 while (1) {
134 m_perr = GetLine(this, line);
135 if (m_perr != wxPROTO_NOERR)
136 return FALSE;
137
138 if (line.Length() == 0)
139 break;
140
141 tokenzr.SetString(line, " :\t\n\r");
142 if (!tokenzr.HasMoreTokens())
143 return FALSE;
144
145 wxString left_str = tokenzr.GetNextToken();
146 wxString *str = new wxString(tokenzr.GetNextToken());
147
148 left_str.MakeUpper();
149
150 m_headers.Append(left_str, (wxObject *) str);
151 }
152 return TRUE;
153 }
154
155 bool wxHTTP::Connect(const wxString& host)
156 {
157 wxIPV4address *addr;
158
159 if (m_addr) {
160 delete m_addr;
161 m_addr = NULL;
162 Close();
163 }
164
165 m_addr = addr = new wxIPV4address();
166
167 if (!addr->Hostname(host)) {
168 delete m_addr;
169 m_addr = NULL;
170 m_perr = wxPROTO_NETERR;
171 return FALSE;
172 }
173
174 if (!addr->Service(_T("http")))
175 addr->Service(80);
176
177 return TRUE;
178 }
179
180 bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
181 {
182 if (m_addr) {
183 delete m_addr;
184 m_addr = NULL;
185 Close();
186 }
187
188 m_addr = (wxSockAddress *) addr.Clone();
189 return TRUE;
190 }
191
192 bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
193 {
194 wxChar *tmp_buf;
195 wxChar buf[200]; // 200 is arbitrary.
196 wxString tmp_str = path;
197
198 switch (req) {
199 case wxHTTP_GET:
200 tmp_buf = _T("GET");
201 break;
202 default:
203 return FALSE;
204 }
205
206 SaveState();
207 SetFlags(NONE);
208 Notify(FALSE);
209
210 wxSprintf(buf, _T("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData());
211 const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf);
212 Write(pathbuf, strlen(MBSTRINGCAST pathbuf));
213 SendHeaders();
214 Write("\n\r", 2);
215
216 m_perr = GetLine(this, tmp_str);
217 if (m_perr != wxPROTO_NOERR) {
218 RestoreState();
219 return FALSE;
220 }
221
222 if (!tmp_str.Contains(_T("HTTP/"))) {
223 // TODO: support HTTP v0.9 which can have no header.
224 SetHeader(_T("Content-Length"), _T("-1"));
225 SetHeader(_T("Content-Type"), _T("none/none"));
226 RestoreState();
227 return TRUE;
228 }
229
230 wxStringTokenizer token(tmp_str,_T(' '));
231 wxString tmp_str2;
232 bool ret_value;
233
234 token.NextToken();
235 tmp_str2 = token.NextToken();
236
237 switch (wxAtoi(tmp_str2)) {
238 case 200:
239 break;
240 default:
241 m_perr = wxPROTO_NOFILE;
242 RestoreState();
243 return FALSE;
244 }
245
246 ret_value = ParseHeaders();
247 RestoreState();
248 return ret_value;
249 }
250
251 class wxHTTPStream : public wxSocketInputStream {
252 public:
253 wxHTTP *m_http;
254 size_t m_httpsize;
255 unsigned long m_read_bytes;
256
257 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
258 size_t GetSize() const { return m_httpsize; }
259 virtual ~wxHTTPStream(void) { m_http->Abort(); }
260
261 protected:
262 size_t OnSysRead(void *buffer, size_t bufsize);
263 };
264
265 size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
266 {
267 size_t ret;
268
269 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
270 return 0;
271
272 ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
273 m_read_bytes += ret;
274
275 return ret;
276 }
277
278 bool wxHTTP::Abort(void)
279 {
280 bool ret, connected;
281
282 ret = wxSocketClient::Close();
283
284 return ret;
285 }
286
287 wxInputStream *wxHTTP::GetInputStream(const wxString& path)
288 {
289 wxHTTPStream *inp_stream = new wxHTTPStream(this);
290 wxString new_path;
291
292 m_perr = wxPROTO_CONNERR;
293 if (!m_addr)
294 return NULL;
295
296 // We set m_connected back to FALSE so wxSocketBase will know what to do.
297 if (!wxProtocol::Connect(*m_addr))
298 return NULL;
299
300 if (!BuildRequest(path, wxHTTP_GET))
301 return NULL;
302
303 if (!GetHeader(_T("Content-Length")).IsEmpty())
304 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(_T("Content-Length")));
305 else
306 inp_stream->m_httpsize = (size_t)-1;
307
308 inp_stream->m_read_bytes = 0;
309
310 Notify(FALSE);
311 SetFlags(SPEED | WAITALL);
312
313 return inp_stream;
314 }
315
316 #endif
317 // wxUSE_SOCKETS