]> git.saurik.com Git - wxWidgets.git/blame - src/common/http.cpp
setting and restoring the graf port
[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
02244615
VZ
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 179 addr->Service(80);
5b96a71a
VS
180
181 SetHeader(wxT("Host"), host);
f4ada568
GL
182
183 return TRUE;
184}
185
8a2c6ef8 186bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
f4ada568 187{
a324a7bc
GL
188 if (m_addr) {
189 delete m_addr;
a324a7bc
GL
190 Close();
191 }
f4ada568 192
acd15a3f
VZ
193 m_addr = addr.Clone();
194
5b96a71a
VS
195 wxIPV4address *ipv4addr = wxDynamicCast(&addr, wxIPV4address);
196 if (ipv4addr)
197 SetHeader(wxT("Host"), ipv4addr->Hostname());
198
f4ada568
GL
199 return TRUE;
200}
201
202bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
203{
fae05df5 204 wxChar *tmp_buf;
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
02244615
VZ
214 // If there is no User-Agent defined, define it.
215 if (GetHeader(wxT("User-Agent")).IsNull())
216 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
217
41895a05 218 SaveState();
3f04dfd3 219 SetFlags(wxSOCKET_NONE);
41895a05 220 Notify(FALSE);
41895a05 221
02244615
VZ
222 wxString buf;
223 buf.Printf(wxT("%s %s HTTP/1.0\r\n"), tmp_buf, path.c_str());
e7fc59f4 224 const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf);
e90c1d2a 225 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
f4ada568 226 SendHeaders();
df4b7d98 227 Write("\r\n", 2);
f4ada568 228
02244615 229 wxString tmp_str;
a324a7bc
GL
230 m_perr = GetLine(this, tmp_str);
231 if (m_perr != wxPROTO_NOERR) {
41895a05 232 RestoreState();
f4ada568 233 return FALSE;
41895a05 234 }
f4ada568 235
223d09f6 236 if (!tmp_str.Contains(wxT("HTTP/"))) {
f4ada568 237 // TODO: support HTTP v0.9 which can have no header.
7e1e0960 238 // FIXME: tmp_str is not put back in the in-queue of the socket.
223d09f6
KB
239 SetHeader(wxT("Content-Length"), wxT("-1"));
240 SetHeader(wxT("Content-Type"), wxT("none/none"));
41895a05 241 RestoreState();
f4ada568
GL
242 return TRUE;
243 }
244
223d09f6 245 wxStringTokenizer token(tmp_str,wxT(' '));
f4ada568 246 wxString tmp_str2;
41895a05 247 bool ret_value;
f4ada568
GL
248
249 token.NextToken();
250 tmp_str2 = token.NextToken();
251
02244615 252 switch (tmp_str2[0u]) {
223d09f6 253 case wxT('1'):
7e1e0960
GL
254 /* INFORMATION / SUCCESS */
255 break;
223d09f6 256 case wxT('2'):
7e1e0960
GL
257 /* SUCCESS */
258 break;
223d09f6 259 case wxT('3'):
7e1e0960 260 /* REDIRECTION */
f4ada568
GL
261 break;
262 default:
a324a7bc 263 m_perr = wxPROTO_NOFILE;
41895a05 264 RestoreState();
f4ada568
GL
265 return FALSE;
266 }
267
41895a05
GL
268 ret_value = ParseHeaders();
269 RestoreState();
270 return ret_value;
f4ada568
GL
271}
272
fc4b32c2
GRG
273class wxHTTPStream : public wxSocketInputStream
274{
f4ada568
GL
275public:
276 wxHTTP *m_http;
9a1b2c28 277 size_t m_httpsize;
a324a7bc 278 unsigned long m_read_bytes;
9a1b2c28 279
f4ada568 280 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
f61815af 281 size_t GetSize() const { return m_httpsize; }
f4ada568 282 virtual ~wxHTTPStream(void) { m_http->Abort(); }
a324a7bc
GL
283
284protected:
285 size_t OnSysRead(void *buffer, size_t bufsize);
f4ada568
GL
286};
287
a324a7bc
GL
288size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
289{
290 size_t ret;
291
292 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
293 return 0;
294
295 ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
296 m_read_bytes += ret;
297
298 return ret;
299}
300
f4ada568
GL
301bool wxHTTP::Abort(void)
302{
fc4b32c2 303 return wxSocketClient::Close();
f4ada568
GL
304}
305
306wxInputStream *wxHTTP::GetInputStream(const wxString& path)
307{
8f5bda17
JS
308 wxHTTPStream *inp_stream;
309
f61815af 310 wxString new_path;
f4ada568 311
f61815af
GL
312 m_perr = wxPROTO_CONNERR;
313 if (!m_addr)
f4ada568 314 return NULL;
f4ada568 315
f61815af 316 // We set m_connected back to FALSE so wxSocketBase will know what to do.
ad8b8498
SC
317#ifdef __WXMAC__
318 wxSocketClient::Connect(*m_addr , FALSE );
319 wxSocketClient::WaitOnConnect(10);
320
321 if (!wxSocketClient::IsConnected())
322 return NULL;
323#else
f4ada568
GL
324 if (!wxProtocol::Connect(*m_addr))
325 return NULL;
ad8b8498 326#endif
f4ada568
GL
327
328 if (!BuildRequest(path, wxHTTP_GET))
329 return NULL;
330
8f5bda17
JS
331 inp_stream = new wxHTTPStream(this);
332
223d09f6
KB
333 if (!GetHeader(wxT("Content-Length")).IsEmpty())
334 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
a324a7bc
GL
335 else
336 inp_stream->m_httpsize = (size_t)-1;
337
338 inp_stream->m_read_bytes = 0;
339
340 Notify(FALSE);
fc4b32c2 341 SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
9a1b2c28 342
f4ada568
GL
343 return inp_stream;
344}
35a4dab7
GL
345
346#endif
347 // wxUSE_SOCKETS