]>
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"
18 #include "wx/string.h"
19 #include "wx/tokenzr.h"
20 #include "wx/socket.h"
21 #include "wx/protocol/protocol.h"
22 #include "wx/protocol/http.h"
23 #include "wx/sckstrm.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxHTTP
, wxProtocol
)
31 IMPLEMENT_PROTOCOL(wxHTTP
, "http", "80", TRUE
)
34 #define HTTP_BSIZE 2048
38 m_headers(wxKEY_STRING
)
48 // wxString isn't a wxObject
49 wxNode
*node
= m_headers
.First();
53 string
= (wxString
*)node
->Data();
59 wxString
wxHTTP::GetContentType()
61 return GetHeader("Content-Type");
64 void wxHTTP::SetHeader(const wxString
& header
, const wxString
& h_data
)
71 wxNode
*node
= m_headers
.Find(header
);
74 m_headers
.Append(header
, (wxObject
*)(new wxString(h_data
)));
76 wxString
*str
= (wxString
*)node
->Data();
81 wxString
wxHTTP::GetHeader(const wxString
& header
)
83 wxNode
*node
= m_headers
.Find(header
);
87 return *((wxString
*)node
->Data());
90 void wxHTTP::SendHeaders()
92 wxNode
*head
= m_headers
.First();
95 wxString
*str
= (wxString
*)head
->Data();
98 sprintf(buf
, "%s: %s\n\r", head
->key
.string
, str
->GetData());
99 Write(buf
, strlen(buf
));
105 bool wxHTTP::ParseHeaders()
113 m_error
= GetLine(this, line
);
114 if (m_error
!= wxPROTO_NOERR
)
117 if (line
.Length() == 0)
120 int pos
= line
.Find(':');
124 wxString left_str
= line(0, pos
);
125 wxString right_str
= line(pos
+1, line
.Length());
127 right_str
= right_str
.Strip(wxString::leading
);
129 wxString
*str
= new wxString(right_str
);
131 m_headers
.Append(left_str
, (wxObject
*) str
);
136 bool wxHTTP::Connect(const wxString
& host
)
146 m_addr
= addr
= new wxIPV4address();
148 if (!addr
->Hostname(host
)) {
151 m_error
= wxPROTO_NETERR
;
155 if (!addr
->Service("http"))
161 bool wxHTTP::Connect(wxSockAddress
& addr
)
163 struct sockaddr
*raw_addr
;
166 m_addr
= (wxSockAddress
*)(addr
.GetClassInfo()->CreateObject());
168 addr
.Build(raw_addr
, len
);
169 m_addr
->Disassemble(raw_addr
, len
);
174 bool wxHTTP::BuildRequest(const wxString
& path
, wxHTTP_Req req
)
177 char buf
[HTTP_BSIZE
];
187 sprintf(buf
, "%s %s HTTP/1.0\n\r", tmp_buf
, (const char *)path
);
188 Write(buf
, strlen(buf
));
190 sprintf(buf
, "\n\r");
191 Write(buf
, strlen(buf
));
195 m_error
= GetLine(this, tmp_str
);
196 if (m_error
!= wxPROTO_NOERR
)
199 if (!tmp_str
.Contains("HTTP/")) {
200 // TODO: support HTTP v0.9 which can have no header.
201 SetHeader("Content-Length", "-1");
202 SetHeader("Content-Type", "none/none");
206 wxStringTokenizer
token(tmp_str
,' ');
210 tmp_str2
= token
.NextToken();
212 switch (atoi(tmp_str2
)) {
216 m_error
= wxPROTO_NOFILE
;
220 return ParseHeaders();
223 class wxHTTPStream
: public wxSocketInputStream
{
226 wxHTTPStream(wxHTTP
*http
) : wxSocketInputStream(*http
), m_http(http
) {}
227 virtual ~wxHTTPStream(void) { m_http
->Abort(); }
230 bool wxHTTP::Abort(void)
232 return wxSocketClient::Close();
235 wxInputStream
*wxHTTP::GetInputStream(const wxString
& path
)
237 wxHTTPStream
*inp_stream
= new wxHTTPStream(this);
239 if (!m_addr
|| m_connected
) {
240 m_error
= wxPROTO_CONNERR
;
244 if (!wxProtocol::Connect(*m_addr
))
247 if (!BuildRequest(path
, wxHTTP_GET
))