Made wxSocket compile using makefiles; #ifdefed out <<, >> operators in stream.cpp
[wxWidgets.git] / src / common / http.cpp
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
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
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
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
55 wxNode *node = m_headers.First();
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
100 while (head) {
101 wxString *str = (wxString *)head->Data();
102 char buf[100];
103
104 sprintf(buf, "%s: %s\n\r", head->key.string, str->GetData());
105 Write(buf, strlen(buf));
106
107 head = head->Next();
108 }
109 }
110
111 bool wxHTTP::ParseHeaders()
112 {
113 wxString line;
114
115 m_headers.Clear();
116 m_read = TRUE;
117
118 while (1) {
119 m_error = GetLine(this, line);
120 if (m_error != wxPROTO_NOERR)
121 return FALSE;
122
123 if (line.Length() == 0)
124 break;
125
126 int pos = line.Find(':');
127 if (pos == -1)
128 return FALSE;
129
130 wxString left_str = line(0, pos);
131 wxString right_str = line(pos+1, line.Length());
132
133 right_str = right_str.Strip(wxString::leading);
134
135 wxString *str = new wxString(right_str);
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;
157 m_error = wxPROTO_NETERR;
158 return FALSE;
159 }
160
161 if (!addr->Service("http"))
162 addr->Service(80);
163
164 return TRUE;
165 }
166
167 bool wxHTTP::Connect(wxSockAddress& addr)
168 {
169 struct sockaddr *raw_addr;
170 size_t len;
171
172 m_addr = (wxSockAddress *)(addr.GetClassInfo()->CreateObject());
173
174 addr.Build(raw_addr, len);
175 m_addr->Disassemble(raw_addr, len);
176
177 return TRUE;
178 }
179
180 bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
181 {
182 char *tmp_buf;
183 char buf[HTTP_BSIZE];
184
185 switch (req) {
186 case wxHTTP_GET:
187 tmp_buf = "GET";
188 break;
189 default:
190 return FALSE;
191 }
192
193 sprintf(buf, "%s %s HTTP/1.0\n\r", tmp_buf, (const char *)path);
194 Write(buf, strlen(buf));
195 SendHeaders();
196 sprintf(buf, "\n\r");
197 Write(buf, strlen(buf));
198
199 wxString tmp_str;
200
201 m_error = GetLine(this, tmp_str);
202 if (m_error != wxPROTO_NOERR)
203 return FALSE;
204
205 if (!tmp_str.Contains("HTTP/")) {
206 // TODO: support HTTP v0.9 which can have no header.
207 SetHeader("Content-Length", "-1");
208 SetHeader("Content-Type", "none/none");
209 return TRUE;
210 }
211
212 wxStringTokenizer token(tmp_str,' ');
213 wxString tmp_str2;
214
215 token.NextToken();
216 tmp_str2 = token.NextToken();
217
218 switch (atoi(tmp_str2)) {
219 case 200:
220 break;
221 default:
222 m_error = wxPROTO_NOFILE;
223 return FALSE;
224 }
225
226 return ParseHeaders();
227 }
228
229 class wxHTTPStream : public wxSocketInputStream {
230 public:
231 wxHTTP *m_http;
232 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
233 virtual ~wxHTTPStream(void) { m_http->Abort(); }
234 };
235
236 bool wxHTTP::Abort(void)
237 {
238 return wxSocketClient::Close();
239 }
240
241 wxInputStream *wxHTTP::GetInputStream(const wxString& path)
242 {
243 wxHTTPStream *inp_stream = new wxHTTPStream(this);
244
245 if (!m_addr || m_connected) {
246 m_error = wxPROTO_CONNERR;
247 return NULL;
248 }
249
250 if (!wxProtocol::Connect(*m_addr))
251 return NULL;
252
253 if (!BuildRequest(path, wxHTTP_GET))
254 return NULL;
255
256 return inp_stream;
257 }