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_PROTOCOL_HTTP
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
);
58 void wxHTTP::ClearHeaders()
60 // wxString isn't a wxObject
61 wxNode
*node
= m_headers
.First();
65 string
= (wxString
*)node
->Data();
73 wxString
wxHTTP::GetContentType()
75 return GetHeader(wxT("Content-Type"));
78 void wxHTTP::SetProxyMode(bool on
)
83 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
90 wxNode
*node
= m_headers
.Find(header
);
93 m_headers
.Append(header
, (wxObject
*)(new wxString(h_data
)));
95 wxString
*str
= (wxString
*)node
->Data();
100 wxString
wxHTTP::GetHeader(const wxString
& header
)
103 wxString upper_header
;
105 upper_header
= header
.Upper();
107 node
= m_headers
.Find(upper_header
);
109 return wxEmptyString
;
111 return *((wxString
*)node
->Data());
114 void wxHTTP::SendHeaders()
116 wxNode
*head
= m_headers
.First();
120 wxString
*str
= (wxString
*)head
->Data();
123 buf
.Printf(wxT("%s: %s\r\n"), head
->GetKeyString(), str
->GetData());
125 const wxWX2MBbuf cbuf
= buf
.mb_str();
126 Write(cbuf
, strlen(cbuf
));
132 bool wxHTTP::ParseHeaders()
135 wxStringTokenizer tokenzr
;
140 #if defined(__VISAGECPP__)
141 // VA just can't stand while(1)
147 m_perr
= GetLine(this, line
);
148 if (m_perr
!= wxPROTO_NOERR
)
151 if (line
.Length() == 0)
154 wxString left_str
= line
.BeforeFirst(':');
155 wxString
*str
= new wxString(line
.AfterFirst(':').Strip(wxString::both
));
156 left_str
.MakeUpper();
158 m_headers
.Append(left_str
, (wxObject
*) str
);
163 bool wxHTTP::Connect(const wxString
& host
)
173 m_addr
= addr
= new wxIPV4address();
175 if (!addr
->Hostname(host
)) {
178 m_perr
= wxPROTO_NETERR
;
182 if (!addr
->Service(wxT("http")))
185 SetHeader(wxT("Host"), host
);
190 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
197 m_addr
= addr
.Clone();
199 wxIPV4address
*ipv4addr
= wxDynamicCast(&addr
, wxIPV4address
);
201 SetHeader(wxT("Host"), ipv4addr
->OrigHostname());
206 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
208 const wxChar
*request
;
212 request
= wxT("GET");
218 // If there is no User-Agent defined, define it.
219 if (GetHeader(wxT("User-Agent")).IsNull())
220 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
223 SetFlags(wxSOCKET_NONE
);
227 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
228 const wxWX2MBbuf pathbuf
= wxConvLibc
.cWX2MB(buf
);
229 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
234 m_perr
= GetLine(this, tmp_str
);
235 if (m_perr
!= wxPROTO_NOERR
) {
240 if (!tmp_str
.Contains(wxT("HTTP/"))) {
241 // TODO: support HTTP v0.9 which can have no header.
242 // FIXME: tmp_str is not put back in the in-queue of the socket.
243 SetHeader(wxT("Content-Length"), wxT("-1"));
244 SetHeader(wxT("Content-Type"), wxT("none/none"));
249 wxStringTokenizer
token(tmp_str
,wxT(' '));
254 tmp_str2
= token
.NextToken();
256 switch (tmp_str2
[0u]) {
258 /* INFORMATION / SUCCESS */
267 m_perr
= wxPROTO_NOFILE
;
272 ret_value
= ParseHeaders();
277 class wxHTTPStream
: public wxSocketInputStream
282 unsigned long m_read_bytes
;
284 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
285 size_t GetSize() const { return m_httpsize
; }
286 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
289 size_t OnSysRead(void *buffer
, size_t bufsize
);
292 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
296 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
299 ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
305 bool wxHTTP::Abort(void)
307 return wxSocketClient::Close();
310 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
312 wxHTTPStream
*inp_stream
;
316 m_perr
= wxPROTO_CONNERR
;
320 // We set m_connected back to FALSE so wxSocketBase will know what to do.
322 wxSocketClient::Connect(*m_addr
, FALSE
);
323 wxSocketClient::WaitOnConnect(10);
325 if (!wxSocketClient::IsConnected())
328 if (!wxProtocol::Connect(*m_addr
))
332 if (!BuildRequest(path
, wxHTTP_GET
))
335 inp_stream
= new wxHTTPStream(this);
337 if (!GetHeader(wxT("Content-Length")).IsEmpty())
338 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
340 inp_stream
->m_httpsize
= (size_t)-1;
342 inp_stream
->m_read_bytes
= 0;
345 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
350 #endif // wxUSE_PROTOCOL_HTTP