Applied modified #10655 (Added Cookie (receive) support to wxHTTP)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62160 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2009-09-26 19:47:23 +00:00
parent b0a9bfc8b8
commit 906cb3f119
4 changed files with 74 additions and 2 deletions

View File

@@ -398,6 +398,7 @@ All:
- Added wxAny class; a modern, backwards-incompatible replacement for - Added wxAny class; a modern, backwards-incompatible replacement for
wxVariant. wxVariant.
- wxDateTime timezone functions now dynamic (no caching). - wxDateTime timezone functions now dynamic (no caching).
- Added wxHttp::GetCookie and wxHttp::HasCookies (dodge).
All (GUI): All (GUI):

View File

@@ -39,6 +39,10 @@ public:
void SetPostBuffer(const wxString& post_buf); void SetPostBuffer(const wxString& post_buf);
void SetProxyMode(bool on); void SetProxyMode(bool on);
/* Cookies */
wxString GetCookie(const wxString& cookie) const;
bool HasCookies() const { return m_cookies.size() > 0; }
protected: protected:
enum wxHTTP_Req enum wxHTTP_Req
{ {
@@ -49,6 +53,8 @@ protected:
typedef wxStringToStringHashMap::iterator wxHeaderIterator; typedef wxStringToStringHashMap::iterator wxHeaderIterator;
typedef wxStringToStringHashMap::const_iterator wxHeaderConstIterator; typedef wxStringToStringHashMap::const_iterator wxHeaderConstIterator;
typedef wxStringToStringHashMap::iterator wxCookieIterator;
typedef wxStringToStringHashMap::const_iterator wxCookieConstIterator;
bool BuildRequest(const wxString& path, wxHTTP_Req req); bool BuildRequest(const wxString& path, wxHTTP_Req req);
void SendHeaders(); void SendHeaders();
@@ -59,13 +65,17 @@ protected:
// find the header in m_headers // find the header in m_headers
wxHeaderIterator FindHeader(const wxString& header); wxHeaderIterator FindHeader(const wxString& header);
wxHeaderConstIterator FindHeader(const wxString& header) const; wxHeaderConstIterator FindHeader(const wxString& header) const;
wxCookieIterator FindCookie(const wxString& cookie);
wxCookieConstIterator FindCookie(const wxString& cookie) const;
// deletes the header value strings // deletes the header value strings
void ClearHeaders(); void ClearHeaders();
void ClearCookies();
// internal variables: // internal variables:
wxStringToStringHashMap m_cookies;
wxStringToStringHashMap m_headers; wxStringToStringHashMap m_headers;
bool m_read, bool m_read,
m_proxy_mode; m_proxy_mode;

View File

@@ -92,5 +92,17 @@ public:
This is a low level function and it assumes that you know what you are doing. This is a low level function and it assumes that you know what you are doing.
*/ */
void SetHeader(const wxString& header, const wxString& h_data); void SetHeader(const wxString& header, const wxString& h_data);
/**
Returns the value of a cookie.
*/
wxString GetCookie(const wxString& cookie) const;
/**
Returns @true if there were cookies.
*/
bool HasCookies() const;
}; };

View File

@@ -66,6 +66,11 @@ void wxHTTP::ClearHeaders()
m_headers.clear(); m_headers.clear();
} }
void wxHTTP::ClearCookies()
{
m_cookies.clear();
}
wxString wxHTTP::GetContentType() const wxString wxHTTP::GetContentType() const
{ {
return GetHeader(wxT("Content-Type")); return GetHeader(wxT("Content-Type"));
@@ -100,6 +105,30 @@ wxHTTP::wxHeaderConstIterator wxHTTP::FindHeader(const wxString& header) const
return it; return it;
} }
wxHTTP::wxCookieIterator wxHTTP::FindCookie(const wxString& cookie)
{
wxCookieIterator it = m_cookies.begin();
for ( wxCookieIterator en = m_cookies.end(); it != en; ++it )
{
if ( cookie.CmpNoCase(it->first) == 0 )
break;
}
return it;
}
wxHTTP::wxCookieConstIterator wxHTTP::FindCookie(const wxString& cookie) const
{
wxCookieConstIterator it = m_cookies.begin();
for ( wxCookieConstIterator en = m_cookies.end(); it != en; ++it )
{
if ( cookie.CmpNoCase(it->first) == 0 )
break;
}
return it;
}
void wxHTTP::SetHeader(const wxString& header, const wxString& h_data) void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
{ {
if (m_read) { if (m_read) {
@@ -121,6 +150,13 @@ wxString wxHTTP::GetHeader(const wxString& header) const
return it == m_headers.end() ? wxGetEmptyString() : it->second; return it == m_headers.end() ? wxGetEmptyString() : it->second;
} }
wxString wxHTTP::GetCookie(const wxString& cookie) const
{
wxCookieConstIterator it = FindCookie(cookie);
return it == m_cookies.end() ? wxGetEmptyString() : it->second;
}
wxString wxHTTP::GenerateAuthString(const wxString& user, const wxString& pass) const wxString wxHTTP::GenerateAuthString(const wxString& user, const wxString& pass) const
{ {
static const char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -178,6 +214,7 @@ bool wxHTTP::ParseHeaders()
wxStringTokenizer tokenzr; wxStringTokenizer tokenzr;
ClearHeaders(); ClearHeaders();
ClearCookies();
m_read = true; m_read = true;
for ( ;; ) for ( ;; )
@@ -190,7 +227,19 @@ bool wxHTTP::ParseHeaders()
break; break;
wxString left_str = line.BeforeFirst(':'); wxString left_str = line.BeforeFirst(':');
m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both); if(!left_str.CmpNoCase("Set-Cookie"))
{
wxString cookieName = line.AfterFirst(':').Strip(wxString::both).BeforeFirst('=');
wxString cookieValue = line.AfterFirst(':').Strip(wxString::both).AfterFirst('=').BeforeFirst(';');
m_cookies[cookieName] = cookieValue;
// For compatibility
m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
}
else
{
m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
}
} }
return true; return true;
} }