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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "http.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #if wxUSE_PROTOCOL_HTTP
29 #include "wx/string.h"
33 #include "wx/tokenzr.h"
34 #include "wx/socket.h"
35 #include "wx/protocol/protocol.h"
37 #include "wx/protocol/http.h"
38 #include "wx/sckstrm.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxHTTP
, wxProtocol
)
41 IMPLEMENT_PROTOCOL(wxHTTP
, wxT("http"), wxT("80"), true)
49 m_post_buf
= wxEmptyString
;
52 SetNotify(wxSOCKET_LOST_FLAG
);
62 void wxHTTP::ClearHeaders()
67 wxString
wxHTTP::GetContentType()
69 return GetHeader(wxT("Content-Type"));
72 void wxHTTP::SetProxyMode(bool on
)
77 wxHTTP::wxHeaderIterator
wxHTTP::FindHeader(const wxString
& header
)
79 wxHeaderIterator it
= m_headers
.begin();
80 for ( wxHeaderIterator en
= m_headers
.end(); it
!= en
; ++it
)
82 if ( wxStricmp(it
->first
, header
) == 0 )
89 wxHTTP::wxHeaderConstIterator
wxHTTP::FindHeader(const wxString
& header
) const
91 wxHeaderConstIterator it
= m_headers
.begin();
92 for ( wxHeaderConstIterator en
= m_headers
.end(); it
!= en
; ++it
)
94 if ( wxStricmp(it
->first
, header
) == 0 )
101 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
108 wxHeaderIterator it
= FindHeader(header
);
109 if (it
!= m_headers
.end())
112 m_headers
[header
] = h_data
;
115 wxString
wxHTTP::GetHeader(const wxString
& header
) const
117 wxHeaderConstIterator it
= FindHeader(header
);
119 return it
== m_headers
.end() ? wxGetEmptyString() : it
->second
;
122 void wxHTTP::SetPostBuffer(const wxString
& post_buf
)
124 m_post_buf
= post_buf
;
127 void wxHTTP::SendHeaders()
129 typedef wxStringToStringHashMap::iterator iterator
;
132 for (iterator it
= m_headers
.begin(), en
= m_headers
.end(); it
!= en
; ++it
)
134 buf
.Printf(wxT("%s: %s\r\n"), it
->first
.c_str(), it
->second
.c_str());
136 const wxWX2MBbuf cbuf
= buf
.mb_str();
137 Write(cbuf
, strlen(cbuf
));
141 bool wxHTTP::ParseHeaders()
144 wxStringTokenizer tokenzr
;
149 #if defined(__VISAGECPP__)
150 // VA just can't stand while(1)
157 m_perr
= ReadLine(this, line
);
158 if (m_perr
!= wxPROTO_NOERR
)
161 if (line
.Length() == 0)
164 wxString left_str
= line
.BeforeFirst(':');
165 m_headers
[left_str
] = line
.AfterFirst(':').Strip(wxString::both
);
170 bool wxHTTP::Connect(const wxString
& host
, unsigned short port
)
180 m_addr
= addr
= new wxIPV4address();
182 if (!addr
->Hostname(host
)) {
185 m_perr
= wxPROTO_NETERR
;
191 else if (!addr
->Service(wxT("http")))
194 SetHeader(wxT("Host"), host
);
199 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
206 m_addr
= addr
.Clone();
208 wxIPV4address
*ipv4addr
= wxDynamicCast(&addr
, wxIPV4address
);
210 SetHeader(wxT("Host"), ipv4addr
->OrigHostname());
215 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
217 const wxChar
*request
;
222 request
= wxT("GET");
226 request
= wxT("POST");
227 if ( GetHeader( wxT("Content-Length") ).IsNull() )
228 SetHeader( wxT("Content-Length"), wxString::Format( wxT("%lu"), (unsigned long)m_post_buf
.Len() ) );
237 // If there is no User-Agent defined, define it.
238 if (GetHeader(wxT("User-Agent")).IsNull())
239 SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
243 // we may use non blocking sockets only if we can dispatch events from them
244 SetFlags( wxIsMainThread() && wxApp::IsMainLoopRunning() ? wxSOCKET_NONE
249 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
250 const wxWX2MBbuf pathbuf
= wxConvLocal
.cWX2MB(buf
);
251 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
255 if ( req
== wxHTTP_POST
) {
256 Write(m_post_buf
.mbc_str(), m_post_buf
.Len());
257 m_post_buf
= wxEmptyString
;
261 m_perr
= ReadLine(this, tmp_str
);
262 if (m_perr
!= wxPROTO_NOERR
) {
267 if (!tmp_str
.Contains(wxT("HTTP/"))) {
268 // TODO: support HTTP v0.9 which can have no header.
269 // FIXME: tmp_str is not put back in the in-queue of the socket.
270 SetHeader(wxT("Content-Length"), wxT("-1"));
271 SetHeader(wxT("Content-Type"), wxT("none/none"));
276 wxStringTokenizer
token(tmp_str
,wxT(' '));
281 tmp_str2
= token
.NextToken();
283 m_http_response
= wxAtoi(tmp_str2
);
285 switch (tmp_str2
[0u])
288 /* INFORMATION / SUCCESS */
300 m_perr
= wxPROTO_NOFILE
;
305 ret_value
= ParseHeaders();
310 class wxHTTPStream
: public wxSocketInputStream
315 unsigned long m_read_bytes
;
317 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
318 size_t GetSize() const { return m_httpsize
; }
319 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
322 size_t OnSysRead(void *buffer
, size_t bufsize
);
324 DECLARE_NO_COPY_CLASS(wxHTTPStream
)
327 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
329 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
331 m_lasterror
= wxSTREAM_EOF
;
335 size_t ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
338 if (m_httpsize
==(size_t)-1 && m_lasterror
== wxSTREAM_READ_ERROR
)
340 // if m_httpsize is (size_t) -1 this means read until connection closed
341 // which is equivalent to getting a READ_ERROR, for clients however this
342 // must be translated into EOF, as it is the expected way of signalling
343 // end end of the content
344 m_lasterror
= wxSTREAM_EOF
;
350 bool wxHTTP::Abort(void)
352 return wxSocketClient::Close();
355 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
357 wxHTTPStream
*inp_stream
;
361 m_perr
= wxPROTO_CONNERR
;
365 // We set m_connected back to false so wxSocketBase will know what to do.
367 wxSocketClient::Connect(*m_addr
, false );
368 wxSocketClient::WaitOnConnect(10);
370 if (!wxSocketClient::IsConnected())
373 if (!wxProtocol::Connect(*m_addr
))
377 if (!BuildRequest(path
, m_post_buf
.empty() ? wxHTTP_GET
: wxHTTP_POST
))
380 inp_stream
= new wxHTTPStream(this);
382 if (!GetHeader(wxT("Content-Length")).empty())
383 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
385 inp_stream
->m_httpsize
= (size_t)-1;
387 inp_stream
->m_read_bytes
= 0;
390 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
395 #endif // wxUSE_PROTOCOL_HTTP