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)
43 #define HTTP_BSIZE 2048
51 m_post_buf
= wxEmptyString
;
54 SetNotify(wxSOCKET_LOST_FLAG
);
64 void wxHTTP::ClearHeaders()
69 wxString
wxHTTP::GetContentType()
71 return GetHeader(wxT("Content-Type"));
74 void wxHTTP::SetProxyMode(bool on
)
79 wxHTTP::wxHeaderIterator
wxHTTP::FindHeader(const wxString
& header
)
81 wxHeaderIterator it
= m_headers
.begin();
82 for ( wxHeaderIterator en
= m_headers
.end(); it
!= en
; ++it
)
84 if ( wxStricmp(it
->first
, header
) == 0 )
91 wxHTTP::wxHeaderConstIterator
wxHTTP::FindHeader(const wxString
& header
) const
93 wxHeaderConstIterator it
= m_headers
.begin();
94 for ( wxHeaderConstIterator en
= m_headers
.end(); it
!= en
; ++it
)
96 if ( wxStricmp(it
->first
, header
) == 0 )
103 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
110 wxHeaderIterator it
= FindHeader(header
);
111 if (it
!= m_headers
.end())
114 m_headers
[header
] = h_data
;
117 wxString
wxHTTP::GetHeader(const wxString
& header
) const
119 wxHeaderConstIterator it
= FindHeader(header
);
121 return it
== m_headers
.end() ? wxGetEmptyString() : it
->second
;
124 void wxHTTP::SetPostBuffer(const wxString
& post_buf
)
126 m_post_buf
= post_buf
;
129 void wxHTTP::SendHeaders()
131 typedef wxStringToStringHashMap::iterator iterator
;
134 for (iterator it
= m_headers
.begin(), en
= m_headers
.end(); it
!= en
; ++it
)
136 buf
.Printf(wxT("%s: %s\r\n"), it
->first
.c_str(), it
->second
.c_str());
138 const wxWX2MBbuf cbuf
= buf
.mb_str();
139 Write(cbuf
, strlen(cbuf
));
143 bool wxHTTP::ParseHeaders()
146 wxStringTokenizer tokenzr
;
151 #if defined(__VISAGECPP__)
152 // VA just can't stand while(1)
159 m_perr
= GetLine(this, line
);
160 if (m_perr
!= wxPROTO_NOERR
)
163 if (line
.Length() == 0)
166 wxString left_str
= line
.BeforeFirst(':');
167 m_headers
[left_str
] = line
.AfterFirst(':').Strip(wxString::both
);
172 bool wxHTTP::Connect(const wxString
& host
, unsigned short port
)
182 m_addr
= addr
= new wxIPV4address();
184 if (!addr
->Hostname(host
)) {
187 m_perr
= wxPROTO_NETERR
;
191 if ( port
) addr
->Service(port
);
192 else if (!addr
->Service(wxT("http")))
195 SetHeader(wxT("Host"), host
);
200 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
207 m_addr
= addr
.Clone();
209 wxIPV4address
*ipv4addr
= wxDynamicCast(&addr
, wxIPV4address
);
211 SetHeader(wxT("Host"), ipv4addr
->OrigHostname());
216 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
218 const wxChar
*request
;
222 request
= wxT("GET");
225 request
= wxT("POST");
226 if ( GetHeader( wxT("Content-Length") ).IsNull() )
227 SetHeader( wxT("Content-Length"), wxString::Format( wxT("%lu"), (unsigned long)m_post_buf
.Len() ) );
235 // If there is no User-Agent defined, define it.
236 if (GetHeader(wxT("User-Agent")).IsNull())
237 SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
241 // we may use non blocking sockets only if we can dispatch events from them
242 SetFlags( wxIsMainThread() && wxApp::IsMainLoopRunning() ? wxSOCKET_NONE
247 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
248 const wxWX2MBbuf pathbuf
= wxConvLocal
.cWX2MB(buf
);
249 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
253 if ( req
== wxHTTP_POST
) {
254 Write(m_post_buf
.mbc_str(), m_post_buf
.Len());
255 m_post_buf
= wxEmptyString
;
259 m_perr
= GetLine(this, tmp_str
);
260 if (m_perr
!= wxPROTO_NOERR
) {
265 if (!tmp_str
.Contains(wxT("HTTP/"))) {
266 // TODO: support HTTP v0.9 which can have no header.
267 // FIXME: tmp_str is not put back in the in-queue of the socket.
268 SetHeader(wxT("Content-Length"), wxT("-1"));
269 SetHeader(wxT("Content-Type"), wxT("none/none"));
274 wxStringTokenizer
token(tmp_str
,wxT(' '));
279 tmp_str2
= token
.NextToken();
281 m_http_response
= wxAtoi(tmp_str2
);
283 switch (tmp_str2
[0u]) {
285 /* INFORMATION / SUCCESS */
294 m_perr
= wxPROTO_NOFILE
;
299 ret_value
= ParseHeaders();
304 class wxHTTPStream
: public wxSocketInputStream
309 unsigned long m_read_bytes
;
311 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
312 size_t GetSize() const { return m_httpsize
; }
313 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
316 size_t OnSysRead(void *buffer
, size_t bufsize
);
318 DECLARE_NO_COPY_CLASS(wxHTTPStream
)
321 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
323 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
325 m_lasterror
= wxSTREAM_EOF
;
329 size_t ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
335 bool wxHTTP::Abort(void)
337 return wxSocketClient::Close();
340 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
342 wxHTTPStream
*inp_stream
;
346 m_perr
= wxPROTO_CONNERR
;
350 // We set m_connected back to false so wxSocketBase will know what to do.
352 wxSocketClient::Connect(*m_addr
, false );
353 wxSocketClient::WaitOnConnect(10);
355 if (!wxSocketClient::IsConnected())
358 if (!wxProtocol::Connect(*m_addr
))
362 if (!BuildRequest(path
, m_post_buf
.IsEmpty() ? wxHTTP_GET
: wxHTTP_POST
))
365 inp_stream
= new wxHTTPStream(this);
367 if (!GetHeader(wxT("Content-Length")).IsEmpty())
368 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
370 inp_stream
->m_httpsize
= (size_t)-1;
372 inp_stream
->m_read_bytes
= 0;
375 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
380 #endif // wxUSE_PROTOCOL_HTTP