]>
git.saurik.com Git - wxWidgets.git/blob - src/common/http.cpp
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
, "http", "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("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("%s: %s\n\r", head
->GetKeyString(), str
->GetData());
109 Write(buf
, buf
.Len());
115 bool wxHTTP::ParseHeaders()
123 m_error
= GetLine(this, line
);
124 if (m_error
!= wxPROTO_NOERR
)
127 if (line
.Length() == 0)
130 printf("Header: %s\n", WXSTRINGCAST line
);
131 int pos
= line
.Find(':');
135 wxString left_str
= line(0, pos
);
136 wxString right_str
= line(pos
+1, line
.Length());
138 right_str
= right_str
.Strip(wxString::leading
);
140 wxString
*str
= new wxString(right_str
);
142 m_headers
.Append(left_str
, (wxObject
*) str
);
147 bool wxHTTP::Connect(const wxString
& host
)
157 m_addr
= addr
= new wxIPV4address();
159 if (!addr
->Hostname(host
)) {
162 m_error
= wxPROTO_NETERR
;
166 if (!addr
->Service("http"))
172 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
174 struct sockaddr
*raw_addr
;
177 m_addr
= (wxSockAddress
*)(addr
.GetClassInfo()->CreateObject());
179 addr
.Build(raw_addr
, len
);
180 m_addr
->Disassemble(raw_addr
, len
);
185 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
188 char buf
[HTTP_BSIZE
];
202 sprintf(buf
, "%s %s HTTP/1.0\n\r", tmp_buf
, (const char *)path
);
203 Write(buf
, strlen(buf
));
205 sprintf(buf
, "\n\r");
206 Write(buf
, strlen(buf
));
210 m_error
= GetLine(this, tmp_str
);
211 if (m_error
!= wxPROTO_NOERR
) {
216 if (!tmp_str
.Contains("HTTP/")) {
217 // TODO: support HTTP v0.9 which can have no header.
218 SetHeader("Content-Length", "-1");
219 SetHeader("Content-Type", "none/none");
224 wxStringTokenizer
token(tmp_str
,' ');
229 tmp_str2
= token
.NextToken();
231 switch (atoi(tmp_str2
)) {
235 m_error
= wxPROTO_NOFILE
;
240 ret_value
= ParseHeaders();
245 class wxHTTPStream
: public wxSocketInputStream
{
250 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
251 size_t StreamSize() const { return m_httpsize
; }
252 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
255 bool wxHTTP::Abort(void)
257 return wxSocketClient::Close();
260 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
262 wxHTTPStream
*inp_stream
= new wxHTTPStream(this);
264 if (!m_addr
|| m_connected
) {
265 m_error
= wxPROTO_CONNERR
;
269 if (!wxProtocol::Connect(*m_addr
))
272 if (!BuildRequest(path
, wxHTTP_GET
))
275 printf("Len = %s\n", WXSTRINGCAST
GetHeader("Content-Length"));
276 if (!GetHeader("Content-Length").IsEmpty())
277 inp_stream
->m_httpsize
= atoi(WXSTRINGCAST
GetHeader("Content-Length"));