]> git.saurik.com Git - wxWidgets.git/blame - src/common/http.cpp
fix for the dialog controls font
[wxWidgets.git] / src / common / http.cpp
CommitLineData
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
f6bcfd97 23#if wxUSE_SOCKETS && wxUSE_STREAMS
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 35IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
223d09f6 36IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), TRUE)
f4ada568
GL
37
38#define HTTP_BSIZE 2048
39
40wxHTTP::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
fc4b32c2 48 SetNotify(wxSOCKET_LOST_FLAG);
f4ada568
GL
49}
50
51wxHTTP::~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
69wxString wxHTTP::GetContentType()
70{
223d09f6 71 return GetHeader(wxT("Content-Type"));
f4ada568
GL
72}
73
f61815af
GL
74void wxHTTP::SetProxyMode(bool on)
75{
76 m_proxy_mode = on;
77}
78
f4ada568
GL
79void 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
96wxString 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
110void 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;
df4b7d98 119 buf.Printf(wxT("%s: %s\r\n"), 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
128bool 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
159bool 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 184bool 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
196bool 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();
3f04dfd3 215 SetFlags(wxSOCKET_NONE);
41895a05 216 Notify(FALSE);
41895a05 217
df4b7d98 218 wxSprintf(buf, wxT("%s %s HTTP/1.0\r\n"), tmp_buf, tmp_str.GetData());
e7fc59f4 219 const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf);
e90c1d2a 220 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
f4ada568 221 SendHeaders();
df4b7d98 222 Write("\r\n", 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
fc4b32c2
GRG
267class wxHTTPStream : public wxSocketInputStream
268{
f4ada568
GL
269public:
270 wxHTTP *m_http;
9a1b2c28 271 size_t m_httpsize;
a324a7bc 272 unsigned long m_read_bytes;
9a1b2c28 273
f4ada568 274 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
f61815af 275 size_t GetSize() const { return m_httpsize; }
f4ada568 276 virtual ~wxHTTPStream(void) { m_http->Abort(); }
a324a7bc
GL
277
278protected:
279 size_t OnSysRead(void *buffer, size_t bufsize);
f4ada568
GL
280};
281
a324a7bc
GL
282size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
283{
284 size_t ret;
285
286 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
287 return 0;
288
289 ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
290 m_read_bytes += ret;
291
292 return ret;
293}
294
f4ada568
GL
295bool wxHTTP::Abort(void)
296{
fc4b32c2 297 return wxSocketClient::Close();
f4ada568
GL
298}
299
300wxInputStream *wxHTTP::GetInputStream(const wxString& path)
301{
8f5bda17
JS
302 wxHTTPStream *inp_stream;
303
f61815af 304 wxString new_path;
f4ada568 305
f61815af
GL
306 m_perr = wxPROTO_CONNERR;
307 if (!m_addr)
f4ada568 308 return NULL;
f4ada568 309
f61815af 310 // We set m_connected back to FALSE so wxSocketBase will know what to do.
ad8b8498
SC
311#ifdef __WXMAC__
312 wxSocketClient::Connect(*m_addr , FALSE );
313 wxSocketClient::WaitOnConnect(10);
314
315 if (!wxSocketClient::IsConnected())
316 return NULL;
317#else
f4ada568
GL
318 if (!wxProtocol::Connect(*m_addr))
319 return NULL;
ad8b8498 320#endif
f4ada568
GL
321
322 if (!BuildRequest(path, wxHTTP_GET))
323 return NULL;
324
8f5bda17
JS
325 inp_stream = new wxHTTPStream(this);
326
223d09f6
KB
327 if (!GetHeader(wxT("Content-Length")).IsEmpty())
328 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
a324a7bc
GL
329 else
330 inp_stream->m_httpsize = (size_t)-1;
331
332 inp_stream->m_read_bytes = 0;
333
334 Notify(FALSE);
fc4b32c2 335 SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
9a1b2c28 336
f4ada568
GL
337 return inp_stream;
338}
35a4dab7
GL
339
340#endif
341 // wxUSE_SOCKETS