Example #1
0
void UPnpCDS::HandleBrowse( HTTPRequest *pRequest )
{
    UPnpCDSExtensionResults *pResult  = NULL;
    UPnpCDSRequest           request;

    request.m_sObjectId         = pRequest->m_mapParams[ "ObjectID"      ];
    request.m_sContainerID      = pRequest->m_mapParams[ "ContainerID"   ];
    request.m_sParentId         = "0";
    request.m_eBrowseFlag       = GetBrowseFlag( pRequest->m_mapParams[ "BrowseFlag"    ] );
    request.m_sFilter           = pRequest->m_mapParams[ "Filter"        ];
    request.m_nStartingIndex    = pRequest->m_mapParams[ "StartingIndex" ].toLong();
    request.m_nRequestedCount   = pRequest->m_mapParams[ "RequestedCount"].toLong();
    request.m_sSortCriteria     = pRequest->m_mapParams[ "SortCriteria"  ];

/*
    VERBOSE(VB_UPNP,QString("UPnpCDS::ProcessRequest \n"
                            ": url            = %1 \n"
                            ": Method         = %2 \n"
                            ": ObjectId       = %3 \n"
                            ": BrowseFlag     = %4 \n"
                            ": Filter         = %5 \n"
                            ": StartingIndex  = %6 \n"
                            ": RequestedCount = %7 \n"
                            ": SortCriteria   = %8 " )
                       .arg( pRequest->m_sBaseUrl     )
                       .arg( pRequest->m_sMethod      )
                       .arg( request.m_sObjectId      )
                       .arg( request.m_eBrowseFlag    )
                       .arg( request.m_sFilter        )
                       .arg( request.m_nStartingIndex )
                       .arg( request.m_nRequestedCount)
                       .arg( request.m_sSortCriteria  ));
*/

    UPnPResultCode eErrorCode      = UPnPResult_CDS_NoSuchObject;
    QString        sErrorDesc      = "";
    short          nNumberReturned = 0;
    short          nTotalMatches   = 0;
    short          nUpdateID       = 0;
    QString        sResultXML;

    VERBOSE(VB_UPNP, QString("UPnpCDS::HandleBrowse ObjectID=%1, ContainerId=%2")
		             .arg(request.m_sObjectId)
		             .arg(request.m_sContainerID));

    if (request.m_sObjectId == "0")
    {
        // ------------------------------------------------------------------
        // This is for the root object... lets handle it.
        // ------------------------------------------------------------------

        switch( request.m_eBrowseFlag )
        {
            case CDS_BrowseMetadata:
            {
                // ----------------------------------------------------------------------
                // Return Root Object Only
                // ----------------------------------------------------------------------

                eErrorCode      = UPnPResult_Success;
                nNumberReturned = 1;
                nTotalMatches   = 1;
                nUpdateID       = m_root.m_nUpdateId;

                m_root.SetChildCount( m_extensions.count() );

                sResultXML      = m_root.toXml();

                break;
            }

            case CDS_BrowseDirectChildren:
            {
                // Loop Through each extension and Build the Root Folders

                // -=>TODO: Need to handle StartingIndex & RequestedCount

                eErrorCode      = UPnPResult_Success;
                nTotalMatches   = m_extensions.count();
                nUpdateID       = m_root.m_nUpdateId;

                if (request.m_nRequestedCount == 0)
                    request.m_nRequestedCount = nTotalMatches;

                short nStart = Max( request.m_nStartingIndex, short( 0 ));
                short nCount = Min( nTotalMatches, request.m_nRequestedCount );

                UPnpCDSExtension    *pExtension = m_extensions.at( nStart );
                UPnpCDSRequest       childRequest;

                childRequest.m_sParentId         = "0";
                childRequest.m_eBrowseFlag       = CDS_BrowseMetadata;
                childRequest.m_sFilter           = "";
                childRequest.m_nStartingIndex    = 0;
                childRequest.m_nRequestedCount   = 1;
                childRequest.m_sSortCriteria     = "";

                while (( pExtension != NULL ) && (nNumberReturned < nCount ))
                {
                    childRequest.m_sObjectId = pExtension->m_sExtensionId;

                    pResult = pExtension->Browse( &childRequest );

                    if (pResult != NULL)
                    {
                        if (pResult->m_eErrorCode == UPnPResult_Success)
                        {
                            sResultXML  += pResult->GetResultXML();
                            nNumberReturned ++;
                        }

                        delete pResult;
                    }

                    pExtension = m_extensions.next();
                }

                break;
            }
            default: break;
        }
    }
    else
    {
        // ------------------------------------------------------------------
        // Look for a CDS Extension that knows how to handle this ObjectID
        // ------------------------------------------------------------------

        UPnpCDSExtension *pExtension = m_extensions.first();

        while (( pExtension != NULL ) && (pResult == NULL))
        {
            VERBOSE(VB_UPNP, QString("UPNP Browse : Searching for : %1  / ObjectID : %2").arg(pExtension->m_sExtensionId).arg(request.m_sObjectId));
            pResult = pExtension->Browse( &request );

            pExtension = m_extensions.next();
        }

        if (pResult != NULL)
        {
            eErrorCode  = pResult->m_eErrorCode;
            sErrorDesc  = pResult->m_sErrorDesc;

            if (eErrorCode == UPnPResult_Success)
            {
                nNumberReturned = pResult->m_List.count();
                nTotalMatches   = pResult->m_nTotalMatches;
                nUpdateID       = pResult->m_nUpdateID;
                sResultXML      = pResult->GetResultXML();
            }

            delete pResult;
        }
    }

    // ----------------------------------------------------------------------
    // Output Results of Browse Method
    // ----------------------------------------------------------------------

    if (eErrorCode == UPnPResult_Success)
    {
        NameValueList list;

        QString sResults = DIDL_LITE_BEGIN;
        sResults += sResultXML;
        sResults += DIDL_LITE_END;

        list.append( new NameValue( "Result"        , sResults                          ));
        list.append( new NameValue( "NumberReturned", QString::number( nNumberReturned )));
        list.append( new NameValue( "TotalMatches"  , QString::number( nTotalMatches   )));
        list.append( new NameValue( "UpdateID"      , QString::number( nUpdateID       )));

        pRequest->FormatActionResponse( &list );
    }
    else
        UPnp::FormatErrorResponse ( pRequest, eErrorCode, sErrorDesc );

}
Example #2
0
void UPnpCDS::HandleBrowse( HTTPRequest *pRequest )
{
    UPnpCDSExtensionResults *pResult  = NULL;
    UPnpCDSRequest           request;

    DetermineClient( pRequest, &request );
    request.m_sObjectId         = pRequest->m_mapParams[ "ObjectID"      ];
    request.m_sContainerID      = pRequest->m_mapParams[ "ContainerID"   ];
    request.m_sParentId         = "0";
    request.m_eBrowseFlag       =
        GetBrowseFlag( pRequest->m_mapParams[ "BrowseFlag"    ] );
    request.m_sFilter           = pRequest->m_mapParams[ "Filter"        ];
    request.m_nStartingIndex    =
        pRequest->m_mapParams[ "StartingIndex" ].toLong();
    request.m_nRequestedCount   =
        pRequest->m_mapParams[ "RequestedCount"].toLong();
    request.m_sSortCriteria     = pRequest->m_mapParams[ "SortCriteria"  ];

#if 0
    LOG(VB_UPNP, LOG_DEBUG, QString("UPnpCDS::ProcessRequest \n"
                                    ": url            = %1 \n"
                                    ": Method         = %2 \n"
                                    ": ObjectId       = %3 \n"
                                    ": BrowseFlag     = %4 \n"
                                    ": Filter         = %5 \n"
                                    ": StartingIndex  = %6 \n"
                                    ": RequestedCount = %7 \n"
                                    ": SortCriteria   = %8 " )
                       .arg( pRequest->m_sBaseUrl     )
                       .arg( pRequest->m_sMethod      )
                       .arg( request.m_sObjectId      )
                       .arg( request.m_eBrowseFlag    )
                       .arg( request.m_sFilter        )
                       .arg( request.m_nStartingIndex )
                       .arg( request.m_nRequestedCount)
                       .arg( request.m_sSortCriteria  ));
#endif

    UPnPResultCode eErrorCode      = UPnPResult_CDS_NoSuchObject;
    QString        sErrorDesc      = "";
    short          nNumberReturned = 0;
    short          nTotalMatches   = 0;
    short          nUpdateID       = 0;
    QString        sResultXML;
    FilterMap filter =  (FilterMap) request.m_sFilter.split(',');

    LOG(VB_UPNP, LOG_INFO,
        QString("UPnpCDS::HandleBrowse ObjectID=%1, ContainerId=%2")
            .arg(request.m_sObjectId) .arg(request.m_sContainerID));

    if (request.m_sObjectId == "0")
    {
        // ------------------------------------------------------------------
        // This is for the root object... lets handle it.
        // ------------------------------------------------------------------

        switch( request.m_eBrowseFlag )
        {
            case CDS_BrowseMetadata:
            {
                // -----------------------------------------------------------
                // Return Root Object Only
                // -----------------------------------------------------------

                eErrorCode      = UPnPResult_Success;
                nNumberReturned = 1;
                nTotalMatches   = 1;
                nUpdateID       = m_root.m_nUpdateId;

                m_root.SetChildCount( m_extensions.count() );

                sResultXML      = m_root.toXml(filter);

                break;
            }

            case CDS_BrowseDirectChildren:
            {
                // Loop Through each extension and Build the Root Folders

                // -=>TODO: Need to handle StartingIndex & RequestedCount

                eErrorCode      = UPnPResult_Success;
                nTotalMatches   = m_extensions.count();
                nUpdateID       = m_root.m_nUpdateId;

                if (request.m_nRequestedCount == 0)
                    request.m_nRequestedCount = nTotalMatches;

                short nStart = Max( request.m_nStartingIndex, short( 0 ));
                short nCount = Min( nTotalMatches, request.m_nRequestedCount );

                UPnpCDSRequest       childRequest;

                DetermineClient( pRequest, &request );
                childRequest.m_sParentId         = "0";
                childRequest.m_eBrowseFlag       = CDS_BrowseMetadata;
                childRequest.m_sFilter           = "";
                childRequest.m_nStartingIndex    = 0;
                childRequest.m_nRequestedCount   = 1;
                childRequest.m_sSortCriteria     = "";

                for (uint i = nStart;
                     (i < (uint)m_extensions.size()) &&
                         (nNumberReturned < nCount);
                     i++)
                {
                    UPnpCDSExtension *pExtension = m_extensions[i];
                    childRequest.m_sObjectId = pExtension->m_sExtensionId;

                    pResult = pExtension->Browse( &childRequest );

                    if (pResult != NULL)
                    {
                        if (pResult->m_eErrorCode == UPnPResult_Success)
                        {
                            sResultXML  += pResult->GetResultXML(filter);
                            nNumberReturned ++;
                        }

                        delete pResult;
                    }
                }

                break;
            }
            default: break;
        }
    }
    else
    {
        // ------------------------------------------------------------------
        // Look for a CDS Extension that knows how to handle this ObjectID
        // ------------------------------------------------------------------

        UPnpCDSExtensionList::iterator it = m_extensions.begin();
        for (; (it != m_extensions.end()) && !pResult; ++it)
        {
            LOG(VB_UPNP, LOG_INFO,
                QString("UPNP Browse : Searching for : %1  / ObjectID : %2")
                    .arg((*it)->m_sExtensionId).arg(request.m_sObjectId));

            pResult = (*it)->Browse(&request);
        }

        if (pResult != NULL)
        {
            eErrorCode  = pResult->m_eErrorCode;
            sErrorDesc  = pResult->m_sErrorDesc;

            if (eErrorCode == UPnPResult_Success)
            {
                nNumberReturned = pResult->m_List.count();
                nTotalMatches   = pResult->m_nTotalMatches;
                nUpdateID       = pResult->m_nUpdateID;
                sResultXML      = pResult->GetResultXML(filter);
            }

            delete pResult;
        }
    }

    // ----------------------------------------------------------------------
    // Output Results of Browse Method
    // ----------------------------------------------------------------------

    if (eErrorCode == UPnPResult_Success)
    {
        NameValues list;

        QString sResults = DIDL_LITE_BEGIN;
        sResults += sResultXML;
        sResults += DIDL_LITE_END;

        list.push_back(NameValue("Result",         sResults));
        list.push_back(NameValue("NumberReturned", nNumberReturned));
        list.push_back(NameValue("TotalMatches",   nTotalMatches));
        list.push_back(NameValue("UpdateID",       nUpdateID));

        pRequest->FormatActionResponse(list);
    }
    else
        UPnp::FormatErrorResponse ( pRequest, eErrorCode, sErrorDesc );

}