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 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
80 wxStringToStringHashMap::iterator it
= m_headers
.find(header
);
81 if (it
!= m_headers
.end())
84 m_headers
[header
.Upper()] = h_data
;
87 wxString
wxHTTP::GetHeader(const wxString
& header
)
89 wxStringToStringHashMap::iterator it
= m_headers
.find(header
.Upper());
91 if (it
== m_headers
.end())
97 void wxHTTP::SetPostBuffer(const wxString
& post_buf
)
99 m_post_buf
= post_buf
;
102 void wxHTTP::SendHeaders()
104 typedef wxStringToStringHashMap::iterator iterator
;
107 for (iterator it
= m_headers
.begin(), en
= m_headers
.end(); it
!= en
; ++it
)
109 buf
.Printf(wxT("%s: %s\r\n"), it
->first
.c_str(), it
->second
.c_str());
111 const wxWX2MBbuf cbuf
= buf
.mb_str();
112 Write(cbuf
, strlen(cbuf
));
116 bool wxHTTP::ParseHeaders()
119 wxStringTokenizer tokenzr
;
124 #if defined(__VISAGECPP__)
125 // VA just can't stand while(1)
131 m_perr
= GetLine(this, line
);
132 if (m_perr
!= wxPROTO_NOERR
)
135 if (line
.Length() == 0)
138 wxString left_str
= line
.BeforeFirst(':');
139 left_str
.MakeUpper();
141 m_headers
[left_str
] = line
.AfterFirst(':').Strip(wxString::both
);
146 bool wxHTTP::Connect(const wxString
& host
, unsigned short port
)
156 m_addr
= addr
= new wxIPV4address();
158 if (!addr
->Hostname(host
)) {
161 m_perr
= wxPROTO_NETERR
;
165 if ( port
) addr
->Service(port
);
166 else if (!addr
->Service(wxT("http")))
169 SetHeader(wxT("Host"), host
);
174 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
181 m_addr
= addr
.Clone();
183 wxIPV4address
*ipv4addr
= wxDynamicCast(&addr
, wxIPV4address
);
185 SetHeader(wxT("Host"), ipv4addr
->OrigHostname());
190 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
192 const wxChar
*request
;
196 request
= wxT("GET");
199 request
= wxT("POST");
205 // If there is no User-Agent defined, define it.
206 if (GetHeader(wxT("User-Agent")).IsNull())
207 SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
210 SetFlags(wxSOCKET_NONE
);
214 buf
.Printf(wxT("%s %s HTTP/1.0\r\n"), request
, path
.c_str());
215 const wxWX2MBbuf pathbuf
= wxConvLocal
.cWX2MB(buf
);
216 Write(pathbuf
, strlen(wxMBSTRINGCAST pathbuf
));
220 if ( req
== wxHTTP_POST
) {
221 Write(m_post_buf
, m_post_buf
.Len());
222 m_post_buf
= wxEmptyString
;
226 m_perr
= GetLine(this, tmp_str
);
227 if (m_perr
!= wxPROTO_NOERR
) {
232 if (!tmp_str
.Contains(wxT("HTTP/"))) {
233 // TODO: support HTTP v0.9 which can have no header.
234 // FIXME: tmp_str is not put back in the in-queue of the socket.
235 SetHeader(wxT("Content-Length"), wxT("-1"));
236 SetHeader(wxT("Content-Type"), wxT("none/none"));
241 wxStringTokenizer
token(tmp_str
,wxT(' '));
246 tmp_str2
= token
.NextToken();
248 switch (tmp_str2
[0u]) {
250 /* INFORMATION / SUCCESS */
259 m_perr
= wxPROTO_NOFILE
;
264 ret_value
= ParseHeaders();
269 class wxHTTPStream
: public wxSocketInputStream
274 unsigned long m_read_bytes
;
276 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
277 size_t GetSize() const { return m_httpsize
; }
278 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
281 size_t OnSysRead(void *buffer
, size_t bufsize
);
283 DECLARE_NO_COPY_CLASS(wxHTTPStream
)
286 size_t wxHTTPStream::OnSysRead(void *buffer
, size_t bufsize
)
288 if (m_httpsize
> 0 && m_read_bytes
>= m_httpsize
)
290 m_lasterror
= wxSTREAM_EOF
;
294 size_t ret
= wxSocketInputStream::OnSysRead(buffer
, bufsize
);
300 bool wxHTTP::Abort(void)
302 return wxSocketClient::Close();
305 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
307 wxHTTPStream
*inp_stream
;
311 m_perr
= wxPROTO_CONNERR
;
315 // We set m_connected back to FALSE so wxSocketBase will know what to do.
317 wxSocketClient::Connect(*m_addr
, FALSE
);
318 wxSocketClient::WaitOnConnect(10);
320 if (!wxSocketClient::IsConnected())
323 if (!wxProtocol::Connect(*m_addr
))
327 if (!BuildRequest(path
, m_post_buf
.IsEmpty() ? wxHTTP_GET
: wxHTTP_POST
))
330 inp_stream
= new wxHTTPStream(this);
332 if (!GetHeader(wxT("Content-Length")).IsEmpty())
333 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(wxT("Content-Length")));
335 inp_stream
->m_httpsize
= (size_t)-1;
337 inp_stream
->m_read_bytes
= 0;
340 SetFlags(wxSOCKET_BLOCK
| wxSOCKET_WAITALL
);
345 #endif // wxUSE_PROTOCOL_HTTP