1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HTTP protocol
4 // Author: Guilhem Lavaux
6 // Created: August 1997
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "http.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/string.h"
28 #include "wx/tokenzr.h"
29 #include "wx/socket.h"
30 #include "wx/protocol/protocol.h"
32 #include "wx/protocol/http.h"
33 #include "wx/sckstrm.h"
35 IMPLEMENT_DYNAMIC_CLASS(wxHTTP
, wxProtocol
)
36 IMPLEMENT_PROTOCOL(wxHTTP
, wxT("http"), wxT("80"), TRUE
)
38 #define HTTP_BSIZE 2048
42 m_headers(wxKEY_STRING
)
48 SetNotify(GSOCK_LOST_FLAG
);
53 // wxString isn't a wxObject
54 wxNode
*node
= m_headers
.First();
58 string
= (wxString
*)node
->Data();
64 wxString
wxHTTP::GetContentType()
66 return GetHeader(wxT("Content-Type"));
69 void wxHTTP::SetProxyMode(bool on
)
74 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
81 wxNode
*node
= m_headers
.Find(header
);
84 m_headers
.Append(header
, (wxObject
*)(new wxString(h_data
)));
86 wxString
*str
= (wxString
*)node
->Data();
91 wxString
wxHTTP::GetHeader(const wxString
& header
)
94 wxString upper_header
;
96 upper_header
= header
.Upper();
98 node
= m_headers
.Find(upper_header
);
100 return wxEmptyString
;
102 return *((wxString
*)node
->Data());
105 void wxHTTP::SendHeaders()
107 wxNode
*head
= m_headers
.First();
111 wxString
*str
= (wxString
*)head
->Data();
114 buf
.Printf(wxT("%s: %s\n\r"), head
->GetKeyString(), str
->GetData());
116 const wxWX2MBbuf cbuf
= buf
.mb_str();
117 Write(cbuf
, strlen(cbuf
));
123 bool wxHTTP::ParseHeaders()
126 wxStringTokenizer tokenzr
;
131 #if defined(__VISAGECPP__)
132 // VA just can't stand while(1)
138 m_perr
= GetLine(this, line
);
139 if (m_perr
!= wxPROTO_NOERR
)
142 if (line
.Length() == 0)
145 wxString left_str
= line
.BeforeFirst(':');
146 wxString
*str
= new wxString(line
.AfterFirst(':').Strip(wxString::both
));
147 left_str
.MakeUpper();
149 m_headers
.Append(left_str
, (wxObject
*) str
);
154 bool wxHTTP::Connect(const wxString
& host
)
164 m_addr
= addr
= new wxIPV4address();
166 if (!addr
->Hostname(host
)) {
169 m_perr
= wxPROTO_NETERR
;
173 if (!addr
->Service(wxT("http")))
179 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
187 m_addr
= (wxSockAddress
*) addr
.Clone();
191 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
194 wxChar buf
[200]; // 200 is arbitrary.
195 wxString tmp_str
= path
;
197 // If there is no User-Agent defined, define it.
198 if (GetHeader(wxT("User-Agent")).IsNull())
199 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
203 tmp_buf
= wxT("GET");
213 wxSprintf(buf
, wxT("%s %s HTTP/1.0\n\r"), tmp_buf
, tmp_str
.GetData());
214 const wxWX2MBbuf pathbuf
= wxConvLibc
.cWX2MB(buf
);
215 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
219 m_perr
= GetLine(this, tmp_str
);
220 if (m_perr
!= wxPROTO_NOERR
) {
225 if (!tmp_str
.Contains(wxT("HTTP/"))) {
226 // TODO: support HTTP v0.9 which can have no header.
227 // FIXME: tmp_str is not put back in the in-queue of the socket.
228 SetHeader(wxT("Content-Length"), wxT("-1"));
229 SetHeader(wxT("Content-Type"), wxT("none/none"));
234 wxStringTokenizer
token(tmp_str
,wxT(' '));
239 tmp_str2
= token
.NextToken();
241 switch (tmp_str2
[(unsigned int) 0]) {
243 /* INFORMATION / SUCCESS */
252 m_perr
= wxPROTO_NOFILE
;
257 ret_value
= ParseHeaders();
262 class wxHTTPStream
: public wxSocketInputStream
{
266 unsigned long m_read_bytes
;
268 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
269 size_t GetSize() const { return m_httpsize
; }
270 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
273 size_t OnSysRead(void *buffer
, size_t bufsize
);
276 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
280 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
283 ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
289 bool wxHTTP::Abort(void)
293 ret
= wxSocketClient::Close();
298 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
300 wxHTTPStream
*inp_stream
= new wxHTTPStream(this);
303 m_perr
= wxPROTO_CONNERR
;
307 // We set m_connected back to FALSE so wxSocketBase will know what to do.
308 if (!wxProtocol::Connect(*m_addr
))
311 if (!BuildRequest(path
, wxHTTP_GET
))
314 if (!GetHeader(wxT("Content-Length")).IsEmpty())
315 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
317 inp_stream
->m_httpsize
= (size_t)-1;
319 inp_stream
->m_read_bytes
= 0;
322 SetFlags(SPEED
| WAITALL
);