1 /*****************************************************************
3 | Platinum - Downloader
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
7 | http://www.plutinosoft.com
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 | licensing@plutinosoft.com
22 | This program is distributed in the hope that it will be useful,
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 | GNU General Public License for more details.
27 | You should have received a copy of the GNU General Public License
28 | along with this program; see the file LICENSE.txt. If not, write to
29 | the Free Software Foundation, Inc.,
30 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 | http://www.gnu.org/licenses/gpl-2.0.html
33 ****************************************************************/
35 /*----------------------------------------------------------------------
37 +---------------------------------------------------------------------*/
38 #include "PltDownloader.h"
39 #include "PltTaskManager.h"
43 NPT_SET_LOCAL_LOGGER("platinum.extra.downloader")
45 /*----------------------------------------------------------------------
46 | PLT_Downloader::PLT_Downloader
47 +---------------------------------------------------------------------*/
48 PLT_Downloader::PLT_Downloader(NPT_HttpUrl
& url
,
49 NPT_OutputStreamReference
& output
) :
50 PLT_HttpClientSocketTask(new NPT_HttpRequest(url
,
52 NPT_HTTP_PROTOCOL_1_1
)),
55 m_State(PLT_DOWNLOADER_IDLE
)
59 /*----------------------------------------------------------------------
60 | PLT_Downloader::~PLT_Downloader
61 +---------------------------------------------------------------------*/
62 PLT_Downloader::~PLT_Downloader()
66 /*----------------------------------------------------------------------
67 | PLT_Downloader::DoRun
68 +---------------------------------------------------------------------*/
70 PLT_Downloader::DoRun()
72 m_State
= PLT_DOWNLOADER_STARTED
;
73 return PLT_HttpClientSocketTask::DoRun();
76 /*----------------------------------------------------------------------
77 | PLT_Downloader::DoAbort
78 +---------------------------------------------------------------------*/
80 PLT_Downloader::DoAbort()
82 PLT_HttpClientSocketTask::DoAbort();
83 m_State
= PLT_DOWNLOADER_IDLE
;
86 /*----------------------------------------------------------------------
87 | PLT_Downloader::ProcessResponse
88 +---------------------------------------------------------------------*/
90 PLT_Downloader::ProcessResponse(NPT_Result res
,
91 const NPT_HttpRequest
& request
,
92 const NPT_HttpRequestContext
& context
,
93 NPT_HttpResponse
* response
)
95 NPT_COMPILER_UNUSED(request
);
96 NPT_COMPILER_UNUSED(context
);
98 if (NPT_FAILED(res
)) {
99 NPT_LOG_WARNING_2("Downloader error %d for %s", res
, m_URL
.ToString().GetChars());
100 m_State
= PLT_DOWNLOADER_ERROR
;
104 m_State
= PLT_DOWNLOADER_DOWNLOADING
;
106 NPT_HttpEntity
* entity
;
107 NPT_InputStreamReference body
;
109 !(entity
= response
->GetEntity()) ||
110 NPT_FAILED(entity
->GetInputStream(body
)) ||
112 m_State
= PLT_DOWNLOADER_ERROR
;
113 NPT_LOG_WARNING_2("No body %d for %s", res
, m_URL
.ToString().GetChars());
117 // Read body (no content length means until socket is closed)
118 res
= NPT_StreamToStreamCopy(*body
.AsPointer(),
119 *m_Output
.AsPointer(),
121 entity
->GetContentLength());
123 if (NPT_FAILED(res
)) {
124 NPT_LOG_WARNING_2("Downloader error %d for %s", res
, m_URL
.ToString().GetChars());
125 m_State
= PLT_DOWNLOADER_ERROR
;
129 NPT_LOG_INFO_1("Finished downloading %s", m_URL
.ToString().GetChars());
130 m_State
= PLT_DOWNLOADER_SUCCESS
;