]>
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__ | |
2df7be7f | 13 | #pragma implementation "http.h" |
f4ada568 GL |
14 | #endif |
15 | ||
fcc6dddd JS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
ce4169a4 | 20 | #pragma hdrstop |
fcc6dddd JS |
21 | #endif |
22 | ||
ce4169a4 RR |
23 | #if wxUSE_SOCKETS |
24 | ||
f4ada568 GL |
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" | |
fae05df5 | 31 | #include "wx/url.h" |
f4ada568 GL |
32 | #include "wx/protocol/http.h" |
33 | #include "wx/sckstrm.h" | |
34 | ||
f4ada568 GL |
35 | #if !USE_SHARED_LIBRARY |
36 | IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol) | |
4846abaf | 37 | IMPLEMENT_PROTOCOL(wxHTTP, _T("http"), _T("80"), TRUE) |
f4ada568 GL |
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 | ||
a324a7bc | 49 | SetNotify(GSOCK_LOST_FLAG); |
f4ada568 GL |
50 | } |
51 | ||
52 | wxHTTP::~wxHTTP() | |
53 | { | |
54 | // wxString isn't a wxObject | |
1f01991f | 55 | wxNode *node = m_headers.First(); |
f4ada568 GL |
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 | { | |
4846abaf | 67 | return GetHeader(_T("Content-Type")); |
f4ada568 GL |
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) | |
ce3ed50d | 91 | return wxEmptyString; |
f4ada568 GL |
92 | |
93 | return *((wxString *)node->Data()); | |
94 | } | |
95 | ||
96 | void wxHTTP::SendHeaders() | |
97 | { | |
98 | wxNode *head = m_headers.First(); | |
99 | ||
53c6e7cc VZ |
100 | while (head) |
101 | { | |
f4ada568 | 102 | wxString *str = (wxString *)head->Data(); |
f4ada568 | 103 | |
53c6e7cc | 104 | wxString buf; |
4846abaf | 105 | buf.Printf(_T("%s: %s\n\r"), head->GetKeyString(), str->GetData()); |
53c6e7cc | 106 | |
fde7d98e | 107 | const wxWX2MBbuf cbuf = buf.mb_str(); |
4846abaf | 108 | Write(cbuf, strlen(cbuf)); |
f4ada568 GL |
109 | |
110 | head = head->Next(); | |
111 | } | |
112 | } | |
113 | ||
114 | bool wxHTTP::ParseHeaders() | |
115 | { | |
116 | wxString line; | |
a737331d | 117 | wxStringTokenizer tokenzr; |
f4ada568 GL |
118 | |
119 | m_headers.Clear(); | |
120 | m_read = TRUE; | |
121 | ||
122 | while (1) { | |
a324a7bc GL |
123 | m_perr = GetLine(this, line); |
124 | if (m_perr != wxPROTO_NOERR) | |
f4ada568 GL |
125 | return FALSE; |
126 | ||
127 | if (line.Length() == 0) | |
128 | break; | |
129 | ||
a737331d | 130 | tokenzr.SetString(line, " :\t\n\r"); |
91b8de8d | 131 | if (!tokenzr.HasMoreTokens()) |
f4ada568 GL |
132 | return FALSE; |
133 | ||
a737331d GL |
134 | wxString left_str = tokenzr.GetNextToken(); |
135 | wxString *str = new wxString(tokenzr.GetNextToken()); | |
f4ada568 GL |
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; | |
a324a7bc | 157 | m_perr = wxPROTO_NETERR; |
f4ada568 GL |
158 | return FALSE; |
159 | } | |
160 | ||
4846abaf | 161 | if (!addr->Service(_T("http"))) |
f4ada568 GL |
162 | addr->Service(80); |
163 | ||
164 | return TRUE; | |
165 | } | |
166 | ||
8a2c6ef8 | 167 | bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait)) |
f4ada568 | 168 | { |
a324a7bc GL |
169 | if (m_addr) { |
170 | delete m_addr; | |
171 | m_addr = NULL; | |
172 | Close(); | |
173 | } | |
f4ada568 | 174 | |
a324a7bc | 175 | m_addr = (wxSockAddress *) addr.Clone(); |
f4ada568 GL |
176 | return TRUE; |
177 | } | |
178 | ||
179 | bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req) | |
180 | { | |
fae05df5 | 181 | wxChar *tmp_buf; |
a324a7bc GL |
182 | wxChar buf[200]; // 200 is arbitrary. |
183 | #if wxUNICODE | |
184 | wxWX2MBbuf pathbuf((size_t)200); | |
185 | #else | |
186 | const wxWX2MBbuf pathbuf; | |
187 | #endif | |
fae05df5 | 188 | wxString tmp_str; |
f4ada568 GL |
189 | |
190 | switch (req) { | |
191 | case wxHTTP_GET: | |
fae05df5 | 192 | tmp_buf = _T("GET"); |
f4ada568 GL |
193 | break; |
194 | default: | |
195 | return FALSE; | |
196 | } | |
197 | ||
41895a05 | 198 | SaveState(); |
062c4861 | 199 | SetFlags(NONE); |
41895a05 | 200 | Notify(FALSE); |
41895a05 | 201 | |
fae05df5 | 202 | tmp_str = wxURL::ConvertToValidURI(path); |
a324a7bc | 203 | wxSprintf(buf, _T("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData()); |
fae05df5 | 204 | pathbuf = wxConvLibc.cWX2MB(buf); |
a324a7bc | 205 | Write(pathbuf, strlen(MBSTRINGCAST pathbuf)); |
f4ada568 | 206 | SendHeaders(); |
fae05df5 | 207 | Write("\n\r", 2); |
f4ada568 | 208 | |
a324a7bc GL |
209 | m_perr = GetLine(this, tmp_str); |
210 | if (m_perr != wxPROTO_NOERR) { | |
41895a05 | 211 | RestoreState(); |
f4ada568 | 212 | return FALSE; |
41895a05 | 213 | } |
f4ada568 | 214 | |
4846abaf | 215 | if (!tmp_str.Contains(_T("HTTP/"))) { |
f4ada568 | 216 | // TODO: support HTTP v0.9 which can have no header. |
4846abaf OK |
217 | SetHeader(_T("Content-Length"), _T("-1")); |
218 | SetHeader(_T("Content-Type"), _T("none/none")); | |
41895a05 | 219 | RestoreState(); |
f4ada568 GL |
220 | return TRUE; |
221 | } | |
222 | ||
4846abaf | 223 | wxStringTokenizer token(tmp_str,_T(' ')); |
f4ada568 | 224 | wxString tmp_str2; |
41895a05 | 225 | bool ret_value; |
f4ada568 GL |
226 | |
227 | token.NextToken(); | |
228 | tmp_str2 = token.NextToken(); | |
229 | ||
4846abaf | 230 | switch (wxAtoi(tmp_str2)) { |
f4ada568 GL |
231 | case 200: |
232 | break; | |
233 | default: | |
a324a7bc | 234 | m_perr = wxPROTO_NOFILE; |
41895a05 | 235 | RestoreState(); |
f4ada568 GL |
236 | return FALSE; |
237 | } | |
238 | ||
41895a05 GL |
239 | ret_value = ParseHeaders(); |
240 | RestoreState(); | |
241 | return ret_value; | |
f4ada568 GL |
242 | } |
243 | ||
244 | class wxHTTPStream : public wxSocketInputStream { | |
245 | public: | |
246 | wxHTTP *m_http; | |
9a1b2c28 | 247 | size_t m_httpsize; |
a324a7bc | 248 | unsigned long m_read_bytes; |
9a1b2c28 | 249 | |
f4ada568 | 250 | wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {} |
41895a05 | 251 | size_t StreamSize() const { return m_httpsize; } |
f4ada568 | 252 | virtual ~wxHTTPStream(void) { m_http->Abort(); } |
a324a7bc GL |
253 | |
254 | protected: | |
255 | size_t OnSysRead(void *buffer, size_t bufsize); | |
f4ada568 GL |
256 | }; |
257 | ||
a324a7bc GL |
258 | size_t wxHTTPStream::OnSysRead(void *buffer, size_t bufsize) |
259 | { | |
260 | size_t ret; | |
261 | ||
262 | if (m_httpsize > 0 && m_read_bytes >= m_httpsize) | |
263 | return 0; | |
264 | ||
265 | ret = wxSocketInputStream::OnSysRead(buffer, bufsize); | |
266 | m_read_bytes += ret; | |
267 | ||
268 | return ret; | |
269 | } | |
270 | ||
f4ada568 GL |
271 | bool wxHTTP::Abort(void) |
272 | { | |
273 | return wxSocketClient::Close(); | |
274 | } | |
275 | ||
276 | wxInputStream *wxHTTP::GetInputStream(const wxString& path) | |
277 | { | |
278 | wxHTTPStream *inp_stream = new wxHTTPStream(this); | |
279 | ||
280 | if (!m_addr || m_connected) { | |
a324a7bc | 281 | m_perr = wxPROTO_CONNERR; |
f4ada568 GL |
282 | return NULL; |
283 | } | |
284 | ||
285 | if (!wxProtocol::Connect(*m_addr)) | |
286 | return NULL; | |
287 | ||
288 | if (!BuildRequest(path, wxHTTP_GET)) | |
289 | return NULL; | |
290 | ||
4846abaf OK |
291 | if (!GetHeader(_T("Content-Length")).IsEmpty()) |
292 | inp_stream->m_httpsize = wxAtoi(WXSTRINGCAST GetHeader(_T("Content-Length"))); | |
a324a7bc GL |
293 | else |
294 | inp_stream->m_httpsize = (size_t)-1; | |
295 | ||
296 | inp_stream->m_read_bytes = 0; | |
297 | ||
298 | Notify(FALSE); | |
9a1b2c28 | 299 | |
f4ada568 GL |
300 | return inp_stream; |
301 | } | |
35a4dab7 GL |
302 | |
303 | #endif | |
304 | // wxUSE_SOCKETS |