|
1 /** @file |
|
2 * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Declares HttpServer class. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "upnphttpsession.h" |
|
21 #include "upnphttpfileaccess.h" |
|
22 #include "upnpcommonupnplits.h" |
|
23 #include "upnpfileutils.h" |
|
24 #include "upnpcons.h" |
|
25 #define KLogFile _L("DLNAWebServer.txt") |
|
26 #include "upnpcustomlog.h" |
|
27 |
|
28 #include <e32math.h> |
|
29 |
|
30 // ============================ MEMBER FUNCTIONS =============================== |
|
31 |
|
32 // ----------------------------------------------------------------------------- |
|
33 // CUpnpHttpFileAccess::NewL |
|
34 // Two-phased constructor |
|
35 // ----------------------------------------------------------------------------- |
|
36 // |
|
37 CUpnpHttpFileAccess* CUpnpHttpFileAccess::NewL( CUpnpHttpSession* aSession, |
|
38 const TDesC8& aHeaderBuffer, const TDesC16& aFilename, TInt aFileSize ) |
|
39 { |
|
40 LOGSH( aHandle, "CUpnpHttpFileAccess::NewL" ); |
|
41 CUpnpHttpFileAccess* self = new (ELeave) CUpnpHttpFileAccess( |
|
42 aFileSize ); |
|
43 CleanupStack::PushL( self ); |
|
44 self->ConstructL( aSession, aHeaderBuffer, aFilename ); |
|
45 CleanupStack::Pop( self ); |
|
46 |
|
47 return self; |
|
48 } |
|
49 |
|
50 // ----------------------------------------------------------------------------- |
|
51 // CUpnpHttpFileAccess::NewL |
|
52 // C++ default constructor, called when saving download |
|
53 // ----------------------------------------------------------------------------- |
|
54 // |
|
55 CUpnpHttpFileAccess* CUpnpHttpFileAccess::NewL( CUpnpHttpSession* aSession, |
|
56 const TDesC16& aFilename ) |
|
57 { |
|
58 LOG_FUNC_NAME; |
|
59 CUpnpHttpFileAccess* self = new (ELeave) CUpnpHttpFileAccess(); |
|
60 CleanupStack::PushL( self ); |
|
61 self->ConstructL( aSession, aFilename ); |
|
62 CleanupStack::Pop( self ); |
|
63 |
|
64 return self; |
|
65 } |
|
66 |
|
67 // ----------------------------------------------------------------------------- |
|
68 // CUpnpHttpFileAccess::~CUpnpHttpBuffer |
|
69 // C++ default destructor |
|
70 // ----------------------------------------------------------------------------- |
|
71 // |
|
72 CUpnpHttpFileAccess::~CUpnpHttpFileAccess() |
|
73 { |
|
74 LOGS1( "%i, CUpnpHttpFileAccess::~CUpnpHttpFileAccess", this ); |
|
75 if ( iIsDownload ) |
|
76 { |
|
77 if ( !iIsDeleted ) |
|
78 { |
|
79 iFile.Close( ); |
|
80 if ( (iTotalSizeOfDownload - iBytesWritten )!= 0 |
|
81 || ( !iIsChunkCompleted && iIsChunked ) ) |
|
82 iFsSession.Delete( *iFileToServe ); |
|
83 } |
|
84 |
|
85 } |
|
86 if ( iOpen ) |
|
87 { |
|
88 iTargetFile.Close( ); |
|
89 } |
|
90 |
|
91 iFsSession.Close( ); |
|
92 |
|
93 delete iHeaderBuffer; |
|
94 |
|
95 delete iFileToServe; |
|
96 delete iOpenedFile; |
|
97 } |
|
98 |
|
99 // ----------------------------------------------------------------------------- |
|
100 // CUpnpHttpFileAccess::CUpnpHttpFileAccess |
|
101 // C++ default constructor |
|
102 // ----------------------------------------------------------------------------- |
|
103 // |
|
104 CUpnpHttpFileAccess::CUpnpHttpFileAccess( TInt aFileSize ) |
|
105 { |
|
106 LOGS1H( aHandle, "%i, CUpnpHttpFileAccess::CUpnpHttpFileAccess", this ); |
|
107 SetPosOfFile( 0 ); |
|
108 SetEndPosOfFile( KErrNotFound ); |
|
109 SetPosOfHeader( 0 ); |
|
110 iSizeOfFile = aFileSize; |
|
111 iHeadersSent = EFalse; |
|
112 iTotalSizeOfDownload = KErrNotFound; |
|
113 iIsDownload = EFalse; |
|
114 iIsDeleted = EFalse; |
|
115 } |
|
116 |
|
117 // ----------------------------------------------------------------------------- |
|
118 // CUpnpHttpFileAccess::CUpnpHttpFileAccess |
|
119 // C++ default constructor, called when saving download |
|
120 // ----------------------------------------------------------------------------- |
|
121 // |
|
122 CUpnpHttpFileAccess::CUpnpHttpFileAccess() |
|
123 { |
|
124 LOGS1H( aHandle, "%i, CUpnpHttpFileAccess::CUpnpHttpFileAccess", this ); |
|
125 SetPosOfFile( 0 ); |
|
126 SetEndPosOfFile( KErrNotFound ); |
|
127 iHeadersSent = ETrue; |
|
128 iTotalSizeOfDownload = KErrNotFound; |
|
129 iSizeOfFile = KErrNotFound; |
|
130 iIsDownload = ETrue; |
|
131 iBytesWritten = 0; |
|
132 iIsDeleted = EFalse; |
|
133 } |
|
134 |
|
135 // ----------------------------------------------------------------------------- |
|
136 // CUpnpHttpFileAccess::CUpnpHttpFileAccess |
|
137 // Two-phased constructor, called when saving download |
|
138 // ----------------------------------------------------------------------------- |
|
139 // |
|
140 |
|
141 |
|
142 void CUpnpHttpFileAccess::ConstructL( CUpnpHttpSession* aSession, |
|
143 const TDesC16& aFilename ) |
|
144 { |
|
145 LOGS1( |
|
146 "%i, CUpnpHttpFileAccess::ConstructL(CUpnpHttpSession*, TDesC16&)", |
|
147 this ); |
|
148 |
|
149 User::LeaveIfError(iFsSession.Connect()); |
|
150 iFileToServe = aFilename.AllocL(); |
|
151 |
|
152 TInt error = 0; |
|
153 iPosInFile = 0; |
|
154 iHeaderLength = 0; |
|
155 iSession = aSession; |
|
156 if (iSession->OverwriteExisting() && !iSession->SaveAtOffset()) |
|
157 { |
|
158 error = iFile.Replace(iFsSession, *iFileToServe, EFileWrite |
|
159 | EFileShareAny); |
|
160 User::LeaveIfError(error); |
|
161 } |
|
162 else if (iSession->OverwriteExisting() && iSession->SaveAtOffset()) |
|
163 { |
|
164 error = iFile.Open(iFsSession, *iFileToServe, EFileWrite |
|
165 | EFileShareAny); |
|
166 if (error != KErrNotFound) |
|
167 { |
|
168 TInt size = 0; |
|
169 error = iFile.Size(size); |
|
170 if (size >= (iSession->Offset())) |
|
171 { |
|
172 iPosInFile = iSession->Offset(); |
|
173 } |
|
174 else |
|
175 { |
|
176 iFile.Close(); |
|
177 error = iFile.Replace(iFsSession, *iFileToServe, EFileWrite |
|
178 | EFileShareAny); |
|
179 User::LeaveIfError(error); |
|
180 } |
|
181 } |
|
182 else |
|
183 { |
|
184 error = iFile.Create(iFsSession, *iFileToServe, EFileWrite |
|
185 | EFileShareAny); |
|
186 User::LeaveIfError(error); |
|
187 } |
|
188 } |
|
189 else |
|
190 { |
|
191 error = iFile.Open(iFsSession, *iFileToServe, EFileWrite |
|
192 | EFileShareAny); |
|
193 if (error == KErrNotFound) |
|
194 { |
|
195 error = iFile.Create(iFsSession, *iFileToServe, EFileWrite |
|
196 | EFileShareAny); |
|
197 User::LeaveIfError(error); |
|
198 } |
|
199 } |
|
200 } |
|
201 |
|
202 // ----------------------------------------------------------------------------- |
|
203 // CUpnpHttpFileAccess::ConstructL |
|
204 // Two-phased constructor, called when serving file |
|
205 // ----------------------------------------------------------------------------- |
|
206 // |
|
207 void CUpnpHttpFileAccess::ConstructL( CUpnpHttpSession* aSession, |
|
208 const TDesC8& aHeaderBuffer, const TDesC16& aFilename ) |
|
209 { |
|
210 LOGS1H( |
|
211 iHandle, |
|
212 "%i, CUpnpHttpFileAccess::ConstructL(CUpnpHttpSession*, TDesC8&, TDesC16&)", |
|
213 this ); |
|
214 iHeaderBuffer = HBufC8::NewL( aHeaderBuffer.Length( ) ); |
|
215 iHeaderBuffer->Des().Zero( ); |
|
216 iHeaderBuffer->Des().Append( aHeaderBuffer ); |
|
217 |
|
218 iFileToServe = HBufC16::NewL( aFilename.Length( ) ); |
|
219 iFileToServe->Des().Zero( ); |
|
220 iFileToServe->Des().Append( aFilename ); |
|
221 |
|
222 iHeaderLength = aHeaderBuffer.Length( ); |
|
223 User::LeaveIfError( iFsSession.Connect( ) ); |
|
224 iSession = aSession; |
|
225 iOpenedFile = KNullDesC().AllocL( ); |
|
226 } |
|
227 |
|
228 // ----------------------------------------------------------------------------- |
|
229 // CUpnpHttpFileAccess::SetPosOfFile |
|
230 // |
|
231 // ----------------------------------------------------------------------------- |
|
232 // |
|
233 void CUpnpHttpFileAccess::SetPosOfFile( TUint aNewPos ) |
|
234 { |
|
235 iPosInFile = aNewPos; |
|
236 } |
|
237 |
|
238 // ----------------------------------------------------------------------------- |
|
239 // CUpnpHttpFileAccess::PosOfFile |
|
240 // |
|
241 // ----------------------------------------------------------------------------- |
|
242 // |
|
243 TUint CUpnpHttpFileAccess::PosOfFile() |
|
244 { |
|
245 return iPosInFile; |
|
246 } |
|
247 |
|
248 // ----------------------------------------------------------------------------- |
|
249 // CUpnpHttpFileAccess::SetPosOfFile |
|
250 // |
|
251 // ----------------------------------------------------------------------------- |
|
252 // |
|
253 void CUpnpHttpFileAccess::SetEndPosOfFile( TInt aNewEndPos ) |
|
254 { |
|
255 iEndPosOfFile = aNewEndPos; |
|
256 } |
|
257 |
|
258 // ----------------------------------------------------------------------------- |
|
259 // CUpnpHttpFileAccess::PosOfFile |
|
260 // |
|
261 // ----------------------------------------------------------------------------- |
|
262 // |
|
263 TInt CUpnpHttpFileAccess::EndPosOfFile() |
|
264 { |
|
265 return iEndPosOfFile; |
|
266 } |
|
267 // ----------------------------------------------------------------------------- |
|
268 // CUpnpHttpFileAccess::GetHeaders |
|
269 // |
|
270 // ----------------------------------------------------------------------------- |
|
271 // |
|
272 const TDesC8& CUpnpHttpFileAccess::GetHeaders() |
|
273 { |
|
274 return (iHeaderBuffer) ? *iHeaderBuffer : KNullDesC8( ); |
|
275 } |
|
276 |
|
277 // ----------------------------------------------------------------------------- |
|
278 // CUpnpHttpFileAccess::SetPosOfHeader |
|
279 // |
|
280 // ----------------------------------------------------------------------------- |
|
281 // |
|
282 void CUpnpHttpFileAccess::SetPosOfHeader( TUint aNewPos ) |
|
283 { |
|
284 iPosToReadHeader = aNewPos; |
|
285 } |
|
286 |
|
287 // ----------------------------------------------------------------------------- |
|
288 // CUpnpHttpFileAccess::SetTotalSize |
|
289 // |
|
290 // ----------------------------------------------------------------------------- |
|
291 // |
|
292 void CUpnpHttpFileAccess::SetTotalSize( TInt aSizeToSet ) |
|
293 { |
|
294 iTotalSizeOfDownload = aSizeToSet; |
|
295 } |
|
296 |
|
297 // ----------------------------------------------------------------------------- |
|
298 // CUpnpHttpFileAccess::SaveL |
|
299 // |
|
300 // ----------------------------------------------------------------------------- |
|
301 // |
|
302 TInt CUpnpHttpFileAccess::SaveL( TDesC8& aBuffer ) |
|
303 { |
|
304 LOGS1( "%i, CUpnpHttpFileAccess::SaveL()", this ); |
|
305 if ( iIsDeleted ) |
|
306 { |
|
307 LOGS( "file closed" ); |
|
308 return KErrGeneral; |
|
309 } |
|
310 LOGS( "file not closed" ); |
|
311 TInt toWrite = (EncodingMode( )|| (TransferTotal( ) == KErrNotFound ) ) |
|
312 ? aBuffer.Length( ) : (TransferTotal( )- iBytesWritten); |
|
313 |
|
314 if ( aBuffer.Size( ) < toWrite ) |
|
315 { |
|
316 toWrite = aBuffer.Size( ); |
|
317 } |
|
318 |
|
319 if ( UpnpFileUtil::CheckDiskSpaceShortL( iFileToServe->Des( ), toWrite, |
|
320 iFsSession ) ) |
|
321 { |
|
322 DeleteFile( ); |
|
323 return EHttpInsufficientStorage; |
|
324 } |
|
325 |
|
326 TInt error = KErrNone; |
|
327 |
|
328 // At first time iPosInFile == 0 or range offset, see ConstructL, and the next time |
|
329 // saving will continue at stopped point. |
|
330 |
|
331 error = iFile.Write( iPosInFile, aBuffer.Right( toWrite ) ); |
|
332 if ( error != KErrNone ) |
|
333 { |
|
334 return error; |
|
335 } |
|
336 iPosInFile += toWrite; |
|
337 iBytesWritten += toWrite; |
|
338 return KErrNone; |
|
339 |
|
340 } |
|
341 |
|
342 // ----------------------------------------------------------------------------- |
|
343 // CUpnpHttpFileAccess::TransferredBytes |
|
344 // |
|
345 // ----------------------------------------------------------------------------- |
|
346 // |
|
347 TInt CUpnpHttpFileAccess::TransferredBytes() |
|
348 { |
|
349 LOGS1( "%i, CUpnpHttpFileAccess::TransferredBytes()", this); |
|
350 TInt sent = 0; |
|
351 if ( !iHeadersSent ) |
|
352 { |
|
353 sent = iPosToReadHeader; |
|
354 } |
|
355 else |
|
356 { |
|
357 sent = PosOfFile(); |
|
358 } |
|
359 return sent; |
|
360 } |
|
361 |
|
362 // ----------------------------------------------------------------------------- |
|
363 // CUpnpHttpFileAccess::TransferTotal |
|
364 // |
|
365 // ----------------------------------------------------------------------------- |
|
366 // |
|
367 TInt CUpnpHttpFileAccess::TransferTotal() |
|
368 { |
|
369 LOGS1( "%i, CUpnpHttpFileAccess::TransferTotal()", this); |
|
370 if ( iTotalSizeOfDownload == KErrNotFound ) |
|
371 { |
|
372 return iSizeOfFile; |
|
373 } |
|
374 else |
|
375 { |
|
376 return iTotalSizeOfDownload; |
|
377 } |
|
378 } |
|
379 |
|
380 // ----------------------------------------------------------------------------- |
|
381 // CUpnpHttpFileAccess::FileToServe |
|
382 // |
|
383 // ----------------------------------------------------------------------------- |
|
384 // |
|
385 TDesC16& CUpnpHttpFileAccess::FileToServe() |
|
386 { |
|
387 return *iFileToServe; |
|
388 } |
|
389 |
|
390 // ----------------------------------------------------------------------------- |
|
391 // CUpnpHttpFileAccess::GetL |
|
392 // |
|
393 // ----------------------------------------------------------------------------- |
|
394 // |
|
395 TBool CUpnpHttpFileAccess::GetL( TPtr8& aPointer, TInt aBytesToSend ) |
|
396 { |
|
397 LOGS1H( aHandle, "%i, CUpnpHttpFileAccess::GetL", this ); |
|
398 if ( iFileToServe->Compare( *iOpenedFile ) ) // ignore if the same file |
|
399 { |
|
400 if ( iOpen ) |
|
401 { |
|
402 iTargetFile.Close( ); |
|
403 } |
|
404 iOpen = EFalse; |
|
405 } |
|
406 |
|
407 if ( !iOpen ) |
|
408 { |
|
409 User::LeaveIfError( iTargetFile.Open( iFsSession, *iFileToServe, |
|
410 EFileRead|EFileShareReadersOnly ) ); |
|
411 HBufC* tmp = iFileToServe->AllocL( ); |
|
412 delete iOpenedFile; |
|
413 iOpenedFile = tmp; |
|
414 iOpen = ETrue; |
|
415 } |
|
416 |
|
417 User::LeaveIfError( iTargetFile.Seek( ESeekStart, iPosInFile ) ); |
|
418 |
|
419 TInt BytesToRead = aBytesToSend; |
|
420 |
|
421 // Get min value from iEndPosInFile and buffer length |
|
422 // iEndPosInFile + - iPosInFile 1 because indicated byte should also be included |
|
423 if ( EndPosOfFile( ) != KErrNotFound ) |
|
424 BytesToRead = (EndPosOfFile( ) - iPosInFile + 1) < BytesToRead |
|
425 ? (EndPosOfFile( ) - iPosInFile + 1) : BytesToRead; |
|
426 |
|
427 LOGS1( "BytesToRead: %d", BytesToRead ); |
|
428 |
|
429 User::LeaveIfError( iTargetFile.Read( aPointer, BytesToRead ) ); |
|
430 iPosInFile = iPosInFile + aPointer.Length( ); |
|
431 |
|
432 return IsEndOfFile(); |
|
433 } |
|
434 // ----------------------------------------------------------------------------- |
|
435 //CUpnpHttpFileAccess::DeleteServedFile |
|
436 // |
|
437 // ----------------------------------------------------------------------------- |
|
438 // |
|
439 void CUpnpHttpFileAccess::DeleteFile() |
|
440 { |
|
441 LOGS1( "%i, CUpnpHttpFileAccess::DeleteFile()", this ); |
|
442 if ( !iIsDeleted ) |
|
443 { |
|
444 iFile.Close( ); |
|
445 iFsSession.Delete( *iFileToServe ); |
|
446 iIsDeleted = ETrue; |
|
447 } |
|
448 |
|
449 } |
|
450 |
|
451 // ----------------------------------------------------------------------------- |
|
452 // CUpnpHttpFileAccess::Download |
|
453 // |
|
454 // ----------------------------------------------------------------------------- |
|
455 // |
|
456 TBool CUpnpHttpFileAccess::Download() |
|
457 { |
|
458 return iIsDownload; |
|
459 } |
|
460 |
|
461 // ----------------------------------------------------------------------------- |
|
462 // CUpnpHttpFileAccess::FileExist |
|
463 // |
|
464 // ----------------------------------------------------------------------------- |
|
465 // |
|
466 TBool CUpnpHttpFileAccess::FileExist() |
|
467 { |
|
468 return !iIsDeleted; |
|
469 } |
|
470 |
|
471 // ----------------------------------------------------------------------------- |
|
472 // CUpnpHttpFileAccess::BytesWritten |
|
473 // |
|
474 // ----------------------------------------------------------------------------- |
|
475 // |
|
476 TBool CUpnpHttpFileAccess::BytesWritten() |
|
477 { |
|
478 return (iIsDownload ? iBytesWritten : 0); |
|
479 } |
|
480 |
|
481 // ----------------------------------------------------------------------------- |
|
482 // CUpnpHttpFileAccess::SetEncodingMode(TBool aIsChunked) |
|
483 // |
|
484 // ----------------------------------------------------------------------------- |
|
485 // |
|
486 void CUpnpHttpFileAccess::SetEncodingMode( TBool aIsChunked ) |
|
487 { |
|
488 iIsChunked = aIsChunked; |
|
489 } |
|
490 |
|
491 // ----------------------------------------------------------------------------- |
|
492 // CUpnpHttpFileAccess::EncodingMode() |
|
493 // |
|
494 // ----------------------------------------------------------------------------- |
|
495 // |
|
496 TBool CUpnpHttpFileAccess::EncodingMode() |
|
497 { |
|
498 return iIsChunked; |
|
499 } |
|
500 |
|
501 // ----------------------------------------------------------------------------- |
|
502 // CUpnpHttpFileAccess::SetTransferTotal(aLength) |
|
503 // |
|
504 // ----------------------------------------------------------------------------- |
|
505 // |
|
506 void CUpnpHttpFileAccess::SetTransferTotal( TInt aLength ) |
|
507 { |
|
508 iTotalSizeOfDownload = aLength; |
|
509 } |
|
510 |
|
511 // ----------------------------------------------------------------------------- |
|
512 // CUpnpHttpFileAccess::HeadersSent |
|
513 // |
|
514 // ----------------------------------------------------------------------------- |
|
515 // |
|
516 void CUpnpHttpFileAccess::SetHeadersSent() |
|
517 { |
|
518 delete iHeaderBuffer; |
|
519 iHeaderBuffer = NULL; |
|
520 iPosToReadHeader = NULL; |
|
521 iHeadersSent = ETrue; |
|
522 } |
|
523 |
|
524 // ----------------------------------------------------------------------------- |
|
525 // CUpnpHttpFileAccess::HeadersSent() |
|
526 // |
|
527 // ----------------------------------------------------------------------------- |
|
528 // |
|
529 TBool CUpnpHttpFileAccess::HeadersSent() |
|
530 { |
|
531 return iHeadersSent; |
|
532 } |
|
533 |
|
534 // ----------------------------------------------------------------------------- |
|
535 // CUpnpHttpFileAccess::SetChunkComplete( TBool aCompleted ) |
|
536 // |
|
537 // ----------------------------------------------------------------------------- |
|
538 // |
|
539 void CUpnpHttpFileAccess::SetChunkCompleted( TBool aCompleted ) |
|
540 { |
|
541 iIsChunkCompleted = aCompleted; |
|
542 } |
|
543 |
|
544 // ----------------------------------------------------------------------------- |
|
545 // CUpnpHttpFileAccess::IsEndOfFile |
|
546 // |
|
547 // ----------------------------------------------------------------------------- |
|
548 // |
|
549 TBool CUpnpHttpFileAccess::IsEndOfFile() |
|
550 { |
|
551 TInt limit = iSizeOfFile; |
|
552 if ( EndPosOfFile() != KErrNotFound ) |
|
553 { |
|
554 limit = ( EndPosOfFile() + 1 ) < iSizeOfFile ? ( EndPosOfFile() + 1 ) : iSizeOfFile; |
|
555 } |
|
556 |
|
557 if ( iPosInFile >= limit ) |
|
558 { |
|
559 iTargetFile.Close(); |
|
560 iOpen = EFalse; |
|
561 return ETrue; |
|
562 } |
|
563 else |
|
564 { |
|
565 return EFalse; |
|
566 } |
|
567 } |
|
568 |
|
569 // End Of File |
|
570 |