From c5020ed38f5af7638000a0385056ad537e2dd242 Mon Sep 17 00:00:00 2001 From: Kandrashin Denis Date: Sat, 12 Mar 2011 02:04:45 +0300 Subject: Try to use wxCurlHTTP object for downloading --- sources/MyRuLib/FbInternetBook.cpp | 26 ++++++++------------- sources/MyRuLib/FbUpdateThread.cpp | 48 +++++--------------------------------- sources/MyRuLib/FbUpdateThread.h | 3 +-- sources/MyRuLib/MyRuLib.cbp | 2 ++ sources/MyRuLib/MyRuLibApp.cpp | 2 ++ sources/wxCURL/WxCURL.cbp | 4 +++- sources/wxXML2/WxXML2.cbp | 4 +++- 7 files changed, 27 insertions(+), 62 deletions(-) diff --git a/sources/MyRuLib/FbInternetBook.cpp b/sources/MyRuLib/FbInternetBook.cpp index eadf7143..0a50b54d 100644 --- a/sources/MyRuLib/FbInternetBook.cpp +++ b/sources/MyRuLib/FbInternetBook.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "FbDataPath.h" #include "FbDateTime.h" #include "controls/FbURL.h" @@ -95,25 +96,19 @@ bool FbInternetBook::DownloadUrl(const wxString &cookie) { if (m_owner->IsClosed()) return false; - FbURL url(m_url); - if (url.GetError() != wxURL_NOERR) { - FbLogError(_("URL error"), m_url); - return false; - } - wxHTTP & http = (wxHTTP&)url.GetProtocol(); - if ( !cookie.IsEmpty() ) http.SetHeader(wxT("Cookie"), cookie); + m_filename = wxFileName::CreateTempFileName(wxT("~")); - wxInputStream * in = url.GetInputStream(); - if (url.GetError() != wxURL_NOERR) { + wxCurlHTTP url(m_url); + url.SetOpt(CURLOPT_TIMEOUT, FbParams::GetInt(FB_WEB_TIMEOUT)); + url.SetOpt(CURLOPT_CONNECTTIMEOUT, FbParams::GetInt(FB_WEB_TIMEOUT)); + bool ok = url.Get(m_filename); + + if (!ok) { FbLogError(_("Connect error"), m_url); return false; } - if (http.GetResponse() == 302) { - m_url = http.GetHeader(wxT("Location")); - FbLogMessage(_("Redirect"), m_url); - return DownloadUrl(cookie); - } - return ReadFile(in); + + return ok; } bool FbInternetBook::ReadFile(wxInputStream * in) @@ -236,4 +231,3 @@ void FbInternetBook::SaveFile(const bool success) FbMasterEvent(ID_UPDATE_MASTER, info, m_id, true).Post(); } } - diff --git a/sources/MyRuLib/FbUpdateThread.cpp b/sources/MyRuLib/FbUpdateThread.cpp index 1e46234a..14da44e3 100644 --- a/sources/MyRuLib/FbUpdateThread.cpp +++ b/sources/MyRuLib/FbUpdateThread.cpp @@ -7,6 +7,7 @@ #include "MyRuLibApp.h" #include #include +#include //----------------------------------------------------------------------------- // FbUpdateItem @@ -70,7 +71,6 @@ FbUpdateItem::~FbUpdateItem() int FbUpdateItem::Execute() { bool ok = OpenURL(); - if (ok) ok = ReadURL(); if (ok) ok = OpenZip(); if (ok) return DoUpdate(); return 0; @@ -78,49 +78,13 @@ int FbUpdateItem::Execute() bool FbUpdateItem::OpenURL() { - m_input = m_url.GetInputStream(); - - wxHTTP & http = (wxHTTP&)m_url.GetProtocol(); - if (http.GetResponse() == 404) { - FbLogError(_("The update file is missing"), m_url.GetURL()); - return false; - } - - FbLogWarning(_("Update collection"), m_url.GetURL()); - - if (m_url.GetError() != wxURL_NOERR) { - FbLogError(_("Download error"), m_url.GetURL()); - return false; - } + m_filename = wxFileName::CreateTempFileName(wxT("~")); + wxCurlHTTP url(m_url); + bool ok = url.Get(m_filename); - return true; -} + if (!ok) FbLogError(_("Download error"), m_url); -bool FbUpdateItem::ReadURL() -{ - m_filename = wxFileName::CreateTempFileName(wxT("~")); - wxFileOutputStream out(m_filename); - - const size_t BUFSIZE = 1024; - unsigned char buf[BUFSIZE]; - size_t size = m_input->GetSize(); - if (!size) size = 0xFFFFFFFF; - size_t count = 0; - size_t pos = 0; - - int step = 1; - do { - FbProgressEvent(ID_PROGRESS_UPDATE, m_url.GetURL(), pos * 1000 / size, _("File download")).Post(); - count = m_input->Read(buf, BUFSIZE).LastRead(); - if ( count ) { - out.Write(buf, count); - pos += count; - } else step++; - } while (pos < size && step < 5); - - FbProgressEvent(ID_PROGRESS_UPDATE).Post(); - - return true; + return ok; } bool FbUpdateItem::OpenZip() diff --git a/sources/MyRuLib/FbUpdateThread.h b/sources/MyRuLib/FbUpdateThread.h index 92321829..959e6a59 100644 --- a/sources/MyRuLib/FbUpdateThread.h +++ b/sources/MyRuLib/FbUpdateThread.h @@ -23,7 +23,6 @@ class FbUpdateItem: public wxObject int Execute(); private: bool OpenURL(); - bool ReadURL(); bool OpenZip(); int DoUpdate(); void ExecInsert(); @@ -33,7 +32,7 @@ class FbUpdateItem: public wxObject wxSQLite3Database & m_database; int m_code; wxString m_type; - FbURL m_url; + wxString m_url; wxString m_filename; wxString m_dataname; wxInputStream * m_input; diff --git a/sources/MyRuLib/MyRuLib.cbp b/sources/MyRuLib/MyRuLib.cbp index 857bc8d5..00dbd553 100644 --- a/sources/MyRuLib/MyRuLib.cbp +++ b/sources/MyRuLib/MyRuLib.cbp @@ -166,7 +166,9 @@ + + diff --git a/sources/MyRuLib/MyRuLibApp.cpp b/sources/MyRuLib/MyRuLibApp.cpp index 4b03dd12..d63b56a4 100644 --- a/sources/MyRuLib/MyRuLibApp.cpp +++ b/sources/MyRuLib/MyRuLibApp.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "FbDataPath.h" #include "FbMainFrame.h" #include "FbLogStream.h" @@ -85,6 +86,7 @@ bool MyRuLibApp::OnInit() ::wxInitAllImageHandlers(); wxFileSystem::AddHandler(new wxMemoryFSHandler); wxFileSystem::AddHandler(new wxInternetFSHandler); + wxCurlHTTP::Init(); LoadBlankImage(); diff --git a/sources/wxCURL/WxCURL.cbp b/sources/wxCURL/WxCURL.cbp index afceb21a..c7d3f828 100644 --- a/sources/wxCURL/WxCURL.cbp +++ b/sources/wxCURL/WxCURL.cbp @@ -17,6 +17,7 @@ + @@ -32,8 +33,9 @@