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