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 /////////////////////////////////////////////////////////////////////////////
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
;
48 SetNotify(wxSOCKET_LOST_FLAG
);
58 void wxHTTP::ClearHeaders()
63 wxString
wxHTTP::GetContentType()
65 return GetHeader(wxT("Content-Type"));
68 void wxHTTP::SetProxyMode(bool on
)
73 wxHTTP::wxHeaderIterator
wxHTTP::FindHeader(const wxString
& header
) const
75 // we can't convert between const_iterator to iterator otherwise...
76 wxStringToStringHashMap
& headers
= (wxStringToStringHashMap
&)m_headers
;
78 wxHeaderIterator it
= headers
.begin();
79 for ( wxHeaderIterator en
= headers
.end(); it
!= en
; ++it
)
81 if ( wxStricmp(it
->first
, header
) == 0 )
88 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
95 wxHeaderIterator it
= FindHeader(header
);
96 if (it
!= m_headers
.end())
99 m_headers
[header
] = h_data
;
102 wxString
wxHTTP::GetHeader(const wxString
& header
) const
104 wxHeaderIterator it
= FindHeader(header
);
106 return it
== m_headers
.end() ? wxGetEmptyString() : it
->second
;
109 void wxHTTP::SetPostBuffer(const wxString
& post_buf
)
111 m_post_buf
= post_buf
;
114 void wxHTTP::SendHeaders()
116 typedef wxStringToStringHashMap::iterator iterator
;
119 for (iterator it
= m_headers
.begin(), en
= m_headers
.end(); it
!= en
; ++it
)
121 buf
.Printf(wxT("%s: %s\r\n"), it
->first
.c_str(), it
->second
.c_str());
123 const wxWX2MBbuf cbuf
= buf
.mb_str();
124 Write(cbuf
, strlen(cbuf
));
128 bool wxHTTP::ParseHeaders()
131 wxStringTokenizer tokenzr
;
136 #if defined(__VISAGECPP__)
137 // VA just can't stand while(1)
144 m_perr
= GetLine(this, line
);
145 if (m_perr
!= wxPROTO_NOERR
)
148 if (line
.Length() == 0)
151 wxString left_str
= line
.BeforeFirst(':');
152 m_headers
[left_str
] = line
.AfterFirst(':').Strip(wxString::both
);
157 bool wxHTTP::Connect(const wxString
& host
, unsigned short port
)
167 m_addr
= addr
= new wxIPV4address();
169 if (!addr
->Hostname(host
)) {
172 m_perr
= wxPROTO_NETERR
;
176 if ( port
) addr
->Service(port
);
177 else if (!addr
->Service(wxT("http")))
180 SetHeader(wxT("Host"), host
);
185 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
192 m_addr
= addr
.Clone();
194 wxIPV4address
*ipv4addr
= wxDynamicCast(&addr
, wxIPV4address
);
196 SetHeader(wxT("Host"), ipv4addr
->OrigHostname());
201 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
203 const wxChar
*request
;
207 request
= wxT("GET");
210 request
= wxT("POST");
216 // If there is no User-Agent defined, define it.
217 if (GetHeader(wxT("User-Agent")).IsNull())
218 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
221 SetFlags(wxSOCKET_NONE
);
225 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
226 const wxWX2MBbuf pathbuf
= wxConvLocal
.cWX2MB(buf
);
227 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
231 if ( req
== wxHTTP_POST
) {
232 Write(m_post_buf
, m_post_buf
.Len());
233 m_post_buf
= wxEmptyString
;
237 m_perr
= GetLine(this, tmp_str
);
238 if (m_perr
!= wxPROTO_NOERR
) {
243 if (!tmp_str
.Contains(wxT("HTTP/"))) {
244 // TODO: support HTTP v0.9 which can have no header.
245 // FIXME: tmp_str is not put back in the in-queue of the socket.
246 SetHeader(wxT("Content-Length"), wxT("-1"));
247 SetHeader(wxT("Content-Type"), wxT("none/none"));
252 wxStringTokenizer
token(tmp_str
,wxT(' '));
257 tmp_str2
= token
.NextToken();
259 switch (tmp_str2
[0u]) {
261 /* INFORMATION / SUCCESS */
270 m_perr
= wxPROTO_NOFILE
;
275 ret_value
= ParseHeaders();
280 class wxHTTPStream
: public wxSocketInputStream
285 unsigned long m_read_bytes
;
287 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
288 size_t GetSize() const { return m_httpsize
; }
289 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
292 size_t OnSysRead(void *buffer
, size_t bufsize
);
294 DECLARE_NO_COPY_CLASS(wxHTTPStream
)
297 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
299 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
301 m_lasterror
= wxSTREAM_EOF
;
305 size_t ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
311 bool wxHTTP::Abort(void)
313 return wxSocketClient::Close();
316 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
318 wxHTTPStream
*inp_stream
;
322 m_perr
= wxPROTO_CONNERR
;
326 // We set m_connected back to FALSE so wxSocketBase will know what to do.
328 wxSocketClient::Connect(*m_addr
, FALSE
);
329 wxSocketClient::WaitOnConnect(10);
331 if (!wxSocketClient::IsConnected())
334 if (!wxProtocol::Connect(*m_addr
))
338 if (!BuildRequest(path
, m_post_buf
.IsEmpty() ? wxHTTP_GET
: wxHTTP_POST
))
341 inp_stream
= new wxHTTPStream(this);
343 if (!GetHeader(wxT("Content-Length")).IsEmpty())
344 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
346 inp_stream
->m_httpsize
= (size_t)-1;
348 inp_stream
->m_read_bytes
= 0;
351 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
356 #endif // wxUSE_PROTOCOL_HTTP