]> git.saurik.com Git - wxWidgets.git/blob - src/common/http.cpp
Removed USING_CONFIGURE define. As VZ pointed out, checking HAVE_CONFIG_H
[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/url.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, _T("http"), _T("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(_T("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(_T("%s: %s\n\r"), head->GetKeyString(), str->GetData());
106
107 const wxWX2MBbuf cbuf = buf.mb_str();
108 Write(cbuf, strlen(cbuf));
109
110 head = head->Next();
111 }
112 }
113
114 bool wxHTTP::ParseHeaders()
115 {
116 wxString line;
117 wxStringTokenizer tokenzr;
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
130 tokenzr.SetString(line, " :\t\n\r");
131 if (!tokenzr.HasMoreTokens())
132 return FALSE;
133
134 wxString left_str = tokenzr.GetNextToken();
135 wxString *str = new wxString(tokenzr.GetNextToken());
136
137 m_headers.Append(left_str, (wxObject *) str);
138 }
139 return TRUE;
140 }
141
142 bool wxHTTP::Connect(const wxString& host)
143 {
144 wxIPV4address *addr;
145
146 if (m_connected) {
147 delete m_addr;
148 m_addr = NULL;
149 Close();
150 }
151
152 m_addr = addr = new wxIPV4address();
153
154 if (!addr->Hostname(host)) {
155 delete m_addr;
156 m_addr = NULL;
157 m_error = wxPROTO_NETERR;
158 return FALSE;
159 }
160
161 if (!addr->Service(_T("http")))
162 addr->Service(80);
163
164 return TRUE;
165 }
166
167 bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
168 {
169 struct sockaddr *raw_addr;
170 size_t len;
171
172 m_addr = (wxSockAddress *)(addr.GetClassInfo()->CreateObject());
173
174 addr.Build(raw_addr, len);
175 m_addr->Disassemble(raw_addr, len);
176
177 return TRUE;
178 }
179
180 bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
181 {
182 wxChar *tmp_buf;
183 wxChar buf[200];
184 wxWX2MBbuf pathbuf(200);
185 wxString tmp_str;
186
187 switch (req) {
188 case wxHTTP_GET:
189 tmp_buf = _T("GET");
190 break;
191 default:
192 return FALSE;
193 }
194
195 SaveState();
196 SetFlags(NONE);
197 Notify(FALSE);
198
199 tmp_str = wxURL::ConvertToValidURI(path);
200 wxSprintf(buf, _T("%s %s\n\r"), tmp_buf, tmp_str.GetData());
201 pathbuf = wxConvLibc.cWX2MB(buf);
202 Write(pathbuf, strlen(pathbuf));
203 SendHeaders();
204 Write("\n\r", 2);
205
206 m_error = GetLine(this, tmp_str);
207 if (m_error != wxPROTO_NOERR) {
208 RestoreState();
209 return FALSE;
210 }
211
212 if (!tmp_str.Contains(_T("HTTP/"))) {
213 // TODO: support HTTP v0.9 which can have no header.
214 SetHeader(_T("Content-Length"), _T("-1"));
215 SetHeader(_T("Content-Type"), _T("none/none"));
216 RestoreState();
217 return TRUE;
218 }
219
220 wxStringTokenizer token(tmp_str,_T(' '));
221 wxString tmp_str2;
222 bool ret_value;
223
224 token.NextToken();
225 tmp_str2 = token.NextToken();
226
227 switch (wxAtoi(tmp_str2)) {
228 case 200:
229 break;
230 default:
231 m_error = wxPROTO_NOFILE;
232 RestoreState();
233 return FALSE;
234 }
235
236 ret_value = ParseHeaders();
237 RestoreState();
238 return ret_value;
239 }
240
241 class wxHTTPStream : public wxSocketInputStream {
242 public:
243 wxHTTP *m_http;
244 size_t m_httpsize;
245
246 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
247 size_t StreamSize() const { return m_httpsize; }
248 virtual ~wxHTTPStream(void) { m_http->Abort(); }
249 };
250
251 bool wxHTTP::Abort(void)
252 {
253 return wxSocketClient::Close();
254 }
255
256 wxInputStream *wxHTTP::GetInputStream(const wxString& path)
257 {
258 wxHTTPStream *inp_stream = new wxHTTPStream(this);
259
260 if (!m_addr || m_connected) {
261 m_error = wxPROTO_CONNERR;
262 return NULL;
263 }
264
265 if (!wxProtocol::Connect(*m_addr))
266 return NULL;
267
268 if (!BuildRequest(path, wxHTTP_GET))
269 return NULL;
270
271 wxPrintf(_T("Len = %s\n"), WXSTRINGCAST GetHeader(_T("Content-Length")));
272 if (!GetHeader(_T("Content-Length")).IsEmpty())
273 inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(_T("Content-Length")));
274
275 SetFlags(WAITALL);
276 return inp_stream;
277 }
278
279 #endif
280 // wxUSE_SOCKETS