Ticket #10655: wxhttp_cookies.patch

File wxhttp_cookies.patch, 4.0 KB (added by dodge, 12 years ago)

.patch file for SVN Diff

  • include/wx/protocol/http.h

     
    3939    void SetPostBuffer(const wxString& post_buf);
    4040    void SetProxyMode(bool on);
    4141
     42    /* Cookies */
     43    wxString GetCookie(const wxString& cookie) const;
     44        bool HasCookies() { return m_cookies.size() > 0; }
     45    void EnableCookies(bool on = true) { m_useCookies = on; }
     46        void DisableCookies() { EnableCookies(false); }
     47
    4248protected:
    4349    enum wxHTTP_Req
    4450    {
     
    4955
    5056    typedef wxStringToStringHashMap::iterator wxHeaderIterator;
    5157    typedef wxStringToStringHashMap::const_iterator wxHeaderConstIterator;
     58    typedef wxStringToStringHashMap::iterator wxCookieIterator;
     59    typedef wxStringToStringHashMap::const_iterator wxCookieConstIterator;
    5260
    5361    bool BuildRequest(const wxString& path, wxHTTP_Req req);
    5462    void SendHeaders();
     
    5967    // find the header in m_headers
    6068    wxHeaderIterator FindHeader(const wxString& header);
    6169    wxHeaderConstIterator FindHeader(const wxString& header) const;
     70    wxCookieIterator FindCookie(const wxString& cookie);
     71    wxCookieConstIterator FindCookie(const wxString& cookie) const;
    6272
    6373    // deletes the header value strings
    6474    void ClearHeaders();
     75    void ClearCookies();
    6576
    66 
    6777    // internal variables:
    6878
     79    bool m_useCookies;
     80    wxStringToStringHashMap m_cookies;
     81
    6982    wxStringToStringHashMap m_headers;
    7083    bool m_read,
    7184         m_proxy_mode;
  • src/common/http.cpp

     
    4848    m_addr = NULL;
    4949    m_read = false;
    5050    m_proxy_mode = false;
     51    m_useCookies = false;
    5152    m_post_buf = wxEmptyString;
    5253    m_http_response = 0;
    5354
     
    6667    m_headers.clear();
    6768}
    6869
     70void wxHTTP::ClearCookies()
     71{
     72    m_cookies.clear();
     73}
     74
    6975wxString wxHTTP::GetContentType() const
    7076{
    7177    return GetHeader(wxT("Content-Type"));
     
    100106    return it;
    101107}
    102108
     109wxHTTP::wxCookieIterator wxHTTP::FindCookie(const wxString& cookie)
     110{
     111    wxCookieIterator it = m_cookies.begin();
     112    for ( wxCookieIterator en = m_cookies.end(); it != en; ++it )
     113    {
     114        if ( cookie.CmpNoCase(it->first) == 0 )
     115            break;
     116    }
     117
     118    return it;
     119}
     120
     121wxHTTP::wxCookieConstIterator wxHTTP::FindCookie(const wxString& cookie) const
     122{
     123    wxCookieConstIterator it = m_cookies.begin();
     124    for ( wxCookieConstIterator en = m_cookies.end(); it != en; ++it )
     125    {
     126        if ( cookie.CmpNoCase(it->first) == 0 )
     127            break;
     128    }
     129
     130    return it;
     131}
     132
    103133void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
    104134{
    105135    if (m_read) {
     
    121151    return it == m_headers.end() ? wxGetEmptyString() : it->second;
    122152}
    123153
     154wxString wxHTTP::GetCookie(const wxString& cookie) const
     155{
     156    wxCookieConstIterator it = FindCookie(cookie);
     157
     158    return it == m_cookies.end() ? wxGetEmptyString() : it->second;
     159}
     160
    124161wxString wxHTTP::GenerateAuthString(const wxString& user, const wxString& pass) const
    125162{
    126163    static const char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
     
    178215    wxStringTokenizer tokenzr;
    179216
    180217    ClearHeaders();
     218    ClearCookies();
    181219    m_read = true;
    182220
    183221    for ( ;; )
     
    190228            break;
    191229
    192230        wxString left_str = line.BeforeFirst(':');
    193         m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
     231        if(!left_str.CmpNoCase("Set-Cookie") && m_useCookies)
     232        {
     233            wxString cookieName = line.AfterFirst(':').Strip(wxString::both).BeforeFirst('=');
     234            wxString cookieValue = line.AfterFirst(':').Strip(wxString::both).AfterFirst('=').BeforeFirst(';');
     235            m_cookies[cookieName] = cookieValue;
     236        }
     237        else
     238        {
     239            m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
     240        }
    194241    }
    195242    return true;
    196243}