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"
23 #if wxUSE_SOCKETS && wxUSE_STREAMS
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(wxSOCKET_LOST_FLAG
);
53 // wxString isn't a wxObject
54 wxNode
*node
= m_headers
.First();
58 string
= (wxString
*)node
->Data();
69 wxString
wxHTTP::GetContentType()
71 return GetHeader(wxT("Content-Type"));
74 void wxHTTP::SetProxyMode(bool on
)
79 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
86 wxNode
*node
= m_headers
.Find(header
);
89 m_headers
.Append(header
, (wxObject
*)(new wxString(h_data
)));
91 wxString
*str
= (wxString
*)node
->Data();
96 wxString
wxHTTP::GetHeader(const wxString
& header
)
99 wxString upper_header
;
101 upper_header
= header
.Upper();
103 node
= m_headers
.Find(upper_header
);
105 return wxEmptyString
;
107 return *((wxString
*)node
->Data());
110 void wxHTTP::SendHeaders()
112 wxNode
*head
= m_headers
.First();
116 wxString
*str
= (wxString
*)head
->Data();
119 buf
.Printf(wxT("%s: %s\r\n"), head
->GetKeyString(), str
->GetData());
121 const wxWX2MBbuf cbuf
= buf
.mb_str();
122 Write(cbuf
, strlen(cbuf
));
128 bool wxHTTP::ParseHeaders()
131 wxStringTokenizer tokenzr
;
136 #if defined(__VISAGECPP__)
137 // VA just can't stand while(1)
143 m_perr
= GetLine(this, line
);
144 if (m_perr
!= wxPROTO_NOERR
)
147 if (line
.Length() == 0)
150 wxString left_str
= line
.BeforeFirst(':');
151 wxString
*str
= new wxString(line
.AfterFirst(':').Strip(wxString::both
));
152 left_str
.MakeUpper();
154 m_headers
.Append(left_str
, (wxObject
*) str
);
159 bool wxHTTP::Connect(const wxString
& host
)
169 m_addr
= addr
= new wxIPV4address();
171 if (!addr
->Hostname(host
)) {
174 m_perr
= wxPROTO_NETERR
;
178 if (!addr
->Service(wxT("http")))
184 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
192 m_addr
= (wxSockAddress
*) addr
.Clone();
196 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
199 wxChar buf
[200]; // 200 is arbitrary.
200 wxString tmp_str
= path
;
202 // If there is no User-Agent defined, define it.
203 if (GetHeader(wxT("User-Agent")).IsNull())
204 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
208 tmp_buf
= wxT("GET");
215 SetFlags(wxSOCKET_NONE
);
218 wxSprintf(buf
, wxT("%s %s HTTP/1.0\r\n"), tmp_buf
, tmp_str
.GetData());
219 const wxWX2MBbuf pathbuf
= wxConvLibc
.cWX2MB(buf
);
220 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
224 m_perr
= GetLine(this, tmp_str
);
225 if (m_perr
!= wxPROTO_NOERR
) {
230 if (!tmp_str
.Contains(wxT("HTTP/"))) {
231 // TODO: support HTTP v0.9 which can have no header.
232 // FIXME: tmp_str is not put back in the in-queue of the socket.
233 SetHeader(wxT("Content-Length"), wxT("-1"));
234 SetHeader(wxT("Content-Type"), wxT("none/none"));
239 wxStringTokenizer
token(tmp_str
,wxT(' '));
244 tmp_str2
= token
.NextToken();
246 switch (tmp_str2
[(unsigned int) 0]) {
248 /* INFORMATION / SUCCESS */
257 m_perr
= wxPROTO_NOFILE
;
262 ret_value
= ParseHeaders();
267 class wxHTTPStream
: public wxSocketInputStream
272 unsigned long m_read_bytes
;
274 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
275 size_t GetSize() const { return m_httpsize
; }
276 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
279 size_t OnSysRead(void *buffer
, size_t bufsize
);
282 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
286 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
289 ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
295 bool wxHTTP::Abort(void)
297 return wxSocketClient::Close();
300 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
302 wxHTTPStream
*inp_stream
;
306 m_perr
= wxPROTO_CONNERR
;
310 // We set m_connected back to FALSE so wxSocketBase will know what to do.
312 wxSocketClient::Connect(*m_addr
, FALSE
);
313 wxSocketClient::WaitOnConnect(10);
315 if (!wxSocketClient::IsConnected())
318 if (!wxProtocol::Connect(*m_addr
))
322 if (!BuildRequest(path
, wxHTTP_GET
))
325 inp_stream
= new wxHTTPStream(this);
327 if (!GetHeader(wxT("Content-Length")).IsEmpty())
328 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
330 inp_stream
->m_httpsize
= (size_t)-1;
332 inp_stream
->m_read_bytes
= 0;
335 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);