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