]>
Commit | Line | Data |
---|---|---|
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 | 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(wxT("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 | // If there is no User-Agent defined, define it. | |
199 | if (GetHeader(wxT("User-Agent")).IsNull()) | |
200 | SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x")); | |
201 | ||
202 | switch (req) { | |
203 | case wxHTTP_GET: | |
204 | tmp_buf = wxT("GET"); | |
205 | break; | |
206 | default: | |
207 | return FALSE; | |
208 | } | |
209 | ||
210 | SaveState(); | |
211 | SetFlags(NONE); | |
212 | Notify(FALSE); | |
213 | ||
214 | wxSprintf(buf, wxT("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData()); | |
215 | const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf); | |
216 | Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf)); | |
217 | SendHeaders(); | |
218 | Write("\n\r", 2); | |
219 | ||
220 | m_perr = GetLine(this, tmp_str); | |
221 | if (m_perr != wxPROTO_NOERR) { | |
222 | RestoreState(); | |
223 | return FALSE; | |
224 | } | |
225 | ||
226 | if (!tmp_str.Contains(wxT("HTTP/"))) { | |
227 | // TODO: support HTTP v0.9 which can have no header. | |
228 | // FIXME: tmp_str is not put back in the in-queue of the socket. | |
229 | SetHeader(wxT("Content-Length"), wxT("-1")); | |
230 | SetHeader(wxT("Content-Type"), wxT("none/none")); | |
231 | RestoreState(); | |
232 | return TRUE; | |
233 | } | |
234 | ||
235 | wxStringTokenizer token(tmp_str,wxT(' ')); | |
236 | wxString tmp_str2; | |
237 | bool ret_value; | |
238 | ||
239 | token.NextToken(); | |
240 | tmp_str2 = token.NextToken(); | |
241 | ||
242 | switch (tmp_str2[(unsigned int) 0]) { | |
243 | case wxT('1'): | |
244 | /* INFORMATION / SUCCESS */ | |
245 | break; | |
246 | case wxT('2'): | |
247 | /* SUCCESS */ | |
248 | break; | |
249 | case wxT('3'): | |
250 | /* REDIRECTION */ | |
251 | break; | |
252 | default: | |
253 | m_perr = wxPROTO_NOFILE; | |
254 | RestoreState(); | |
255 | return FALSE; | |
256 | } | |
257 | ||
258 | ret_value = ParseHeaders(); | |
259 | RestoreState(); | |
260 | return ret_value; | |
261 | } | |
262 | ||
263 | class wxHTTPStream : public wxSocketInputStream { | |
264 | public: | |
265 | wxHTTP *m_http; | |
266 | size_t m_httpsize; | |
267 | unsigned long m_read_bytes; | |
268 | ||
269 | wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {} | |
270 | size_t GetSize() const { return m_httpsize; } | |
271 | virtual ~wxHTTPStream(void) { m_http->Abort(); } | |
272 | ||
273 | protected: | |
274 | size_t OnSysRead(void *buffer, size_t bufsize); | |
275 | }; | |
276 | ||
277 | size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize) | |
278 | { | |
279 | size_t ret; | |
280 | ||
281 | if (m_httpsize > 0 && m_read_bytes >= m_httpsize) | |
282 | return 0; | |
283 | ||
284 | ret = wxSocketInputStream::OnSysRead(buffer, bufsize); | |
285 | m_read_bytes += ret; | |
286 | ||
287 | return ret; | |
288 | } | |
289 | ||
290 | bool wxHTTP::Abort(void) | |
291 | { | |
292 | bool ret; | |
293 | ||
294 | ret = wxSocketClient::Close(); | |
295 | ||
296 | return ret; | |
297 | } | |
298 | ||
299 | wxInputStream *wxHTTP::GetInputStream(const wxString& path) | |
300 | { | |
301 | wxHTTPStream *inp_stream = new wxHTTPStream(this); | |
302 | wxString new_path; | |
303 | ||
304 | m_perr = wxPROTO_CONNERR; | |
305 | if (!m_addr) | |
306 | return NULL; | |
307 | ||
308 | // We set m_connected back to FALSE so wxSocketBase will know what to do. | |
309 | if (!wxProtocol::Connect(*m_addr)) | |
310 | return NULL; | |
311 | ||
312 | if (!BuildRequest(path, wxHTTP_GET)) | |
313 | return NULL; | |
314 | ||
315 | if (!GetHeader(wxT("Content-Length")).IsEmpty()) | |
316 | inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length"))); | |
317 | else | |
318 | inp_stream->m_httpsize = (size_t)-1; | |
319 | ||
320 | inp_stream->m_read_bytes = 0; | |
321 | ||
322 | Notify(FALSE); | |
323 | SetFlags(SPEED | WAITALL); | |
324 | ||
325 | return inp_stream; | |
326 | } | |
327 | ||
328 | #endif | |
329 | // wxUSE_SOCKETS |