upnp/upnpstack/dlnawebserver/inc/upnphttpsession.h
changeset 0 f5a58ecadc66
child 8 92b4d337bbaf
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     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:  HttpSession is class that is used to handle single 
       
    15 *                 connections to HttpServer
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #ifndef C_CUPNPHTTPSESSION_H
       
    21 #define C_CUPNPHTTPSESSION_H
       
    22 
       
    23 // INCLUDES
       
    24 
       
    25 #include <e32base.h>
       
    26 #include <f32file.h>
       
    27 #include <apasvst.h>
       
    28 #include <apaflrec.h>
       
    29 #include <e32std.h>
       
    30 
       
    31 #include "upnphttpmessage.h"
       
    32 #include "upnptcpsession.h"
       
    33 
       
    34 
       
    35 // FORWARD DECLARATIONS
       
    36 
       
    37 class CFileReader;
       
    38 class CUpnpHttpBuffer;
       
    39 class CUpnpHttpServer;
       
    40 class CUpnpHttpFileAccess;
       
    41 class CUpnpHttpServerTransaction;
       
    42 
       
    43 // CONSTANTS
       
    44 
       
    45 _LIT8( KMedia,  "\\MEDIA\\" );
       
    46 _LIT8( KLog,    "\\LOG\\" );              
       
    47 _LIT( KPercent,   "%" );
       
    48 
       
    49 static const TInt KDefaultLastActionSize = 2048;
       
    50 static const TInt KMaxRedirectCount=5;
       
    51 
       
    52 // CLASS DECLARATION
       
    53 
       
    54 /**
       
    55 *  @brief HttpSession is class that is used to handle single connections to HttpServer.  
       
    56 *
       
    57 *  @since Series60 2.6
       
    58 */
       
    59 NONSHARABLE_CLASS (CUpnpHttpSession) : public CUpnpTcpSession
       
    60     {
       
    61     
       
    62 public: // Construtors and destructor
       
    63 
       
    64     /**
       
    65     * Constructor function. This constructor is used when connection is iniatiated
       
    66     *  from network.
       
    67     * @param aSocket Pointer to new socket.
       
    68     * @param aServer Pointer to owning HttpServer.
       
    69     * @param aSessionId Session id of this session.
       
    70     * @param aPriority Priority of this session
       
    71     * @return A new CUpnpHttpServer instance.
       
    72     **/
       
    73     static CUpnpHttpSession* NewL( RSocket aSocket, 
       
    74                                CUpnpHttpServer* aServer,
       
    75                                TInt aSessionId,
       
    76                                TThreadPriority aPriority );
       
    77 
       
    78     /**
       
    79     * Virtual Destructor function.
       
    80     */
       
    81     virtual ~CUpnpHttpSession();
       
    82 
       
    83 public: // From CUpnpTcpSession
       
    84     /**
       
    85     * Callback function from TcpSession. Session has received aBuffer 
       
    86     *  from network.
       
    87     * @since Series60 2.6
       
    88     * @param aBuffer Buffer that has been received.
       
    89     * @return None.
       
    90     **/
       
    91     void BufferReceivedL( TDesC8& aBuffer );
       
    92 
       
    93 public: // New functions
       
    94 
       
    95     /**
       
    96     * Sends aMessage to session's remote host.
       
    97     * @since Series60 2.6
       
    98     * @param aMessage Message to be sent.
       
    99     **/
       
   100     void SendL( CUpnpHttpMessage* aMessage );
       
   101 
       
   102     /**
       
   103     * Deletes current session.
       
   104     * @since Series60 2.6
       
   105     * @param aSession Session to b deleted.
       
   106     **/
       
   107     void DeleteThisSessionL( CUpnpTcpSession* aSession );
       
   108     
       
   109     /**
       
   110     * Deletes a file named aFilename if it exists. Used in receiving files
       
   111     * to delete file if it already exists.
       
   112     * @since Series60 2.6
       
   113     * @param aFilename File to be deleted.
       
   114     * @return None.
       
   115     **/
       
   116     void DeleteFileIfExists( TDesC16& aFilename );
       
   117 
       
   118     /**
       
   119     * Returns a pointer to file transfer object of this session.
       
   120     * @since Series60 2.6
       
   121     * @return Pointer to file transfer object.
       
   122     **/
       
   123     CUpnpHttpFileAccess* FileAccess();
       
   124        
       
   125     /**
       
   126     * Should the existing local file be overwritten with the requested content.
       
   127     *  If not then the new file with name: iInFilename_serialNumber will be created
       
   128     *  to save incoming content. It's connected only with download.
       
   129     * @return Should the local in-file be overwritten with incoming content.
       
   130     **/
       
   131     TBool OverwriteExisting();
       
   132     
       
   133     // Range request parameters, only used for GET and HEAD requests with Range HTTP header.
       
   134     /**
       
   135     * Returns offset of the file that should be requested from server. 
       
   136     * @return offset to save content at.
       
   137     **/
       
   138     TInt Offset();
       
   139     
       
   140     /**
       
   141     * Returns the length of file content to be requested.
       
   142     * @return Length of file content to be requested.
       
   143     **/
       
   144     TInt Length(); 
       
   145     
       
   146     /**
       
   147     * Returns if should the requested offset of the remote file be 
       
   148     *  saved at physical offset of existing local file.
       
   149     *  Only for Range requests
       
   150     * @return Should the requested offset of the remote file be saved at 
       
   151     *  physical offset of existing local file.
       
   152     */
       
   153     TBool SaveAtOffset();
       
   154     
       
   155     /**
       
   156     * Notifies upper layers that there is a HTTP error.
       
   157     * @since Series60 3.2
       
   158     * @param the HTTP error code
       
   159     **/
       
   160     void NotifyErrorL(TInt aError, TInt aInternalError = KErrNone);
       
   161     
       
   162     /**
       
   163     * Returns ETrue when message is totally downloaded [completed]
       
   164     * @since Series60 3.2
       
   165     * @return ETrue if message is completed
       
   166     **/
       
   167     TBool IsMsgReady();
       
   168     
       
   169     /**
       
   170     * Increments the redirect counter
       
   171     * @since Series60 3.2
       
   172     * @return ETrue if the number of Redirecting has exceeded the maximum
       
   173     **/
       
   174     TBool SetRedirectCount();
       
   175 
       
   176     /**
       
   177     * Returns HttpServer pointer
       
   178     * @since Series60 3.2
       
   179     **/    
       
   180     CUpnpHttpServer* HttpServer();
       
   181     
       
   182     /**
       
   183     * Deletes the FileAccess of this session.
       
   184     * @since Series60 2.6
       
   185     * @return None.
       
   186     **/ 
       
   187     void DeleteServeFileL();
       
   188 
       
   189 protected: // New functions
       
   190     
       
   191     void DestroyIt(CUpnpTcpSession* aSession );
       
   192     
       
   193     /**
       
   194     * Returns ETrue, if fileserve exists.
       
   195     * @since Series60 2.6
       
   196     * @return ETrue, if fileserve exists; EFalse otherwise.
       
   197     **/
       
   198     TBool FileServeExists();
       
   199    
       
   200     /**
       
   201     * Returns the port of this HttpSession.
       
   202     * @since Series60 2.6
       
   203     * @return Number of server port.
       
   204     **/   
       
   205     TInt ServerPort();
       
   206     
       
   207 private: // Constructors
       
   208 
       
   209     /**
       
   210     * First phase constructor. This constructor is used when connection is
       
   211     *  iniatiated from network.
       
   212     * @since Series60 2.6
       
   213     * @param aSocket Pointer to new socket.
       
   214     * @param aServer Pointer to owning HttpServer.
       
   215     * @param aSessionId Session id of this session.
       
   216     * @param aPriority Priority of this session
       
   217     **/
       
   218     CUpnpHttpSession( RSocket aSocket, 
       
   219                   CUpnpHttpServer* aServer, 
       
   220                   TInt aSessionId,
       
   221                   TThreadPriority aPriority );
       
   222     
       
   223     /**
       
   224     * Symbian constructor
       
   225     */
       
   226     void ConstructL();
       
   227 
       
   228     /**
       
   229     * Received buffer processing from network.
       
   230     * @since Series60 2.6
       
   231     * @param aBuffer Buffer that has been received.
       
   232     * @return None.
       
   233     **/
       
   234     void BufferReceivedProcessL( TDesC8& aBuffer );
       
   235 
       
   236 /**
       
   237     * Received buffer checking headers for a message coming from network.
       
   238     * @since Series60 2.6
       
   239     * @param aBuffer Buffer that has been received.
       
   240     * @return None.
       
   241     **/
       
   242     void BufferReceivedCheckingHeadersL( TDesC8& aBuffer );
       
   243 
       
   244 
       
   245     /**
       
   246     * Single POST buffer processing
       
   247     *  
       
   248     * @since Series60 2.6
       
   249     * @param aBuffer Buffer that has been received.
       
   250     * @return None.
       
   251     **/
       
   252     void CUpnpHttpSession::SinglePostBufferReceivedL( TDesC8& aBuffer ); 
       
   253     
       
   254     
       
   255 
       
   256     TInt PrepareRangeHeaderL( CUpnpHttpMessage* aMsg,
       
   257         TBool aServingOutFileName,
       
   258         TInt aFileSize, CUpnpHttpServerTransaction &aTransaction, TInt& aRangeStatus,
       
   259         TInt& aStartPos, TInt& aEndPos );
       
   260 
       
   261     
       
   262 private: // New funtions
       
   263 
       
   264     enum THttpInvalidMessage
       
   265         {
       
   266         EMessageOk = 0,
       
   267         EUnknownMethod
       
   268         };
       
   269     
       
   270     /**
       
   271     * Matches an error to a proper HTTP error code
       
   272     * @error to be matched
       
   273     * @return HTTP error code.
       
   274     **/
       
   275     TInt MatchHttpError( TInt error );
       
   276     /**
       
   277     * Checks message validity. Returns type of error.
       
   278     * @since Series60 2.6
       
   279     * @param aMsg Message to check.
       
   280     * @return Errorcode.
       
   281     **/
       
   282     THttpInvalidMessage IsValidMessage( CUpnpHttpMessage* aMsg );
       
   283 
       
   284     
       
   285     /**
       
   286     * Decides what to do with the message, should it be sent to upper layer,        
       
   287     * Takes ownership of a parameter (it should be popped from cleanup stack
       
   288     * before call
       
   289     * @since Series60 2.6
       
   290     * @param aMsg Message to check.
       
   291     **/
       
   292     void HandleQueryL( CUpnpHttpMessage* aMsg );
       
   293 
       
   294 
       
   295     /**
       
   296     * Repsonse with HTTP error mesage to remoteServer
       
   297     * @since Series60 2.6
       
   298     * @param aStatus Number of status code
       
   299     **/
       
   300     void ResponseErrorL(TInt aStatus);
       
   301 
       
   302     
       
   303   
       
   304 
       
   305     /**
       
   306     * Prepares serve a file. Checks that file exists and paths are correct. 
       
   307     *  Checks filesize and so on.
       
   308     * @since Series60 2.6
       
   309     * @param aMsg Message to check.
       
   310     * @return error code
       
   311     **/
       
   312     TInt PrepareToServeFileL( CUpnpHttpMessage* aMsg );
       
   313 
       
   314     /**
       
   315     * Notifies upper layers that session has disconnected.
       
   316     * @since Series60 2.6
       
   317     * @param aAddr Address of remote host to which the connection has been
       
   318     *  disconnected.
       
   319     **/
       
   320     void NotifyDisconnectL( TInetAddr aAddr );
       
   321     
       
   322     /**
       
   323     * Notifies upper layers that session has expired
       
   324     * @since Series60 3.2        
       
   325     **/
       
   326     void NotifyTimeoutL();
       
   327     
       
   328     /**
       
   329     * File Transfer Reader callback when upload done
       
   330     * @since Series60 3.2        
       
   331     **/
       
   332     void FileTransferReaderDoneL();
       
   333     
       
   334     /**
       
   335     * File Transfer Reader callback when upload with error
       
   336     * @since Series60 3.2        
       
   337     * @param error        
       
   338     **/    
       
   339     void FileTransferReaderErrorL(TInt aError);
       
   340     
       
   341     /**
       
   342     * Resets settings after upload
       
   343     * @since Series60 3.2            
       
   344     **/
       
   345     void ResetSettingsL();
       
   346     
       
   347     
       
   348     /**
       
   349     * File Transfer Writer callback when download done
       
   350     * @since Series60 3.2                 
       
   351     **/
       
   352     void FileTransferWriterDoneL();
       
   353     
       
   354     /**
       
   355     * File Transfer Writer callback for handling more downloads
       
   356     * @since Series60 3.2               
       
   357     **/
       
   358     void FileTransferWriterHandleMoreL();
       
   359     
       
   360     /**
       
   361     * Checks session and message against persistence
       
   362     * @since Series60 3.2                 
       
   363     **/
       
   364     void CheckSessionPersistence(CUpnpHttpMessage* aMsg);
       
   365     
       
   366     /**
       
   367     * Notifies upper layers that Socket Server is unable to handle any more data by \
       
   368     *  sending Expectation failed HTTP error to upper layers.
       
   369     * @since Series60 2.6
       
   370     * @param aAddr Address of remote host to which the connection has been tried to connect
       
   371     **/
       
   372     void SocketServerBusyL( const TInetAddr& aAddr );
       
   373     
       
   374      /**
       
   375     * Notifies upper layers with error message. Method guarantee destrustion of parameter
       
   376     * (even when leave ocure), and parameter should be poped from cleanupstack
       
   377     * @since Series60 2.6
       
   378     * @param aMessage message that will be sent to upper layer.
       
   379     **/
       
   380     void NotifyUpperLayerLD( CUpnpHttpMessage* aMessage );
       
   381     
       
   382     /**
       
   383     * Checks if session uses Connection close communication.
       
   384     * @since Series60 2.6
       
   385     **/ 
       
   386     void CheckConnectionCloseL();
       
   387 
       
   388     /**
       
   389     * Callback function. Informs upper layers that HTTP POST has been started 
       
   390     *  by sending HTTP error code 801 message to upper layers.
       
   391     * @since Series60 2.6
       
   392     **/
       
   393     void NotifyUpperLayersOnPostL();
       
   394 
       
   395     /**
       
   396     * Callback function. Informs upper layers that HTTP POST has been finished.
       
   397     * @since Series60 2.6
       
   398     **/
       
   399     void PostingFileCompleteL();
       
   400     
       
   401     void WriteMsgAndHeaderL( CUpnpHttpMessage* aMsg, const TDesC8& aHeaderToWrite,
       
   402         const TDesC& aPathWithNewMethod, TInt aFileSize, TInt aRangeStatus,
       
   403         const TInt aStartPos, const TInt aEndPos );
       
   404     
       
   405     /**
       
   406     * Function to cut Ip and port from absoulute Uri in get request, it only cuts them when 
       
   407     * they are same as http server ones
       
   408     * @since S60 5.1
       
   409     **/    
       
   410     TPtrC8 PrepareRelativeUriL( const TDesC8& aUri );
       
   411     
       
   412 private: 
       
   413 
       
   414     // Request type of last sent message. This is used to store CHttpMessages type. 
       
   415     // This information is used by upper layers, not by HttpServer.
       
   416     THTTPMsgType iRequestType;
       
   417 
       
   418     // Resend counter. This information is used by upper layers, not by HttpServer.
       
   419     TInt iRetryCounter;
       
   420 
       
   421     // Pointer to owning HttpServer.
       
   422     CUpnpHttpServer* iServer;
       
   423 
       
   424     // Used to check that notification of receiving of large files has been 
       
   425     // sent on this session.
       
   426     TBool iRequestedTransferNotifySent;
       
   427 
       
   428     // Used to check if session uses Connection close.
       
   429     TBool iUsesConnectionClose;
       
   430 
       
   431     // Buffer that contains the filename of file, that is meant to be 
       
   432     // received and saved.
       
   433     HBufC8* iInFilename;
       
   434       
       
   435     // Should the existing local file be overwritten with the requested content.
       
   436     // If not then the new file with name: iInFilename_serialNumber will be created
       
   437     // to save incoming content. It's connected only with download.
       
   438     TBool iOverwriteExisting;
       
   439     
       
   440     /* Range request parameters, only used for GET and HEAD requests with 
       
   441        Range HTTP header. 
       
   442     */
       
   443     // Offset of the file that should be requested from server. 
       
   444     TInt iOffset;
       
   445     
       
   446     //Length of file content to be requested.
       
   447     TInt iLength; 
       
   448     
       
   449     // Should the requested offset of the remote file be saved at physical 
       
   450     // offset of existing local file.
       
   451     // Only for Range requests!
       
   452     TBool iSaveAtOffset;
       
   453     /**/
       
   454 
       
   455     // Buffer that contains the filename of file, that is meant to be sent 
       
   456     // out as body of CUpnpHttpMessage.
       
   457     HBufC8* iOutFilename;
       
   458 
       
   459     // Buffer that contains the name of destination service. Used by upper 
       
   460     // layer, not by CUpnpHttpServer
       
   461     HBufC8* iDestinationPath;
       
   462 
       
   463     // Buffer that contains the name of destination service. Used by upper
       
   464     // layer, not by CUpnpHttpServer
       
   465     HBufC8* iSenderPath;
       
   466 
       
   467     // Pointer used by upper layer, not by CUpnpHttpServer.
       
   468     TAny* iPendingRequest;
       
   469 
       
   470     // Requests that have not yet been served.
       
   471     RPointerArray<CUpnpHttpMessage>* iPendingRequests;
       
   472 
       
   473     // Pointer to this session's file access object.
       
   474     CUpnpHttpFileAccess* iFileServe;
       
   475 
       
   476     // Variable that is used to check if we can delete this session after 
       
   477     // we get response from remote end.
       
   478     TBool iSessionIsDeletedAfterResponse;
       
   479     
       
   480     // Flag informing if content-length of incoming message was checked
       
   481     // and it's value is not bigger than maximum size allowed.
       
   482     TBool iMaximumSizeChecked;
       
   483     
       
   484     // Flag informing session if message headers were checked
       
   485     TBool iHeadersCompleted;
       
   486 
       
   487     //error has been notified to the remote client
       
   488     TBool iErrorNotified;
       
   489     //if received message is chunked-encoded
       
   490     TBool iIsChunked;
       
   491     };
       
   492 
       
   493 #endif // C_CUPNPHTTPSESSION_H
       
   494 
       
   495 // End Of File