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