]>
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"
28 #include "wx/string.h"
29 #include "wx/tokenzr.h"
30 #include "wx/socket.h"
31 #include "wx/protocol/protocol.h"
32 #include "wx/protocol/http.h"
33 #include "wx/sckstrm.h"
35 #if !USE_SHARED_LIBRARY
36 IMPLEMENT_DYNAMIC_CLASS(wxHTTP
, wxProtocol
)
37 IMPLEMENT_PROTOCOL(wxHTTP
, "http", "80", TRUE
)
40 #define HTTP_BSIZE 2048
44 m_headers(wxKEY_STRING
)
54 // wxString isn't a wxObject
55 wxNode
*node
= m_headers
.First();
59 string
= (wxString
*)node
->Data();
65 wxString
wxHTTP::GetContentType()
67 return GetHeader("Content-Type");
70 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
77 wxNode
*node
= m_headers
.Find(header
);
80 m_headers
.Append(header
, (wxObject
*)(new wxString(h_data
)));
82 wxString
*str
= (wxString
*)node
->Data();
87 wxString
wxHTTP::GetHeader(const wxString
& header
)
89 wxNode
*node
= m_headers
.Find(header
);
93 return *((wxString
*)node
->Data());
96 void wxHTTP::SendHeaders()
98 wxNode
*head
= m_headers
.First();
102 wxString
*str
= (wxString
*)head
->Data();
105 buf
.Printf("%s: %s\n\r", head
->GetKeyString(), str
->GetData());
107 Write(buf
, buf
.Len());
113 bool wxHTTP::ParseHeaders()
121 m_error
= GetLine(this, line
);
122 if (m_error
!= wxPROTO_NOERR
)
125 if (line
.Length() == 0)
128 int pos
= line
.Find(':');
132 wxString left_str
= line(0, pos
);
133 wxString right_str
= line(pos
+1, line
.Length());
135 right_str
= right_str
.Strip(wxString::leading
);
137 wxString
*str
= new wxString(right_str
);
139 m_headers
.Append(left_str
, (wxObject
*) str
);
144 bool wxHTTP::Connect(const wxString
& host
)
154 m_addr
= addr
= new wxIPV4address();
156 if (!addr
->Hostname(host
)) {
159 m_error
= wxPROTO_NETERR
;
163 if (!addr
->Service("http"))
169 bool wxHTTP::Connect(wxSockAddress
& addr
, bool WXUNUSED(wait
))
171 struct sockaddr
*raw_addr
;
174 m_addr
= (wxSockAddress
*)(addr
.GetClassInfo()->CreateObject());
176 addr
.Build(raw_addr
, len
);
177 m_addr
->Disassemble(raw_addr
, len
);
182 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
185 char buf
[HTTP_BSIZE
];
195 sprintf(buf
, "%s %s HTTP/1.0\n\r", tmp_buf
, (const char *)path
);
196 Write(buf
, strlen(buf
));
198 sprintf(buf
, "\n\r");
199 Write(buf
, strlen(buf
));
203 m_error
= GetLine(this, tmp_str
);
204 if (m_error
!= wxPROTO_NOERR
)
207 if (!tmp_str
.Contains("HTTP/")) {
208 // TODO: support HTTP v0.9 which can have no header.
209 SetHeader("Content-Length", "-1");
210 SetHeader("Content-Type", "none/none");
214 wxStringTokenizer
token(tmp_str
,' ');
218 tmp_str2
= token
.NextToken();
220 switch (atoi(tmp_str2
)) {
224 m_error
= wxPROTO_NOFILE
;
228 return ParseHeaders();
231 class wxHTTPStream
: public wxSocketInputStream
{
236 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
237 size_t StreamSize() { return m_httpsize
; }
238 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
241 bool wxHTTP::Abort(void)
243 return wxSocketClient::Close();
246 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
248 wxHTTPStream
*inp_stream
= new wxHTTPStream(this);
250 if (!m_addr
|| m_connected
) {
251 m_error
= wxPROTO_CONNERR
;
255 if (!wxProtocol::Connect(*m_addr
))
258 if (!BuildRequest(path
, wxHTTP_GET
))
261 if (GetHeader("Content-Length").IsEmpty())
262 inp_stream
->m_httpsize
= atoi(WXSTRINGCAST
GetHeader("Content-Length"));