]> git.saurik.com Git - wxWidgets.git/blame - src/common/http.cpp
Very bad wxPrintData destructor... fixed typo.
[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__
13#pragma implementation "http.h"
14#endif
15
fcc6dddd
JS
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
35a4dab7
GL
23#if wxUSE_SOCKETS
24
fcc6dddd
JS
25#ifndef WX_PRECOMP
26#endif
27
f4ada568
GL
28#include <stdio.h>
29#include <stdlib.h>
30#include "wx/string.h"
31#include "wx/tokenzr.h"
32#include "wx/socket.h"
33#include "wx/protocol/protocol.h"
34#include "wx/protocol/http.h"
35#include "wx/sckstrm.h"
36
f4ada568
GL
37#if !USE_SHARED_LIBRARY
38IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
39IMPLEMENT_PROTOCOL(wxHTTP, "http", "80", TRUE)
40#endif
41
42#define HTTP_BSIZE 2048
43
44wxHTTP::wxHTTP()
45 : wxProtocol(),
46 m_headers(wxKEY_STRING)
47{
48 m_addr = NULL;
49 m_read = FALSE;
50
51 SetNotify(REQ_LOST);
52}
53
54wxHTTP::~wxHTTP()
55{
56 // wxString isn't a wxObject
1f01991f 57 wxNode *node = m_headers.First();
f4ada568
GL
58 wxString *string;
59
60 while (node) {
61 string = (wxString *)node->Data();
62 delete string;
63 node = node->Next();
64 }
65}
66
67wxString wxHTTP::GetContentType()
68{
69 return GetHeader("Content-Type");
70}
71
72void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
73{
74 if (m_read) {
75 m_headers.Clear();
76 m_read = FALSE;
77 }
78
79 wxNode *node = m_headers.Find(header);
80
81 if (!node)
82 m_headers.Append(header, (wxObject *)(new wxString(h_data)));
83 else {
84 wxString *str = (wxString *)node->Data();
85 (*str) = h_data;
86 }
87}
88
89wxString wxHTTP::GetHeader(const wxString& header)
90{
91 wxNode *node = m_headers.Find(header);
92 if (!node)
ce3ed50d 93 return wxEmptyString;
f4ada568
GL
94
95 return *((wxString *)node->Data());
96}
97
98void wxHTTP::SendHeaders()
99{
100 wxNode *head = m_headers.First();
101
53c6e7cc
VZ
102 while (head)
103 {
f4ada568 104 wxString *str = (wxString *)head->Data();
f4ada568 105
53c6e7cc
VZ
106 wxString buf;
107 buf.Printf("%s: %s\n\r", head->GetKeyString(), str->GetData());
108
109 Write(buf, buf.Len());
f4ada568
GL
110
111 head = head->Next();
112 }
113}
114
115bool wxHTTP::ParseHeaders()
116{
117 wxString line;
118
119 m_headers.Clear();
120 m_read = TRUE;
121
122 while (1) {
123 m_error = GetLine(this, line);
124 if (m_error != wxPROTO_NOERR)
125 return FALSE;
126
127 if (line.Length() == 0)
128 break;
129
41895a05 130 printf("Header: %s\n", WXSTRINGCAST line);
f4ada568
GL
131 int pos = line.Find(':');
132 if (pos == -1)
133 return FALSE;
134
135 wxString left_str = line(0, pos);
136 wxString right_str = line(pos+1, line.Length());
137
138 right_str = right_str.Strip(wxString::leading);
139
140 wxString *str = new wxString(right_str);
141
142 m_headers.Append(left_str, (wxObject *) str);
143 }
144 return TRUE;
145}
146
147bool wxHTTP::Connect(const wxString& host)
148{
149 wxIPV4address *addr;
150
151 if (m_connected) {
152 delete m_addr;
153 m_addr = NULL;
154 Close();
155 }
156
157 m_addr = addr = new wxIPV4address();
158
159 if (!addr->Hostname(host)) {
160 delete m_addr;
161 m_addr = NULL;
162 m_error = wxPROTO_NETERR;
163 return FALSE;
164 }
165
166 if (!addr->Service("http"))
167 addr->Service(80);
168
169 return TRUE;
170}
171
8a2c6ef8 172bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
f4ada568
GL
173{
174 struct sockaddr *raw_addr;
175 size_t len;
176
177 m_addr = (wxSockAddress *)(addr.GetClassInfo()->CreateObject());
178
179 addr.Build(raw_addr, len);
180 m_addr->Disassemble(raw_addr, len);
181
182 return TRUE;
183}
184
185bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
186{
187 char *tmp_buf;
188 char buf[HTTP_BSIZE];
189
190 switch (req) {
191 case wxHTTP_GET:
192 tmp_buf = "GET";
193 break;
194 default:
195 return FALSE;
196 }
197
41895a05
GL
198 SaveState();
199 Notify(FALSE);
200 SetFlags(WAITALL);
201
f4ada568
GL
202 sprintf(buf, "%s %s HTTP/1.0\n\r", tmp_buf, (const char *)path);
203 Write(buf, strlen(buf));
204 SendHeaders();
205 sprintf(buf, "\n\r");
206 Write(buf, strlen(buf));
207
208 wxString tmp_str;
209
210 m_error = GetLine(this, tmp_str);
41895a05
GL
211 if (m_error != wxPROTO_NOERR) {
212 RestoreState();
f4ada568 213 return FALSE;
41895a05 214 }
f4ada568
GL
215
216 if (!tmp_str.Contains("HTTP/")) {
217 // TODO: support HTTP v0.9 which can have no header.
218 SetHeader("Content-Length", "-1");
219 SetHeader("Content-Type", "none/none");
41895a05 220 RestoreState();
f4ada568
GL
221 return TRUE;
222 }
223
224 wxStringTokenizer token(tmp_str,' ');
225 wxString tmp_str2;
41895a05 226 bool ret_value;
f4ada568
GL
227
228 token.NextToken();
229 tmp_str2 = token.NextToken();
230
231 switch (atoi(tmp_str2)) {
232 case 200:
233 break;
234 default:
235 m_error = wxPROTO_NOFILE;
41895a05 236 RestoreState();
f4ada568
GL
237 return FALSE;
238 }
239
41895a05
GL
240 ret_value = ParseHeaders();
241 RestoreState();
242 return ret_value;
f4ada568
GL
243}
244
245class wxHTTPStream : public wxSocketInputStream {
246public:
247 wxHTTP *m_http;
9a1b2c28
GL
248 size_t m_httpsize;
249
f4ada568 250 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
41895a05 251 size_t StreamSize() const { return m_httpsize; }
f4ada568
GL
252 virtual ~wxHTTPStream(void) { m_http->Abort(); }
253};
254
255bool wxHTTP::Abort(void)
256{
257 return wxSocketClient::Close();
258}
259
260wxInputStream *wxHTTP::GetInputStream(const wxString& path)
261{
262 wxHTTPStream *inp_stream = new wxHTTPStream(this);
263
264 if (!m_addr || m_connected) {
265 m_error = wxPROTO_CONNERR;
266 return NULL;
267 }
268
269 if (!wxProtocol::Connect(*m_addr))
270 return NULL;
271
272 if (!BuildRequest(path, wxHTTP_GET))
273 return NULL;
274
41895a05
GL
275 printf("Len = %s\n", WXSTRINGCAST GetHeader("Content-Length"));
276 if (!GetHeader("Content-Length").IsEmpty())
9a1b2c28
GL
277 inp_stream->m_httpsize = atoi(WXSTRINGCAST GetHeader("Content-Length"));
278
f4ada568
GL
279 return inp_stream;
280}
35a4dab7
GL
281
282#endif
283 // wxUSE_SOCKETS