]>
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 | ||
23 | #ifndef WX_PRECOMP | |
24 | #endif | |
25 | ||
f4ada568 GL |
26 | #include <stdio.h> |
27 | #include <stdlib.h> | |
28 | #include "wx/string.h" | |
29 | #include "wx/tokenzr.h" | |
30 | #include "wx/socket.h" | |
31 | #include "wx/protocol/protocol.h" | |
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) | |
37 | IMPLEMENT_PROTOCOL(wxHTTP, "http", "80", TRUE) | |
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 | ||
49 | SetNotify(REQ_LOST); | |
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 | { | |
67 | return GetHeader("Content-Type"); | |
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) | |
91 | return (char *)NULL; | |
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 VZ |
104 | wxString buf; |
105 | buf.Printf("%s: %s\n\r", head->GetKeyString(), str->GetData()); | |
106 | ||
107 | Write(buf, buf.Len()); | |
f4ada568 GL |
108 | |
109 | head = head->Next(); | |
110 | } | |
111 | } | |
112 | ||
113 | bool wxHTTP::ParseHeaders() | |
114 | { | |
115 | wxString line; | |
116 | ||
117 | m_headers.Clear(); | |
118 | m_read = TRUE; | |
119 | ||
120 | while (1) { | |
121 | m_error = GetLine(this, line); | |
122 | if (m_error != wxPROTO_NOERR) | |
123 | return FALSE; | |
124 | ||
125 | if (line.Length() == 0) | |
126 | break; | |
127 | ||
128 | int pos = line.Find(':'); | |
129 | if (pos == -1) | |
130 | return FALSE; | |
131 | ||
132 | wxString left_str = line(0, pos); | |
133 | wxString right_str = line(pos+1, line.Length()); | |
134 | ||
135 | right_str = right_str.Strip(wxString::leading); | |
136 | ||
137 | wxString *str = new wxString(right_str); | |
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 | ||
163 | if (!addr->Service("http")) | |
164 | addr->Service(80); | |
165 | ||
166 | return TRUE; | |
167 | } | |
168 | ||
169 | bool wxHTTP::Connect(wxSockAddress& addr) | |
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]; | |
186 | ||
187 | switch (req) { | |
188 | case wxHTTP_GET: | |
189 | tmp_buf = "GET"; | |
190 | break; | |
191 | default: | |
192 | return FALSE; | |
193 | } | |
194 | ||
195 | sprintf(buf, "%s %s HTTP/1.0\n\r", tmp_buf, (const char *)path); | |
196 | Write(buf, strlen(buf)); | |
197 | SendHeaders(); | |
198 | sprintf(buf, "\n\r"); | |
199 | Write(buf, strlen(buf)); | |
200 | ||
201 | wxString tmp_str; | |
202 | ||
203 | m_error = GetLine(this, tmp_str); | |
204 | if (m_error != wxPROTO_NOERR) | |
205 | return FALSE; | |
206 | ||
207 | if (!tmp_str.Contains("HTTP/")) { | |
208 | // TODO: support HTTP v0.9 which can have no header. | |
209 | SetHeader("Content-Length", "-1"); | |
210 | SetHeader("Content-Type", "none/none"); | |
211 | return TRUE; | |
212 | } | |
213 | ||
214 | wxStringTokenizer token(tmp_str,' '); | |
215 | wxString tmp_str2; | |
216 | ||
217 | token.NextToken(); | |
218 | tmp_str2 = token.NextToken(); | |
219 | ||
220 | switch (atoi(tmp_str2)) { | |
221 | case 200: | |
222 | break; | |
223 | default: | |
224 | m_error = wxPROTO_NOFILE; | |
225 | return FALSE; | |
226 | } | |
227 | ||
228 | return ParseHeaders(); | |
229 | } | |
230 | ||
231 | class wxHTTPStream : public wxSocketInputStream { | |
232 | public: | |
233 | wxHTTP *m_http; | |
234 | wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {} | |
235 | virtual ~wxHTTPStream(void) { m_http->Abort(); } | |
236 | }; | |
237 | ||
238 | bool wxHTTP::Abort(void) | |
239 | { | |
240 | return wxSocketClient::Close(); | |
241 | } | |
242 | ||
243 | wxInputStream *wxHTTP::GetInputStream(const wxString& path) | |
244 | { | |
245 | wxHTTPStream *inp_stream = new wxHTTPStream(this); | |
246 | ||
247 | if (!m_addr || m_connected) { | |
248 | m_error = wxPROTO_CONNERR; | |
249 | return NULL; | |
250 | } | |
251 | ||
252 | if (!wxProtocol::Connect(*m_addr)) | |
253 | return NULL; | |
254 | ||
255 | if (!BuildRequest(path, wxHTTP_GET)) | |
256 | return NULL; | |
257 | ||
258 | return inp_stream; | |
259 | } |