upnp/upnpstack/upnputils/src/upnphttpmessagefactory.cpp
changeset 0 f5a58ecadc66
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:  HTTP message factory
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "upnphttpmessagefactory.h"
       
    21 #include "upnpstring.h"
       
    22 #include "upnpcons.h"
       
    23 #include "upnpcommonupnplits.h"
       
    24 
       
    25 _LIT8(KUpnpBodyMsgError1,"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n<html><head>\n<title>");
       
    26 _LIT8(KUpnpBodyMsgError2,"</title>\n</head><body>\n<h1>");
       
    27 _LIT8(KUpnpBodyMsgError3,"</h1>\n<hr />\n</body></html>");
       
    28 // ================= MEMBER FUNCTIONS =======================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // RUpnpHttpMessageFactory::HttpGetL
       
    32 // HTTP GET
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpGetL( TInetAddr& aDestination,
       
    36                                                       const TDesC8& aPath )
       
    37     {
       
    38     CUpnpHttpMessage* msg = CUpnpHttpMessage::NewL(aDestination);
       
    39     CleanupStack::PushL(msg);
       
    40     msg->SetDestinationPathL( aPath );
       
    41 
       
    42     TInt headerLen = 0;
       
    43 
       
    44     headerLen += KHttpGet().Length();
       
    45     headerLen += UpnpString::KSpace().Length();
       
    46     headerLen += UpnpString::KSlash().Length();
       
    47     headerLen += aPath.Length();
       
    48     headerLen += KHttp11().Length();
       
    49 
       
    50     HBufC8* header = HBufC8::NewLC(headerLen);
       
    51 
       
    52     header->Des().Zero();
       
    53     header->Des().Append(KHttpGet());
       
    54     header->Des().Append(UpnpString::KSpace());
       
    55     if (aPath.Length() >= UpnpString::KSlash().Length())
       
    56         {
       
    57         if (aPath.Find(UpnpString::KSlash()) != 0)
       
    58             {
       
    59             header->Des().Append(UpnpString::KSlash());
       
    60             }
       
    61         }
       
    62     else
       
    63         {
       
    64         header->Des().Append(UpnpString::KSlash());
       
    65         }
       
    66     header->Des().Append(aPath);
       
    67     header->Des().Append(KHttp11);
       
    68 
       
    69     msg->AddPairL(*header, KNullDesC8());
       
    70 
       
    71     SetHostHeaderL( msg, aDestination );
       
    72 
       
    73     msg->AddPairL( UpnpHTTP::KHdrCacheControl(), UpnpHTTP::KNoCache());
       
    74 
       
    75     CleanupStack::PopAndDestroy(); // header
       
    76     CleanupStack::Pop(msg);
       
    77     return msg;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // RUpnpHttpMessageFactory::HttpGetL
       
    82 // HTTP GET
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpGetL( TInetAddr& aDestination,
       
    86                                                       const TDesC8& aHeadPath,
       
    87                                                       const TDesC8& aTailPath )
       
    88     {
       
    89     HBufC8 *wholePath = HBufC8::NewLC(aHeadPath.Length() + aTailPath.Length());
       
    90     wholePath->Des().Append(aHeadPath);
       
    91     wholePath->Des().Append(aTailPath);
       
    92     CUpnpHttpMessage* msg = HttpGetL(aDestination, *wholePath);
       
    93     CleanupStack::PopAndDestroy(wholePath);
       
    94     return msg;
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // RUpnpHttpMessageFactory::HttpGetL
       
    99 // HTTP GET
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpGetL(const TDesC8& aUrl)
       
   103     {
       
   104     TPtrC8 PathPtr;
       
   105     PathPtr.Set(aUrl);
       
   106     TInt PosOfDoubleSlash = PathPtr.FindC(UpnpString::KDoubleSlash());
       
   107     TInt PosOfSlash=0;
       
   108     if(PosOfDoubleSlash==KErrNotFound)
       
   109         {
       
   110         // no "//" found, so we assume that protocol (http://, ftp://)
       
   111         // is not used in front of aPath
       
   112         PosOfSlash=PathPtr.Find(UpnpString::KSlash());
       
   113         if(PosOfSlash != 0)
       
   114             {
       
   115             PathPtr.Set(PathPtr.Mid(PosOfSlash+1));
       
   116             }
       
   117         }
       
   118     else
       
   119         {
       
   120         // DoubleSlash found, so we have to get next slash after doubleSlash.
       
   121         PathPtr.Set(PathPtr.Mid( PosOfDoubleSlash+2 ));
       
   122         PosOfSlash=PathPtr.Find(UpnpString::KSlash());
       
   123         if(PosOfSlash != KErrNotFound)
       
   124             {
       
   125             PathPtr.Set(PathPtr.Mid(PosOfSlash));
       
   126             }
       
   127         }
       
   128 
       
   129     if(PathPtr.Length() == 0)
       
   130         {
       
   131         PathPtr.Set(aUrl.Mid( PosOfSlash+PosOfDoubleSlash+2 ));
       
   132         }
       
   133 
       
   134     TPtrC8 addressPtr;
       
   135     addressPtr.Set(aUrl);
       
   136     if(PosOfDoubleSlash != KErrNotFound)
       
   137         {
       
   138         addressPtr.Set(addressPtr.Mid(PosOfDoubleSlash+2));
       
   139         }
       
   140 
       
   141     PosOfSlash=0;
       
   142     PosOfSlash=addressPtr.Find(UpnpString::KSlash());
       
   143     if(PosOfSlash != KErrNotFound)
       
   144         {
       
   145         addressPtr.Set(addressPtr.Left(PosOfSlash));
       
   146         }
       
   147 
       
   148     TInetAddr addr;
       
   149 
       
   150     TInt port=KDefaultHttpPortNumber; // default port of http.
       
   151 
       
   152     TInt PosOfColon=addressPtr.Find(UpnpString::KColon());
       
   153     if(PosOfColon != KErrNotFound)
       
   154         {
       
   155 
       
   156         // port is given in url, so we use that one.
       
   157         TPtrC8 portPtr;
       
   158         portPtr.Set(addressPtr);
       
   159         portPtr.Set(addressPtr.Mid(PosOfColon+1));
       
   160 
       
   161         TLex8 lex = TLex8(portPtr);
       
   162         TInt error = lex.Val(port);
       
   163         if( error == KErrNone )
       
   164             {
       
   165             addressPtr.Set(addressPtr.Left(PosOfColon));
       
   166             }
       
   167         }
       
   168 
       
   169     addr.SetPort(port);
       
   170 
       
   171     CUpnpHttpMessage::AddrInput(addr, addressPtr);
       
   172 
       
   173     CUpnpHttpMessage* msg =  HttpGetL(addr, PathPtr);
       
   174 
       
   175     return msg;
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // RUpnpHttpMessageFactory::HttpGetL
       
   180 // HTTP GET
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpGetL(
       
   184                         const TDesC8& aDestination,
       
   185                         const TDesC8& aPath )
       
   186     {
       
   187 
       
   188     TPtrC8 tempPtr(aPath);
       
   189     TInt PosOfDoubleSlash = tempPtr.FindC(UpnpString::KDoubleSlash());
       
   190     TInt PosOfSlash=0;
       
   191     if(PosOfDoubleSlash==KErrNotFound)
       
   192         {
       
   193         // no "//" found, so we assume that protocol (http://, ftp://)
       
   194         // is not used in front of aPath
       
   195         PosOfSlash=tempPtr.Find( UpnpString::KSlash() );
       
   196         if(PosOfSlash != 0)
       
   197             {
       
   198             tempPtr.Set(tempPtr.Mid(PosOfSlash+1));
       
   199             }
       
   200         }
       
   201     else
       
   202         {
       
   203         // DoubleSlash found, so we have to get next slash after doubleSlash.
       
   204         tempPtr.Set(tempPtr.Mid( PosOfDoubleSlash+2 ));
       
   205         PosOfSlash=tempPtr.Find( UpnpString::KSlash() );
       
   206         tempPtr.Set(tempPtr.Mid(PosOfSlash));
       
   207         }
       
   208 
       
   209     if(tempPtr.Length() == 0)
       
   210         {
       
   211         tempPtr.Set(aPath.Mid( PosOfSlash+PosOfDoubleSlash+2 ));
       
   212         }
       
   213 
       
   214     TInt port(KDefaultHttpPortNumber);
       
   215 
       
   216     RPointerArray<TPtrC8> array;
       
   217     CleanupClosePushL(array);
       
   218     UpnpString::CutToPiecesL(aDestination, ':', array);
       
   219 
       
   220     if( array.Count() == 0 )
       
   221         {
       
   222         CleanupStack::PopAndDestroy( &array ); //array
       
   223         return NULL;
       
   224         }
       
   225 
       
   226     //if the port is defined. otherwise use 80 as a default
       
   227     if(array.Count() > 1)
       
   228         {
       
   229         UpnpString::StringToInt(  *(array)[1], &port );
       
   230         }
       
   231 
       
   232     TPtrC8 temp = *(array)[0];
       
   233     CleanupStack::Pop(); //array
       
   234     array.ResetAndDestroy();
       
   235 
       
   236     TInetAddr* addr = new (ELeave) TInetAddr();
       
   237     CleanupStack::PushL( addr );
       
   238 
       
   239     TInt slash = temp.Find( UpnpString::KSlash() );
       
   240 
       
   241     if(slash != KErrNotFound)
       
   242         {
       
   243         temp.Set(temp.Left(slash));
       
   244         }
       
   245 
       
   246     CUpnpHttpMessage::AddrInput(*addr, temp);
       
   247     addr->SetPort(port);
       
   248 
       
   249     CUpnpHttpMessage* msg =  HttpGetL(*addr, tempPtr);
       
   250 
       
   251     CleanupStack::PopAndDestroy( addr );
       
   252 
       
   253     return msg;
       
   254     }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // RUpnpHttpMessageFactory::HttpHeadL
       
   258 // HTTP HEAD
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpHeadL(const TDesC8& aUrl)
       
   262     {
       
   263     TPtrC8 PathPtr;
       
   264     PathPtr.Set(aUrl);
       
   265     TInt PosOfDoubleSlash = PathPtr.FindC(UpnpString::KDoubleSlash());
       
   266     TInt PosOfSlash=0;
       
   267     if(PosOfDoubleSlash==KErrNotFound)
       
   268         {
       
   269         // no "//" found, so we assume that protocol (http://, ftp://)
       
   270         // is not used in front of aPath
       
   271         PosOfSlash=PathPtr.Find(UpnpString::KSlash());
       
   272         if(PosOfSlash != 0)
       
   273             {
       
   274             PathPtr.Set(PathPtr.Mid(PosOfSlash+1));
       
   275             }
       
   276         }
       
   277     else
       
   278         {
       
   279         // DoubleSlash found, so we have to get next slash after doubleSlash.
       
   280         PathPtr.Set(PathPtr.Mid( PosOfDoubleSlash+2 ));
       
   281         PosOfSlash=PathPtr.Find(UpnpString::KSlash());
       
   282         if(PosOfSlash != KErrNotFound)
       
   283             {
       
   284             PathPtr.Set(PathPtr.Mid(PosOfSlash));
       
   285             }
       
   286         }
       
   287 
       
   288     if(PathPtr.Length() == 0)
       
   289         {
       
   290         PathPtr.Set(aUrl.Mid( PosOfSlash+PosOfDoubleSlash+2 ));
       
   291         }
       
   292 
       
   293     TPtrC8 addressPtr;
       
   294     addressPtr.Set(aUrl);
       
   295     if(PosOfDoubleSlash != KErrNotFound)
       
   296         {
       
   297         addressPtr.Set(addressPtr.Mid(PosOfDoubleSlash+2));
       
   298         }
       
   299 
       
   300     PosOfSlash=0;
       
   301     PosOfSlash=addressPtr.Find(UpnpString::KSlash());
       
   302     if(PosOfSlash != KErrNotFound)
       
   303         {
       
   304         addressPtr.Set(addressPtr.Left(PosOfSlash));
       
   305         }
       
   306 
       
   307     TInetAddr addr;
       
   308 
       
   309     TInt port=KDefaultHttpPortNumber; // default port of http.
       
   310 
       
   311     TInt PosOfColon=addressPtr.Find(UpnpString::KColon());
       
   312     if(PosOfColon != KErrNotFound)
       
   313         {
       
   314 
       
   315         // port is given in url, so we use that one.
       
   316         TPtrC8 portPtr;
       
   317         portPtr.Set(addressPtr);
       
   318         portPtr.Set(addressPtr.Mid(PosOfColon+1));
       
   319 
       
   320         TLex8 lex = TLex8(portPtr);
       
   321         TInt error = lex.Val(port);
       
   322         if( error == KErrNone )
       
   323             {
       
   324             addressPtr.Set(addressPtr.Left(PosOfColon));
       
   325             }
       
   326         }
       
   327 
       
   328     addr.SetPort(port);
       
   329 
       
   330     CUpnpHttpMessage::AddrInput(addr, addressPtr);
       
   331 
       
   332     CUpnpHttpMessage* msg =  HttpHeadL(addr, PathPtr);
       
   333 
       
   334     return msg;
       
   335     }
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // RUpnpHttpMessageFactory::HttpHeadL
       
   339 // HTTP HEAD
       
   340 // -----------------------------------------------------------------------------
       
   341 //
       
   342 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpHeadL(
       
   343                         const TDesC8& aDestination,
       
   344                         const TDesC8& aPath )
       
   345     {
       
   346 
       
   347     TPtrC8 tempPtr(aPath);
       
   348     TInt PosOfDoubleSlash = tempPtr.FindC(UpnpString::KDoubleSlash());
       
   349     TInt PosOfSlash=0;
       
   350     if(PosOfDoubleSlash==KErrNotFound)
       
   351         {
       
   352         // no "//" found, so we assume that protocol (http://, ftp://)
       
   353         // is not used in front of aPath
       
   354         PosOfSlash=tempPtr.Find( UpnpString::KSlash() );
       
   355         if(PosOfSlash != 0)
       
   356             {
       
   357             tempPtr.Set(tempPtr.Mid(PosOfSlash+1));
       
   358             }
       
   359         }
       
   360     else
       
   361         {
       
   362         // DoubleSlash found, so we have to get next slash after doubleSlash.
       
   363         tempPtr.Set(tempPtr.Mid( PosOfDoubleSlash+2 ));
       
   364         PosOfSlash=tempPtr.Find( UpnpString::KSlash() );
       
   365         tempPtr.Set(tempPtr.Mid(PosOfSlash));
       
   366         }
       
   367 
       
   368     if(tempPtr.Length() == 0)
       
   369         {
       
   370         tempPtr.Set(aPath.Mid( PosOfSlash+PosOfDoubleSlash+2 ));
       
   371         }
       
   372 
       
   373     RPointerArray<TPtrC8> array;
       
   374     CleanupClosePushL(array);
       
   375     UpnpString::CutToPiecesL(aDestination, ':', array);
       
   376         if( array.Count() == 0 )
       
   377         {
       
   378         CleanupStack::PopAndDestroy( &array ); //array
       
   379         return NULL;
       
   380         }
       
   381     TInt port( KDefaultHttpPortNumber);
       
   382     //if the port is defined. otherwise use 80 as a default
       
   383     if(array.Count() > 1)
       
   384         {
       
   385         UpnpString::StringToInt(  *(array)[1], &port );
       
   386         }
       
   387     TPtrC8 temp = *(array)[0];
       
   388     CleanupStack::Pop(); //array
       
   389     array.ResetAndDestroy();
       
   390 
       
   391     TInetAddr* addr = new (ELeave) TInetAddr();
       
   392     CleanupStack::PushL( addr );
       
   393 
       
   394     TInt slash = temp.Find( UpnpString::KSlash() );
       
   395 
       
   396     if(slash != KErrNotFound)
       
   397         {
       
   398         temp.Set(temp.Left(slash));
       
   399         }
       
   400 
       
   401     CUpnpHttpMessage::AddrInput(*addr, temp);
       
   402     addr->SetPort(port);
       
   403 
       
   404     CUpnpHttpMessage* msg =  HttpHeadL(*addr, tempPtr);
       
   405 
       
   406     CleanupStack::PopAndDestroy( addr );
       
   407 
       
   408     return msg;
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // RUpnpHttpMessageFactory::HttpHeadL
       
   413 // HTTP HEAD
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpHeadL( TInetAddr& aDestination,
       
   417                                                       const TDesC8& aPath )
       
   418     {
       
   419     CUpnpHttpMessage* msg = CUpnpHttpMessage::NewL(aDestination);
       
   420     CleanupStack::PushL(msg);
       
   421     msg->SetDestinationPathL( aPath );
       
   422 
       
   423     TInt headerLen = 0;
       
   424 
       
   425     headerLen += KHttpHead().Length();
       
   426     headerLen += UpnpString::KSpace().Length();
       
   427     headerLen += UpnpString::KSlash().Length();
       
   428     headerLen += aPath.Length();
       
   429     headerLen += KHttp11().Length();
       
   430 
       
   431     HBufC8* header = HBufC8::NewLC(headerLen);
       
   432 
       
   433     header->Des().Zero();
       
   434     header->Des().Append(KHttpHead());
       
   435     header->Des().Append(UpnpString::KSpace());
       
   436     if (aPath.Length() >= UpnpString::KSlash().Length())
       
   437         {
       
   438         if (aPath.Find(UpnpString::KSlash()) != 0)
       
   439             {
       
   440             header->Des().Append(UpnpString::KSlash());
       
   441             }
       
   442         }
       
   443     else
       
   444         {
       
   445         header->Des().Append(UpnpString::KSlash());
       
   446         }
       
   447     header->Des().Append(aPath);
       
   448     header->Des().Append(KHttp11);
       
   449 
       
   450     msg->AddPairL(*header, KNullDesC8());
       
   451 
       
   452     SetHostHeaderL( msg, aDestination );
       
   453 
       
   454     CleanupStack::PopAndDestroy(); // header
       
   455     CleanupStack::Pop(msg);
       
   456     return msg;
       
   457     }
       
   458 
       
   459 // -----------------------------------------------------------------------------
       
   460 // RUpnpHttpMessageFactory::HttpPostL
       
   461 // HTTP POST
       
   462 // -----------------------------------------------------------------------------
       
   463 //
       
   464 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpPostL(const TDesC8& aUrl)
       
   465     {
       
   466     TPtrC8 PathPtr;
       
   467     PathPtr.Set(aUrl);
       
   468     TInt PosOfDoubleSlash = PathPtr.FindC(UpnpString::KDoubleSlash());
       
   469     TInt PosOfSlash=0;
       
   470     if(PosOfDoubleSlash==KErrNotFound)
       
   471         {
       
   472         // no "//" found, so we assume that protocol (http://, ftp://)
       
   473         // is not used in front of aPath
       
   474         PosOfSlash=PathPtr.Find(UpnpString::KSlash());
       
   475         if(PosOfSlash != 0)
       
   476             {
       
   477             PathPtr.Set(PathPtr.Mid(PosOfSlash+1));
       
   478             }
       
   479         }
       
   480     else
       
   481         {
       
   482         // DoubleSlash found, so we have to get next slash after doubleSlash.
       
   483         PathPtr.Set(PathPtr.Mid( PosOfDoubleSlash+2 ));
       
   484         PosOfSlash=PathPtr.Find(UpnpString::KSlash());
       
   485         if(PosOfSlash != KErrNotFound)
       
   486             {
       
   487             PathPtr.Set(PathPtr.Mid(PosOfSlash));
       
   488             }
       
   489         }
       
   490 
       
   491     if(PathPtr.Length() == 0)
       
   492         {
       
   493         PathPtr.Set(aUrl.Mid( PosOfSlash+PosOfDoubleSlash+2 ));
       
   494         }
       
   495 
       
   496     TPtrC8 addressPtr;
       
   497     addressPtr.Set(aUrl);
       
   498     if(PosOfDoubleSlash != KErrNotFound)
       
   499         {
       
   500         addressPtr.Set(addressPtr.Mid(PosOfDoubleSlash+2));
       
   501         }
       
   502 
       
   503     PosOfSlash=0;
       
   504     PosOfSlash=addressPtr.Find(UpnpString::KSlash());
       
   505     if(PosOfSlash != KErrNotFound)
       
   506         {
       
   507         addressPtr.Set(addressPtr.Left(PosOfSlash));
       
   508         }
       
   509 
       
   510     TInetAddr addr;
       
   511     HBufC* addrBuf;
       
   512     TInt port=KDefaultHttpPortNumber; // default port of http
       
   513 
       
   514     TInt PosOfColon=addressPtr.Find(UpnpString::KColon());
       
   515     if(PosOfColon != KErrNotFound)
       
   516         {
       
   517         // port is given in url, so we use that one.
       
   518         TPtrC8 portPtr;
       
   519         portPtr.Set(addressPtr);
       
   520         portPtr.Set(addressPtr.Mid(PosOfColon+1));
       
   521 
       
   522         TLex8 lex = TLex8(portPtr);
       
   523         lex.Val(port);
       
   524 
       
   525         addressPtr.Set(addressPtr.Left(PosOfColon));
       
   526         }
       
   527 
       
   528     addr.SetPort(port);
       
   529 
       
   530     addrBuf=UpnpString::ToUnicodeL(addressPtr);
       
   531 
       
   532     CUpnpHttpMessage::AddrInput(addr, addressPtr);
       
   533 
       
   534     delete addrBuf;
       
   535     addrBuf=NULL;
       
   536 
       
   537     CUpnpHttpMessage* msg =  HttpPostL(addr, PathPtr);
       
   538 
       
   539     return msg;
       
   540     }
       
   541 
       
   542 // -----------------------------------------------------------------------------
       
   543 // RUpnpHttpMessageFactory::HttpPostL
       
   544 // HTTP POST
       
   545 // -----------------------------------------------------------------------------
       
   546 //
       
   547 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpPostL(
       
   548                             const TDesC8& aDestination,
       
   549                             const TDesC8& aPath )
       
   550     {
       
   551     RPointerArray<TPtrC8> array;
       
   552     CleanupClosePushL(array);
       
   553     UpnpString::CutToPiecesL(aDestination, ':',array);
       
   554     TInetAddr addr;
       
   555     if(array.Count() > 1)
       
   556         {
       
   557         TInt port;
       
   558         if((array)[1]->Length() >0)
       
   559             {
       
   560             TLex8 lex = TLex8( *(array)[1] );
       
   561             lex.Val(port);
       
   562             }
       
   563         else
       
   564             {
       
   565             CleanupStack::Pop(); //array
       
   566             array.ResetAndDestroy();
       
   567             return NULL;
       
   568             }
       
   569 
       
   570         if((array)[0]->Length() > 0)
       
   571             {
       
   572             CUpnpHttpMessage::AddrInput(addr, *(array)[0]);
       
   573             addr.SetPort( port);
       
   574             }
       
   575         else
       
   576             {
       
   577             CleanupStack::Pop(); //array
       
   578             array.ResetAndDestroy();
       
   579             return NULL;
       
   580             }
       
   581         }
       
   582     else
       
   583         {
       
   584         CleanupStack::Pop(); //array
       
   585         array.ResetAndDestroy();
       
   586         return NULL;
       
   587         }
       
   588     CleanupStack::Pop();
       
   589     array.ResetAndDestroy();
       
   590     return HttpPostL(addr, aPath);
       
   591     }
       
   592 
       
   593 // -----------------------------------------------------------------------------
       
   594 // RUpnpHttpMessageFactory::HttpPostL
       
   595 // HTTP POST
       
   596 // -----------------------------------------------------------------------------
       
   597 //
       
   598 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpPostL(
       
   599                             const TInetAddr& aDestination,
       
   600                             const TDesC8& aPath )
       
   601     {
       
   602     CUpnpHttpMessage* msg = CUpnpHttpMessage::NewL(aDestination);
       
   603     CleanupStack::PushL(msg);
       
   604     msg->SetDestinationPathL( aPath );
       
   605     HBufC8* temp = HBufC8::NewLC( KHttpPost().Length() +
       
   606                                   UpnpString::KSpace().Length() +
       
   607                                   aPath.Length() +
       
   608                                   KHttp11().Length() );
       
   609 
       
   610     temp->Des().Zero();
       
   611     temp->Des().Append(KHttpPost());
       
   612     temp->Des().Append(UpnpString::KSpace());
       
   613     temp->Des().Append(aPath);
       
   614     temp->Des().Append(KHttp11);
       
   615     msg->AddPairL(*temp, KNullDesC8());
       
   616 
       
   617     SetHostHeaderL( msg, aDestination );
       
   618 
       
   619     msg->AddPairL(UpnpHTTP::KConnection(), UpnpHTTP::KClose());
       
   620     CleanupStack::PopAndDestroy(temp);
       
   621     CleanupStack::Pop();
       
   622     return msg;
       
   623     }
       
   624 
       
   625 // -----------------------------------------------------------------------------
       
   626 // RUpnpHttpMessageFactory::HttpResponseOkL
       
   627 // HTTP response OK
       
   628 // -----------------------------------------------------------------------------
       
   629 //
       
   630 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpResponseOkL(
       
   631                                         CUpnpHttpMessage* aMessage )
       
   632     {
       
   633     CUpnpHttpMessage* msg = CUpnpHttpMessage::NewL(aMessage->Sender(),
       
   634         aMessage->SessionId());
       
   635     CleanupStack::PushL(msg);
       
   636 
       
   637     msg->AddPairL(KHttp11Ok(), KNullDesC8());
       
   638 
       
   639 
       
   640     msg->SetDestinationPathL(aMessage->SenderPath());
       
   641     msg->SetSenderPathL(aMessage->DestinationPath());
       
   642 
       
   643     CleanupStack::Pop(msg);
       
   644     return msg;
       
   645     }
       
   646 
       
   647 // -----------------------------------------------------------------------------
       
   648 // RUpnpHttpMessageFactory::HttpResponseOkL
       
   649 // HTTP response OK
       
   650 // -----------------------------------------------------------------------------
       
   651 //
       
   652 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpResponseOkL(
       
   653                                     const TInetAddr& aDestination )
       
   654     {
       
   655     CUpnpHttpMessage* msg = CUpnpHttpMessage::NewL(aDestination);
       
   656     CleanupStack::PushL(msg);
       
   657 
       
   658     msg->AddPairL(KHttp11Ok(), KNullDesC8());
       
   659 
       
   660     CleanupStack::Pop(msg);
       
   661     return msg;
       
   662     }
       
   663 
       
   664 // -----------------------------------------------------------------------------
       
   665 // RUpnpHttpMessageFactory::Http11Response
       
   666 // HTTP response
       
   667 // -----------------------------------------------------------------------------
       
   668 //
       
   669 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::Http11ResponseL(
       
   670                                     CUpnpHttpMessage *aMessage,
       
   671                                     TInt aStatus )
       
   672     {
       
   673     CUpnpHttpMessage* msg = RUpnpHttpMessageFactory::Http11ResponseL( aMessage->Sender(), aStatus );
       
   674     CleanupStack::PushL(msg);
       
   675 
       
   676     msg->SetSessionId( aMessage->SessionId() );
       
   677     msg->SetDestinationPathL(aMessage->SenderPath());
       
   678     msg->SetSenderPathL(aMessage->DestinationPath());
       
   679 
       
   680     CleanupStack::Pop(msg);
       
   681     return msg;
       
   682     }
       
   683 
       
   684 // -----------------------------------------------------------------------------
       
   685 // RUpnpHttpMessageFactory::Http11Response
       
   686 // HTTP response
       
   687 // -----------------------------------------------------------------------------
       
   688 //
       
   689 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::Http11ResponseL(
       
   690                                     const TInetAddr& aDestination,
       
   691                                     TInt aStatus )
       
   692     {
       
   693     CUpnpHttpMessage* msg = CUpnpHttpMessage::NewL(aDestination);
       
   694     CleanupStack::PushL(msg);
       
   695     THttpStatusCode status = (THttpStatusCode) aStatus;
       
   696 
       
   697     TBuf8<KMaxIntegerLength> num;
       
   698     num.Num( aStatus );
       
   699 
       
   700     HBufC8* tempBuf;
       
   701     tempBuf=HBufC8::NewLC( KHttp11WithoutSpace().Length() + KSpace().Length() +
       
   702                             num.Length() + KSpace().Length() +
       
   703                             (CUpnpHttpMessage::HttpError(status)).Length() );
       
   704     tempBuf->Des().Zero();
       
   705     tempBuf->Des().Append(KHttp11WithoutSpace());
       
   706     tempBuf->Des().Append( KSpace() );
       
   707     tempBuf->Des().Append( num );
       
   708     tempBuf->Des().Append( KSpace );
       
   709     tempBuf->Des().Append(CUpnpHttpMessage::HttpError(status));
       
   710 
       
   711 
       
   712     msg->AddPairL((TDesC8&) *tempBuf, (TDesC8&) KNullDesC8());
       
   713 
       
   714     CleanupStack::PopAndDestroy(tempBuf);
       
   715     tempBuf=NULL;
       
   716 
       
   717     CleanupStack::Pop(msg);
       
   718     return msg;
       
   719     }
       
   720 
       
   721 // -----------------------------------------------------------------------------
       
   722 // RUpnpHttpMessageFactory::HttpResponseErrorL
       
   723 // HTTP response ERROR
       
   724 // -----------------------------------------------------------------------------
       
   725 //
       
   726 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpResponseErrorL(
       
   727                                     CUpnpHttpMessage *aMessage,
       
   728                                     TInt aError )
       
   729     {
       
   730     CUpnpHttpMessage* msg = RUpnpHttpMessageFactory::HttpResponseErrorL(
       
   731         aMessage->Sender(), aError );
       
   732     CleanupStack::PushL(msg);
       
   733 
       
   734     msg->SetSessionId( aMessage->SessionId() );
       
   735     msg->SetDestinationPathL(aMessage->SenderPath());
       
   736     msg->SetSenderPathL(aMessage->DestinationPath());
       
   737 
       
   738     CleanupStack::Pop(msg);
       
   739     return msg;
       
   740     }
       
   741 
       
   742 // -----------------------------------------------------------------------------
       
   743 // RUpnpHttpMessageFactory::HttpResponseErrorL
       
   744 // HTTP response ERROR
       
   745 // -----------------------------------------------------------------------------
       
   746 //
       
   747 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::HttpResponseErrorL(
       
   748                             const TInetAddr& aDestination,
       
   749                             TInt aError )
       
   750     {
       
   751     CUpnpHttpMessage* msg = CUpnpHttpMessage::NewL(aDestination);
       
   752     CleanupStack::PushL(msg);
       
   753     THttpStatusCode error = (THttpStatusCode) aError;
       
   754 
       
   755     HBufC8* tempBuf;
       
   756     tempBuf=HBufC8::NewLC((CUpnpHttpMessage::HttpError(error)).Length()+KMaxIntegerLength);
       
   757     tempBuf->Des().Zero();
       
   758 
       
   759     tempBuf->Des().Num(aError);
       
   760     tempBuf->Des().Append( KSpace );
       
   761     tempBuf->Des().Append(CUpnpHttpMessage::HttpError(error));
       
   762 
       
   763     HBufC8* tempBuf2;
       
   764     tempBuf2=HBufC8::NewLC(tempBuf->Des().Length()+KHttp11().Length()+1);
       
   765     tempBuf2->Des().Zero();
       
   766     tempBuf2->Des().Append(KHttp11WithoutSpace());
       
   767     tempBuf2->Des().Append(UpnpString::KSpace());
       
   768     tempBuf2->Des().Append(*tempBuf);
       
   769 
       
   770     msg->AddPairL((TDesC8&) *tempBuf2, (TDesC8&) KNullDesC8());
       
   771 
       
   772     CleanupStack::PopAndDestroy(tempBuf2);
       
   773     tempBuf2=NULL;
       
   774 
       
   775     HBufC8* BodyBuf;
       
   776     BodyBuf=HBufC8::NewLC( KUpnpBodyMsgError1().Length() +
       
   777                            KUpnpBodyMsgError2().Length() +
       
   778                            KUpnpBodyMsgError3().Length() +
       
   779                            ( tempBuf->Length() * 2 ) );
       
   780     BodyBuf->Des().Zero();
       
   781     BodyBuf->Des().Append(KUpnpBodyMsgError1());
       
   782     BodyBuf->Des().Append(*tempBuf);
       
   783     BodyBuf->Des().Append(KUpnpBodyMsgError2());
       
   784     BodyBuf->Des().Append(CUpnpHttpMessage::HttpError(error));
       
   785     BodyBuf->Des().Append(KUpnpBodyMsgError3());
       
   786 
       
   787     msg->SetBodyL( *BodyBuf);
       
   788 
       
   789     CleanupStack::PopAndDestroy(BodyBuf);
       
   790     CleanupStack::PopAndDestroy(tempBuf);
       
   791     tempBuf=NULL;
       
   792     BodyBuf=NULL;
       
   793 
       
   794     CleanupStack::Pop(msg);
       
   795     return msg;
       
   796     }
       
   797 
       
   798 // -----------------------------------------------------------------------------
       
   799 // RUpnpHttpMessageFactory::UpnpResponseErrorL
       
   800 // UPnP response ERROR
       
   801 // -----------------------------------------------------------------------------
       
   802 //
       
   803 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::UpnpResponseErrorL(
       
   804                             CUpnpHttpMessage* aMessage,
       
   805                             TUpnpErrorCode aError )
       
   806     {
       
   807     CUpnpHttpMessage* msg = RUpnpHttpMessageFactory::UpnpResponseErrorL(
       
   808         aMessage->Sender(), aError);
       
   809     CleanupStack::PushL(msg);
       
   810 
       
   811     msg->SetSessionId(aMessage->SessionId());
       
   812     msg->SetDestinationPathL(aMessage->SenderPath());
       
   813     msg->SetSenderPathL(aMessage->DestinationPath());
       
   814 
       
   815     CleanupStack::Pop(msg);
       
   816     return msg;
       
   817     }
       
   818 
       
   819 // -----------------------------------------------------------------------------
       
   820 // RUpnpHttpMessageFactory::UpnpResponseErrorL
       
   821 // UPnP response ERROR
       
   822 // -----------------------------------------------------------------------------
       
   823 //
       
   824 EXPORT_C CUpnpHttpMessage* RUpnpHttpMessageFactory::UpnpResponseErrorL(
       
   825                             const TInetAddr& aDestination,
       
   826                             TUpnpErrorCode aError )
       
   827     {
       
   828     CUpnpHttpMessage* msg = CUpnpHttpMessage::NewL(aDestination);
       
   829     CleanupStack::PushL(msg);
       
   830 
       
   831     TBuf8<KMaxIntegerLength> num;
       
   832     num.Num((TInt)aError);
       
   833 
       
   834     HBufC8* tempBuf;
       
   835     tempBuf=HBufC8::NewLC((CUpnpHttpMessage::UpnpError(aError)).Length()
       
   836                             +KMaxIntegerLength+KHttp11().Length()+1);
       
   837     tempBuf->Des().Zero();
       
   838     tempBuf->Des().Append(KHttp11WithoutSpace());
       
   839     tempBuf->Des().Append(UpnpString::KSpace());
       
   840     tempBuf->Des().Append(num);
       
   841     tempBuf->Des().Append( KSpace );
       
   842     tempBuf->Des().Append(CUpnpHttpMessage::UpnpError(aError));
       
   843 
       
   844     msg->AddPairL((TDesC8&) *tempBuf, (TDesC8&) KNullDesC8());
       
   845 
       
   846     CleanupStack::PopAndDestroy(tempBuf);
       
   847     tempBuf=NULL;
       
   848 
       
   849     CleanupStack::Pop(msg);
       
   850     return msg;
       
   851     }
       
   852 
       
   853 // -----------------------------------------------------------------------------
       
   854 // RUpnpHttpMessageFactory::SetHostHeaderL
       
   855 // Sets Host HTTP header to given message basing on given destinstion address.
       
   856 // -----------------------------------------------------------------------------
       
   857 //
       
   858 void RUpnpHttpMessageFactory::SetHostHeaderL( CUpnpHttpMessage* aMessage, const TInetAddr& aDestination )
       
   859     {
       
   860 
       
   861     TInt port = aDestination.Port();
       
   862     TBuf8<UpnpString::KShortStringLength> addrBuf;
       
   863     TBuf8<UpnpString::KMaxTUintLength> portBuf;
       
   864     portBuf.Num( port );
       
   865 
       
   866     CUpnpHttpMessage::AddrOutput(aDestination, addrBuf);
       
   867 
       
   868     HBufC8* hostBuf=HBufC8::NewLC(addrBuf.Length() + KColon().Length() + portBuf.Length());
       
   869     TPtr8 hostPtr = hostBuf->Des();
       
   870     hostPtr.Append( addrBuf );
       
   871     hostPtr.Append( KColon() );
       
   872     hostPtr.Append( portBuf );
       
   873 
       
   874     aMessage->AddPairL(UpnpSSDP::KHdrHost(),*hostBuf);
       
   875 
       
   876     CleanupStack::PopAndDestroy( hostBuf );
       
   877     }
       
   878 
       
   879 //  End of File
       
   880