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");
221 if ( GetHeader( wxT("Content-Length") ).IsNull() )
222 SetHeader( wxT("Content-Length"), wxString::Format( wxT("%lu"), (unsigned long)m_post_buf
.Len() ) );
230 // If there is no User-Agent defined, define it.
231 if (GetHeader(wxT("User-Agent")).IsNull())
232 SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
236 SetFlags( wxThread::IsMain() ? wxSOCKET_NONE
: wxSOCKET_BLOCK
);
238 SetFlags( wxSOCKET_NONE
);
243 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
244 const wxWX2MBbuf pathbuf
= wxConvLocal
.cWX2MB(buf
);
245 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
249 if ( req
== wxHTTP_POST
) {
250 Write(m_post_buf
.mbc_str(), m_post_buf
.Len());
251 m_post_buf
= wxEmptyString
;
255 m_perr
= GetLine(this, tmp_str
);
256 if (m_perr
!= wxPROTO_NOERR
) {
261 if (!tmp_str
.Contains(wxT("HTTP/"))) {
262 // TODO: support HTTP v0.9 which can have no header.
263 // FIXME: tmp_str is not put back in the in-queue of the socket.
264 SetHeader(wxT("Content-Length"), wxT("-1"));
265 SetHeader(wxT("Content-Type"), wxT("none/none"));
270 wxStringTokenizer
token(tmp_str
,wxT(' '));
275 tmp_str2
= token
.NextToken();
277 m_http_response
= wxAtoi(tmp_str2
);
279 switch (tmp_str2
[0u]) {
281 /* INFORMATION / SUCCESS */
290 m_perr
= wxPROTO_NOFILE
;
295 ret_value
= ParseHeaders();
300 class wxHTTPStream
: public wxSocketInputStream
305 unsigned long m_read_bytes
;
307 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
308 size_t GetSize() const { return m_httpsize
; }
309 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
312 size_t OnSysRead(void *buffer
, size_t bufsize
);
314 DECLARE_NO_COPY_CLASS(wxHTTPStream
)
317 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
319 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
321 m_lasterror
= wxSTREAM_EOF
;
325 size_t ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
331 bool wxHTTP::Abort(void)
333 return wxSocketClient::Close();
336 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
338 wxHTTPStream
*inp_stream
;
342 m_perr
= wxPROTO_CONNERR
;
346 // We set m_connected back to false so wxSocketBase will know what to do.
348 wxSocketClient::Connect(*m_addr
, false );
349 wxSocketClient::WaitOnConnect(10);
351 if (!wxSocketClient::IsConnected())
354 if (!wxProtocol::Connect(*m_addr
))
358 if (!BuildRequest(path
, m_post_buf
.IsEmpty() ? wxHTTP_GET
: wxHTTP_POST
))
361 inp_stream
= new wxHTTPStream(this);
363 if (!GetHeader(wxT("Content-Length")).IsEmpty())
364 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
366 inp_stream
->m_httpsize
= (size_t)-1;
368 inp_stream
->m_read_bytes
= 0;
371 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
376 #endif // wxUSE_PROTOCOL_HTTP