1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: HTTP protocol
4 // Author: Guilhem Lavaux
6 // Created: August 1997
8 // Copyright: (c) 1997, 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "http.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
30 #include "wx/string.h"
31 #include "wx/tokenzr.h"
32 #include "wx/socket.h"
33 #include "wx/protocol/protocol.h"
34 #include "wx/protocol/http.h"
35 #include "wx/sckstrm.h"
37 #if !USE_SHARED_LIBRARY
38 IMPLEMENT_DYNAMIC_CLASS(wxHTTP
, wxProtocol
)
39 IMPLEMENT_PROTOCOL(wxHTTP
, _T("http"), _T("80"), TRUE
)
42 #define HTTP_BSIZE 2048
46 m_headers(wxKEY_STRING
)
56 // wxString isn't a wxObject
57 wxNode
*node
= m_headers
.First();
61 string
= (wxString
*)node
->Data();
67 wxString
wxHTTP::GetContentType()
69 return GetHeader(_T("Content-Type"));
72 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
79 wxNode
*node
= m_headers
.Find(header
);
82 m_headers
.Append(header
, (wxObject
*)(new wxString(h_data
)));
84 wxString
*str
= (wxString
*)node
->Data();
89 wxString
wxHTTP::GetHeader(const wxString
& header
)
91 wxNode
*node
= m_headers
.Find(header
);
95 return *((wxString
*)node
->Data());
98 void wxHTTP::SendHeaders()
100 wxNode
*head
= m_headers
.First();
104 wxString
*str
= (wxString
*)head
->Data();
107 buf
.Printf(_T("%s: %s\n\r"), head
->GetKeyString(), str
->GetData());
109 const wxWX2MBbuf cbuf
= buf
.mb_str();
110 Write(cbuf
, strlen(cbuf
));
116 bool wxHTTP::ParseHeaders()
119 wxStringTokenizer tokenzr
;
125 m_error
= GetLine(this, line
);
126 if (m_error
!= wxPROTO_NOERR
)
129 if (line
.Length() == 0)
132 printf("Header: %s\n", WXSTRINGCAST line
);
133 tokenzr
.SetString(line
, " :\t\n\r");
134 if (!tokenzr
.HasMoreToken())
137 wxString left_str
= tokenzr
.GetNextToken();
138 wxString
*str
= new wxString(tokenzr
.GetNextToken());
140 m_headers
.Append(left_str
, (wxObject
*) str
);
145 bool wxHTTP::Connect(const wxString
& host
)
155 m_addr
= addr
= new wxIPV4address();
157 if (!addr
->Hostname(host
)) {
160 m_error
= wxPROTO_NETERR
;
164 if (!addr
->Service(_T("http")))
170 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
172 struct sockaddr
*raw_addr
;
175 m_addr
= (wxSockAddress
*)(addr
.GetClassInfo()->CreateObject());
177 addr
.Build(raw_addr
, len
);
178 m_addr
->Disassemble(raw_addr
, len
);
183 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
186 char buf
[HTTP_BSIZE
];
187 const wxWX2MBbuf pathbuf
= path
.mb_str();
201 sprintf(buf
, "%s %s HTTP/1.0\n\r", tmp_buf
, (const char*)pathbuf
);
202 Write(buf
, strlen(buf
));
204 sprintf(buf
, "\n\r");
205 Write(buf
, strlen(buf
));
209 m_error
= GetLine(this, tmp_str
);
210 if (m_error
!= wxPROTO_NOERR
) {
215 if (!tmp_str
.Contains(_T("HTTP/"))) {
216 // TODO: support HTTP v0.9 which can have no header.
217 SetHeader(_T("Content-Length"), _T("-1"));
218 SetHeader(_T("Content-Type"), _T("none/none"));
223 wxStringTokenizer
token(tmp_str
,_T(' '));
228 tmp_str2
= token
.NextToken();
230 switch (wxAtoi(tmp_str2
)) {
234 m_error
= wxPROTO_NOFILE
;
239 ret_value
= ParseHeaders();
244 class wxHTTPStream
: public wxSocketInputStream
{
249 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
250 size_t StreamSize() const { return m_httpsize
; }
251 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
254 bool wxHTTP::Abort(void)
256 return wxSocketClient::Close();
259 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
261 wxHTTPStream
*inp_stream
= new wxHTTPStream(this);
263 if (!m_addr
|| m_connected
) {
264 m_error
= wxPROTO_CONNERR
;
268 if (!wxProtocol::Connect(*m_addr
))
271 if (!BuildRequest(path
, wxHTTP_GET
))
274 wxPrintf(_T("Len = %s\n"), WXSTRINGCAST
GetHeader(_T("Content-Length")));
275 if (!GetHeader(_T("Content-Length")).IsEmpty())
276 inp_stream
->m_httpsize
= wxAtoi(WXSTRINGCAST
GetHeader(_T("Content-Length")));