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
);
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")))
181 SetHeader(wxT("Host"), host
);
186 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
193 m_addr
= addr
.Clone();
195 wxIPV4address
*ipv4addr
= wxDynamicCast(&addr
, wxIPV4address
);
197 SetHeader(wxT("Host"), ipv4addr
->Hostname());
202 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
208 tmp_buf
= wxT("GET");
214 // If there is no User-Agent defined, define it.
215 if (GetHeader(wxT("User-Agent")).IsNull())
216 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
219 SetFlags(wxSOCKET_NONE
);
223 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), tmp_buf
, path
.c_str());
224 const wxWX2MBbuf pathbuf
= wxConvLibc
.cWX2MB(buf
);
225 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
230 m_perr
= GetLine(this, tmp_str
);
231 if (m_perr
!= wxPROTO_NOERR
) {
236 if (!tmp_str
.Contains(wxT("HTTP/"))) {
237 // TODO: support HTTP v0.9 which can have no header.
238 // FIXME: tmp_str is not put back in the in-queue of the socket.
239 SetHeader(wxT("Content-Length"), wxT("-1"));
240 SetHeader(wxT("Content-Type"), wxT("none/none"));
245 wxStringTokenizer
token(tmp_str
,wxT(' '));
250 tmp_str2
= token
.NextToken();
252 switch (tmp_str2
[0u]) {
254 /* INFORMATION / SUCCESS */
263 m_perr
= wxPROTO_NOFILE
;
268 ret_value
= ParseHeaders();
273 class wxHTTPStream
: public wxSocketInputStream
278 unsigned long m_read_bytes
;
280 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
281 size_t GetSize() const { return m_httpsize
; }
282 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
285 size_t OnSysRead(void *buffer
, size_t bufsize
);
288 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
292 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
295 ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
301 bool wxHTTP::Abort(void)
303 return wxSocketClient::Close();
306 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
308 wxHTTPStream
*inp_stream
;
312 m_perr
= wxPROTO_CONNERR
;
316 // We set m_connected back to FALSE so wxSocketBase will know what to do.
318 wxSocketClient::Connect(*m_addr
, FALSE
);
319 wxSocketClient::WaitOnConnect(10);
321 if (!wxSocketClient::IsConnected())
324 if (!wxProtocol::Connect(*m_addr
))
328 if (!BuildRequest(path
, wxHTTP_GET
))
331 inp_stream
= new wxHTTPStream(this);
333 if (!GetHeader(wxT("Content-Length")).IsEmpty())
334 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
336 inp_stream
->m_httpsize
= (size_t)-1;
338 inp_stream
->m_read_bytes
= 0;
341 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
346 #endif // wxUSE_PROTOCOL_HTTP