2 // This file is part of the aMule Project.
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Timo Kujala ( tiku@users.sourceforge.net )
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <wx/wfstream.h>
28 #include <wx/protocol/http.h>
31 #include "HTTPDownload.h" // Interface declarations
32 #include <common/StringFunctions.h> // Needed for unicode2char
33 #include "OtherFunctions.h" // Needed for CastChild
34 #include "Logger.h" // Needed for AddLogLine*
35 #include <common/Format.h> // Needed for CFormat
36 #include "InternalEvents.h" // Needed for CMuleInternalEvent
37 #include "Preferences.h"
38 #include "ScopedPtr.h"
39 #include <wx/filename.h> // Needed for wxFileName
43 #include "inetdownload.h" // Needed for inetDownload
44 #include "muuli_wdr.h" // Needed for ID_CANCEL: Let it here or will fail on win32
45 #include "MuleGifCtrl.h"
47 typedef wxGauge wxGaugeControl
;
49 DECLARE_LOCAL_EVENT_TYPE(wxEVT_HTTP_PROGRESS
, wxANY_ID
)
50 DECLARE_LOCAL_EVENT_TYPE(wxEVT_HTTP_SHUTDOWN
, wxANY_ID
)
53 class CHTTPDownloadDialog
: public wxDialog
56 CHTTPDownloadDialog(CHTTPDownloadThread
* thread
)
57 : wxDialog(wxTheApp
->GetTopWindow(), -1, _("Downloading..."),
58 wxDefaultPosition
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxSYSTEM_MENU
)
60 downloadDlg(this, true)->Show(this, true);
62 m_progressbar
= CastChild(ID_HTTPDOWNLOADPROGRESS
, wxGaugeControl
);
63 m_progressbar
->SetRange(100);
65 m_ani
= CastChild(ID_ANIMATE
, MuleGifCtrl
);
66 m_ani
->LoadData((const char*)inetDownload
, sizeof(inetDownload
));
72 ~CHTTPDownloadDialog() {
76 void UpdateGauge(int total
, int current
) {
77 CFormat
label(_("( %s / %s )"));
79 label
% CastItoXBytes(current
);
81 label
% CastItoXBytes(total
);
86 CastChild(IDC_DOWNLOADSIZE
, wxStaticText
)->SetLabel(label
.GetString());
88 if (total
&& (total
!= m_progressbar
->GetRange())) {
89 m_progressbar
->SetRange(total
);
92 if (current
&& (current
<= total
)) {
93 m_progressbar
->SetValue(current
);
108 void OnBtnCancel(wxCommandEvent
& WXUNUSED(evt
)) {
109 AddLogLineN(_("HTTP download cancelled"));
114 void OnProgress(CMuleInternalEvent
& evt
) {
115 UpdateGauge(evt
.GetExtraLong(), evt
.GetInt());
118 void OnShutdown(CMuleInternalEvent
& WXUNUSED(evt
)) {
123 CMuleThread
* m_thread
;
125 wxGaugeControl
* m_progressbar
;
127 DECLARE_EVENT_TABLE()
131 BEGIN_EVENT_TABLE(CHTTPDownloadDialog
, wxDialog
)
132 EVT_BUTTON(ID_HTTPCANCEL
, CHTTPDownloadDialog::OnBtnCancel
)
133 EVT_MULE_INTERNAL(wxEVT_HTTP_PROGRESS
, -1, CHTTPDownloadDialog::OnProgress
)
134 EVT_MULE_INTERNAL(wxEVT_HTTP_SHUTDOWN
, -1, CHTTPDownloadDialog::OnShutdown
)
137 DEFINE_LOCAL_EVENT_TYPE(wxEVT_HTTP_PROGRESS
)
138 DEFINE_LOCAL_EVENT_TYPE(wxEVT_HTTP_SHUTDOWN
)
143 CHTTPDownloadThread::CHTTPDownloadThread(const wxString
& url
, const wxString
& filename
, const wxString
& oldfilename
, HTTP_Download_File file_id
, bool showDialog
)
145 : CMuleThread(wxTHREAD_DETACHED
),
147 : CMuleThread(showDialog
? wxTHREAD_JOINABLE
: wxTHREAD_DETACHED
),
150 m_tempfile(filename
),
157 CHTTPDownloadDialog
* dialog
= new CHTTPDownloadDialog(this);
159 m_companion
= dialog
;
162 // Get the date on which the original file was last modified
163 // Only if it's the same URL we used for the last download and if the file exists.
164 if (thePrefs::GetLastHTTPDownloadURL(file_id
) == url
) {
165 wxFileName origFile
= wxFileName(oldfilename
);
166 if (origFile
.FileExists()) {
167 AddDebugLogLineN(logHTTP
, CFormat(wxT("URL %s matches and file %s exists, only download if newer")) % url
% oldfilename
);
168 m_lastmodified
= origFile
.GetModificationTime();
171 wxMutexLocker
lock(s_allThreadsMutex
);
172 s_allThreads
.insert(this);
176 // Format the given date to a RFC-2616 compliant HTTP date
177 // Example: Thu, 14 Jan 2010 15:40:23 GMT
178 wxString
CHTTPDownloadThread::FormatDateHTTP(const wxDateTime
& date
)
180 static const wxChar
* s_months
[] = { wxT("Jan"), wxT("Feb"), wxT("Mar"), wxT("Apr"), wxT("May"), wxT("Jun"), wxT("Jul"), wxT("Aug"), wxT("Sep"), wxT("Oct"), wxT("Nov"), wxT("Dec") };
181 static const wxChar
* s_dow
[] = { wxT("Sun"), wxT("Mon"), wxT("Tue"), wxT("Wed"), wxT("Thu"), wxT("Fri"), wxT("Sat") };
183 return CFormat(wxT("%s, %02d %s %d %02d:%02d:%02d GMT")) % s_dow
[date
.GetWeekDay(wxDateTime::UTC
)] % date
.GetDay(wxDateTime::UTC
) % s_months
[date
.GetMonth(wxDateTime::UTC
)] % date
.GetYear(wxDateTime::UTC
) % date
.GetHour(wxDateTime::UTC
) % date
.GetMinute(wxDateTime::UTC
) % date
.GetSecond(wxDateTime::UTC
);
187 CMuleThread::ExitCode
CHTTPDownloadThread::Entry()
193 wxHTTP
* url_handler
= NULL
;
195 AddDebugLogLineN(logHTTP
, wxT("HTTP download thread started"));
197 const CProxyData
* proxy_data
= thePrefs::GetProxyData();
198 bool use_proxy
= proxy_data
!= NULL
&& proxy_data
->m_proxyEnable
;
201 wxFFileOutputStream
outfile(m_tempfile
);
204 throw wxString(CFormat(_("Unable to create destination file %s for download!")) % m_tempfile
);
207 if (m_url
.IsEmpty()) {
208 // Nowhere to download from!
209 throw wxString(_("The URL to download can't be empty"));
212 url_handler
= new wxHTTP
;
213 url_handler
->SetProxyMode(use_proxy
);
215 // Build a conditional get request if the last modified date of the file being updated is known
216 if (m_lastmodified
.IsValid()) {
217 // Set a flag in the HTTP header that we only download if the file is newer.
218 // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25
219 AddDebugLogLineN(logHTTP
, wxT("If-Modified-Since: ") + FormatDateHTTP(m_lastmodified
));
220 url_handler
->SetHeader(wxT("If-Modified-Since"), FormatDateHTTP(m_lastmodified
));
223 CScopedPtr
<wxInputStream
> url_read_stream(GetInputStream(url_handler
, m_url
, use_proxy
));
225 if (!url_read_stream
.get()) {
226 if (m_response
== 304) {
227 m_result
= HTTP_Skipped
;
228 AddDebugLogLineN(logHTTP
, wxT("Skipped download because requested file is not newer."));
229 throw wxString(wxEmptyString
);
231 m_result
= HTTP_Error
;
232 throw wxString(CFormat(_("The URL %s returned: %i - Error (%i)!")) % m_url
% m_response
% m_error
);
236 int download_size
= url_read_stream
->GetSize();
237 if (download_size
== -1) {
238 AddDebugLogLineN(logHTTP
, wxT("Download size not received, downloading until connection is closed"));
240 AddDebugLogLineN(logHTTP
, CFormat(wxT("Download size: %i")) % download_size
);
243 // Here is our read buffer
244 // <ken> Still, I'm sure 4092 is probably a better size.
245 // MP: Most people can download at least at 32kb/s from http...
246 const unsigned MAX_HTTP_READ
= 32768;
248 char buffer
[MAX_HTTP_READ
];
249 int current_read
= 0;
252 url_read_stream
->Read(buffer
, MAX_HTTP_READ
);
253 current_read
= url_read_stream
->LastRead();
255 total_read
+= current_read
;
256 outfile
.Write(buffer
,current_read
);
257 int current_write
= outfile
.LastWrite();
258 if (current_read
!= current_write
) {
259 throw wxString(_("Critical error while writing downloaded file"));
260 } else if (m_companion
) {
262 CMuleInternalEvent
evt(wxEVT_HTTP_PROGRESS
);
263 evt
.SetInt(total_read
);
264 evt
.SetExtraLong(download_size
);
265 wxPostEvent(m_companion
, evt
);
269 } while (current_read
&& !TestDestroy());
271 if (current_read
== 0) {
272 if (download_size
== -1) {
273 // Download was probably succesful.
274 AddLogLineN(CFormat(_("Downloaded %d bytes")) % total_read
);
275 m_result
= HTTP_Success
;
276 } else if (total_read
!= download_size
) {
277 m_result
= HTTP_Error
;
278 throw wxString(CFormat(_("Expected %d bytes, but downloaded %d bytes")) % download_size
% total_read
);
280 // Download was succesful.
281 m_result
= HTTP_Success
;
284 } catch (const wxString
& error
) {
285 if (wxFileExists(m_tempfile
)) {
286 wxRemoveFile(m_tempfile
);
288 if (!error
.IsEmpty()) {
293 if (m_result
== HTTP_Success
) {
294 thePrefs::SetLastHTTPDownloadURL(m_file_id
, m_url
);
298 url_handler
->Destroy();
301 AddDebugLogLineN(logHTTP
, wxT("HTTP download thread ended"));
307 void CHTTPDownloadThread::OnExit()
311 CMuleInternalEvent
termEvent(wxEVT_HTTP_SHUTDOWN
);
312 wxPostEvent(m_companion
, termEvent
);
316 // Notice the app that the file finished download
317 CMuleInternalEvent
evt(wxEVT_CORE_FINISHED_HTTP_DOWNLOAD
);
318 evt
.SetInt((int)m_file_id
);
319 evt
.SetExtraLong((long)m_result
);
320 wxPostEvent(wxTheApp
, evt
);
321 wxMutexLocker
lock(s_allThreadsMutex
);
322 s_allThreads
.erase(this);
326 //! This function's purpose is to handle redirections in a proper way.
327 wxInputStream
* CHTTPDownloadThread::GetInputStream(wxHTTP
* & url_handler
, const wxString
& location
, bool proxy
)
333 if (!location
.StartsWith(wxT("http://"))) {
334 // This is not a http url
335 throw wxString(_("Invalid URL for HTTP download or HTTP redirection (did you forget 'http://' ?)"));
340 // Remove the "http://"
341 wxString host
= location
.Right(location
.Len() - 7); // strlen("http://") -> 7
343 // I belive this is a bug...
344 // Sometimes "Location" header looks like this:
345 // "http://www.whatever.com:8080http://www.whatever.com/downloads/something.zip"
346 // So let's clean it...
348 int bad_url_pos
= host
.Find(wxT("http://"));
349 wxString location_url
;
350 if (bad_url_pos
!= -1) {
351 // Malformed Location field on header (bug above?)
352 location_url
= host
.Mid(bad_url_pos
);
353 host
= host
.Left(bad_url_pos
);
354 // After the first '/' non-http-related, it's the location part of the URL
355 location_url
= location_url
.Right(location_url
.Len() - 7).AfterFirst(wxT('/'));
357 // Regular Location field
358 // After the first '/', it's the location part of the URL
359 location_url
= host
.AfterFirst(wxT('/'));
360 // The host is everything till the first "/"
361 host
= host
.BeforeFirst(wxT('/'));
364 // Build the cleaned url now
365 wxString url
= wxT("http://") + host
+ wxT("/") + location_url
;
368 if (host
.Find(wxT(':')) != -1) {
369 // This http url has a port
370 port
= wxAtoi(host
.AfterFirst(wxT(':')));
371 host
= host
.BeforeFirst(wxT(':'));
377 if (!url_handler
->Connect(addr
, true)) {
378 throw wxString(_("Unable to connect to HTTP download server"));
381 wxInputStream
* url_read_stream
= url_handler
->GetInputStream(url
);
383 /* store the HTTP response code */
384 m_response
= url_handler
->GetResponse();
386 /* store the HTTP error code */
387 m_error
= url_handler
->GetError();
389 AddDebugLogLineN(logHTTP
, CFormat(wxT("Host: %s:%i\n")) % host
% port
);
390 AddDebugLogLineN(logHTTP
, CFormat(wxT("URL: %s\n")) % url
);
391 AddDebugLogLineN(logHTTP
, CFormat(wxT("Response: %i (Error: %i)")) % m_response
% m_error
);
394 AddDebugLogLineC(logHTTP
, wxT("WARNING: Void response on stream creation"));
395 // WTF? Why does this happen?
396 // This is probably produced by an already existing connection, because
397 // the input stream is created nevertheless. However, data is not the same.
398 delete url_read_stream
;
399 throw wxString(_("Invalid response from HTTP download server"));
402 if (m_response
== 301 // Moved permanently
403 || m_response
== 302 // Moved temporarily
404 // What about 300 (multiple choices)? Do we have to handle it?
407 // We have to remove the current stream.
408 delete url_read_stream
;
410 wxString new_location
= url_handler
->GetHeader(wxT("Location"));
412 url_handler
->Destroy();
413 if (!new_location
.IsEmpty()) {
414 url_handler
= new wxHTTP
;
415 url_handler
->SetProxyMode(proxy
);
416 if (m_lastmodified
.IsValid()) {
417 // Set a flag in the HTTP header that we only download if the file is newer.
418 // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25
419 url_handler
->SetHeader(wxT("If-Modified-Since"), FormatDateHTTP(m_lastmodified
));
421 url_read_stream
= GetInputStream(url_handler
, new_location
, proxy
);
423 AddDebugLogLineC(logHTTP
, wxT("ERROR: Redirection code received with no URL"));
425 url_read_stream
= NULL
;
427 } else if (m_response
== 304) { // "Not Modified"
428 delete url_read_stream
;
429 url_handler
->Destroy();
430 url_read_stream
= NULL
;
434 return url_read_stream
;
437 void CHTTPDownloadThread::StopAll()
439 ThreadSet allThreads
;
441 wxMutexLocker
lock(s_allThreadsMutex
);
442 std::swap(allThreads
, s_allThreads
);
444 for (ThreadSet::iterator it
= allThreads
.begin(); it
!= allThreads
.end(); it
++) {
449 CHTTPDownloadThread::ThreadSet
CHTTPDownloadThread::s_allThreads
;
450 wxMutex
CHTTPDownloadThread::s_allThreadsMutex
;
452 // File_checked_for_headers