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