]> git.saurik.com Git - wxWidgets.git/blob - src/common/http.cpp
FreeBSD compilation fixes
[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, "http", "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("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("%s: %s\n\r", head->GetKeyString(), str->GetData());
108
109 Write(buf, buf.Len());
110
111 head = head->Next();
112 }
113 }
114
115 bool 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
130 printf("Header: %s\n", WXSTRINGCAST line);
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
147 bool 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
172 bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
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
185 bool 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
198 SaveState();
199 Notify(FALSE);
200 SetFlags(WAITALL);
201
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);
211 if (m_error != wxPROTO_NOERR) {
212 RestoreState();
213 return FALSE;
214 }
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");
220 RestoreState();
221 return TRUE;
222 }
223
224 wxStringTokenizer token(tmp_str,' ');
225 wxString tmp_str2;
226 bool ret_value;
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;
236 RestoreState();
237 return FALSE;
238 }
239
240 ret_value = ParseHeaders();
241 RestoreState();
242 return ret_value;
243 }
244
245 class wxHTTPStream : public wxSocketInputStream {
246 public:
247 wxHTTP *m_http;
248 size_t m_httpsize;
249
250 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
251 size_t StreamSize() const { return m_httpsize; }
252 virtual ~wxHTTPStream(void) { m_http->Abort(); }
253 };
254
255 bool wxHTTP::Abort(void)
256 {
257 return wxSocketClient::Close();
258 }
259
260 wxInputStream *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
275 printf("Len = %s\n", WXSTRINGCAST GetHeader("Content-Length"));
276 if (!GetHeader("Content-Length").IsEmpty())
277 inp_stream->m_httpsize = atoi(WXSTRINGCAST GetHeader("Content-Length"));
278
279 return inp_stream;
280 }
281
282 #endif
283 // wxUSE_SOCKETS