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 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_DYNAMIC_CLASS(wxHTTP
, wxProtocol
)
37 IMPLEMENT_PROTOCOL(wxHTTP
, _T("http"), _T("80"), TRUE
)
40 #define HTTP_BSIZE 2048
44 m_headers(wxKEY_STRING
)
50 SetNotify(GSOCK_LOST_FLAG
);
55 // wxString isn't a wxObject
56 wxNode
*node
= m_headers
.First();
60 string
= (wxString
*)node
->Data();
66 wxString
wxHTTP::GetContentType()
68 return GetHeader(_T("Content-Type"));
71 void wxHTTP::SetProxyMode(bool on
)
76 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
83 wxNode
*node
= m_headers
.Find(header
);
86 m_headers
.Append(header
, (wxObject
*)(new wxString(h_data
)));
88 wxString
*str
= (wxString
*)node
->Data();
93 wxString
wxHTTP::GetHeader(const wxString
& header
)
96 wxString upper_header
;
98 upper_header
= header
.Upper();
100 node
= m_headers
.Find(upper_header
);
102 return wxEmptyString
;
104 return *((wxString
*)node
->Data());
107 void wxHTTP::SendHeaders()
109 wxNode
*head
= m_headers
.First();
113 wxString
*str
= (wxString
*)head
->Data();
116 buf
.Printf(_T("%s: %s\n\r"), head
->GetKeyString(), str
->GetData());
118 const wxWX2MBbuf cbuf
= buf
.mb_str();
119 Write(cbuf
, strlen(cbuf
));
125 bool wxHTTP::ParseHeaders()
128 wxStringTokenizer tokenzr
;
134 m_perr
= GetLine(this, line
);
135 if (m_perr
!= wxPROTO_NOERR
)
138 if (line
.Length() == 0)
141 tokenzr
.SetString(line
, " :\t\n\r");
142 if (!tokenzr
.HasMoreTokens())
145 wxString left_str
= tokenzr
.GetNextToken();
146 wxString
*str
= new wxString(tokenzr
.GetNextToken());
148 left_str
.MakeUpper();
150 m_headers
.Append(left_str
, (wxObject
*) str
);
155 bool wxHTTP::Connect(const wxString
& host
)
165 m_addr
= addr
= new wxIPV4address();
167 if (!addr
->Hostname(host
)) {
170 m_perr
= wxPROTO_NETERR
;
174 if (!addr
->Service(_T("http")))
180 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
188 m_addr
= (wxSockAddress
*) addr
.Clone();
192 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
195 wxChar buf
[200]; // 200 is arbitrary.
196 wxString tmp_str
= path
;
198 // If there is no User-Agent defined, define it.
199 if (GetHeader(_T("User-Agent")).IsNull())
200 SetHeader(_T("User-Agent"), _T("wxWindows 2.x"));
214 wxSprintf(buf
, _T("%s %s HTTP/1.0\n\r"), tmp_buf
, tmp_str
.GetData());
215 const wxWX2MBbuf pathbuf
= wxConvLibc
.cWX2MB(buf
);
216 Write(pathbuf
, strlen(MBSTRINGCAST pathbuf
));
220 m_perr
= GetLine(this, tmp_str
);
221 if (m_perr
!= wxPROTO_NOERR
) {
226 if (!tmp_str
.Contains(_T("HTTP/"))) {
227 // TODO: support HTTP v0.9 which can have no header.
228 // FIXME: tmp_str is not put back in the in-queue of the socket.
229 SetHeader(_T("Content-Length"), _T("-1"));
230 SetHeader(_T("Content-Type"), _T("none/none"));
235 wxStringTokenizer
token(tmp_str
,_T(' '));
240 tmp_str2
= token
.NextToken();
242 switch (tmp_str2
[(unsigned int) 0]) {
244 /* INFORMATION / SUCCESS */
253 m_perr
= wxPROTO_NOFILE
;
258 ret_value
= ParseHeaders();
263 class wxHTTPStream
: public wxSocketInputStream
{
267 unsigned long m_read_bytes
;
269 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
270 size_t GetSize() const { return m_httpsize
; }
271 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
274 size_t OnSysRead(void *buffer
, size_t bufsize
);
277 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
281 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
284 ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
290 bool wxHTTP::Abort(void)
294 ret
= wxSocketClient::Close();
299 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
301 wxHTTPStream
*inp_stream
= new wxHTTPStream(this);
304 m_perr
= wxPROTO_CONNERR
;
308 // We set m_connected back to FALSE so wxSocketBase will know what to do.
309 if (!wxProtocol::Connect(*m_addr
))
312 if (!BuildRequest(path
, wxHTTP_GET
))
315 if (!GetHeader(_T("Content-Length")).IsEmpty())
316 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(_T("Content-Length")));
318 inp_stream
->m_httpsize
= (size_t)-1;
320 inp_stream
->m_read_bytes
= 0;
323 SetFlags(SPEED
| WAITALL
);