]> git.saurik.com Git - wxWidgets.git/blame - src/common/http.cpp
* wxSocket fixes: FTP, HTTP works really now. GTK fixes to prevent infinite loop.
[wxWidgets.git] / src / common / http.cpp
CommitLineData
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
36IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
37IMPLEMENT_PROTOCOL(wxHTTP, "http", "80", TRUE)
38#endif
39
40#define HTTP_BSIZE 2048
41
42wxHTTP::wxHTTP()
43 : wxProtocol(),
44 m_headers(wxKEY_STRING)
45{
46 m_addr = NULL;
47 m_read = FALSE;
48
49 SetNotify(REQ_LOST);
50}
51
52wxHTTP::~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
65wxString wxHTTP::GetContentType()
66{
67 return GetHeader("Content-Type");
68}
69
70void 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
87wxString 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
96void 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
113bool 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
41895a05 128 printf("Header: %s\n", WXSTRINGCAST line);
f4ada568
GL
129 int pos = line.Find(':');
130 if (pos == -1)
131 return FALSE;
132
133 wxString left_str = line(0, pos);
134 wxString right_str = line(pos+1, line.Length());
135
136 right_str = right_str.Strip(wxString::leading);
137
138 wxString *str = new wxString(right_str);
139
140 m_headers.Append(left_str, (wxObject *) str);
141 }
142 return TRUE;
143}
144
145bool 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
164 if (!addr->Service("http"))
165 addr->Service(80);
166
167 return TRUE;
168}
169
8a2c6ef8 170bool 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
183bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
184{
185 char *tmp_buf;
186 char buf[HTTP_BSIZE];
187
188 switch (req) {
189 case wxHTTP_GET:
190 tmp_buf = "GET";
191 break;
192 default:
193 return FALSE;
194 }
195
41895a05
GL
196 SaveState();
197 Notify(FALSE);
198 SetFlags(WAITALL);
199
f4ada568
GL
200 sprintf(buf, "%s %s HTTP/1.0\n\r", tmp_buf, (const char *)path);
201 Write(buf, strlen(buf));
202 SendHeaders();
203 sprintf(buf, "\n\r");
204 Write(buf, strlen(buf));
205
206 wxString tmp_str;
207
208 m_error = GetLine(this, tmp_str);
41895a05
GL
209 if (m_error != wxPROTO_NOERR) {
210 RestoreState();
f4ada568 211 return FALSE;
41895a05 212 }
f4ada568
GL
213
214 if (!tmp_str.Contains("HTTP/")) {
215 // TODO: support HTTP v0.9 which can have no header.
216 SetHeader("Content-Length", "-1");
217 SetHeader("Content-Type", "none/none");
41895a05 218 RestoreState();
f4ada568
GL
219 return TRUE;
220 }
221
222 wxStringTokenizer token(tmp_str,' ');
223 wxString tmp_str2;
41895a05 224 bool ret_value;
f4ada568
GL
225
226 token.NextToken();
227 tmp_str2 = token.NextToken();
228
229 switch (atoi(tmp_str2)) {
230 case 200:
231 break;
232 default:
233 m_error = wxPROTO_NOFILE;
41895a05 234 RestoreState();
f4ada568
GL
235 return FALSE;
236 }
237
41895a05
GL
238 ret_value = ParseHeaders();
239 RestoreState();
240 return ret_value;
f4ada568
GL
241}
242
243class wxHTTPStream : public wxSocketInputStream {
244public:
245 wxHTTP *m_http;
9a1b2c28
GL
246 size_t m_httpsize;
247
f4ada568 248 wxHTTPStream(wxHTTP *http) : wxSocketInputStream(*http), m_http(http) {}
41895a05 249 size_t StreamSize() const { return m_httpsize; }
f4ada568
GL
250 virtual ~wxHTTPStream(void) { m_http->Abort(); }
251};
252
253bool wxHTTP::Abort(void)
254{
255 return wxSocketClient::Close();
256}
257
258wxInputStream *wxHTTP::GetInputStream(const wxString& path)
259{
260 wxHTTPStream *inp_stream = new wxHTTPStream(this);
261
262 if (!m_addr || m_connected) {
263 m_error = wxPROTO_CONNERR;
264 return NULL;
265 }
266
267 if (!wxProtocol::Connect(*m_addr))
268 return NULL;
269
270 if (!BuildRequest(path, wxHTTP_GET))
271 return NULL;
272
41895a05
GL
273 printf("Len = %s\n", WXSTRINGCAST GetHeader("Content-Length"));
274 if (!GetHeader("Content-Length").IsEmpty())
9a1b2c28
GL
275 inp_stream->m_httpsize = atoi(WXSTRINGCAST GetHeader("Content-Length"));
276
f4ada568
GL
277 return inp_stream;
278}