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
, wxT("http"), wxT("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(wxT("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(wxT("%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
;
133 #if defined(__VISAGECPP__)
134 // VA just can't stand while(1)
140 m_perr
= GetLine(this, line
);
141 if (m_perr
!= wxPROTO_NOERR
)
144 if (line
.Length() == 0)
147 tokenzr
.SetString(line
, " :\t\n\r");
148 if (!tokenzr
.HasMoreTokens())
151 wxString left_str
= tokenzr
.GetNextToken();
152 wxString
*str
= new wxString(tokenzr
.GetNextToken());
154 left_str
.MakeUpper();
156 m_headers
.Append(left_str
, (wxObject
*) str
);
161 bool wxHTTP::Connect(const wxString
& host
)
171 m_addr
= addr
= new wxIPV4address();
173 if (!addr
->Hostname(host
)) {
176 m_perr
= wxPROTO_NETERR
;
180 if (!addr
->Service(wxT("http")))
186 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
194 m_addr
= (wxSockAddress
*) addr
.Clone();
198 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
201 wxChar buf
[200]; // 200 is arbitrary.
202 wxString tmp_str
= path
;
204 // If there is no User-Agent defined, define it.
205 if (GetHeader(wxT("User-Agent")).IsNull())
206 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
210 tmp_buf
= wxT("GET");
220 wxSprintf(buf
, wxT("%s %s HTTP/1.0\n\r"), tmp_buf
, tmp_str
.GetData());
221 const wxWX2MBbuf pathbuf
= wxConvLibc
.cWX2MB(buf
);
222 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
226 m_perr
= GetLine(this, tmp_str
);
227 if (m_perr
!= wxPROTO_NOERR
) {
232 if (!tmp_str
.Contains(wxT("HTTP/"))) {
233 // TODO: support HTTP v0.9 which can have no header.
234 // FIXME: tmp_str is not put back in the in-queue of the socket.
235 SetHeader(wxT("Content-Length"), wxT("-1"));
236 SetHeader(wxT("Content-Type"), wxT("none/none"));
241 wxStringTokenizer
token(tmp_str
,wxT(' '));
246 tmp_str2
= token
.NextToken();
248 switch (tmp_str2
[(unsigned int) 0]) {
250 /* INFORMATION / SUCCESS */
259 m_perr
= wxPROTO_NOFILE
;
264 ret_value
= ParseHeaders();
269 class wxHTTPStream
: public wxSocketInputStream
{
273 unsigned long m_read_bytes
;
275 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
276 size_t GetSize() const { return m_httpsize
; }
277 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
280 size_t OnSysRead(void *buffer
, size_t bufsize
);
283 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
287 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
290 ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
296 bool wxHTTP::Abort(void)
300 ret
= wxSocketClient::Close();
305 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
307 wxHTTPStream
*inp_stream
= new wxHTTPStream(this);
310 m_perr
= wxPROTO_CONNERR
;
314 // We set m_connected back to FALSE so wxSocketBase will know what to do.
315 if (!wxProtocol::Connect(*m_addr
))
318 if (!BuildRequest(path
, wxHTTP_GET
))
321 if (!GetHeader(wxT("Content-Length")).IsEmpty())
322 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
324 inp_stream
->m_httpsize
= (size_t)-1;
326 inp_stream
->m_read_bytes
= 0;
329 SetFlags(SPEED
| WAITALL
);