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