comparison deps/Platinum/Source/Core/PltDownloader.cpp @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Platinum - Downloader
4 |
5 | Copyright (c) 2004-2008, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
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.
13 |
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 |
21 | This program is distributed in the hope that it will be useful,
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | GNU General Public License for more details.
25 |
26 | You should have received a copy of the GNU General Public License
27 | along with this program; see the file LICENSE.txt. If not, write to
28 | the Free Software Foundation, Inc.,
29 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 | http://www.gnu.org/licenses/gpl-2.0.html
31 |
32 ****************************************************************/
33
34 /*----------------------------------------------------------------------
35 | includes
36 +---------------------------------------------------------------------*/
37 #include "PltDownloader.h"
38 #include "PltTaskManager.h"
39
40 /*----------------------------------------------------------------------
41 | PLT_Downloader::PLT_Downloader
42 +---------------------------------------------------------------------*/
43 PLT_Downloader::PLT_Downloader(PLT_TaskManager* task_manager,
44 const char* url,
45 NPT_OutputStreamReference& output) :
46 m_URL(url),
47 m_Output(output),
48 m_TaskManager(task_manager),
49 m_Task(NULL),
50 m_State(PLT_DOWNLOADER_IDLE)
51 {
52 }
53
54 /*----------------------------------------------------------------------
55 | PLT_Downloader::~PLT_Downloader
56 +---------------------------------------------------------------------*/
57 PLT_Downloader::~PLT_Downloader()
58 {
59 Stop();
60 }
61
62 /*----------------------------------------------------------------------
63 | PLT_Downloader::Start
64 +---------------------------------------------------------------------*/
65 NPT_Result
66 PLT_Downloader::Start()
67 {
68 Stop();
69
70 m_Task = new PLT_HttpDownloadTask(NPT_HttpUrl(m_URL), this);
71 NPT_Result res = m_TaskManager->StartTask(m_Task, NULL, false);
72 if (NPT_FAILED(res)) {
73 m_State = PLT_DOWNLOADER_ERROR;
74 return res;
75 }
76
77 m_State = PLT_DOWNLOADER_STARTED;
78 return NPT_SUCCESS;
79 }
80
81 /*----------------------------------------------------------------------
82 | PLT_Downloader::Stop
83 +---------------------------------------------------------------------*/
84 NPT_Result
85 PLT_Downloader::Stop()
86 {
87 if (m_Task) {
88 m_Task->Kill();
89 m_Task = NULL;
90 }
91
92 m_State = PLT_DOWNLOADER_IDLE;
93 return NPT_SUCCESS;
94 }
95
96 /*----------------------------------------------------------------------
97 | PLT_Downloader::ProcessResponse
98 +---------------------------------------------------------------------*/
99 NPT_Result
100 PLT_Downloader::ProcessResponse(NPT_Result res,
101 NPT_HttpRequest* request,
102 const NPT_HttpRequestContext& context,
103 NPT_HttpResponse* response)
104 {
105 NPT_COMPILER_UNUSED(request);
106 NPT_COMPILER_UNUSED(context);
107
108 if (NPT_FAILED(res)) {
109 m_State = PLT_DOWNLOADER_ERROR;
110 return res;
111 }
112
113 m_State = PLT_DOWNLOADER_DOWNLOADING;
114
115 NPT_HttpEntity* entity;
116 NPT_InputStreamReference body;
117 if (!response || !(entity = response->GetEntity()) || NPT_FAILED(entity->GetInputStream(body))) {
118 m_State = PLT_DOWNLOADER_ERROR;
119 return NPT_FAILURE;
120 }
121
122 // Read body (no content length means until socket is closed)
123 res = NPT_StreamToStreamCopy(*body.AsPointer(),
124 *m_Output.AsPointer(),
125 0,
126 entity->GetContentLength());
127
128 if (NPT_FAILED(res)) {
129 m_State = PLT_DOWNLOADER_ERROR;
130 return res;
131 }
132
133 m_State = PLT_DOWNLOADER_SUCCESS;
134 return NPT_SUCCESS;
135 }