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