]>
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 | |
9 | // Licence: wxWindows license | |
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 | ||
ce4169a4 RR |
23 | #if wxUSE_SOCKETS |
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() | |
41 | : wxProtocol(), | |
42 | m_headers(wxKEY_STRING) | |
43 | { | |
44 | m_addr = NULL; | |
45 | m_read = FALSE; | |
f61815af | 46 | m_proxy_mode = FALSE; |
f4ada568 | 47 | |
a324a7bc | 48 | SetNotify(GSOCK_LOST_FLAG); |
f4ada568 GL |
49 | } |
50 | ||
51 | wxHTTP::~wxHTTP() | |
52 | { | |
53 | // wxString isn't a wxObject | |
1f01991f | 54 | wxNode *node = m_headers.First(); |
f4ada568 GL |
55 | wxString *string; |
56 | ||
57 | while (node) { | |
58 | string = (wxString *)node->Data(); | |
59 | delete string; | |
60 | node = node->Next(); | |
61 | } | |
d14f4fec RD |
62 | |
63 | if (m_addr) { | |
64 | delete m_addr; | |
65 | m_addr = NULL; | |
66 | } | |
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 | ||
f4ada568 GL |
79 | void wxHTTP::SetHeader(const wxString& header, const wxString& h_data) |
80 | { | |
81 | if (m_read) { | |
82 | m_headers.Clear(); | |
83 | m_read = FALSE; | |
84 | } | |
85 | ||
86 | wxNode *node = m_headers.Find(header); | |
87 | ||
88 | if (!node) | |
89 | m_headers.Append(header, (wxObject *)(new wxString(h_data))); | |
90 | else { | |
91 | wxString *str = (wxString *)node->Data(); | |
92 | (*str) = h_data; | |
93 | } | |
94 | } | |
95 | ||
96 | wxString wxHTTP::GetHeader(const wxString& header) | |
97 | { | |
f61815af GL |
98 | wxNode *node; |
99 | wxString upper_header; | |
100 | ||
101 | upper_header = header.Upper(); | |
2ce0a6e2 | 102 | |
f61815af | 103 | node = m_headers.Find(upper_header); |
f4ada568 | 104 | if (!node) |
ce3ed50d | 105 | return wxEmptyString; |
f4ada568 GL |
106 | |
107 | return *((wxString *)node->Data()); | |
108 | } | |
109 | ||
110 | void wxHTTP::SendHeaders() | |
111 | { | |
112 | wxNode *head = m_headers.First(); | |
113 | ||
53c6e7cc VZ |
114 | while (head) |
115 | { | |
f4ada568 | 116 | wxString *str = (wxString *)head->Data(); |
f4ada568 | 117 | |
53c6e7cc | 118 | wxString buf; |
223d09f6 | 119 | buf.Printf(wxT("%s: %s\n\r"), head->GetKeyString(), str->GetData()); |
53c6e7cc | 120 | |
fde7d98e | 121 | const wxWX2MBbuf cbuf = buf.mb_str(); |
4846abaf | 122 | Write(cbuf, strlen(cbuf)); |
f4ada568 GL |
123 | |
124 | head = head->Next(); | |
125 | } | |
126 | } | |
127 | ||
128 | bool wxHTTP::ParseHeaders() | |
129 | { | |
130 | wxString line; | |
a737331d | 131 | wxStringTokenizer tokenzr; |
f4ada568 GL |
132 | |
133 | m_headers.Clear(); | |
134 | m_read = TRUE; | |
135 | ||
2ce0a6e2 DW |
136 | #if defined(__VISAGECPP__) |
137 | // VA just can't stand while(1) | |
138 | bool bOs2var = TRUE; | |
139 | while(bOs2var) { | |
140 | #else | |
141 | while (1) { | |
142 | #endif | |
a324a7bc GL |
143 | m_perr = GetLine(this, line); |
144 | if (m_perr != wxPROTO_NOERR) | |
f4ada568 GL |
145 | return FALSE; |
146 | ||
147 | if (line.Length() == 0) | |
148 | break; | |
149 | ||
21d73404 RD |
150 | wxString left_str = line.BeforeFirst(':'); |
151 | wxString *str = new wxString(line.AfterFirst(':').Strip(wxString::both)); | |
f61815af GL |
152 | left_str.MakeUpper(); |
153 | ||
f4ada568 GL |
154 | m_headers.Append(left_str, (wxObject *) str); |
155 | } | |
156 | return TRUE; | |
157 | } | |
158 | ||
159 | bool wxHTTP::Connect(const wxString& host) | |
160 | { | |
161 | wxIPV4address *addr; | |
162 | ||
f61815af | 163 | if (m_addr) { |
f4ada568 GL |
164 | delete m_addr; |
165 | m_addr = NULL; | |
166 | Close(); | |
167 | } | |
168 | ||
169 | m_addr = addr = new wxIPV4address(); | |
170 | ||
171 | if (!addr->Hostname(host)) { | |
172 | delete m_addr; | |
173 | m_addr = NULL; | |
a324a7bc | 174 | m_perr = wxPROTO_NETERR; |
f4ada568 GL |
175 | return FALSE; |
176 | } | |
177 | ||
223d09f6 | 178 | if (!addr->Service(wxT("http"))) |
f4ada568 GL |
179 | addr->Service(80); |
180 | ||
181 | return TRUE; | |
182 | } | |
183 | ||
8a2c6ef8 | 184 | bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait)) |
f4ada568 | 185 | { |
a324a7bc GL |
186 | if (m_addr) { |
187 | delete m_addr; | |
188 | m_addr = NULL; | |
189 | Close(); | |
190 | } | |
f4ada568 | 191 | |
a324a7bc | 192 | m_addr = (wxSockAddress *) addr.Clone(); |
f4ada568 GL |
193 | return TRUE; |
194 | } | |
195 | ||
196 | bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req) | |
197 | { | |
fae05df5 | 198 | wxChar *tmp_buf; |
a324a7bc | 199 | wxChar buf[200]; // 200 is arbitrary. |
f61815af | 200 | wxString tmp_str = path; |
f4ada568 | 201 | |
7e1e0960 | 202 | // If there is no User-Agent defined, define it. |
223d09f6 | 203 | if (GetHeader(wxT("User-Agent")).IsNull()) |
2ce0a6e2 | 204 | SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x")); |
7e1e0960 | 205 | |
f4ada568 GL |
206 | switch (req) { |
207 | case wxHTTP_GET: | |
223d09f6 | 208 | tmp_buf = wxT("GET"); |
f4ada568 GL |
209 | break; |
210 | default: | |
211 | return FALSE; | |
212 | } | |
213 | ||
41895a05 | 214 | SaveState(); |
062c4861 | 215 | SetFlags(NONE); |
41895a05 | 216 | Notify(FALSE); |
41895a05 | 217 | |
223d09f6 | 218 | wxSprintf(buf, wxT("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData()); |
e7fc59f4 | 219 | const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf); |
e90c1d2a | 220 | Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf)); |
f4ada568 | 221 | SendHeaders(); |
fae05df5 | 222 | Write("\n\r", 2); |
f4ada568 | 223 | |
a324a7bc GL |
224 | m_perr = GetLine(this, tmp_str); |
225 | if (m_perr != wxPROTO_NOERR) { | |
41895a05 | 226 | RestoreState(); |
f4ada568 | 227 | return FALSE; |
41895a05 | 228 | } |
f4ada568 | 229 | |
223d09f6 | 230 | if (!tmp_str.Contains(wxT("HTTP/"))) { |
f4ada568 | 231 | // TODO: support HTTP v0.9 which can have no header. |
7e1e0960 | 232 | // FIXME: tmp_str is not put back in the in-queue of the socket. |
223d09f6 KB |
233 | SetHeader(wxT("Content-Length"), wxT("-1")); |
234 | SetHeader(wxT("Content-Type"), wxT("none/none")); | |
41895a05 | 235 | RestoreState(); |
f4ada568 GL |
236 | return TRUE; |
237 | } | |
238 | ||
223d09f6 | 239 | wxStringTokenizer token(tmp_str,wxT(' ')); |
f4ada568 | 240 | wxString tmp_str2; |
41895a05 | 241 | bool ret_value; |
f4ada568 GL |
242 | |
243 | token.NextToken(); | |
244 | tmp_str2 = token.NextToken(); | |
245 | ||
ea4f5235 | 246 | switch (tmp_str2[(unsigned int) 0]) { |
223d09f6 | 247 | case wxT('1'): |
7e1e0960 GL |
248 | /* INFORMATION / SUCCESS */ |
249 | break; | |
223d09f6 | 250 | case wxT('2'): |
7e1e0960 GL |
251 | /* SUCCESS */ |
252 | break; | |
223d09f6 | 253 | case wxT('3'): |
7e1e0960 | 254 | /* REDIRECTION */ |
f4ada568 GL |
255 | break; |
256 | default: | |
a324a7bc | 257 | m_perr = wxPROTO_NOFILE; |
41895a05 | 258 | RestoreState(); |
f4ada568 GL |
259 | return FALSE; |
260 | } | |
261 | ||
41895a05 GL |
262 | ret_value = ParseHeaders(); |
263 | RestoreState(); | |
264 | return ret_value; | |
f4ada568 GL |
265 | } |
266 | ||
267 | class wxHTTPStream : public wxSocketInputStream { | |
268 | public: | |
269 | wxHTTP *m_http; | |
9a1b2c28 | 270 | size_t m_httpsize; |
a324a7bc | 271 | unsigned long m_read_bytes; |
9a1b2c28 | 272 | |
f4ada568 | 273 | wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {} |
f61815af | 274 | size_t GetSize() const { return m_httpsize; } |
f4ada568 | 275 | virtual ~wxHTTPStream(void) { m_http->Abort(); } |
a324a7bc GL |
276 | |
277 | protected: | |
278 | size_t OnSysRead(void *buffer, size_t bufsize); | |
f4ada568 GL |
279 | }; |
280 | ||
a324a7bc GL |
281 | size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize) |
282 | { | |
283 | size_t ret; | |
284 | ||
285 | if (m_httpsize > 0 && m_read_bytes >= m_httpsize) | |
286 | return 0; | |
287 | ||
288 | ret = wxSocketInputStream::OnSysRead(buffer, bufsize); | |
289 | m_read_bytes += ret; | |
290 | ||
291 | return ret; | |
292 | } | |
293 | ||
f4ada568 GL |
294 | bool wxHTTP::Abort(void) |
295 | { | |
aa6d9706 | 296 | bool ret; |
f61815af GL |
297 | |
298 | ret = wxSocketClient::Close(); | |
299 | ||
300 | return ret; | |
f4ada568 GL |
301 | } |
302 | ||
303 | wxInputStream *wxHTTP::GetInputStream(const wxString& path) | |
304 | { | |
305 | wxHTTPStream *inp_stream = new wxHTTPStream(this); | |
f61815af | 306 | wxString new_path; |
f4ada568 | 307 | |
f61815af GL |
308 | m_perr = wxPROTO_CONNERR; |
309 | if (!m_addr) | |
f4ada568 | 310 | return NULL; |
f4ada568 | 311 | |
f61815af | 312 | // We set m_connected back to FALSE so wxSocketBase will know what to do. |
f4ada568 GL |
313 | if (!wxProtocol::Connect(*m_addr)) |
314 | return NULL; | |
315 | ||
316 | if (!BuildRequest(path, wxHTTP_GET)) | |
317 | return NULL; | |
318 | ||
223d09f6 KB |
319 | if (!GetHeader(wxT("Content-Length")).IsEmpty()) |
320 | inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length"))); | |
a324a7bc GL |
321 | else |
322 | inp_stream->m_httpsize = (size_t)-1; | |
323 | ||
324 | inp_stream->m_read_bytes = 0; | |
325 | ||
326 | Notify(FALSE); | |
f61815af | 327 | SetFlags(SPEED | WAITALL); |
9a1b2c28 | 328 | |
f4ada568 GL |
329 | return inp_stream; |
330 | } | |
35a4dab7 GL |
331 | |
332 | #endif | |
333 | // wxUSE_SOCKETS |