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
;
210 wxSprintf(buf
, _T("%s %s HTTP/1.0\n\r"), tmp_buf
, tmp_str
.GetData());
211 const wxWX2MBbuf pathbuf
= wxConvLibc
.cWX2MB(buf
);
212 Write(pathbuf
, strlen(MBSTRINGCAST pathbuf
));
216 m_perr
= GetLine(this, tmp_str
);
217 if (m_perr
!= wxPROTO_NOERR
) {
222 if (!tmp_str
.Contains(_T("HTTP/"))) {
223 // TODO: support HTTP v0.9 which can have no header.
224 SetHeader(_T("Content-Length"), _T("-1"));
225 SetHeader(_T("Content-Type"), _T("none/none"));
230 wxStringTokenizer
token(tmp_str
,_T(' '));
235 tmp_str2
= token
.NextToken();
237 switch (wxAtoi(tmp_str2
)) {
241 m_perr
= wxPROTO_NOFILE
;
246 ret_value
= ParseHeaders();
251 class wxHTTPStream
: public wxSocketInputStream
{
255 unsigned long m_read_bytes
;
257 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
258 size_t GetSize() const { return m_httpsize
; }
259 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
262 size_t OnSysRead(void *buffer
, size_t bufsize
);
265 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
269 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
272 ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
278 bool wxHTTP::Abort(void)
282 ret
= wxSocketClient::Close();
287 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
289 wxHTTPStream
*inp_stream
= new wxHTTPStream(this);
292 m_perr
= wxPROTO_CONNERR
;
296 // We set m_connected back to FALSE so wxSocketBase will know what to do.
297 if (!wxProtocol::Connect(*m_addr
))
300 if (!BuildRequest(path
, wxHTTP_GET
))
303 if (!GetHeader(_T("Content-Length")).IsEmpty())
304 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(_T("Content-Length")));
306 inp_stream
->m_httpsize
= (size_t)-1;
308 inp_stream
->m_read_bytes
= 0;
311 SetFlags(SPEED
| WAITALL
);