]>
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 | } | |
62 | } | |
63 | ||
64 | wxString wxHTTP::GetContentType() | |
65 | { | |
223d09f6 | 66 | return GetHeader(wxT("Content-Type")); |
f4ada568 GL |
67 | } |
68 | ||
f61815af GL |
69 | void wxHTTP::SetProxyMode(bool on) |
70 | { | |
71 | m_proxy_mode = on; | |
72 | } | |
73 | ||
f4ada568 GL |
74 | void wxHTTP::SetHeader(const wxString& header, const wxString& h_data) |
75 | { | |
76 | if (m_read) { | |
77 | m_headers.Clear(); | |
78 | m_read = FALSE; | |
79 | } | |
80 | ||
81 | wxNode *node = m_headers.Find(header); | |
82 | ||
83 | if (!node) | |
84 | m_headers.Append(header, (wxObject *)(new wxString(h_data))); | |
85 | else { | |
86 | wxString *str = (wxString *)node->Data(); | |
87 | (*str) = h_data; | |
88 | } | |
89 | } | |
90 | ||
91 | wxString wxHTTP::GetHeader(const wxString& header) | |
92 | { | |
f61815af GL |
93 | wxNode *node; |
94 | wxString upper_header; | |
95 | ||
96 | upper_header = header.Upper(); | |
2ce0a6e2 | 97 | |
f61815af | 98 | node = m_headers.Find(upper_header); |
f4ada568 | 99 | if (!node) |
ce3ed50d | 100 | return wxEmptyString; |
f4ada568 GL |
101 | |
102 | return *((wxString *)node->Data()); | |
103 | } | |
104 | ||
105 | void wxHTTP::SendHeaders() | |
106 | { | |
107 | wxNode *head = m_headers.First(); | |
108 | ||
53c6e7cc VZ |
109 | while (head) |
110 | { | |
f4ada568 | 111 | wxString *str = (wxString *)head->Data(); |
f4ada568 | 112 | |
53c6e7cc | 113 | wxString buf; |
223d09f6 | 114 | buf.Printf(wxT("%s: %s\n\r"), head->GetKeyString(), str->GetData()); |
53c6e7cc | 115 | |
fde7d98e | 116 | const wxWX2MBbuf cbuf = buf.mb_str(); |
4846abaf | 117 | Write(cbuf, strlen(cbuf)); |
f4ada568 GL |
118 | |
119 | head = head->Next(); | |
120 | } | |
121 | } | |
122 | ||
123 | bool wxHTTP::ParseHeaders() | |
124 | { | |
125 | wxString line; | |
a737331d | 126 | wxStringTokenizer tokenzr; |
f4ada568 GL |
127 | |
128 | m_headers.Clear(); | |
129 | m_read = TRUE; | |
130 | ||
2ce0a6e2 DW |
131 | #if defined(__VISAGECPP__) |
132 | // VA just can't stand while(1) | |
133 | bool bOs2var = TRUE; | |
134 | while(bOs2var) { | |
135 | #else | |
136 | while (1) { | |
137 | #endif | |
a324a7bc GL |
138 | m_perr = GetLine(this, line); |
139 | if (m_perr != wxPROTO_NOERR) | |
f4ada568 GL |
140 | return FALSE; |
141 | ||
142 | if (line.Length() == 0) | |
143 | break; | |
144 | ||
a737331d | 145 | tokenzr.SetString(line, " :\t\n\r"); |
91b8de8d | 146 | if (!tokenzr.HasMoreTokens()) |
f4ada568 GL |
147 | return FALSE; |
148 | ||
a737331d GL |
149 | wxString left_str = tokenzr.GetNextToken(); |
150 | wxString *str = new wxString(tokenzr.GetNextToken()); | |
f4ada568 | 151 | |
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 |