]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
f4ada568 GL |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
fcc6dddd JS |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
ce4169a4 | 16 | #pragma hdrstop |
fcc6dddd JS |
17 | #endif |
18 | ||
a5d46b73 | 19 | #if wxUSE_PROTOCOL_HTTP |
ce4169a4 | 20 | |
f4ada568 GL |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
a8d628fc DS |
23 | |
24 | #ifndef WX_PRECOMP | |
f4ada568 | 25 | #include "wx/string.h" |
a8d628fc DS |
26 | #include "wx/app.h" |
27 | #endif | |
28 | ||
f4ada568 GL |
29 | #include "wx/tokenzr.h" |
30 | #include "wx/socket.h" | |
31 | #include "wx/protocol/protocol.h" | |
fae05df5 | 32 | #include "wx/url.h" |
f4ada568 GL |
33 | #include "wx/protocol/http.h" |
34 | #include "wx/sckstrm.h" | |
35 | ||
f4ada568 | 36 | IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol) |
5d3e7b52 | 37 | IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), true) |
f4ada568 | 38 | |
f4ada568 | 39 | wxHTTP::wxHTTP() |
df5168c4 | 40 | : wxProtocol() |
f4ada568 | 41 | { |
48f7ffbe WS |
42 | m_addr = NULL; |
43 | m_read = false; | |
44 | m_proxy_mode = false; | |
45 | m_post_buf = wxEmptyString; | |
46 | m_http_response = 0; | |
f4ada568 | 47 | |
48f7ffbe | 48 | SetNotify(wxSOCKET_LOST_FLAG); |
f4ada568 GL |
49 | } |
50 | ||
51 | wxHTTP::~wxHTTP() | |
2fb203e6 VZ |
52 | { |
53 | ClearHeaders(); | |
54 | ||
55 | delete m_addr; | |
56 | } | |
57 | ||
58 | void wxHTTP::ClearHeaders() | |
f4ada568 | 59 | { |
df5168c4 | 60 | m_headers.clear(); |
f4ada568 GL |
61 | } |
62 | ||
63 | wxString wxHTTP::GetContentType() | |
64 | { | |
48f7ffbe | 65 | return GetHeader(wxT("Content-Type")); |
f4ada568 GL |
66 | } |
67 | ||
f61815af GL |
68 | void wxHTTP::SetProxyMode(bool on) |
69 | { | |
48f7ffbe | 70 | m_proxy_mode = on; |
f61815af GL |
71 | } |
72 | ||
bdcade0a | 73 | wxHTTP::wxHeaderIterator wxHTTP::FindHeader(const wxString& header) |
71414756 | 74 | { |
bdcade0a MB |
75 | wxHeaderIterator it = m_headers.begin(); |
76 | for ( wxHeaderIterator en = m_headers.end(); it != en; ++it ) | |
77 | { | |
78 | if ( wxStricmp(it->first, header) == 0 ) | |
79 | break; | |
80 | } | |
71414756 | 81 | |
bdcade0a MB |
82 | return it; |
83 | } | |
84 | ||
85 | wxHTTP::wxHeaderConstIterator wxHTTP::FindHeader(const wxString& header) const | |
86 | { | |
87 | wxHeaderConstIterator it = m_headers.begin(); | |
88 | for ( wxHeaderConstIterator en = m_headers.end(); it != en; ++it ) | |
71414756 VZ |
89 | { |
90 | if ( wxStricmp(it->first, header) == 0 ) | |
91 | break; | |
92 | } | |
93 | ||
94 | return it; | |
95 | } | |
96 | ||
f4ada568 GL |
97 | void wxHTTP::SetHeader(const wxString& header, const wxString& h_data) |
98 | { | |
48f7ffbe WS |
99 | if (m_read) { |
100 | ClearHeaders(); | |
101 | m_read = false; | |
102 | } | |
f4ada568 | 103 | |
48f7ffbe WS |
104 | wxHeaderIterator it = FindHeader(header); |
105 | if (it != m_headers.end()) | |
106 | it->second = h_data; | |
107 | else | |
108 | m_headers[header] = h_data; | |
f4ada568 GL |
109 | } |
110 | ||
71414756 | 111 | wxString wxHTTP::GetHeader(const wxString& header) const |
f4ada568 | 112 | { |
bdcade0a | 113 | wxHeaderConstIterator it = FindHeader(header); |
f4ada568 | 114 | |
01482482 | 115 | return it == m_headers.end() ? wxGetEmptyString() : it->second; |
f4ada568 GL |
116 | } |
117 | ||
f9133b32 VZ |
118 | void wxHTTP::SetPostBuffer(const wxString& post_buf) |
119 | { | |
71414756 | 120 | m_post_buf = post_buf; |
f9133b32 VZ |
121 | } |
122 | ||
f4ada568 GL |
123 | void wxHTTP::SendHeaders() |
124 | { | |
48f7ffbe WS |
125 | typedef wxStringToStringHashMap::iterator iterator; |
126 | wxString buf; | |
f4ada568 | 127 | |
48f7ffbe WS |
128 | for (iterator it = m_headers.begin(), en = m_headers.end(); it != en; ++it ) |
129 | { | |
130 | buf.Printf(wxT("%s: %s\r\n"), it->first.c_str(), it->second.c_str()); | |
53c6e7cc | 131 | |
48f7ffbe WS |
132 | const wxWX2MBbuf cbuf = buf.mb_str(); |
133 | Write(cbuf, strlen(cbuf)); | |
134 | } | |
f4ada568 GL |
135 | } |
136 | ||
137 | bool wxHTTP::ParseHeaders() | |
138 | { | |
48f7ffbe WS |
139 | wxString line; |
140 | wxStringTokenizer tokenzr; | |
f4ada568 | 141 | |
48f7ffbe WS |
142 | ClearHeaders(); |
143 | m_read = true; | |
f4ada568 | 144 | |
2ce0a6e2 DW |
145 | #if defined(__VISAGECPP__) |
146 | // VA just can't stand while(1) | |
5d3e7b52 | 147 | bool bOs2var = true; |
71414756 | 148 | while(bOs2var) |
2ce0a6e2 | 149 | #else |
48f7ffbe | 150 | while (1) |
2ce0a6e2 | 151 | #endif |
48f7ffbe | 152 | { |
f3d0213e | 153 | m_perr = ReadLine(this, line); |
48f7ffbe WS |
154 | if (m_perr != wxPROTO_NOERR) |
155 | return false; | |
156 | ||
157 | if (line.Length() == 0) | |
158 | break; | |
159 | ||
160 | wxString left_str = line.BeforeFirst(':'); | |
161 | m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both); | |
162 | } | |
163 | return true; | |
f4ada568 GL |
164 | } |
165 | ||
f9133b32 | 166 | bool wxHTTP::Connect(const wxString& host, unsigned short port) |
f4ada568 | 167 | { |
48f7ffbe | 168 | wxIPV4address *addr; |
f4ada568 | 169 | |
48f7ffbe WS |
170 | if (m_addr) { |
171 | delete m_addr; | |
172 | m_addr = NULL; | |
173 | Close(); | |
174 | } | |
f4ada568 | 175 | |
48f7ffbe | 176 | m_addr = addr = new wxIPV4address(); |
f4ada568 | 177 | |
48f7ffbe WS |
178 | if (!addr->Hostname(host)) { |
179 | delete m_addr; | |
180 | m_addr = NULL; | |
181 | m_perr = wxPROTO_NETERR; | |
182 | return false; | |
183 | } | |
f4ada568 | 184 | |
48f7ffbe WS |
185 | if ( port ) |
186 | addr->Service(port); | |
187 | else if (!addr->Service(wxT("http"))) | |
188 | addr->Service(80); | |
ce22d615 | 189 | |
48f7ffbe | 190 | SetHeader(wxT("Host"), host); |
f4ada568 | 191 | |
48f7ffbe | 192 | return true; |
f4ada568 GL |
193 | } |
194 | ||
8a2c6ef8 | 195 | bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait)) |
f4ada568 | 196 | { |
48f7ffbe WS |
197 | if (m_addr) { |
198 | delete m_addr; | |
199 | Close(); | |
200 | } | |
f4ada568 | 201 | |
48f7ffbe | 202 | m_addr = addr.Clone(); |
acd15a3f | 203 | |
48f7ffbe WS |
204 | wxIPV4address *ipv4addr = wxDynamicCast(&addr, wxIPV4address); |
205 | if (ipv4addr) | |
206 | SetHeader(wxT("Host"), ipv4addr->OrigHostname()); | |
5b96a71a | 207 | |
48f7ffbe | 208 | return true; |
f4ada568 GL |
209 | } |
210 | ||
211 | bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req) | |
212 | { | |
48f7ffbe | 213 | const wxChar *request; |
f9133b32 | 214 | |
48f7ffbe WS |
215 | switch (req) |
216 | { | |
217 | case wxHTTP_GET: | |
218 | request = wxT("GET"); | |
219 | break; | |
220 | ||
221 | case wxHTTP_POST: | |
222 | request = wxT("POST"); | |
223 | if ( GetHeader( wxT("Content-Length") ).IsNull() ) | |
224 | SetHeader( wxT("Content-Length"), wxString::Format( wxT("%lu"), (unsigned long)m_post_buf.Len() ) ); | |
225 | break; | |
226 | ||
227 | default: | |
228 | return false; | |
229 | } | |
230 | ||
231 | m_http_response = 0; | |
232 | ||
233 | // If there is no User-Agent defined, define it. | |
234 | if (GetHeader(wxT("User-Agent")).IsNull()) | |
235 | SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x")); | |
236 | ||
237 | SaveState(); | |
238 | ||
239 | // we may use non blocking sockets only if we can dispatch events from them | |
240 | SetFlags( wxIsMainThread() && wxApp::IsMainLoopRunning() ? wxSOCKET_NONE | |
241 | : wxSOCKET_BLOCK ); | |
242 | Notify(false); | |
243 | ||
244 | wxString buf; | |
245 | buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str()); | |
246 | const wxWX2MBbuf pathbuf = wxConvLocal.cWX2MB(buf); | |
247 | Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf)); | |
248 | SendHeaders(); | |
249 | Write("\r\n", 2); | |
250 | ||
251 | if ( req == wxHTTP_POST ) { | |
252 | Write(m_post_buf.mbc_str(), m_post_buf.Len()); | |
253 | m_post_buf = wxEmptyString; | |
254 | } | |
255 | ||
256 | wxString tmp_str; | |
f3d0213e | 257 | m_perr = ReadLine(this, tmp_str); |
48f7ffbe WS |
258 | if (m_perr != wxPROTO_NOERR) { |
259 | RestoreState(); | |
260 | return false; | |
261 | } | |
262 | ||
263 | if (!tmp_str.Contains(wxT("HTTP/"))) { | |
264 | // TODO: support HTTP v0.9 which can have no header. | |
265 | // FIXME: tmp_str is not put back in the in-queue of the socket. | |
266 | SetHeader(wxT("Content-Length"), wxT("-1")); | |
267 | SetHeader(wxT("Content-Type"), wxT("none/none")); | |
268 | RestoreState(); | |
269 | return true; | |
270 | } | |
f4ada568 | 271 | |
48f7ffbe WS |
272 | wxStringTokenizer token(tmp_str,wxT(' ')); |
273 | wxString tmp_str2; | |
274 | bool ret_value; | |
275 | ||
276 | token.NextToken(); | |
277 | tmp_str2 = token.NextToken(); | |
278 | ||
279 | m_http_response = wxAtoi(tmp_str2); | |
280 | ||
281 | switch (tmp_str2[0u]) | |
282 | { | |
283 | case wxT('1'): | |
284 | /* INFORMATION / SUCCESS */ | |
285 | break; | |
286 | ||
287 | case wxT('2'): | |
288 | /* SUCCESS */ | |
289 | break; | |
290 | ||
291 | case wxT('3'): | |
292 | /* REDIRECTION */ | |
293 | break; | |
294 | ||
295 | default: | |
296 | m_perr = wxPROTO_NOFILE; | |
297 | RestoreState(); | |
298 | return false; | |
299 | } | |
300 | ||
301 | ret_value = ParseHeaders(); | |
302 | RestoreState(); | |
303 | return ret_value; | |
f4ada568 GL |
304 | } |
305 | ||
fc4b32c2 GRG |
306 | class wxHTTPStream : public wxSocketInputStream |
307 | { | |
f4ada568 | 308 | public: |
48f7ffbe WS |
309 | wxHTTP *m_http; |
310 | size_t m_httpsize; | |
311 | unsigned long m_read_bytes; | |
9a1b2c28 | 312 | |
48f7ffbe WS |
313 | wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {} |
314 | size_t GetSize() const { return m_httpsize; } | |
315 | virtual ~wxHTTPStream(void) { m_http->Abort(); } | |
a324a7bc GL |
316 | |
317 | protected: | |
48f7ffbe | 318 | size_t OnSysRead(void *buffer, size_t bufsize); |
22f3361e VZ |
319 | |
320 | DECLARE_NO_COPY_CLASS(wxHTTPStream) | |
f4ada568 GL |
321 | }; |
322 | ||
a324a7bc GL |
323 | size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize) |
324 | { | |
32013d27 VZ |
325 | if (m_httpsize > 0 && m_read_bytes >= m_httpsize) |
326 | { | |
327 | m_lasterror = wxSTREAM_EOF; | |
328 | return 0; | |
329 | } | |
a324a7bc | 330 | |
32013d27 VZ |
331 | size_t ret = wxSocketInputStream::OnSysRead(buffer, bufsize); |
332 | m_read_bytes += ret; | |
a324a7bc | 333 | |
8b6b8e21 SC |
334 | if (m_httpsize==(size_t)-1 && m_lasterror == wxSTREAM_READ_ERROR ) |
335 | { | |
336 | // if m_httpsize is (size_t) -1 this means read until connection closed | |
337 | // which is equivalent to getting a READ_ERROR, for clients however this | |
338 | // must be translated into EOF, as it is the expected way of signalling | |
339 | // end end of the content | |
340 | m_lasterror = wxSTREAM_EOF ; | |
341 | } | |
342 | ||
32013d27 | 343 | return ret; |
a324a7bc GL |
344 | } |
345 | ||
f4ada568 GL |
346 | bool wxHTTP::Abort(void) |
347 | { | |
48f7ffbe | 348 | return wxSocketClient::Close(); |
f4ada568 GL |
349 | } |
350 | ||
351 | wxInputStream *wxHTTP::GetInputStream(const wxString& path) | |
352 | { | |
48f7ffbe | 353 | wxHTTPStream *inp_stream; |
8f5bda17 | 354 | |
48f7ffbe | 355 | wxString new_path; |
f4ada568 | 356 | |
48f7ffbe WS |
357 | m_perr = wxPROTO_CONNERR; |
358 | if (!m_addr) | |
359 | return NULL; | |
f4ada568 | 360 | |
48f7ffbe | 361 | // We set m_connected back to false so wxSocketBase will know what to do. |
ad8b8498 | 362 | #ifdef __WXMAC__ |
48f7ffbe WS |
363 | wxSocketClient::Connect(*m_addr , false ); |
364 | wxSocketClient::WaitOnConnect(10); | |
ad8b8498 SC |
365 | |
366 | if (!wxSocketClient::IsConnected()) | |
367 | return NULL; | |
368 | #else | |
48f7ffbe WS |
369 | if (!wxProtocol::Connect(*m_addr)) |
370 | return NULL; | |
ad8b8498 | 371 | #endif |
f4ada568 | 372 | |
48f7ffbe WS |
373 | if (!BuildRequest(path, m_post_buf.empty() ? wxHTTP_GET : wxHTTP_POST)) |
374 | return NULL; | |
f4ada568 | 375 | |
48f7ffbe | 376 | inp_stream = new wxHTTPStream(this); |
8f5bda17 | 377 | |
48f7ffbe WS |
378 | if (!GetHeader(wxT("Content-Length")).empty()) |
379 | inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length"))); | |
380 | else | |
381 | inp_stream->m_httpsize = (size_t)-1; | |
a324a7bc | 382 | |
48f7ffbe | 383 | inp_stream->m_read_bytes = 0; |
a324a7bc | 384 | |
48f7ffbe WS |
385 | Notify(false); |
386 | SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL); | |
9a1b2c28 | 387 | |
48f7ffbe | 388 | return inp_stream; |
f4ada568 | 389 | } |
35a4dab7 | 390 | |
a5d46b73 VZ |
391 | #endif // wxUSE_PROTOCOL_HTTP |
392 |