Upstream tarball 10010
[amule.git] / src / HTTPDownload.cpp
blob6cc2e03002605e4fccc67a091f48b5ad5bc5451f
1 //
2 // This file is part of the aMule Project.
3 //
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 )
6 //
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
9 // respective authors.
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.
20 //
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
42 #ifndef AMULE_DAEMON
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
55 public:
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));
67 m_ani->Start();
69 m_thread = thread;
72 ~CHTTPDownloadDialog() {
73 StopThread();
76 void UpdateGauge(int total, int current) {
77 CFormat label(_("( %s / %s )"));
79 label % CastItoXBytes(current);
80 if (total > 0) {
81 label % CastItoXBytes(total);
82 } else {
83 label % _("Unknown");
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);
96 Layout();
99 private:
100 void StopThread() {
101 if (m_thread) {
102 m_thread->Stop();
103 delete m_thread;
104 m_thread = NULL;
108 void OnBtnCancel(wxCommandEvent& WXUNUSED(evt)) {
109 AddLogLineN(_("HTTP download cancelled"));
110 Show(false);
111 StopThread();
114 void OnProgress(CMuleInternalEvent& evt) {
115 UpdateGauge(evt.GetExtraLong(), evt.GetInt());
118 void OnShutdown(CMuleInternalEvent& WXUNUSED(evt)) {
119 Show(false);
120 Destroy();
123 CMuleThread* m_thread;
124 MuleGifCtrl* m_ani;
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)
135 END_EVENT_TABLE()
137 DEFINE_LOCAL_EVENT_TYPE(wxEVT_HTTP_PROGRESS)
138 DEFINE_LOCAL_EVENT_TYPE(wxEVT_HTTP_SHUTDOWN)
140 #endif
143 CHTTPDownloadThread::CHTTPDownloadThread(const wxChar* url, const wxChar* filename, const wxChar* oldfilename, HTTP_Download_File file_id, bool showDialog)
144 #ifdef AMULE_DAEMON
145 : CMuleThread(wxTHREAD_DETACHED),
146 #else
147 : CMuleThread(showDialog ? wxTHREAD_JOINABLE : wxTHREAD_DETACHED),
148 #endif
149 m_url(url),
150 m_tempfile(filename),
151 m_result(-1),
152 m_file_id(file_id),
153 m_companion(NULL)
155 if (showDialog) {
156 #ifndef AMULE_DAEMON
157 CHTTPDownloadDialog* dialog = new CHTTPDownloadDialog(this);
158 dialog->Show(true);
159 m_companion = dialog;
160 #endif
162 // Get the date on which the original file was last modified
163 // But first: check if the file exists!
164 wxFileName origFile = wxFileName(oldfilename);
165 if (origFile.FileExists()) {
166 m_hasdate = true;
167 m_lastmodified = origFile.GetModificationTime();
168 } else {
169 m_hasdate = false;
174 // Format the given date to a RFC-2616 compliant HTTP date
175 // Example: Thu, 14 Jan 2010 15:40:23 GMT
176 wxString CHTTPDownloadThread::FormatDateHTTP(const wxDateTime& date)
178 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") };
179 static const wxChar* s_dow[] = { wxT("Sun"), wxT("Mon"), wxT("Tue"), wxT("Wed"), wxT("Thu"), wxT("Fri"), wxT("Sat") };
181 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);
185 CMuleThread::ExitCode CHTTPDownloadThread::Entry()
187 if (TestDestroy()) {
188 return NULL;
191 wxHTTP* url_handler = NULL;
193 AddDebugLogLineN(logHTTP, wxT("HTTP download thread started"));
195 const CProxyData* proxy_data = thePrefs::GetProxyData();
196 bool use_proxy = proxy_data != NULL && proxy_data->m_proxyEnable;
198 try {
199 wxFFileOutputStream outfile(m_tempfile);
201 if (!outfile.Ok()) {
202 throw wxString(CFormat(_("Unable to create destination file %s for download!")) % m_tempfile);
205 if (m_url.IsEmpty()) {
206 // Nowhere to download from!
207 throw wxString(_("The URL to download can't be empty"));
210 url_handler = new wxHTTP;
211 url_handler->SetProxyMode(use_proxy);
213 // Build a conditional get request if the last modified date of the file being updated is known
214 if (m_hasdate) {
215 // Set a flag in the HTTP header that we only download if the file is newer.
216 // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25
217 AddDebugLogLineN(logHTTP, wxT("If-Modified-Since: ") + FormatDateHTTP(m_lastmodified));
218 url_handler->SetHeader(wxT("If-Modified-Since"), FormatDateHTTP(m_lastmodified));
221 CScopedPtr<wxInputStream> url_read_stream(GetInputStream(&url_handler, m_url, use_proxy));
223 if (!url_read_stream.get()) {
224 if (m_response == 304) {
225 m_result = HTTP_Skipped;
226 return 0;
227 } else {
228 m_result = HTTP_Error;
229 throw wxString(CFormat(_("The URL %s returned: %i - Error (%i)!")) % m_url % m_response % m_error);
233 int download_size = url_read_stream->GetSize();
234 if (download_size == -1) {
235 AddDebugLogLineN(logHTTP, wxT("Download size not received, downloading until connection is closed"));
236 } else {
237 AddDebugLogLineN(logHTTP, CFormat(wxT("Download size: %i")) % download_size);
240 // Here is our read buffer
241 // <ken> Still, I'm sure 4092 is probably a better size.
242 // MP: Most people can download at least at 32kb/s from http...
243 const unsigned MAX_HTTP_READ = 32768;
245 char buffer[MAX_HTTP_READ];
246 int current_read = 0;
247 int total_read = 0;
248 do {
249 url_read_stream->Read(buffer, MAX_HTTP_READ);
250 current_read = url_read_stream->LastRead();
251 if (current_read) {
252 total_read += current_read;
253 outfile.Write(buffer,current_read);
254 int current_write = outfile.LastWrite();
255 if (current_read != current_write) {
256 throw wxString(_("Critical error while writing downloaded file"));
257 } else if (m_companion) {
258 #ifndef AMULE_DAEMON
259 CMuleInternalEvent evt(wxEVT_HTTP_PROGRESS);
260 evt.SetInt(total_read);
261 evt.SetExtraLong(download_size);
262 wxPostEvent(m_companion, evt);
263 #endif
266 } while (current_read && !TestDestroy());
268 if (current_read == 0) {
269 if (download_size == -1) {
270 // Download was probably succesful.
271 AddLogLineN(CFormat(_("Downloaded %d bytes")) % total_read);
272 m_result = HTTP_Success;
273 } else if (total_read != download_size) {
274 m_result = HTTP_Error;
275 throw wxString(CFormat(_("Expected %d bytes, but downloaded %d bytes")) % download_size % total_read);
276 } else {
277 // Download was succesful.
278 m_result = HTTP_Success;
281 } catch (const wxString& error) {
282 if (wxFileExists(m_tempfile)) {
283 wxRemoveFile(m_tempfile);
285 AddLogLineC(error);
288 if (url_handler) {
289 url_handler->Destroy();
292 AddDebugLogLineN(logHTTP, wxT("HTTP download thread ended"));
294 return 0;
298 void CHTTPDownloadThread::OnExit()
300 #ifndef AMULE_DAEMON
301 if (m_companion) {
302 CMuleInternalEvent termEvent(wxEVT_HTTP_SHUTDOWN);
303 wxPostEvent(m_companion, termEvent);
305 #endif
307 // Notice the app that the file finished download
308 CMuleInternalEvent evt(wxEVT_CORE_FINISHED_HTTP_DOWNLOAD);
309 evt.SetInt((int)m_file_id);
310 evt.SetExtraLong((long)m_result);
311 wxPostEvent(wxTheApp, evt);
315 //! This function's purpose is to handle redirections in a proper way.
316 wxInputStream* CHTTPDownloadThread::GetInputStream(wxHTTP** url_handler, const wxString& location, bool proxy)
318 if (TestDestroy()) {
319 return NULL;
322 if (!location.StartsWith(wxT("http://"))) {
323 // This is not a http url
324 throw wxString(_("Invalid URL for HTTP download or HTTP redirection (did you forget 'http://' ?)"));
327 // Get the host
329 // Remove the "http://"
330 wxString host = location.Right(location.Len() - 7); // strlen("http://") -> 7
332 // I belive this is a bug...
333 // Sometimes "Location" header looks like this:
334 // "http://www.whatever.com:8080http://www.whatever.com/downloads/something.zip"
335 // So let's clean it...
337 int bad_url_pos = host.Find(wxT("http://"));
338 wxString location_url;
339 if (bad_url_pos != -1) {
340 // Malformed Location field on header (bug above?)
341 location_url = host.Mid(bad_url_pos);
342 host = host.Left(bad_url_pos);
343 // After the first '/' non-http-related, it's the location part of the URL
344 location_url = location_url.Right(location_url.Len() - 7).AfterFirst(wxT('/'));
345 } else {
346 // Regular Location field
347 // After the first '/', it's the location part of the URL
348 location_url = host.AfterFirst(wxT('/'));
349 // The host is everything till the first "/"
350 host = host.BeforeFirst(wxT('/'));
353 // Build the cleaned url now
354 wxString url = wxT("http://") + host + wxT("/") + location_url;
356 int port = 80;
357 if (host.Find(wxT(':')) != -1) {
358 // This http url has a port
359 port = wxAtoi(host.AfterFirst(wxT(':')));
360 host = host.BeforeFirst(wxT(':'));
363 wxIPV4address addr;
364 addr.Hostname(host);
365 addr.Service(port);
366 if (!(*url_handler)->Connect(addr, true)) {
367 throw wxString(_("Unable to connect to HTTP download server"));
370 wxInputStream* url_read_stream = (*url_handler)->GetInputStream(url);
372 /* store the HTTP response code */
373 m_response = (*url_handler)->GetResponse();
375 /* store the HTTP error code */
376 m_error = (*url_handler)->GetError();
378 AddDebugLogLineN(logHTTP, CFormat(wxT("Host: %s:%i\n")) % host % port);
379 AddDebugLogLineN(logHTTP, CFormat(wxT("URL: %s\n")) % url);
380 AddDebugLogLineN(logHTTP, CFormat(wxT("Response: %i (Error: %i)")) % m_response % m_error);
382 if (!m_response) {
383 AddDebugLogLineC(logHTTP, wxT("WARNING: Void response on stream creation"));
384 // WTF? Why does this happen?
385 // This is probably produced by an already existing connection, because
386 // the input stream is created nevertheless. However, data is not the same.
387 delete url_read_stream;
388 throw wxString(_("Invalid response from HTTP download server"));
391 if (m_response == 301 // Moved permanently
392 || m_response == 302 // Moved temporarily
393 // What about 300 (multiple choices)? Do we have to handle it?
396 // We have to remove the current stream.
397 delete url_read_stream;
399 wxString new_location = (*url_handler)->GetHeader(wxT("Location"));
401 (*url_handler)->Destroy();
402 if (!new_location.IsEmpty()) {
403 (*url_handler) = new wxHTTP;
404 (*url_handler)->SetProxyMode(proxy);
405 if (m_hasdate) {
406 // Set a flag in the HTTP header that we only download if the file is newer.
407 // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25
408 (*url_handler)->SetHeader(wxT("If-Modified-Since"), FormatDateHTTP(m_lastmodified));
410 url_read_stream = GetInputStream(url_handler, new_location, proxy);
411 } else {
412 AddDebugLogLineC(logHTTP, wxT("ERROR: Redirection code received with no URL"));
413 url_handler = NULL;
414 url_read_stream = NULL;
416 } else if (m_response == 304) { // "Not Modified"
417 delete url_read_stream;
418 (*url_handler)->Destroy();
419 url_read_stream = NULL;
420 url_handler = NULL;
423 return url_read_stream;
426 // File_checked_for_headers