]> git.saurik.com Git - wxWidgets.git/blame - src/common/http.cpp
test for timegm() added
[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)
223d09f6 37IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("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{
223d09f6 68 return GetHeader(wxT("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();
2ce0a6e2 99
f61815af 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;
223d09f6 116 buf.Printf(wxT("%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
2ce0a6e2
DW
133#if defined(__VISAGECPP__)
134// VA just can't stand while(1)
135 bool bOs2var = TRUE;
136 while(bOs2var) {
137#else
138 while (1) {
139#endif
a324a7bc
GL
140 m_perr = GetLine(this, line);
141 if (m_perr != wxPROTO_NOERR)
f4ada568
GL
142 return FALSE;
143
144 if (line.Length() == 0)
145 break;
146
a737331d 147 tokenzr.SetString(line, " :\t\n\r");
91b8de8d 148 if (!tokenzr.HasMoreTokens())
f4ada568
GL
149 return FALSE;
150
a737331d
GL
151 wxString left_str = tokenzr.GetNextToken();
152 wxString *str = new wxString(tokenzr.GetNextToken());
f4ada568 153
f61815af
GL
154 left_str.MakeUpper();
155
f4ada568
GL
156 m_headers.Append(left_str, (wxObject *) str);
157 }
158 return TRUE;
159}
160
161bool wxHTTP::Connect(const wxString& host)
162{
163 wxIPV4address *addr;
164
f61815af 165 if (m_addr) {
f4ada568
GL
166 delete m_addr;
167 m_addr = NULL;
168 Close();
169 }
170
171 m_addr = addr = new wxIPV4address();
172
173 if (!addr->Hostname(host)) {
174 delete m_addr;
175 m_addr = NULL;
a324a7bc 176 m_perr = wxPROTO_NETERR;
f4ada568
GL
177 return FALSE;
178 }
179
223d09f6 180 if (!addr->Service(wxT("http")))
f4ada568
GL
181 addr->Service(80);
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;
190 m_addr = NULL;
191 Close();
192 }
f4ada568 193
a324a7bc 194 m_addr = (wxSockAddress *) addr.Clone();
f4ada568
GL
195 return TRUE;
196}
197
198bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
199{
fae05df5 200 wxChar *tmp_buf;
a324a7bc 201 wxChar buf[200]; // 200 is arbitrary.
f61815af 202 wxString tmp_str = path;
f4ada568 203
7e1e0960 204 // If there is no User-Agent defined, define it.
223d09f6 205 if (GetHeader(wxT("User-Agent")).IsNull())
2ce0a6e2 206 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
7e1e0960 207
f4ada568
GL
208 switch (req) {
209 case wxHTTP_GET:
223d09f6 210 tmp_buf = wxT("GET");
f4ada568
GL
211 break;
212 default:
213 return FALSE;
214 }
215
41895a05 216 SaveState();
062c4861 217 SetFlags(NONE);
41895a05 218 Notify(FALSE);
41895a05 219
223d09f6 220 wxSprintf(buf, wxT("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData());
e7fc59f4 221 const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf);
e90c1d2a 222 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
f4ada568 223 SendHeaders();
fae05df5 224 Write("\n\r", 2);
f4ada568 225
a324a7bc
GL
226 m_perr = GetLine(this, tmp_str);
227 if (m_perr != wxPROTO_NOERR) {
41895a05 228 RestoreState();
f4ada568 229 return FALSE;
41895a05 230 }
f4ada568 231
223d09f6 232 if (!tmp_str.Contains(wxT("HTTP/"))) {
f4ada568 233 // TODO: support HTTP v0.9 which can have no header.
7e1e0960 234 // FIXME: tmp_str is not put back in the in-queue of the socket.
223d09f6
KB
235 SetHeader(wxT("Content-Length"), wxT("-1"));
236 SetHeader(wxT("Content-Type"), wxT("none/none"));
41895a05 237 RestoreState();
f4ada568
GL
238 return TRUE;
239 }
240
223d09f6 241 wxStringTokenizer token(tmp_str,wxT(' '));
f4ada568 242 wxString tmp_str2;
41895a05 243 bool ret_value;
f4ada568
GL
244
245 token.NextToken();
246 tmp_str2 = token.NextToken();
247
ea4f5235 248 switch (tmp_str2[(unsigned int) 0]) {
223d09f6 249 case wxT('1'):
7e1e0960
GL
250 /* INFORMATION / SUCCESS */
251 break;
223d09f6 252 case wxT('2'):
7e1e0960
GL
253 /* SUCCESS */
254 break;
223d09f6 255 case wxT('3'):
7e1e0960 256 /* REDIRECTION */
f4ada568
GL
257 break;
258 default:
a324a7bc 259 m_perr = wxPROTO_NOFILE;
41895a05 260 RestoreState();
f4ada568
GL
261 return FALSE;
262 }
263
41895a05
GL
264 ret_value = ParseHeaders();
265 RestoreState();
266 return ret_value;
f4ada568
GL
267}
268
269class wxHTTPStream : public wxSocketInputStream {
270public:
271 wxHTTP *m_http;
9a1b2c28 272 size_t m_httpsize;
a324a7bc 273 unsigned long m_read_bytes;
9a1b2c28 274
f4ada568 275 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
f61815af 276 size_t GetSize() const { return m_httpsize; }
f4ada568 277 virtual ~wxHTTPStream(void) { m_http->Abort(); }
a324a7bc
GL
278
279protected:
280 size_t OnSysRead(void *buffer, size_t bufsize);
f4ada568
GL
281};
282
a324a7bc
GL
283size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
284{
285 size_t ret;
286
287 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
288 return 0;
289
290 ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
291 m_read_bytes += ret;
292
293 return ret;
294}
295
f4ada568
GL
296bool wxHTTP::Abort(void)
297{
aa6d9706 298 bool ret;
f61815af
GL
299
300 ret = wxSocketClient::Close();
301
302 return ret;
f4ada568
GL
303}
304
305wxInputStream *wxHTTP::GetInputStream(const wxString& path)
306{
307 wxHTTPStream *inp_stream = new wxHTTPStream(this);
f61815af 308 wxString new_path;
f4ada568 309
f61815af
GL
310 m_perr = wxPROTO_CONNERR;
311 if (!m_addr)
f4ada568 312 return NULL;
f4ada568 313
f61815af 314 // We set m_connected back to FALSE so wxSocketBase will know what to do.
f4ada568
GL
315 if (!wxProtocol::Connect(*m_addr))
316 return NULL;
317
318 if (!BuildRequest(path, wxHTTP_GET))
319 return NULL;
320
223d09f6
KB
321 if (!GetHeader(wxT("Content-Length")).IsEmpty())
322 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
a324a7bc
GL
323 else
324 inp_stream->m_httpsize = (size_t)-1;
325
326 inp_stream->m_read_bytes = 0;
327
328 Notify(FALSE);
f61815af 329 SetFlags(SPEED | WAITALL);
9a1b2c28 330
f4ada568
GL
331 return inp_stream;
332}
35a4dab7
GL
333
334#endif
335 // wxUSE_SOCKETS