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