2
|
1 |
// HttpClient.cpp
|
|
2 |
|
|
3 |
|
|
4 |
#include <e32base.h>
|
|
5 |
#include <http/rhttpheaders.h>
|
|
6 |
#include <http.h>
|
|
7 |
#include <commdb.h>
|
|
8 |
#include <eikenv.h>
|
|
9 |
#include <es_sock.h>
|
|
10 |
#include <bautils.h>
|
|
11 |
#include <CommDbConnPref.h>
|
|
12 |
#include "debug.h"
|
|
13 |
#include "constants.h"
|
|
14 |
#include "HttpClient.h"
|
|
15 |
#include "connectionengine.h"
|
|
16 |
#include "settingsengine.h"
|
60
|
17 |
#include "Podcatcher.pan"
|
2
|
18 |
|
|
19 |
const TInt KTempBufferSize = 100;
|
|
20 |
|
|
21 |
CHttpClient::~CHttpClient()
|
|
22 |
{
|
|
23 |
if (iHandler)
|
|
24 |
{
|
|
25 |
iHandler->CloseSaveFile();
|
|
26 |
delete iHandler;
|
|
27 |
}
|
|
28 |
|
|
29 |
iSession.Close();
|
|
30 |
}
|
|
31 |
|
|
32 |
CHttpClient* CHttpClient::NewL(CPodcastModel& aPodcastModel, MHttpClientObserver& aObserver)
|
|
33 |
{
|
|
34 |
CHttpClient* me = NewLC(aPodcastModel, aObserver);
|
|
35 |
CleanupStack::Pop(me);
|
|
36 |
return me;
|
|
37 |
}
|
|
38 |
|
|
39 |
CHttpClient::CHttpClient(CPodcastModel& aPodcastModel, MHttpClientObserver& aObserver) : iPodcastModel(aPodcastModel), iObserver(aObserver)
|
|
40 |
{
|
|
41 |
iResumeEnabled = EFalse;
|
|
42 |
}
|
|
43 |
|
|
44 |
CHttpClient* CHttpClient::NewLC(CPodcastModel& aPodcastModel, MHttpClientObserver& aObserver)
|
|
45 |
{
|
|
46 |
CHttpClient* me = new (ELeave) CHttpClient(aPodcastModel, aObserver);
|
|
47 |
CleanupStack::PushL(me);
|
|
48 |
me->ConstructL();
|
|
49 |
return me;
|
|
50 |
}
|
|
51 |
|
|
52 |
void CHttpClient::ConstructL()
|
|
53 |
{
|
|
54 |
iPodcastModel.ConnectionEngine().AddObserver(this);
|
|
55 |
}
|
|
56 |
|
|
57 |
void CHttpClient::SetHeaderL(RHTTPHeaders aHeaders, TInt aHdrField, const TDesC8& aHdrValue)
|
|
58 |
{
|
|
59 |
RStringF valStr = iSession.StringPool().OpenFStringL(aHdrValue);
|
|
60 |
THTTPHdrVal val(valStr);
|
|
61 |
aHeaders.SetFieldL(iSession.StringPool().StringF(aHdrField, RHTTPSession::GetTable()), val);
|
|
62 |
valStr.Close();
|
|
63 |
}
|
|
64 |
|
|
65 |
TBool CHttpClient::IsActive()
|
|
66 |
{
|
|
67 |
return iIsActive;
|
|
68 |
}
|
|
69 |
|
|
70 |
void CHttpClient::SetResumeEnabled(TBool aEnabled)
|
|
71 |
{
|
|
72 |
iResumeEnabled = aEnabled;
|
|
73 |
}
|
|
74 |
|
|
75 |
|
|
76 |
void CHttpClient::ConnectHttpSessionL()
|
|
77 |
{
|
|
78 |
DP("ConnectHttpSessionL START");
|
|
79 |
CConnectionEngine::TConnectionState connState = iPodcastModel.ConnectionEngine().ConnectionState();
|
|
80 |
if(connState == CConnectionEngine::EConnected)
|
|
81 |
{
|
|
82 |
DP("ConnectionState == CConnectionEngine::EConnected");
|
|
83 |
// Session already connected, call connect complete directly but return status since URLs or so might be faulty
|
|
84 |
ConnectCompleteL(KErrNone);
|
|
85 |
return;
|
|
86 |
}
|
|
87 |
|
|
88 |
if(connState == CConnectionEngine::ENotConnected)
|
|
89 |
{
|
|
90 |
DP1("SpecificIAP() == %d",iPodcastModel.SettingsEngine().SpecificIAP());
|
|
91 |
|
|
92 |
if(iPodcastModel.SettingsEngine().SpecificIAP() == -1)
|
|
93 |
{
|
|
94 |
iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EUserSelectConnection);
|
|
95 |
}
|
|
96 |
else if ( iPodcastModel.SettingsEngine().SpecificIAP() == 0 )
|
|
97 |
{
|
|
98 |
iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EDefaultConnection);
|
|
99 |
}
|
|
100 |
else if( (iPodcastModel.SettingsEngine().SpecificIAP()&KUseIAPFlag))
|
|
101 |
{
|
|
102 |
iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EIAPConnection);
|
|
103 |
}
|
|
104 |
else
|
|
105 |
{
|
|
106 |
iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EMobilityConnection);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
DP("ConnectHttpSessionL END");
|
|
110 |
}
|
|
111 |
|
|
112 |
void CHttpClient::ConnectCompleteL(TInt aErrorCode)
|
|
113 |
{
|
|
114 |
if(iWaitingForGet)
|
|
115 |
{
|
|
116 |
iWaitingForGet = EFalse;
|
|
117 |
if( aErrorCode == KErrNone)
|
|
118 |
{
|
|
119 |
RHTTPConnectionInfo connInfo = iSession.ConnectionInfo();
|
|
120 |
RStringPool pool = iSession.StringPool();
|
|
121 |
// Attach to socket server
|
|
122 |
connInfo.SetPropertyL(pool.StringF(HTTP::EHttpSocketServ, RHTTPSession::GetTable()), THTTPHdrVal(iPodcastModel.ConnectionEngine().SockServ().Handle()));
|
|
123 |
// Attach to connection
|
|
124 |
TInt connPtr = REINTERPRET_CAST(TInt, &iPodcastModel.ConnectionEngine().Connection());
|
|
125 |
connInfo.SetPropertyL(pool.StringF(HTTP::EHttpSocketConnection, RHTTPSession::GetTable()), THTTPHdrVal(connPtr));
|
|
126 |
|
|
127 |
|
|
128 |
iPodcastModel.SetProxyUsageIfNeededL(iSession);
|
|
129 |
DoGetAfterConnectL();
|
|
130 |
}
|
|
131 |
else
|
|
132 |
{
|
|
133 |
ClientRequestCompleteL(KErrCouldNotConnect);
|
|
134 |
iSession.Close();
|
|
135 |
}
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
void CHttpClient::Disconnected()
|
|
140 |
{
|
|
141 |
iIsActive = EFalse;
|
|
142 |
iSession.Close();
|
|
143 |
}
|
|
144 |
|
|
145 |
void CHttpClient::DoGetAfterConnectL()
|
|
146 |
{
|
|
147 |
// since nothing should be downloading now. Delete the handler
|
|
148 |
if (iHandler)
|
|
149 |
{
|
|
150 |
delete iHandler;
|
|
151 |
iHandler = NULL;
|
|
152 |
}
|
|
153 |
|
|
154 |
iHandler = CHttpEventHandler::NewL(this, iObserver, iPodcastModel.FsSession());
|
|
155 |
iHandler->SetSilent(iSilentGet);
|
|
156 |
|
|
157 |
TEntry entry;
|
|
158 |
TBuf8<KTempBufferSize> rangeText;
|
|
159 |
|
|
160 |
if (iResumeEnabled && iPodcastModel.FsSession().Entry(iCurrentFileName, entry) == KErrNone) {
|
|
161 |
DP1("Found file, with size=%d", entry.iSize);
|
|
162 |
// file exists, so we should probably resume
|
60
|
163 |
rangeText.Format(_L8("bytes=%d-"), (entry.iSize-KByteOverlap > 0 ? entry.iSize-KByteOverlap : 0));
|
2
|
164 |
iHandler->SetSaveFileName(iCurrentFileName, ETrue);
|
|
165 |
} else {
|
|
166 |
// otherwise just make sure the directory exists
|
|
167 |
BaflUtils::EnsurePathExistsL(iPodcastModel.FsSession(),iCurrentFileName);
|
|
168 |
iHandler->SetSaveFileName(iCurrentFileName);
|
|
169 |
}
|
|
170 |
|
|
171 |
RStringPool strP = iSession.StringPool();
|
|
172 |
RStringF method;
|
|
173 |
method = strP.StringF(HTTP::EGET, RHTTPSession::GetTable());
|
|
174 |
|
|
175 |
iTrans = iSession.OpenTransactionL(iUriParser, *iHandler, method);
|
|
176 |
RHTTPHeaders hdr = iTrans.Request().GetHeaderCollection();
|
|
177 |
// Add headers appropriate to all methods
|
|
178 |
SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
|
|
179 |
SetHeaderL(hdr, HTTP::EAccept, KAccept);
|
|
180 |
TBuf<KTempBufferSize> range16;
|
|
181 |
range16.Copy(rangeText);
|
|
182 |
DP1("range text: %S", &range16);
|
|
183 |
if (rangeText.Length() > 0) {
|
|
184 |
SetHeaderL(hdr, HTTP::ERange, rangeText);
|
|
185 |
}
|
|
186 |
iTransactionCount++;
|
|
187 |
// submit the transaction
|
|
188 |
iTrans.SubmitL();
|
|
189 |
iIsActive = ETrue;
|
|
190 |
DP("CHttpClient::Get END");
|
|
191 |
}
|
|
192 |
|
|
193 |
TBool CHttpClient::GetL(const TDesC& aUrl, const TDesC& aFileName, TBool aSilent) {
|
|
194 |
DP("CHttpClient::Get START");
|
|
195 |
DP2("Getting '%S' to '%S'", &aUrl, &aFileName);
|
60
|
196 |
__ASSERT_DEBUG((iIsActive==EFalse), Panic(EPodcatcherPanicAlreadyActive));
|
2
|
197 |
iCurrentURL.Copy(aUrl);
|
|
198 |
|
|
199 |
TInt urlError = iUriParser.Parse(iCurrentURL);
|
|
200 |
|
|
201 |
if(urlError != KErrNone ||!iUriParser.IsSchemeValid())
|
|
202 |
{
|
|
203 |
iCurrentURL = KNullDesC8;
|
|
204 |
iSession.Close();
|
|
205 |
iObserver.CompleteL(this, KErrHttpInvalidUri);
|
|
206 |
return EFalse;
|
|
207 |
}
|
|
208 |
|
|
209 |
iSilentGet = aSilent;
|
|
210 |
iCurrentFileName.Copy(aFileName);
|
|
211 |
iWaitingForGet = ETrue;
|
|
212 |
|
|
213 |
if (iTransactionCount == 0)
|
|
214 |
{
|
|
215 |
DP("CHttpClient::GetL\t*** Opening HTTP session ***");
|
|
216 |
iSession.Close();
|
|
217 |
iSession.OpenL();
|
|
218 |
ConnectHttpSessionL();
|
|
219 |
}
|
|
220 |
else
|
|
221 |
{
|
|
222 |
DoGetAfterConnectL();
|
|
223 |
}
|
|
224 |
return ETrue;
|
|
225 |
}
|
|
226 |
|
|
227 |
void CHttpClient::Stop()
|
|
228 |
{
|
|
229 |
iIsActive = EFalse;
|
|
230 |
if(iHandler != NULL)
|
|
231 |
{
|
|
232 |
// cancel the ongoing transaction
|
|
233 |
iTrans.Cancel();
|
|
234 |
iTransactionCount = 0;
|
|
235 |
|
|
236 |
// make sure that we save the file
|
|
237 |
iHandler->CloseSaveFile();
|
|
238 |
|
|
239 |
// we could now delete the handler since a new will be created
|
|
240 |
delete iHandler;
|
|
241 |
iHandler = NULL;
|
|
242 |
|
|
243 |
// close the session
|
|
244 |
DP("CHttpClient::Stop\t*** Closing HTTP session ***");
|
|
245 |
iSession.Close();
|
|
246 |
}
|
|
247 |
|
|
248 |
TRAP_IGNORE(iObserver.CompleteL(this, KErrDisconnected));
|
|
249 |
|
|
250 |
}
|
|
251 |
|
|
252 |
void CHttpClient::ClientRequestCompleteL(TInt aErrorCode) {
|
|
253 |
iIsActive = EFalse;
|
|
254 |
iObserver.CompleteL(this, aErrorCode);
|
|
255 |
DP("CHttpClient::ClientRequestCompleteL");
|
|
256 |
if(iTransactionCount>0)
|
|
257 |
{
|
|
258 |
iTransactionCount--;
|
|
259 |
|
|
260 |
if(iTransactionCount == 0)
|
|
261 |
{
|
|
262 |
DP("CHttpClient::ClientRequestCompleteL\t*** Closing HTTP session ***");
|
|
263 |
delete iHandler;
|
|
264 |
iHandler = NULL;
|
|
265 |
iSession.Close();
|
|
266 |
}
|
|
267 |
}
|
|
268 |
}
|