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