1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HTTP protocol
4 // Author: Guilhem Lavaux
6 // Created: August 1997
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_PROTOCOL_HTTP
25 #include "wx/string.h"
29 #include "wx/tokenzr.h"
30 #include "wx/socket.h"
31 #include "wx/protocol/protocol.h"
33 #include "wx/protocol/http.h"
34 #include "wx/sckstrm.h"
36 IMPLEMENT_DYNAMIC_CLASS(wxHTTP
, wxProtocol
)
37 IMPLEMENT_PROTOCOL(wxHTTP
, wxT("http"), wxT("80"), true)
45 m_post_buf
= wxEmptyString
;
48 SetNotify(wxSOCKET_LOST_FLAG
);
58 void wxHTTP::ClearHeaders()
63 wxString
wxHTTP::GetContentType()
65 return GetHeader(wxT("Content-Type"));
68 void wxHTTP::SetProxyMode(bool on
)
73 wxHTTP::wxHeaderIterator
wxHTTP::FindHeader(const wxString
& header
)
75 wxHeaderIterator it
= m_headers
.begin();
76 for ( wxHeaderIterator en
= m_headers
.end(); it
!= en
; ++it
)
78 if ( wxStricmp(it
->first
, header
) == 0 )
85 wxHTTP::wxHeaderConstIterator
wxHTTP::FindHeader(const wxString
& header
) const
87 wxHeaderConstIterator it
= m_headers
.begin();
88 for ( wxHeaderConstIterator en
= m_headers
.end(); it
!= en
; ++it
)
90 if ( wxStricmp(it
->first
, header
) == 0 )
97 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
104 wxHeaderIterator it
= FindHeader(header
);
105 if (it
!= m_headers
.end())
108 m_headers
[header
] = h_data
;
111 wxString
wxHTTP::GetHeader(const wxString
& header
) const
113 wxHeaderConstIterator it
= FindHeader(header
);
115 return it
== m_headers
.end() ? wxGetEmptyString() : it
->second
;
118 void wxHTTP::SetPostBuffer(const wxString
& post_buf
)
120 m_post_buf
= post_buf
;
123 void wxHTTP::SendHeaders()
125 typedef wxStringToStringHashMap::iterator iterator
;
128 for (iterator it
= m_headers
.begin(), en
= m_headers
.end(); it
!= en
; ++it
)
130 buf
.Printf(wxT("%s: %s\r\n"), it
->first
.c_str(), it
->second
.c_str());
132 const wxWX2MBbuf cbuf
= buf
.mb_str();
133 Write(cbuf
, strlen(cbuf
));
137 bool wxHTTP::ParseHeaders()
140 wxStringTokenizer tokenzr
;
145 #if defined(__VISAGECPP__)
146 // VA just can't stand while(1)
153 m_perr
= ReadLine(this, line
);
154 if (m_perr
!= wxPROTO_NOERR
)
157 if (line
.Length() == 0)
160 wxString left_str
= line
.BeforeFirst(':');
161 m_headers
[left_str
] = line
.AfterFirst(':').Strip(wxString::both
);
166 bool wxHTTP::Connect(const wxString
& host
, unsigned short port
)
176 m_addr
= addr
= new wxIPV4address();
178 if (!addr
->Hostname(host
)) {
181 m_perr
= wxPROTO_NETERR
;
187 else if (!addr
->Service(wxT("http")))
190 SetHeader(wxT("Host"), host
);
195 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
202 m_addr
= addr
.Clone();
204 wxIPV4address
*ipv4addr
= wxDynamicCast(&addr
, wxIPV4address
);
206 SetHeader(wxT("Host"), ipv4addr
->OrigHostname());
211 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
213 const wxChar
*request
;
218 request
= wxT("GET");
222 request
= wxT("POST");
223 if ( GetHeader( wxT("Content-Length") ).IsNull() )
224 SetHeader( wxT("Content-Length"), wxString::Format( wxT("%lu"), (unsigned long)m_post_buf
.Len() ) );
233 // If there is no User-Agent defined, define it.
234 if (GetHeader(wxT("User-Agent")).IsNull())
235 SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
239 // we may use non blocking sockets only if we can dispatch events from them
240 SetFlags( wxIsMainThread() && wxApp::IsMainLoopRunning() ? wxSOCKET_NONE
245 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
246 const wxWX2MBbuf pathbuf
= wxConvLocal
.cWX2MB(buf
);
247 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
251 if ( req
== wxHTTP_POST
) {
252 Write(m_post_buf
.mbc_str(), m_post_buf
.Len());
253 m_post_buf
= wxEmptyString
;
257 m_perr
= ReadLine(this, tmp_str
);
258 if (m_perr
!= wxPROTO_NOERR
) {
263 if (!tmp_str
.Contains(wxT("HTTP/"))) {
264 // TODO: support HTTP v0.9 which can have no header.
265 // FIXME: tmp_str is not put back in the in-queue of the socket.
266 SetHeader(wxT("Content-Length"), wxT("-1"));
267 SetHeader(wxT("Content-Type"), wxT("none/none"));
272 wxStringTokenizer
token(tmp_str
,wxT(' '));
277 tmp_str2
= token
.NextToken();
279 m_http_response
= wxAtoi(tmp_str2
);
281 switch (tmp_str2
[0u])
284 /* INFORMATION / SUCCESS */
296 m_perr
= wxPROTO_NOFILE
;
301 ret_value
= ParseHeaders();
306 class wxHTTPStream
: public wxSocketInputStream
311 unsigned long m_read_bytes
;
313 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
314 size_t GetSize() const { return m_httpsize
; }
315 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
318 size_t OnSysRead(void *buffer
, size_t bufsize
);
320 DECLARE_NO_COPY_CLASS(wxHTTPStream
)
323 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
325 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
327 m_lasterror
= wxSTREAM_EOF
;
331 size_t ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
334 if (m_httpsize
==(size_t)-1 && m_lasterror
== wxSTREAM_READ_ERROR
)
336 // if m_httpsize is (size_t) -1 this means read until connection closed
337 // which is equivalent to getting a READ_ERROR, for clients however this
338 // must be translated into EOF, as it is the expected way of signalling
339 // end end of the content
340 m_lasterror
= wxSTREAM_EOF
;
346 bool wxHTTP::Abort(void)
348 return wxSocketClient::Close();
351 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
353 wxHTTPStream
*inp_stream
;
357 m_perr
= wxPROTO_CONNERR
;
361 // We set m_connected back to false so wxSocketBase will know what to do.
363 wxSocketClient::Connect(*m_addr
, false );
364 wxSocketClient::WaitOnConnect(10);
366 if (!wxSocketClient::IsConnected())
369 if (!wxProtocol::Connect(*m_addr
))
373 if (!BuildRequest(path
, m_post_buf
.empty() ? wxHTTP_GET
: wxHTTP_POST
))
376 inp_stream
= new wxHTTPStream(this);
378 if (!GetHeader(wxT("Content-Length")).empty())
379 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
381 inp_stream
->m_httpsize
= (size_t)-1;
383 inp_stream
->m_read_bytes
= 0;
386 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
391 #endif // wxUSE_PROTOCOL_HTTP