author | Sebastian Brannstrom <sebastianb@symbian.org> |
Sat, 16 Oct 2010 15:58:45 +0100 | |
branch | 3rded |
changeset 276 | 4357d59592dd |
parent 177 | 269e3f3e544a |
child 343 | 9c56bf585696 |
permissions | -rw-r--r-- |
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 |
} |
|
177
269e3f3e544a
Lots of fixes for 3rd edition. First test SIS file, 0.90(3)
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
164
diff
changeset
|
100 |
else if( (iPodcastModel.SettingsEngine().SpecificIAP())) |
2 | 101 |
{ |
102 |
iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EIAPConnection); |
|
103 |
} |
|
177
269e3f3e544a
Lots of fixes for 3rd edition. First test SIS file, 0.90(3)
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
164
diff
changeset
|
104 |
// else |
269e3f3e544a
Lots of fixes for 3rd edition. First test SIS file, 0.90(3)
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
164
diff
changeset
|
105 |
// { |
269e3f3e544a
Lots of fixes for 3rd edition. First test SIS file, 0.90(3)
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
164
diff
changeset
|
106 |
// iPodcastModel.ConnectionEngine().StartL(CConnectionEngine::EMobilityConnection); |
269e3f3e544a
Lots of fixes for 3rd edition. First test SIS file, 0.90(3)
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
164
diff
changeset
|
107 |
// } |
2 | 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 |
|
196 |
if (iIsActive) |
|
197 |
{ |
|
198 |
return EFalse; |
|
199 |
} |
|
164
000f9fc147b2
Catch up with default branch; New v 27 SIS
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
69
diff
changeset
|
200 |
|
000f9fc147b2
Catch up with default branch; New v 27 SIS
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
69
diff
changeset
|
201 |
DP1("Getting URL: %S", &aUrl); |
000f9fc147b2
Catch up with default branch; New v 27 SIS
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
69
diff
changeset
|
202 |
DP1("Writing file: %S", &aFileName); |
000f9fc147b2
Catch up with default branch; New v 27 SIS
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
69
diff
changeset
|
203 |
|
2 | 204 |
iCurrentURL.Copy(aUrl); |
205 |
||
206 |
TInt urlError = iUriParser.Parse(iCurrentURL); |
|
207 |
||
208 |
if(urlError != KErrNone ||!iUriParser.IsSchemeValid()) |
|
209 |
{ |
|
210 |
iCurrentURL = KNullDesC8; |
|
211 |
iSession.Close(); |
|
212 |
iObserver.CompleteL(this, KErrHttpInvalidUri); |
|
213 |
return EFalse; |
|
214 |
} |
|
215 |
||
216 |
iSilentGet = aSilent; |
|
217 |
iCurrentFileName.Copy(aFileName); |
|
218 |
iWaitingForGet = ETrue; |
|
219 |
||
220 |
if (iTransactionCount == 0) |
|
221 |
{ |
|
222 |
DP("CHttpClient::GetL\t*** Opening HTTP session ***"); |
|
223 |
iSession.Close(); |
|
224 |
iSession.OpenL(); |
|
225 |
ConnectHttpSessionL(); |
|
226 |
} |
|
227 |
else |
|
228 |
{ |
|
229 |
DoGetAfterConnectL(); |
|
230 |
} |
|
231 |
return ETrue; |
|
232 |
} |
|
233 |
||
234 |
void CHttpClient::Stop() |
|
235 |
{ |
|
236 |
iIsActive = EFalse; |
|
237 |
if(iHandler != NULL) |
|
238 |
{ |
|
239 |
// cancel the ongoing transaction |
|
240 |
iTrans.Cancel(); |
|
241 |
iTransactionCount = 0; |
|
242 |
||
243 |
// make sure that we save the file |
|
244 |
iHandler->CloseSaveFile(); |
|
245 |
||
246 |
// we could now delete the handler since a new will be created |
|
247 |
delete iHandler; |
|
248 |
iHandler = NULL; |
|
249 |
||
250 |
// close the session |
|
251 |
DP("CHttpClient::Stop\t*** Closing HTTP session ***"); |
|
252 |
iSession.Close(); |
|
253 |
} |
|
254 |
||
255 |
TRAP_IGNORE(iObserver.CompleteL(this, KErrDisconnected)); |
|
256 |
||
257 |
} |
|
258 |
||
259 |
void CHttpClient::ClientRequestCompleteL(TInt aErrorCode) { |
|
260 |
iIsActive = EFalse; |
|
261 |
iObserver.CompleteL(this, aErrorCode); |
|
262 |
DP("CHttpClient::ClientRequestCompleteL"); |
|
263 |
if(iTransactionCount>0) |
|
264 |
{ |
|
265 |
iTransactionCount--; |
|
266 |
||
267 |
if(iTransactionCount == 0) |
|
268 |
{ |
|
269 |
DP("CHttpClient::ClientRequestCompleteL\t*** Closing HTTP session ***"); |
|
270 |
delete iHandler; |
|
271 |
iHandler = NULL; |
|
272 |
iSession.Close(); |
|
273 |
} |
|
274 |
} |
|
275 |
} |