]>
Commit | Line | Data |
---|---|---|
f4ada568 GL |
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 | ||
fcc6dddd JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
35a4dab7 GL |
23 | #if wxUSE_SOCKETS |
24 | ||
fcc6dddd JS |
25 | #ifndef WX_PRECOMP |
26 | #endif | |
27 | ||
f4ada568 GL |
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 | ||
f4ada568 GL |
37 | #if !USE_SHARED_LIBRARY |
38 | IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol) | |
4846abaf | 39 | IMPLEMENT_PROTOCOL(wxHTTP, _T("http"), _T("80"), TRUE) |
f4ada568 GL |
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 | |
1f01991f | 57 | wxNode *node = m_headers.First(); |
f4ada568 GL |
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 | { | |
4846abaf | 69 | return GetHeader(_T("Content-Type")); |
f4ada568 GL |
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) | |
ce3ed50d | 93 | return wxEmptyString; |
f4ada568 GL |
94 | |
95 | return *((wxString *)node->Data()); | |
96 | } | |
97 | ||
98 | void wxHTTP::SendHeaders() | |
99 | { | |
100 | wxNode *head = m_headers.First(); | |
101 | ||
53c6e7cc VZ |
102 | while (head) |
103 | { | |
f4ada568 | 104 | wxString *str = (wxString *)head->Data(); |
f4ada568 | 105 | |
53c6e7cc | 106 | wxString buf; |
4846abaf | 107 | buf.Printf(_T("%s: %s\n\r"), head->GetKeyString(), str->GetData()); |
53c6e7cc | 108 | |
fde7d98e | 109 | const wxWX2MBbuf cbuf = buf.mb_str(); |
4846abaf | 110 | Write(cbuf, strlen(cbuf)); |
f4ada568 GL |
111 | |
112 | head = head->Next(); | |
113 | } | |
114 | } | |
115 | ||
116 | bool wxHTTP::ParseHeaders() | |
117 | { | |
118 | wxString line; | |
a737331d | 119 | wxStringTokenizer tokenzr; |
f4ada568 GL |
120 | |
121 | m_headers.Clear(); | |
122 | m_read = TRUE; | |
123 | ||
124 | while (1) { | |
125 | m_error = GetLine(this, line); | |
126 | if (m_error != wxPROTO_NOERR) | |
127 | return FALSE; | |
128 | ||
129 | if (line.Length() == 0) | |
130 | break; | |
131 | ||
a737331d GL |
132 | tokenzr.SetString(line, " :\t\n\r"); |
133 | if (!tokenzr.HasMoreToken()) | |
f4ada568 GL |
134 | return FALSE; |
135 | ||
a737331d GL |
136 | wxString left_str = tokenzr.GetNextToken(); |
137 | wxString *str = new wxString(tokenzr.GetNextToken()); | |
f4ada568 GL |
138 | |
139 | m_headers.Append(left_str, (wxObject *) str); | |
140 | } | |
141 | return TRUE; | |
142 | } | |
143 | ||
144 | bool wxHTTP::Connect(const wxString& host) | |
145 | { | |
146 | wxIPV4address *addr; | |
147 | ||
148 | if (m_connected) { | |
149 | delete m_addr; | |
150 | m_addr = NULL; | |
151 | Close(); | |
152 | } | |
153 | ||
154 | m_addr = addr = new wxIPV4address(); | |
155 | ||
156 | if (!addr->Hostname(host)) { | |
157 | delete m_addr; | |
158 | m_addr = NULL; | |
159 | m_error = wxPROTO_NETERR; | |
160 | return FALSE; | |
161 | } | |
162 | ||
4846abaf | 163 | if (!addr->Service(_T("http"))) |
f4ada568 GL |
164 | addr->Service(80); |
165 | ||
166 | return TRUE; | |
167 | } | |
168 | ||
8a2c6ef8 | 169 | bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait)) |
f4ada568 GL |
170 | { |
171 | struct sockaddr *raw_addr; | |
172 | size_t len; | |
173 | ||
174 | m_addr = (wxSockAddress *)(addr.GetClassInfo()->CreateObject()); | |
175 | ||
176 | addr.Build(raw_addr, len); | |
177 | m_addr->Disassemble(raw_addr, len); | |
178 | ||
179 | return TRUE; | |
180 | } | |
181 | ||
182 | bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req) | |
183 | { | |
184 | char *tmp_buf; | |
185 | char buf[HTTP_BSIZE]; | |
fde7d98e | 186 | const wxWX2MBbuf pathbuf = path.mb_str(); |
f4ada568 GL |
187 | |
188 | switch (req) { | |
189 | case wxHTTP_GET: | |
190 | tmp_buf = "GET"; | |
191 | break; | |
192 | default: | |
193 | return FALSE; | |
194 | } | |
195 | ||
41895a05 | 196 | SaveState(); |
062c4861 | 197 | SetFlags(NONE); |
41895a05 | 198 | Notify(FALSE); |
41895a05 | 199 | |
1759ff9e | 200 | sprintf(buf, "%s %s\n\r", tmp_buf, (const char*) pathbuf); |
f4ada568 GL |
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); | |
41895a05 GL |
209 | if (m_error != wxPROTO_NOERR) { |
210 | RestoreState(); | |
f4ada568 | 211 | return FALSE; |
41895a05 | 212 | } |
f4ada568 | 213 | |
4846abaf | 214 | if (!tmp_str.Contains(_T("HTTP/"))) { |
f4ada568 | 215 | // TODO: support HTTP v0.9 which can have no header. |
4846abaf OK |
216 | SetHeader(_T("Content-Length"), _T("-1")); |
217 | SetHeader(_T("Content-Type"), _T("none/none")); | |
41895a05 | 218 | RestoreState(); |
f4ada568 GL |
219 | return TRUE; |
220 | } | |
221 | ||
4846abaf | 222 | wxStringTokenizer token(tmp_str,_T(' ')); |
f4ada568 | 223 | wxString tmp_str2; |
41895a05 | 224 | bool ret_value; |
f4ada568 GL |
225 | |
226 | token.NextToken(); | |
227 | tmp_str2 = token.NextToken(); | |
228 | ||
4846abaf | 229 | switch (wxAtoi(tmp_str2)) { |
f4ada568 GL |
230 | case 200: |
231 | break; | |
232 | default: | |
233 | m_error = wxPROTO_NOFILE; | |
41895a05 | 234 | RestoreState(); |
f4ada568 GL |
235 | return FALSE; |
236 | } | |
237 | ||
41895a05 GL |
238 | ret_value = ParseHeaders(); |
239 | RestoreState(); | |
240 | return ret_value; | |
f4ada568 GL |
241 | } |
242 | ||
243 | class wxHTTPStream : public wxSocketInputStream { | |
244 | public: | |
245 | wxHTTP *m_http; | |
9a1b2c28 GL |
246 | size_t m_httpsize; |
247 | ||
f4ada568 | 248 | wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {} |
41895a05 | 249 | size_t StreamSize() const { return m_httpsize; } |
f4ada568 GL |
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 | ||
4846abaf OK |
273 | wxPrintf(_T("Len = %s\n"), WXSTRINGCAST GetHeader(_T("Content-Length"))); |
274 | if (!GetHeader(_T("Content-Length")).IsEmpty()) | |
275 | inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(_T("Content-Length"))); | |
9a1b2c28 | 276 | |
062c4861 | 277 | SetFlags(WAITALL); |
f4ada568 GL |
278 | return inp_stream; |
279 | } | |
35a4dab7 GL |
280 | |
281 | #endif | |
282 | // wxUSE_SOCKETS |