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");
|
69
|
195 |
DP2("Getting '%S' to '%S'", &aUrl, &aFileName);
|
|
196 |
|
|
197 |
if (iIsActive)
|
|
198 |
{
|
|
199 |
return EFalse;
|
|
200 |
}
|
2
|
201 |
iCurrentURL.Copy(aUrl);
|
|
202 |
|
|
203 |
TInt urlError = iUriParser.Parse(iCurrentURL);
|
|
204 |
|
|
205 |
if(urlError != KErrNone ||!iUriParser.IsSchemeValid())
|
|
206 |
{
|
|
207 |
iCurrentURL = KNullDesC8;
|
|
208 |
iSession.Close();
|
|
209 |
iObserver.CompleteL(this, KErrHttpInvalidUri);
|
|
210 |
return EFalse;
|
|
211 |
}
|
|
212 |
|
|
213 |
iSilentGet = aSilent;
|
|
214 |
iCurrentFileName.Copy(aFileName);
|
|
215 |
iWaitingForGet = ETrue;
|
|
216 |
|
|
217 |
if (iTransactionCount == 0)
|
|
218 |
{
|
|
219 |
DP("CHttpClient::GetL\t*** Opening HTTP session ***");
|
|
220 |
iSession.Close();
|
|
221 |
iSession.OpenL();
|
|
222 |
ConnectHttpSessionL();
|
|
223 |
}
|
|
224 |
else
|
|
225 |
{
|
|
226 |
DoGetAfterConnectL();
|
|
227 |
}
|
|
228 |
return ETrue;
|
|
229 |
}
|
|
230 |
|
|
231 |
void CHttpClient::Stop()
|
|
232 |
{
|
|
233 |
iIsActive = EFalse;
|
|
234 |
if(iHandler != NULL)
|
|
235 |
{
|
|
236 |
// cancel the ongoing transaction
|
|
237 |
iTrans.Cancel();
|
|
238 |
iTransactionCount = 0;
|
|
239 |
|
|
240 |
// make sure that we save the file
|
|
241 |
iHandler->CloseSaveFile();
|
|
242 |
|
|
243 |
// we could now delete the handler since a new will be created
|
|
244 |
delete iHandler;
|
|
245 |
iHandler = NULL;
|
|
246 |
|
|
247 |
// close the session
|
|
248 |
DP("CHttpClient::Stop\t*** Closing HTTP session ***");
|
|
249 |
iSession.Close();
|
|
250 |
}
|
|
251 |
|
|
252 |
TRAP_IGNORE(iObserver.CompleteL(this, KErrDisconnected));
|
|
253 |
|
|
254 |
}
|
|
255 |
|
|
256 |
void CHttpClient::ClientRequestCompleteL(TInt aErrorCode) {
|
|
257 |
iIsActive = EFalse;
|
|
258 |
iObserver.CompleteL(this, aErrorCode);
|
|
259 |
DP("CHttpClient::ClientRequestCompleteL");
|
|
260 |
if(iTransactionCount>0)
|
|
261 |
{
|
|
262 |
iTransactionCount--;
|
|
263 |
|
|
264 |
if(iTransactionCount == 0)
|
|
265 |
{
|
|
266 |
DP("CHttpClient::ClientRequestCompleteL\t*** Closing HTTP session ***");
|
|
267 |
delete iHandler;
|
|
268 |
iHandler = NULL;
|
|
269 |
iSession.Close();
|
|
270 |
}
|
|
271 |
}
|
|
272 |
}
|