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