1 /*****************************************************************
3 | Platinum - HTTP Client Tasks
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 "PltHttpClientTask.h"
39 #include "PltConstants.h"
41 NPT_SET_LOCAL_LOGGER("platinum.core.http.clienttask")
43 /*----------------------------------------------------------------------
44 | PLT_HttpClientSocketTask::PLT_HttpClientSocketTask
45 +---------------------------------------------------------------------*/
46 PLT_HttpClientSocketTask::PLT_HttpClientSocketTask(NPT_HttpRequest
* request
/* = NULL */,
47 bool wait_forever
/* = false */) :
48 m_WaitForever(wait_forever
)
50 m_Client
.SetUserAgent(*PLT_Constants::GetInstance().GetDefaultUserAgent());
51 m_Client
.SetTimeouts(60000, 60000, 60000);
52 if (request
) m_Requests
.Push(request
);
55 /*----------------------------------------------------------------------
56 | PLT_HttpClientSocketTask::~PLT_HttpClientSocketTask
57 +---------------------------------------------------------------------*/
58 PLT_HttpClientSocketTask::~PLT_HttpClientSocketTask()
60 // delete any outstanding requests
61 NPT_HttpRequest
* request
;
62 while (NPT_SUCCEEDED(m_Requests
.Pop(request
, false))) {
67 /*----------------------------------------------------------------------
68 | PLT_HttpClientSocketTask::SetHttpClientConfig
69 +---------------------------------------------------------------------*/
71 PLT_HttpClientSocketTask::SetHttpClientConfig(const NPT_HttpClient::Config
& config
)
73 return m_Client
.SetConfig(config
);
76 /*----------------------------------------------------------------------
77 | PLT_HttpClientSocketTask::AddRequest
78 +---------------------------------------------------------------------*/
80 PLT_HttpClientSocketTask::AddRequest(NPT_HttpRequest
* request
)
82 return m_Requests
.Push(request
);
85 /*----------------------------------------------------------------------
86 | PLT_HttpClientSocketTask::GetNextRequest
87 +---------------------------------------------------------------------*/
89 PLT_HttpClientSocketTask::GetNextRequest(NPT_HttpRequest
*& request
, NPT_Timeout timeout_ms
)
91 return m_Requests
.Pop(request
, timeout_ms
);
94 /*----------------------------------------------------------------------
95 | PLT_HttpClientSocketTask::DoAbort
96 +---------------------------------------------------------------------*/
98 PLT_HttpClientSocketTask::DoAbort()
103 /*----------------------------------------------------------------------
104 | PLT_HttpClientSocketTask::DoRun
105 +---------------------------------------------------------------------*/
107 PLT_HttpClientSocketTask::DoRun()
109 NPT_HttpRequest
* request
= NULL
;
110 NPT_HttpResponse
* response
= NULL
;
111 NPT_HttpRequestContext context
;
113 NPT_TimeStamp watchdog
;
115 NPT_System::GetCurrentTimeStamp(watchdog
);
118 // pop next request or wait for one for 100ms
119 while (NPT_SUCCEEDED(GetNextRequest(request
, 100))) {
122 if (IsAborting(0)) goto abort
;
125 res
= m_Client
.SendRequest(*request
, response
, &context
);
127 NPT_String prefix
= NPT_String::Format("PLT_HttpClientSocketTask::DoRun (res = %d):", res
);
128 PLT_LOG_HTTP_RESPONSE(NPT_LOG_LEVEL_FINER
, prefix
, response
);
131 ProcessResponse(res
, *request
, context
, response
);
140 // DLNA requires that we abort unanswered/unused sockets after 60 secs
142 NPT_System::GetCurrentTimeStamp(now
);
143 if (now
> watchdog
+ NPT_TimeInterval(60.)) {
144 NPT_HttpConnectionManager::GetInstance()->Recycle(NULL
);
148 } while (m_WaitForever
&& !IsAborting(0));
151 if (request
) delete request
;
152 if (response
) delete response
;
155 /*----------------------------------------------------------------------
156 | PLT_HttpClientSocketTask::ProcessResponse
157 +---------------------------------------------------------------------*/
159 PLT_HttpClientSocketTask::ProcessResponse(NPT_Result res
,
160 const NPT_HttpRequest
& request
,
161 const NPT_HttpRequestContext
& context
,
162 NPT_HttpResponse
* response
)
164 NPT_COMPILER_UNUSED(request
);
165 NPT_COMPILER_UNUSED(context
);
167 NPT_LOG_FINE_1("PLT_HttpClientSocketTask::ProcessResponse (result=%d)", res
);
168 NPT_CHECK_WARNING(res
);
170 NPT_CHECK_POINTER_WARNING(response
);
172 // check if there's a body to read
173 NPT_HttpEntity
* entity
;
174 NPT_InputStreamReference body
;
175 if (!(entity
= response
->GetEntity()) ||
176 NPT_FAILED(entity
->GetInputStream(body
)) ||
181 // dump body into ether
182 // (if no content-length specified, read until disconnected)
183 NPT_NullOutputStream output
;
184 NPT_CHECK_SEVERE(NPT_StreamToStreamCopy(*body
,
187 entity
->GetContentLength()));