From 906cb3f119409013f52c0976ea8f9cd082ae6a1b Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Sat, 26 Sep 2009 19:47:23 +0000 Subject: [PATCH] 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 --- docs/changes.txt | 1 + include/wx/protocol/http.h | 12 ++++++++- interface/wx/protocol/http.h | 12 +++++++++ src/common/http.cpp | 51 +++++++++++++++++++++++++++++++++++- 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index a4f77f279b..b61db35461 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -398,6 +398,7 @@ All: - Added wxAny class; a modern, backwards-incompatible replacement for wxVariant. - wxDateTime timezone functions now dynamic (no caching). +- Added wxHttp::GetCookie and wxHttp::HasCookies (dodge). All (GUI): diff --git a/include/wx/protocol/http.h b/include/wx/protocol/http.h index 1e99661b1e..9505acdb6d 100644 --- a/include/wx/protocol/http.h +++ b/include/wx/protocol/http.h @@ -39,6 +39,10 @@ public: void SetPostBuffer(const wxString& post_buf); void SetProxyMode(bool on); + /* Cookies */ + wxString GetCookie(const wxString& cookie) const; + bool HasCookies() const { return m_cookies.size() > 0; } + protected: enum wxHTTP_Req { @@ -49,6 +53,8 @@ protected: typedef wxStringToStringHashMap::iterator wxHeaderIterator; typedef wxStringToStringHashMap::const_iterator wxHeaderConstIterator; + typedef wxStringToStringHashMap::iterator wxCookieIterator; + typedef wxStringToStringHashMap::const_iterator wxCookieConstIterator; bool BuildRequest(const wxString& path, wxHTTP_Req req); void SendHeaders(); @@ -59,13 +65,17 @@ protected: // find the header in m_headers wxHeaderIterator FindHeader(const wxString& header); wxHeaderConstIterator FindHeader(const wxString& header) const; + wxCookieIterator FindCookie(const wxString& cookie); + wxCookieConstIterator FindCookie(const wxString& cookie) const; // deletes the header value strings void ClearHeaders(); - + void ClearCookies(); // internal variables: + wxStringToStringHashMap m_cookies; + wxStringToStringHashMap m_headers; bool m_read, m_proxy_mode; diff --git a/interface/wx/protocol/http.h b/interface/wx/protocol/http.h index cdad04236d..9cd5e50342 100644 --- a/interface/wx/protocol/http.h +++ b/interface/wx/protocol/http.h @@ -92,5 +92,17 @@ public: 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); + + /** + Returns the value of a cookie. + */ + + wxString GetCookie(const wxString& cookie) const; + + /** + Returns @true if there were cookies. + */ + + bool HasCookies() const; }; diff --git a/src/common/http.cpp b/src/common/http.cpp index 145ec61f3c..82c5fc065b 100644 --- a/src/common/http.cpp +++ b/src/common/http.cpp @@ -66,6 +66,11 @@ void wxHTTP::ClearHeaders() m_headers.clear(); } +void wxHTTP::ClearCookies() +{ + m_cookies.clear(); +} + wxString wxHTTP::GetContentType() const { return GetHeader(wxT("Content-Type")); @@ -100,6 +105,30 @@ wxHTTP::wxHeaderConstIterator wxHTTP::FindHeader(const wxString& header) const 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) { if (m_read) { @@ -121,6 +150,13 @@ wxString wxHTTP::GetHeader(const wxString& header) const 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 { static const char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -178,6 +214,7 @@ bool wxHTTP::ParseHeaders() wxStringTokenizer tokenzr; ClearHeaders(); + ClearCookies(); m_read = true; for ( ;; ) @@ -190,7 +227,19 @@ bool wxHTTP::ParseHeaders() break; 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; }