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