void vmsHttpFlvTrafficAnalyzer::FindFlvDownloadInInBetweenDlgs(const vmsHttpTrafficCollector *pTraffic, HTTPDLG pWebPage, LPCSTR pszSwfUrl, LPCSTR pszFlashVars, LPCSTR pszSwfContext, HTTPDLGLIST vFlvs, FindFlvDownloadInInBetweenDlgs_Type enFindType)
{
	LOGFN ("vmsHttpFlvTrafficAnalyzer::FindFlvDownloadInInBetweenDlgs");

	if (!pszSwfUrl)
	{
		assert (pWebPage != NULL);
		if (!pWebPage)
			return;
		pszSwfUrl = pWebPage->strRequestUrl.c_str ();
	}

	string strBody;
	if (!pszSwfContext)
	{
		assert (pWebPage != NULL);
		if (!pWebPage)
			return;
		strBody.assign ((LPSTR)&pWebPage->vbResponseBody [0], pWebPage->vbResponseBody.size ());
		pszSwfContext = strBody.c_str ();
	}	

	int nStartIndex = pWebPage ? pTraffic->getHttpDialogIndex (pWebPage) + 1 : 0;

	
	
	

	HTTPDLGLIST vFlvs2;

	DWORD adwCTs [] = {
		vmsHttpTrafficCollector::HttpDialog::AMF,
		vmsHttpTrafficCollector::HttpDialog::X_WWW_FORM_URL_ENCODED,
		vmsHttpTrafficCollector::HttpDialog::XML,
		vmsHttpTrafficCollector::HttpDialog::HTML,
	};

	for (int iCT = 0; iCT < sizeof (adwCTs) / sizeof (DWORD); iCT++)
	{
		
		HTTPDLGLIST vBetwDlgs;
		pTraffic->GetHttpDialogs (nStartIndex, -1, adwCTs [iCT], vBetwDlgs);

		
		HTTPDLGLIST vBetwDlgs2;
		
		
		
		vector <HTTPDLGLIST>  vBetwToFlvMap;
		
		
		for (size_t iDlg = 0; iDlg < vBetwDlgs.size (); iDlg++)
		{
			HTTPDLG pDlg = vBetwDlgs [iDlg];
			if (pDlg->vbResponseBody.empty ())
				continue; 
			if (pDlg->enCT == vmsHttpTrafficCollector::HttpDialog::HTML &&
					pDlg->vbResponseBody.size () > 1000)
				continue; 

			string strBody; 
			
			if (pDlg->enCT == vmsHttpTrafficCollector::HttpDialog::AMF)
			{
				for (size_t i = 0; i < pDlg->vbResponseBody.size (); i++)
				{
					if (pDlg->vbResponseBody [i] >= ' ')
						strBody += pDlg->vbResponseBody [i];
				}
			}
			else if (pDlg->enCT == vmsHttpTrafficCollector::HttpDialog::X_WWW_FORM_URL_ENCODED)
			{
				
				string str; str.assign ((LPSTR)&pDlg->vbResponseBody [0], pDlg->vbResponseBody.size ());
				vmsHttpHelper::DecodeUrl (str.c_str (), strBody);
			}
			else 
			{
				
				strBody.assign ((LPSTR)&pDlg->vbResponseBody [0], pDlg->vbResponseBody.size ());
			}

			
			if (enFindType == FDT_EXACTURLMATCH)
				FindDialogsByUrlExactMatch (strBody.c_str (), vFlvs, vFlvs2);
			else if (enFindType == FDT_FILEURLMATCH)
				FindDialogsByFileUrlMatch (strBody.c_str (), vFlvs, vFlvs2);
			else if (enFindType == FDT_BYUNIQUEWORDS)
				FindDialogsByUniqueUrlWordsMatch (strBody.c_str (), vFlvs, vFlvs2);
			else
				return;

			if (!vFlvs2.empty ())
			{
				
				vBetwDlgs2.push_back (pDlg);
				vBetwToFlvMap.push_back (vFlvs2);
			}
		}

		
		if (vBetwDlgs2.empty ())
			continue; 

		LOGHTTPDLGLIST ("List of dialogs with FLVs", vBetwDlgs2);

		
		

		LOGsnl ("Found dialogs with FLVs. Find their source.");

		HTTPDLGLIST vBetwDlgsFoundInSwfContext;
		FindDialogsByUrlExactMatch (pszSwfContext, vBetwDlgs2, vBetwDlgsFoundInSwfContext);
		LOGHTTPDLGLIST ("List of these dialogs belonging to source page (exact match)", vBetwDlgsFoundInSwfContext);
		if (vBetwDlgsFoundInSwfContext.empty ())
		{
			FindDialogsByFileUrlMatch (pszSwfContext, vBetwDlgs, vBetwDlgsFoundInSwfContext);
			LOGHTTPDLGLIST ("List of these dialogs belonging to source page (file url match)", vBetwDlgsFoundInSwfContext);
		}
		if (vBetwDlgsFoundInSwfContext.empty ())
		{
			FindDialogsByUniqueUrlWordsMatch (pszSwfContext, vBetwDlgs, vBetwDlgsFoundInSwfContext);
			LOGHTTPDLGLIST ("List of these dialogs belonging to source page (unique url word match)", vBetwDlgsFoundInSwfContext);
		}
		if (vBetwDlgsFoundInSwfContext.empty ())
		{
			FindDialogsByUniqueUrlWordsMatch2 (pszSwfUrl, vBetwDlgs, vBetwDlgsFoundInSwfContext);
			LOGHTTPDLGLIST ("List of these dialogs belonging to source page (unique url words2 match)", vBetwDlgsFoundInSwfContext);
		}

		if (!vBetwDlgsFoundInSwfContext.empty ())
		{
			
			

			
			wstring wstrTitle;
			if (pszFlashVars)
				ExtractTitleFromFlashVars (pWebPage, pszFlashVars, wstrTitle);
			if (wstrTitle.empty () && pWebPage)
				ExtractTitleFromHtml (pWebPage, wstrTitle);

			
			for (size_t i = 0; i < vBetwDlgsFoundInSwfContext.size (); i++)
			{
				
				for (size_t j = 0; j < vBetwDlgs2.size (); j++)
				{
					if (vBetwDlgs2 [j] == vBetwDlgsFoundInSwfContext [i])
					{
						
						HTTPDLG pBetwDlg = vBetwDlgs2 [j];

						
						for (size_t k = 0; k < vBetwToFlvMap [j].size (); k++)
						{
							HTTPDLG pFlvDlg = vBetwToFlvMap [j][k];

							
							wstring wstrTitle2;
							if (pBetwDlg->enCT == vmsHttpTrafficCollector::HttpDialog::X_WWW_FORM_URL_ENCODED)
								ExtractTitleFromXWwwFormUrlEncoded (pBetwDlg, wstrTitle2);
							else if (pBetwDlg->enCT == vmsHttpTrafficCollector::HttpDialog::XML)
								ExtractTitleFromXml (pBetwDlg, wstrTitle2, pFlvDlg);

							if (wstrTitle2.empty ())
								wstrTitle2 = wstrTitle;

							AddFlvDownload (pWebPage, pFlvDlg, wstrTitle2.c_str ());
						}
						break;
					}
				}
			}

			return;
		}
	}
}
HRESULT vmsHttpFlvTrafficAnalyzer4::FindFlvDownloads(LPCSTR pszHtmlPageUrl, const vmsHttpTrafficCollector *pTraffic)
{
    LOGFN ("vmsHttpFlvTrafficAnalyzer4::FindFlvDownloads");

    m_vDownloads.clear ();

    const vmsHttpTrafficCollector::HttpDialog* pWebPage =
        pTraffic->FindDialogByRequestUrl (pszHtmlPageUrl, vmsHttpTrafficCollector::HttpDialog::HTML);

    if (!pWebPage)
        return E_INVALIDARG;

    if (pWebPage->vbResponseBody.empty ())
        return S_FALSE;

    string strPageText;
    strPageText.assign ((LPSTR)&pWebPage->vbResponseBody [0], pWebPage->vbResponseBody.size ());

    int nStartIndex = pTraffic->getHttpDialogIndex (pWebPage) + 1;

    HTTPDLGLIST vFlvs;
    pTraffic->GetHttpDialogs (nStartIndex, -1,
                              vmsHttpTrafficCollector::HttpDialog::FLV, vFlvs);

    HTTPDLGLIST vFlvs2;


    FindDialogsByUrlExactMatch (strPageText.c_str (), vFlvs, vFlvs2);
    if (vFlvs2.empty ())
        FindDialogsByFileUrlMatch (strPageText.c_str (), vFlvs, vFlvs2);

    if (!vFlvs2.empty ())
    {

        wstring wstrTitle;
        ExtractTitleFromHtml (pWebPage, wstrTitle);
        for (size_t i = 0; i < vFlvs2.size (); i++)
            AddFlvDownload (pWebPage, vFlvs2 [i], wstrTitle.c_str ());
        return S_OK;
    }



    string strBody;
    strBody.assign ((LPSTR)&pWebPage->vbResponseBody [0], pWebPage->vbResponseBody.size ());



    FindFlvDownloadInInBetweenDlgs (pTraffic, pWebPage, NULL, NULL, strBody.c_str (), vFlvs, FDT_EXACTURLMATCH);
    if (!m_vDownloads.empty ())
        return S_OK;



    FindFlvDownloadInInBetweenDlgs (pTraffic, pWebPage, NULL, NULL, strBody.c_str (), vFlvs, FDT_FILEURLMATCH);
    if (!m_vDownloads.empty ())
        return S_OK;




    FindDialogsByUniqueUrlWordsMatch (strBody.c_str (), vFlvs, vFlvs2);
    if (vFlvs2.empty ())
        FindDialogsByUniqueUrlWordsMatch2 (pWebPage->strRequestUrl.c_str (), vFlvs, vFlvs2);
    if (!vFlvs2.empty ())
    {
        wstring wstrTitle;
        ExtractTitleFromHtml (pWebPage, wstrTitle);
        for (size_t i = 0; i < vFlvs2.size (); i++)
            AddFlvDownload (pWebPage, vFlvs2 [i], wstrTitle.c_str ());
        return S_OK;
    }


    FindFlvDownloadInInBetweenDlgs (pTraffic, pWebPage, NULL, NULL, strBody.c_str (), vFlvs, FDT_BYUNIQUEWORDS);
    if (!m_vDownloads.empty ())
        return S_OK;

    return m_vDownloads.empty () ? S_FALSE : S_OK;
}