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
)
76 wxHeaderIterator it
= m_headers
.begin();
77 for ( wxHeaderIterator en
= m_headers
.end(); it
!= en
; ++it
)
79 if ( wxStricmp(it
->first
, header
) == 0 )
86 wxHTTP::wxHeaderConstIterator
wxHTTP::FindHeader(const wxString
& header
) const
88 wxHeaderConstIterator it
= m_headers
.begin();
89 for ( wxHeaderConstIterator en
= m_headers
.end(); it
!= en
; ++it
)
91 if ( wxStricmp(it
->first
, header
) == 0 )
98 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
105 wxHeaderIterator it
= FindHeader(header
);
106 if (it
!= m_headers
.end())
109 m_headers
[header
] = h_data
;
112 wxString
wxHTTP::GetHeader(const wxString
& header
) const
114 wxHeaderConstIterator it
= FindHeader(header
);
116 return it
== m_headers
.end() ? wxGetEmptyString() : it
->second
;
119 void wxHTTP::SetPostBuffer(const wxString
& post_buf
)
121 m_post_buf
= post_buf
;
124 void wxHTTP::SendHeaders()
126 typedef wxStringToStringHashMap::iterator iterator
;
129 for (iterator it
= m_headers
.begin(), en
= m_headers
.end(); it
!= en
; ++it
)
131 buf
.Printf(wxT("%s: %s\r\n"), it
->first
.c_str(), it
->second
.c_str());
133 const wxWX2MBbuf cbuf
= buf
.mb_str();
134 Write(cbuf
, strlen(cbuf
));
138 bool wxHTTP::ParseHeaders()
141 wxStringTokenizer tokenzr
;
146 #if defined(__VISAGECPP__)
147 // VA just can't stand while(1)
154 m_perr
= GetLine(this, line
);
155 if (m_perr
!= wxPROTO_NOERR
)
158 if (line
.Length() == 0)
161 wxString left_str
= line
.BeforeFirst(':');
162 m_headers
[left_str
] = line
.AfterFirst(':').Strip(wxString::both
);
167 bool wxHTTP::Connect(const wxString
& host
, unsigned short port
)
177 m_addr
= addr
= new wxIPV4address();
179 if (!addr
->Hostname(host
)) {
182 m_perr
= wxPROTO_NETERR
;
186 if ( port
) addr
->Service(port
);
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
;
217 request
= wxT("GET");
220 request
= wxT("POST");
228 // If there is no User-Agent defined, define it.
229 if (GetHeader(wxT("User-Agent")).IsNull())
230 SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
233 SetFlags(wxSOCKET_NONE
);
237 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
238 const wxWX2MBbuf pathbuf
= wxConvLocal
.cWX2MB(buf
);
239 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
243 if ( req
== wxHTTP_POST
) {
244 Write(m_post_buf
, m_post_buf
.Len());
245 m_post_buf
= wxEmptyString
;
249 m_perr
= GetLine(this, tmp_str
);
250 if (m_perr
!= wxPROTO_NOERR
) {
255 if (!tmp_str
.Contains(wxT("HTTP/"))) {
256 // TODO: support HTTP v0.9 which can have no header.
257 // FIXME: tmp_str is not put back in the in-queue of the socket.
258 SetHeader(wxT("Content-Length"), wxT("-1"));
259 SetHeader(wxT("Content-Type"), wxT("none/none"));
264 wxStringTokenizer
token(tmp_str
,wxT(' '));
269 tmp_str2
= token
.NextToken();
271 m_http_response
= wxAtoi(tmp_str2
);
273 switch (tmp_str2
[0u]) {
275 /* INFORMATION / SUCCESS */
284 m_perr
= wxPROTO_NOFILE
;
289 ret_value
= ParseHeaders();
294 class wxHTTPStream
: public wxSocketInputStream
299 unsigned long m_read_bytes
;
301 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
302 size_t GetSize() const { return m_httpsize
; }
303 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
306 size_t OnSysRead(void *buffer
, size_t bufsize
);
308 DECLARE_NO_COPY_CLASS(wxHTTPStream
)
311 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
313 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
315 m_lasterror
= wxSTREAM_EOF
;
319 size_t ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
325 bool wxHTTP::Abort(void)
327 return wxSocketClient::Close();
330 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
332 wxHTTPStream
*inp_stream
;
336 m_perr
= wxPROTO_CONNERR
;
340 // We set m_connected back to FALSE so wxSocketBase will know what to do.
342 wxSocketClient::Connect(*m_addr
, FALSE
);
343 wxSocketClient::WaitOnConnect(10);
345 if (!wxSocketClient::IsConnected())
348 if (!wxProtocol::Connect(*m_addr
))
352 if (!BuildRequest(path
, m_post_buf
.IsEmpty() ? wxHTTP_GET
: wxHTTP_POST
))
355 inp_stream
= new wxHTTPStream(this);
357 if (!GetHeader(wxT("Content-Length")).IsEmpty())
358 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
360 inp_stream
->m_httpsize
= (size_t)-1;
362 inp_stream
->m_read_bytes
= 0;
365 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
370 #endif // wxUSE_PROTOCOL_HTTP