]> git.saurik.com Git - wxWidgets.git/blame - src/common/http.cpp
Committing in .
[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();
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;
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
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
223d09f6 174 if (!addr->Service(wxT("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 197
7e1e0960 198 // If there is no User-Agent defined, define it.
223d09f6
KB
199 if (GetHeader(wxT("User-Agent")).IsNull())
200 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
7e1e0960 201
f4ada568
GL
202 switch (req) {
203 case wxHTTP_GET:
223d09f6 204 tmp_buf = wxT("GET");
f4ada568
GL
205 break;
206 default:
207 return FALSE;
208 }
209
41895a05 210 SaveState();
062c4861 211 SetFlags(NONE);
41895a05 212 Notify(FALSE);
41895a05 213
223d09f6 214 wxSprintf(buf, wxT("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData());
e7fc59f4 215 const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf);
e90c1d2a 216 Write(pathbuf, strlen(wxMBSTRINGCAST pathbuf));
f4ada568 217 SendHeaders();
fae05df5 218 Write("\n\r", 2);
f4ada568 219
a324a7bc
GL
220 m_perr = GetLine(this, tmp_str);
221 if (m_perr != wxPROTO_NOERR) {
41895a05 222 RestoreState();
f4ada568 223 return FALSE;
41895a05 224 }
f4ada568 225
223d09f6 226 if (!tmp_str.Contains(wxT("HTTP/"))) {
f4ada568 227 // TODO: support HTTP v0.9 which can have no header.
7e1e0960 228 // FIXME: tmp_str is not put back in the in-queue of the socket.
223d09f6
KB
229 SetHeader(wxT("Content-Length"), wxT("-1"));
230 SetHeader(wxT("Content-Type"), wxT("none/none"));
41895a05 231 RestoreState();
f4ada568
GL
232 return TRUE;
233 }
234
223d09f6 235 wxStringTokenizer token(tmp_str,wxT(' '));
f4ada568 236 wxString tmp_str2;
41895a05 237 bool ret_value;
f4ada568
GL
238
239 token.NextToken();
240 tmp_str2 = token.NextToken();
241
ea4f5235 242 switch (tmp_str2[(unsigned int) 0]) {
223d09f6 243 case wxT('1'):
7e1e0960
GL
244 /* INFORMATION / SUCCESS */
245 break;
223d09f6 246 case wxT('2'):
7e1e0960
GL
247 /* SUCCESS */
248 break;
223d09f6 249 case wxT('3'):
7e1e0960 250 /* REDIRECTION */
f4ada568
GL
251 break;
252 default:
a324a7bc 253 m_perr = wxPROTO_NOFILE;
41895a05 254 RestoreState();
f4ada568
GL
255 return FALSE;
256 }
257
41895a05
GL
258 ret_value = ParseHeaders();
259 RestoreState();
260 return ret_value;
f4ada568
GL
261}
262
263class wxHTTPStream : public wxSocketInputStream {
264public:
265 wxHTTP *m_http;
9a1b2c28 266 size_t m_httpsize;
a324a7bc 267 unsigned long m_read_bytes;
9a1b2c28 268
f4ada568 269 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
f61815af 270 size_t GetSize() const { return m_httpsize; }
f4ada568 271 virtual ~wxHTTPStream(void) { m_http->Abort(); }
a324a7bc
GL
272
273protected:
274 size_t OnSysRead(void *buffer, size_t bufsize);
f4ada568
GL
275};
276
a324a7bc
GL
277size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize)
278{
279 size_t ret;
280
281 if (m_httpsize > 0 && m_read_bytes >= m_httpsize)
282 return 0;
283
284 ret = wxSocketInputStream::OnSysRead(buffer, bufsize);
285 m_read_bytes += ret;
286
287 return ret;
288}
289
f4ada568
GL
290bool wxHTTP::Abort(void)
291{
aa6d9706 292 bool ret;
f61815af
GL
293
294 ret = wxSocketClient::Close();
295
296 return ret;
f4ada568
GL
297}
298
299wxInputStream *wxHTTP::GetInputStream(const wxString& path)
300{
301 wxHTTPStream *inp_stream = new wxHTTPStream(this);
f61815af 302 wxString new_path;
f4ada568 303
f61815af
GL
304 m_perr = wxPROTO_CONNERR;
305 if (!m_addr)
f4ada568 306 return NULL;
f4ada568 307
f61815af 308 // We set m_connected back to FALSE so wxSocketBase will know what to do.
f4ada568
GL
309 if (!wxProtocol::Connect(*m_addr))
310 return NULL;
311
312 if (!BuildRequest(path, wxHTTP_GET))
313 return NULL;
314
223d09f6
KB
315 if (!GetHeader(wxT("Content-Length")).IsEmpty())
316 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(wxT("Content-Length")));
a324a7bc
GL
317 else
318 inp_stream->m_httpsize = (size_t)-1;
319
320 inp_stream->m_read_bytes = 0;
321
322 Notify(FALSE);
f61815af 323 SetFlags(SPEED | WAITALL);
9a1b2c28 324
f4ada568
GL
325 return inp_stream;
326}
35a4dab7
GL
327
328#endif
329 // wxUSE_SOCKETS