Upstream tarball 9846
[amule.git] / src / HTTPDownload.cpp
blobf8b633c12fd8895f4a2c74ca36dce1f97115c60b
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"
41 #ifndef AMULE_DAEMON
42 #include "inetdownload.h" // Needed for inetDownload
43 #include "muuli_wdr.h" // Needed for ID_CANCEL: Let it here or will fail on win32
44 #include "MuleGifCtrl.h"
46 typedef wxGauge wxGaugeControl;
48 DECLARE_LOCAL_EVENT_TYPE(wxEVT_HTTP_PROGRESS, wxANY_ID)
49 DECLARE_LOCAL_EVENT_TYPE(wxEVT_HTTP_SHUTDOWN, wxANY_ID)
52 class CHTTPDownloadDialog : public wxDialog
54 public:
55 CHTTPDownloadDialog(CHTTPDownloadThread* thread)
56 : wxDialog(wxTheApp->GetTopWindow(), -1, _("Downloading..."),
57 wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxSYSTEM_MENU)
59 downloadDlg(this, true)->Show(this, true);
61 m_progressbar = CastChild(ID_HTTPDOWNLOADPROGRESS, wxGaugeControl);
62 m_progressbar->SetRange(100);
64 m_ani = CastChild(ID_ANIMATE, MuleGifCtrl);
65 m_ani->LoadData((const char*)inetDownload, sizeof(inetDownload));
66 m_ani->Start();
68 m_thread = thread;
71 ~CHTTPDownloadDialog() {
72 StopThread();
75 void UpdateGauge(int total, int current) {
76 CFormat label(_("( %s / %s )"));
78 label % CastItoXBytes(current);
79 if (total > 0) {
80 label % CastItoXBytes(total);
81 } else {
82 label % _("Unknown");
85 CastChild(IDC_DOWNLOADSIZE, wxStaticText)->SetLabel(label.GetString());
87 if (total && (total != m_progressbar->GetRange())) {
88 m_progressbar->SetRange(total);
91 if (current && (current <= total)) {
92 m_progressbar->SetValue(current);
95 Layout();
98 private:
99 void StopThread() {
100 if (m_thread) {
101 m_thread->Stop();
102 delete m_thread;
103 m_thread = NULL;
107 void OnBtnCancel(wxCommandEvent& WXUNUSED(evt)) {
108 AddLogLineNS(_("HTTP download cancelled"));
109 Show(false);
110 StopThread();
113 void OnProgress(CMuleInternalEvent& evt) {
114 UpdateGauge(evt.GetExtraLong(), evt.GetInt());
117 void OnShutdown(CMuleInternalEvent& WXUNUSED(evt)) {
118 Show(false);
119 Destroy();
122 CMuleThread* m_thread;
123 MuleGifCtrl* m_ani;
124 wxGaugeControl* m_progressbar;
126 DECLARE_EVENT_TABLE()
130 BEGIN_EVENT_TABLE(CHTTPDownloadDialog, wxDialog)
131 EVT_BUTTON(ID_HTTPCANCEL, CHTTPDownloadDialog::OnBtnCancel)
132 EVT_MULE_INTERNAL(wxEVT_HTTP_PROGRESS, -1, CHTTPDownloadDialog::OnProgress)
133 EVT_MULE_INTERNAL(wxEVT_HTTP_SHUTDOWN, -1, CHTTPDownloadDialog::OnShutdown)
134 END_EVENT_TABLE()
136 DEFINE_LOCAL_EVENT_TYPE(wxEVT_HTTP_PROGRESS)
137 DEFINE_LOCAL_EVENT_TYPE(wxEVT_HTTP_SHUTDOWN)
139 #endif
142 CHTTPDownloadThread::CHTTPDownloadThread(const wxChar* url, const wxChar* filename, HTTP_Download_File file_id, bool showDialog)
143 #ifdef AMULE_DAEMON
144 : CMuleThread(wxTHREAD_DETACHED),
145 #else
146 : CMuleThread(showDialog ? wxTHREAD_JOINABLE : wxTHREAD_DETACHED),
147 #endif
148 m_url(url),
149 m_tempfile(filename),
150 m_result(-1),
151 m_file_id(file_id),
152 m_companion(NULL)
154 if (showDialog) {
155 #ifndef AMULE_DAEMON
156 CHTTPDownloadDialog* dialog = new CHTTPDownloadDialog(this);
157 dialog->Show(true);
158 m_companion = dialog;
159 #endif
164 CMuleThread::ExitCode CHTTPDownloadThread::Entry()
166 if (TestDestroy()) {
167 return NULL;
170 wxHTTP* url_handler = NULL;
172 AddLogLineNS(_("HTTP download thread started"));
174 const CProxyData* proxy_data = thePrefs::GetProxyData();
175 bool use_proxy = proxy_data != NULL && proxy_data->m_proxyEnable;
177 try {
178 wxFFileOutputStream outfile(m_tempfile);
180 if (!outfile.Ok()) {
181 throw wxString(CFormat(wxT("Unable to create destination file %s for download!\n")) % m_tempfile);
184 if ( m_url.IsEmpty() ) {
185 // Nowhere to download from!
186 throw wxString(wxT("The URL to download can't be empty\n"));
189 url_handler = new wxHTTP;
190 url_handler->SetProxyMode(use_proxy);
192 CScopedPtr<wxInputStream> url_read_stream(GetInputStream(&url_handler, m_url, use_proxy));
194 if (!url_read_stream.get()) {
195 throw wxString(CFormat(wxT("The URL %s returned: %i - Error (%i)!")) % m_url % url_handler->GetResponse() % url_handler->GetError());
198 int download_size = url_read_stream->GetSize();
199 if (download_size == -1) {
200 AddLogLineNS(_("Download size not received, downloading until connection is closed"));
201 } else {
202 AddLogLineNS(CFormat(_("Download size: %i")) % download_size);
205 // Here is our read buffer
206 // <ken> Still, I'm sure 4092 is probably a better size.
207 // MP: Most people can download at least at 32kb/s from http...
208 const unsigned MAX_HTTP_READ = 32768;
210 char buffer[MAX_HTTP_READ];
211 int current_read = 0;
212 int total_read = 0;
213 do {
214 url_read_stream->Read(buffer, MAX_HTTP_READ);
215 current_read = url_read_stream->LastRead();
216 if (current_read) {
217 total_read += current_read;
218 outfile.Write(buffer,current_read);
219 int current_write = outfile.LastWrite();
220 if (current_read != current_write) {
221 throw wxString(wxT("Critical error while writing downloaded file"));
222 } else if (m_companion) {
223 #ifndef AMULE_DAEMON
224 CMuleInternalEvent evt(wxEVT_HTTP_PROGRESS);
225 evt.SetInt(total_read);
226 evt.SetExtraLong(download_size);
227 wxPostEvent(m_companion, evt);
228 #endif
231 } while (current_read && !TestDestroy());
233 if (current_read == 0) {
234 if (download_size == -1) {
235 // Download was probably succesful.
236 AddLogLineNS(CFormat(_("Downloaded %d bytes")) % total_read);
237 m_result = 1;
238 } else if (total_read != download_size) {
239 throw wxString(CFormat(_("Expected %d bytes, but downloaded %d bytes")) % download_size % total_read);
240 } else {
241 // Download was succesful.
242 m_result = 1;
245 } catch (const wxString& error) {
246 if (wxFileExists(m_tempfile)) {
247 wxRemoveFile(m_tempfile);
250 AddLogLineNS(error); // If there's console output anyway there should also be console output on error.
253 if (url_handler) {
254 url_handler->Destroy();
257 AddLogLineNS(_("HTTP download thread ended"));
259 return 0;
263 void CHTTPDownloadThread::OnExit()
265 #ifndef AMULE_DAEMON
266 if (m_companion) {
267 CMuleInternalEvent termEvent(wxEVT_HTTP_SHUTDOWN);
268 wxPostEvent(m_companion, termEvent);
270 #endif
272 // Notice the app that the file finished download
273 CMuleInternalEvent evt(wxEVT_CORE_FINISHED_HTTP_DOWNLOAD);
274 evt.SetInt((int)m_file_id);
275 evt.SetExtraLong((long)m_result);
276 wxPostEvent(wxTheApp, evt);
280 //! This function's purpose is to handle redirections in a proper way.
281 wxInputStream* CHTTPDownloadThread::GetInputStream(wxHTTP** url_handler, const wxString& location, bool proxy)
283 if (TestDestroy()) {
284 return NULL;
287 if (!location.StartsWith(wxT("http://"))) {
288 // This is not a http url
289 throw wxString(wxT("Invalid URL for http download or http redirection (did you forget 'http://' ?)"));
292 // Get the host
294 // Remove the "http://"
295 wxString host = location.Right(location.Len() - 7); // strlen("http://") -> 7
297 // I belive this is a bug...
298 // Sometimes "Location" header looks like this:
299 // "http://www.whatever.com:8080http://www.whatever.com/downloads/something.zip"
300 // So let's clean it...
302 int bad_url_pos = host.Find(wxT("http://"));
303 wxString location_url;
304 if (bad_url_pos != -1) {
305 // Malformed Location field on header (bug above?)
306 location_url = host.Mid(bad_url_pos);
307 host = host.Left(bad_url_pos);
308 // After the first '/' non-http-related, it's the location part of the URL
309 location_url = location_url.Right(location_url.Len() - 7).AfterFirst(wxT('/'));
310 } else {
311 // Regular Location field
312 // After the first '/', it's the location part of the URL
313 location_url = host.AfterFirst(wxT('/'));
314 // The host is everything till the first "/"
315 host = host.BeforeFirst(wxT('/'));
318 // Build the cleaned url now
319 wxString url = wxT("http://") + host + wxT("/") + location_url;
321 int port = 80;
322 if (host.Find(wxT(':')) != -1) {
323 // This http url has a port
324 port = wxAtoi(host.AfterFirst(wxT(':')));
325 host = host.BeforeFirst(wxT(':'));
328 wxIPV4address addr;
329 addr.Hostname(host);
330 addr.Service(port);
331 if (!(*url_handler)->Connect(addr, true)) {
332 throw wxString(wxT("Unable to connect to http download server"));
335 wxInputStream* url_read_stream = (*url_handler)->GetInputStream(url);
337 AddLogLineNS(CFormat(_("Host: %s:%i\n")) % host % port);
338 AddLogLineNS(CFormat(wxT("URL: %s\n")) % url);
339 AddLogLineNS(CFormat(_("Response: %i (Error: %i)")) % (*url_handler)->GetResponse() % (*url_handler)->GetError());
341 if (!(*url_handler)->GetResponse()) {
342 AddLogLineNS(_("WARNING: Void response on stream creation"));
343 // WTF? Why does this happen?
344 // This is probably produced by an already existing connection, because
345 // the input stream is created nevertheless. However, data is not the same.
346 delete url_read_stream;
347 throw wxString(wxT("Invalid response from http download server"));
350 if ((*url_handler)->GetResponse() == 301 // Moved permanently
351 || (*url_handler)->GetResponse() == 302 // Moved temporarily
352 // What about 300 (multiple choices)? Do we have to handle it?
353 ) {
355 // We have to remove the current stream.
356 delete url_read_stream;
358 wxString new_location = (*url_handler)->GetHeader(wxT("Location"));
360 (*url_handler)->Destroy();
361 if (!new_location.IsEmpty()) {
362 (*url_handler) = new wxHTTP;
363 (*url_handler)->SetProxyMode(proxy);
364 url_read_stream = GetInputStream(url_handler, new_location, proxy);
365 } else {
366 AddLogLineCS(_("ERROR: Redirection code received with no URL"));
367 url_handler = NULL;
368 url_read_stream = NULL;
372 return url_read_stream;
375 // File_checked_for_headers