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
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
46 m_post_buf
= wxEmptyString
;
49 SetNotify(wxSOCKET_LOST_FLAG
);
59 void wxHTTP::ClearHeaders()
64 wxString
wxHTTP::GetContentType()
66 return GetHeader(wxT("Content-Type"));
69 void wxHTTP::SetProxyMode(bool on
)
74 wxHTTP::wxHeaderIterator
wxHTTP::FindHeader(const wxString
& header
) const
76 // we can't convert between const_iterator to iterator otherwise...
77 wxStringToStringHashMap
& headers
= (wxStringToStringHashMap
&)m_headers
;
79 wxHeaderIterator it
= headers
.begin();
80 for ( wxHeaderIterator en
= headers
.end(); it
!= en
; ++it
)
82 if ( wxStricmp(it
->first
, header
) == 0 )
89 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
96 wxHeaderIterator it
= FindHeader(header
);
97 if (it
!= m_headers
.end())
100 m_headers
[header
] = h_data
;
103 wxString
wxHTTP::GetHeader(const wxString
& header
) const
105 wxHeaderIterator it
= FindHeader(header
);
107 return it
== m_headers
.end() ? wxGetEmptyString() : it
->second
;
110 void wxHTTP::SetPostBuffer(const wxString
& post_buf
)
112 m_post_buf
= post_buf
;
115 void wxHTTP::SendHeaders()
117 typedef wxStringToStringHashMap::iterator iterator
;
120 for (iterator it
= m_headers
.begin(), en
= m_headers
.end(); it
!= en
; ++it
)
122 buf
.Printf(wxT("%s: %s\r\n"), it
->first
.c_str(), it
->second
.c_str());
124 const wxWX2MBbuf cbuf
= buf
.mb_str();
125 Write(cbuf
, strlen(cbuf
));
129 bool wxHTTP::ParseHeaders()
132 wxStringTokenizer tokenzr
;
137 #if defined(__VISAGECPP__)
138 // VA just can't stand while(1)
145 m_perr
= GetLine(this, line
);
146 if (m_perr
!= wxPROTO_NOERR
)
149 if (line
.Length() == 0)
152 wxString left_str
= line
.BeforeFirst(':');
153 m_headers
[left_str
] = line
.AfterFirst(':').Strip(wxString::both
);
158 bool wxHTTP::Connect(const wxString
& host
, unsigned short port
)
168 m_addr
= addr
= new wxIPV4address();
170 if (!addr
->Hostname(host
)) {
173 m_perr
= wxPROTO_NETERR
;
177 if ( port
) addr
->Service(port
);
178 else 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
->OrigHostname());
202 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
204 const wxChar
*request
;
208 request
= wxT("GET");
211 request
= wxT("POST");
219 // If there is no User-Agent defined, define it.
220 if (GetHeader(wxT("User-Agent")).IsNull())
221 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
224 SetFlags(wxSOCKET_NONE
);
228 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
229 const wxWX2MBbuf pathbuf
= wxConvLocal
.cWX2MB(buf
);
230 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
234 if ( req
== wxHTTP_POST
) {
235 Write(m_post_buf
, m_post_buf
.Len());
236 m_post_buf
= wxEmptyString
;
240 m_perr
= GetLine(this, tmp_str
);
241 if (m_perr
!= wxPROTO_NOERR
) {
246 if (!tmp_str
.Contains(wxT("HTTP/"))) {
247 // TODO: support HTTP v0.9 which can have no header.
248 // FIXME: tmp_str is not put back in the in-queue of the socket.
249 SetHeader(wxT("Content-Length"), wxT("-1"));
250 SetHeader(wxT("Content-Type"), wxT("none/none"));
255 wxStringTokenizer
token(tmp_str
,wxT(' '));
260 tmp_str2
= token
.NextToken();
262 m_http_response
= wxAtoi(tmp_str2
);
264 switch (tmp_str2
[0u]) {
266 /* INFORMATION / SUCCESS */
275 m_perr
= wxPROTO_NOFILE
;
280 ret_value
= ParseHeaders();
285 class wxHTTPStream
: public wxSocketInputStream
290 unsigned long m_read_bytes
;
292 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
293 size_t GetSize() const { return m_httpsize
; }
294 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
297 size_t OnSysRead(void *buffer
, size_t bufsize
);
299 DECLARE_NO_COPY_CLASS(wxHTTPStream
)
302 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
304 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
306 m_lasterror
= wxSTREAM_EOF
;
310 size_t ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
316 bool wxHTTP::Abort(void)
318 return wxSocketClient::Close();
321 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
323 wxHTTPStream
*inp_stream
;
327 m_perr
= wxPROTO_CONNERR
;
331 // We set m_connected back to FALSE so wxSocketBase will know what to do.
333 wxSocketClient::Connect(*m_addr
, FALSE
);
334 wxSocketClient::WaitOnConnect(10);
336 if (!wxSocketClient::IsConnected())
339 if (!wxProtocol::Connect(*m_addr
))
343 if (!BuildRequest(path
, m_post_buf
.IsEmpty() ? wxHTTP_GET
: wxHTTP_POST
))
346 inp_stream
= new wxHTTPStream(this);
348 if (!GetHeader(wxT("Content-Length")).IsEmpty())
349 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
351 inp_stream
->m_httpsize
= (size_t)-1;
353 inp_stream
->m_read_bytes
= 0;
356 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
361 #endif // wxUSE_PROTOCOL_HTTP