]>
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 licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
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 | m_http_response = 0; | |
48 | ||
49 | SetNotify(wxSOCKET_LOST_FLAG); | |
50 | } | |
51 | ||
52 | wxHTTP::~wxHTTP() | |
53 | { | |
54 | ClearHeaders(); | |
55 | ||
56 | delete m_addr; | |
57 | } | |
58 | ||
59 | void wxHTTP::ClearHeaders() | |
60 | { | |
61 | m_headers.clear(); | |
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 | wxHTTP::wxHeaderIterator wxHTTP::FindHeader(const wxString& header) | |
75 | { | |
76 | wxHeaderIterator it = m_headers.begin(); | |
77 | for ( wxHeaderIterator en = m_headers.end(); it != en; ++it ) | |
78 | { | |
79 | if ( wxStricmp(it->first, header) == 0 ) | |
80 | break; | |
81 | } | |
82 | ||
83 | return it; | |
84 | } | |
85 | ||
86 | wxHTTP::wxHeaderConstIterator wxHTTP::FindHeader(const wxString& header) const | |
87 | { | |
88 | wxHeaderConstIterator it = m_headers.begin(); | |
89 | for ( wxHeaderConstIterator en = m_headers.end(); it != en; ++it ) | |
90 | { | |
91 | if ( wxStricmp(it->first, header) == 0 ) | |
92 | break; | |
93 | } | |
94 | ||
95 | return it; | |
96 | } | |
97 | ||
98 | void wxHTTP::SetHeader(const wxString& header, const wxString& h_data) | |
99 | { | |
100 | if (m_read) { | |
101 | ClearHeaders(); | |
102 | m_read = FALSE; | |
103 | } | |
104 | ||
105 | wxHeaderIterator it = FindHeader(header); | |
106 | if (it != m_headers.end()) | |
107 | it->second = h_data; | |
108 | else | |
109 | m_headers[header] = h_data; | |
110 | } | |
111 | ||
112 | wxString wxHTTP::GetHeader(const wxString& header) const | |
113 | { | |
114 | wxHeaderConstIterator it = FindHeader(header); | |
115 | ||
116 | return it == m_headers.end() ? wxGetEmptyString() : it->second; | |
117 | } | |
118 | ||
119 | void wxHTTP::SetPostBuffer(const wxString& post_buf) | |
120 | { | |
121 | m_post_buf = post_buf; | |
122 | } | |
123 | ||
124 | void wxHTTP::SendHeaders() | |
125 | { | |
126 | typedef wxStringToStringHashMap::iterator iterator; | |
127 | wxString buf; | |
128 | ||
129 | for (iterator it = m_headers.begin(), en = m_headers.end(); it != en; ++it ) | |
130 | { | |
131 | buf.Printf(wxT("%s: %s\r\n"), it->first.c_str(), it->second.c_str()); | |
132 | ||
133 | const wxWX2MBbuf cbuf = buf.mb_str(); | |
134 | Write(cbuf, strlen(cbuf)); | |
135 | } | |
136 | } | |
137 | ||
138 | bool wxHTTP::ParseHeaders() | |
139 | { | |
140 | wxString line; | |
141 | wxStringTokenizer tokenzr; | |
142 | ||
143 | ClearHeaders(); | |
144 | m_read = TRUE; | |
145 | ||
146 | #if defined(__VISAGECPP__) | |
147 | // VA just can't stand while(1) | |
148 | bool bOs2var = TRUE; | |
149 | while(bOs2var) | |
150 | #else | |
151 | while (1) | |
152 | #endif | |
153 | { | |
154 | m_perr = GetLine(this, line); | |
155 | if (m_perr != wxPROTO_NOERR) | |
156 | return FALSE; | |
157 | ||
158 | if (line.Length() == 0) | |
159 | break; | |
160 | ||
161 | wxString left_str = line.BeforeFirst(':'); | |
162 | m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both); | |
163 | } | |
164 | return TRUE; | |
165 | } | |
166 | ||
167 | bool wxHTTP::Connect(const wxString& host, unsigned short port) | |
168 | { | |
169 | wxIPV4address *addr; | |
170 | ||
171 | if (m_addr) { | |
172 | delete m_addr; | |
173 | m_addr = NULL; | |
174 | Close(); | |
175 | } | |
176 | ||
177 | m_addr = addr = new wxIPV4address(); | |
178 | ||
179 | if (!addr->Hostname(host)) { | |
180 | delete m_addr; | |
181 | m_addr = NULL; | |
182 | m_perr = wxPROTO_NETERR; | |
183 | return FALSE; | |
184 | } | |
185 | ||
186 | if ( port ) addr->Service(port); | |
187 | else if (!addr->Service(wxT("http"))) | |
188 | addr->Service(80); | |
189 | ||
190 | SetHeader(wxT("Host"), host); | |
191 | ||
192 | return TRUE; | |
193 | } | |
194 | ||
195 | bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait)) | |
196 | { | |
197 | if (m_addr) { | |
198 | delete m_addr; | |
199 | Close(); | |
200 | } | |
201 | ||
202 | m_addr = addr.Clone(); | |
203 | ||
204 | wxIPV4address *ipv4addr = wxDynamicCast(&addr, wxIPV4address); | |
205 | if (ipv4addr) | |
206 | SetHeader(wxT("Host"), ipv4addr->OrigHostname()); | |
207 | ||
208 | return TRUE; | |
209 | } | |
210 | ||
211 | bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req) | |
212 | { | |
213 | const wxChar *request; | |
214 | ||
215 | switch (req) { | |
216 | case wxHTTP_GET: | |
217 | request = wxT("GET"); | |
218 | break; | |
219 | case wxHTTP_POST: | |
220 | request = wxT("POST"); | |
221 | break; | |
222 | default: | |
223 | return FALSE; | |
224 | } | |
225 | ||
226 | m_http_response = 0; | |
227 | ||
228 | // If there is no User-Agent defined, define it. | |
229 | if (GetHeader(wxT("User-Agent")).IsNull()) | |
230 | SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x")); | |
231 | ||
232 | SaveState(); | |
233 | SetFlags(wxSOCKET_NONE); | |
234 | Notify(FALSE); | |
235 | ||
236 | wxString buf; | |
237 | buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str()); | |
238 | const wxWX2MBbuf pathbuf = wxConvLocal.cWX2MB(buf); | |
239 | Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf)); | |
240 | SendHeaders(); | |
241 | Write("\r\n", 2); | |
242 | ||
243 | if ( req == wxHTTP_POST ) { | |
244 | Write(m_post_buf, m_post_buf.Len()); | |
245 | m_post_buf = wxEmptyString; | |
246 | } | |
247 | ||
248 | wxString tmp_str; | |
249 | m_perr = GetLine(this, tmp_str); | |
250 | if (m_perr != wxPROTO_NOERR) { | |
251 | RestoreState(); | |
252 | return FALSE; | |
253 | } | |
254 | ||
255 | if (!tmp_str.Contains(wxT("HTTP/"))) { | |
256 | // TODO: support HTTP v0.9 which can have no header. | |
257 | // FIXME: tmp_str is not put back in the in-queue of the socket. | |
258 | SetHeader(wxT("Content-Length"), wxT("-1")); | |
259 | SetHeader(wxT("Content-Type"), wxT("none/none")); | |
260 | RestoreState(); | |
261 | return TRUE; | |
262 | } | |
263 | ||
264 | wxStringTokenizer token(tmp_str,wxT(' ')); | |
265 | wxString tmp_str2; | |
266 | bool ret_value; | |
267 | ||
268 | token.NextToken(); | |
269 | tmp_str2 = token.NextToken(); | |
270 | ||
271 | m_http_response = wxAtoi(tmp_str2); | |
272 | ||
273 | switch (tmp_str2[0u]) { | |
274 | case wxT('1'): | |
275 | /* INFORMATION / SUCCESS */ | |
276 | break; | |
277 | case wxT('2'): | |
278 | /* SUCCESS */ | |
279 | break; | |
280 | case wxT('3'): | |
281 | /* REDIRECTION */ | |
282 | break; | |
283 | default: | |
284 | m_perr = wxPROTO_NOFILE; | |
285 | RestoreState(); | |
286 | return FALSE; | |
287 | } | |
288 | ||
289 | ret_value = ParseHeaders(); | |
290 | RestoreState(); | |
291 | return ret_value; | |
292 | } | |
293 | ||
294 | class wxHTTPStream : public wxSocketInputStream | |
295 | { | |
296 | public: | |
297 | wxHTTP *m_http; | |
298 | size_t m_httpsize; | |
299 | unsigned long m_read_bytes; | |
300 | ||
301 | wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {} | |
302 | size_t GetSize() const { return m_httpsize; } | |
303 | virtual ~wxHTTPStream(void) { m_http->Abort(); } | |
304 | ||
305 | protected: | |
306 | size_t OnSysRead(void *buffer, size_t bufsize); | |
307 | ||
308 | DECLARE_NO_COPY_CLASS(wxHTTPStream) | |
309 | }; | |
310 | ||
311 | size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize) | |
312 | { | |
313 | if (m_httpsize > 0 && m_read_bytes >= m_httpsize) | |
314 | { | |
315 | m_lasterror = wxSTREAM_EOF; | |
316 | return 0; | |
317 | } | |
318 | ||
319 | size_t ret = wxSocketInputStream::OnSysRead(buffer, bufsize); | |
320 | m_read_bytes += ret; | |
321 | ||
322 | return ret; | |
323 | } | |
324 | ||
325 | bool wxHTTP::Abort(void) | |
326 | { | |
327 | return wxSocketClient::Close(); | |
328 | } | |
329 | ||
330 | wxInputStream *wxHTTP::GetInputStream(const wxString& path) | |
331 | { | |
332 | wxHTTPStream *inp_stream; | |
333 | ||
334 | wxString new_path; | |
335 | ||
336 | m_perr = wxPROTO_CONNERR; | |
337 | if (!m_addr) | |
338 | return NULL; | |
339 | ||
340 | // We set m_connected back to FALSE so wxSocketBase will know what to do. | |
341 | #ifdef __WXMAC__ | |
342 | wxSocketClient::Connect(*m_addr , FALSE ); | |
343 | wxSocketClient::WaitOnConnect(10); | |
344 | ||
345 | if (!wxSocketClient::IsConnected()) | |
346 | return NULL; | |
347 | #else | |
348 | if (!wxProtocol::Connect(*m_addr)) | |
349 | return NULL; | |
350 | #endif | |
351 | ||
352 | if (!BuildRequest(path, m_post_buf.IsEmpty() ? wxHTTP_GET : wxHTTP_POST)) | |
353 | return NULL; | |
354 | ||
355 | inp_stream = new wxHTTPStream(this); | |
356 | ||
357 | if (!GetHeader(wxT("Content-Length")).IsEmpty()) | |
358 | inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length"))); | |
359 | else | |
360 | inp_stream->m_httpsize = (size_t)-1; | |
361 | ||
362 | inp_stream->m_read_bytes = 0; | |
363 | ||
364 | Notify(FALSE); | |
365 | SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL); | |
366 | ||
367 | return inp_stream; | |
368 | } | |
369 | ||
370 | #endif // wxUSE_PROTOCOL_HTTP | |
371 |