Chinaunix首页 | 论坛 | 博客
  • 博客访问: 642925
  • 博文数量: 133
  • 博客积分: 1566
  • 博客等级: 上尉
  • 技术积分: 1230
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-01 09:31
文章分类

全部博文(133)

文章存档

2019年(1)

2018年(1)

2017年(8)

2016年(9)

2015年(17)

2014年(4)

2013年(31)

2012年(25)

2011年(36)

2010年(1)

我的朋友

分类: C/C++

2013-04-18 11:38:44


点击(此处)折叠或打开

  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. #include "../NSimple.h"
  3. #include "../class/stdEx.h"

  4. /*
  5. Yofoo-HtmlFilter
  6. Code By    Yofoo
  7. Last Edit: 2010-12-10
  8. */

  9. #define MIDL_DEFINE_GUID(type,name,l,w1,w2,b1,b2,b3,b4,b5,b6,b7,b8) /
  10.     const type name = {l,w1,w2,{b1,b2,b3,b4,b5,b6,b7,b8}}

  11. MIDL_DEFINE_GUID(CLSID, CLSID_HtmlFilter, 0x6F666F59, 0x2D6F, 0x7448, 0x6D, 0x6C, 0x46, 0x69, 0x6C, 0x74, 0x65, 0x72);


  12. #if 0
  13. #define MEM_TRACE        ATLTRACE
  14. #else
  15. #define MEM_TRACE        __noop
  16. #endif

  17. #if 0
  18. #define FLT_TRACE        ATLTRACE
  19. #else
  20. #define FLT_TRACE        __noop
  21. #endif

  22. #define FunInlineDefine        {return S_OK;}

  23. class CoInterface : public IClassFactory
  24. {
  25. public:
  26.     STDMETHOD(GetTypeInfoCount)(unsigned int*)FunInlineDefine
  27.     STDMETHOD(GetTypeInfo)(unsigned int, LCID, ITypeInfo**)FunInlineDefine
  28.     STDMETHOD(GetIDsOfNames)(REFIID, LPOLESTR*, unsigned int, LCID, DISPID*)FunInlineDefine
  29.     STDMETHOD(Invoke)(DISPID, REFIID, LCID, unsigned short, DISPPARAMS*,
  30.         VARIANT*, EXCEPINFO*, unsigned int*)FunInlineDefine

  31.     STDMETHOD(Start)(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo, DWORD grfPI, HANDLE_PTR dwReserved)FunInlineDefine
  32.     STDMETHOD(Continue)(PROTOCOLDATA *pProtocolData)FunInlineDefine
  33.     STDMETHOD(Abort)(HRESULT hrReason, DWORD dwOptions)FunInlineDefine
  34.     STDMETHOD(Terminate)(DWORD dwOptions)FunInlineDefine
  35.     STDMETHOD(Suspend)()FunInlineDefine
  36.     STDMETHOD(Resume)()FunInlineDefine

  37.     STDMETHOD(Read)(void *pv, ULONG cb, ULONG *pcbRead)FunInlineDefine
  38.     STDMETHOD(Seek)(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)FunInlineDefine
  39.     STDMETHOD(UnlockRequest)()FunInlineDefine
  40.     STDMETHOD(LockRequest)(DWORD dwOptions)FunInlineDefine

  41.     STDMETHOD (ReportData)( DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)FunInlineDefine
  42.     STDMETHOD (ReportProgress)( ULONG ulStatusCode, LPCWSTR szStatusText)FunInlineDefine
  43.     STDMETHOD (ReportResult)( HRESULT hrResult, DWORD dwError, LPCWSTR szResult)FunInlineDefine
  44.     STDMETHOD (Switch)( PROTOCOLDATA *pProtocolData)FunInlineDefine

  45.     STDMETHOD (SetPriority)(LONG nPriority)FunInlineDefine
  46.     STDMETHOD (GetPriority)(LONG * pnPriority)FunInlineDefine

  47.     virtual ~CoInterface(){}
  48. };

  49. class CHtmlFilter;
  50. //#define FunInlineDefine        ;

  51. #define DefAddRefFun    /

  52. #define Co_AgnetFun0(_CoVar, _FunRet, _FunName)    /
  53.     _FunRet STDMETHODCALLTYPE _FunName()/
  54.     {/
  55.         return _CoVar->_FunName();/
  56.     }
  57. #define Co_AgnetFun1(_CoVar, _FunRet, _FunName, _P1Type, _P1)    /
  58.     _FunRet STDMETHODCALLTYPE _FunName(_P1Type _P1)/
  59.     {/
  60.         return _CoVar->_FunName(_P1);/
  61.     }
  62. #define Co_AgnetFun2(_CoVar, _FunRet, _FunName, _P1Type, _P1, _P2Type, _P2)    /
  63.     _FunRet STDMETHODCALLTYPE _FunName(_P1Type _P1, _P2Type _P2)/
  64.     {/
  65.         return _CoVar->_FunName(_P1, _P2);/
  66.     }
  67. #define Co_AgnetFun3(_CoVar, _FunRet, _FunName, _P1Type, _P1, _P2Type, _P2, _P3Type, _P3)    /
  68.     _FunRet STDMETHODCALLTYPE _FunName(_P1Type _P1, _P2Type _P2, _P3Type _P3)/
  69.     {/
  70.         return _CoVar->_FunName(_P1, _P2, _P3);/
  71.     }
  72. #define Co_AgnetFun4(_CoVar, _FunRet, _FunName, _P1Type, _P1, _P2Type, _P2, _P3Type, _P3, _P4Type, _P4)    /
  73.     _FunRet STDMETHODCALLTYPE _FunName(_P1Type _P1, _P2Type _P2, _P3Type _P3, _P4Type _P4)/
  74.     {/
  75.         return _CoVar->_FunName(_P1, _P2, _P3, _P4);/
  76.     }
  77. #define Co_AgnetFun5(_CoVar, _FunRet, _FunName, _P1Type, _P1, _P2Type, _P2, _P3Type, _P3, _P4Type, _P4, _P5Type, _P5)    /
  78.     _FunRet STDMETHODCALLTYPE _FunName(_P1Type _P1, _P2Type _P2, _P3Type _P3, _P4Type _P4, _P5Type _P5)/
  79.     {/
  80.         return _CoVar->_FunName(_P1, _P2, _P3, _P4, _P5);/
  81.     }
  82. #define Co_AgnetFun8(_CoVar, _FunRet, _FunName, _P1Type, _P1, _P2Type, _P2, _P3Type, _P3, _P4Type, _P4, _P5Type, _P5, _P6Type, _P6, _P7Type, _P7, _P8Type, _P8)    /
  83.     _FunRet STDMETHODCALLTYPE _FunName(_P1Type _P1, _P2Type _P2, _P3Type _P3, _P4Type _P4, _P5Type _P5, _P6Type _P6, _P7Type _P7, _P8Type _P8)/
  84.     {/
  85.         return _CoVar->_FunName(_P1, _P2, _P3, _P4, _P5, _P6, _P7, _P8);/
  86.     }

  87. #define CoAgentJmp0(_CoVar, _FunRet, _FunName)    /
  88.     _FunRet STDMETHODCALLTYPE _FunName()/
  89.     {/
  90.         __asm jmp _CoVar->_FunName;/
  91.     }

  92. class CHtmlFilter_IDispatch: public IDispatch
  93. {
  94. public:
  95.     CHtmlFilter_IDispatch(CoInterface *pFacClass)
  96.     {
  97.         m_pFacClass = pFacClass;
  98.     }
  99.     CoInterface        *m_pFacClass;

  100.     Co_AgnetFun2(m_pFacClass, HRESULT, QueryInterface, REFIID, riid, LPVOID*, ppv);
  101.     Co_AgnetFun0(m_pFacClass, ULONG, AddRef);
  102.     Co_AgnetFun0(m_pFacClass, ULONG, Release);

  103.     Co_AgnetFun1(m_pFacClass, HRESULT, GetTypeInfoCount, unsigned int*, pCount);
  104.     Co_AgnetFun3(m_pFacClass, HRESULT, GetTypeInfo, unsigned int, iTInfo, LCID, lcid, ITypeInfo**, ppTInfo);
  105.     Co_AgnetFun5(m_pFacClass, HRESULT, GetIDsOfNames, REFIID, riid, LPOLESTR *, rgszNames, UINT, cNames, LCID, lcid, DISPID *, rgDispId);
  106.     Co_AgnetFun8(m_pFacClass, HRESULT, Invoke, DISPID, dispIdMember, REFIID, riid, LCID, lcid, WORD, wFlags, DISPPARAMS *, pDispParams, VARIANT *, pVarResult, EXCEPINFO *, pExcepInfo, UINT *, puArgErr);
  107. };

  108. class CHtmlFilter_IInternetPriority : public IInternetPriority
  109. {
  110. public:
  111.     CHtmlFilter_IInternetPriority(CoInterface *pFacClass)
  112.     {
  113.         m_pFacClass = pFacClass;
  114.     }
  115.     CoInterface        *m_pFacClass;

  116.     Co_AgnetFun2(m_pFacClass, HRESULT, QueryInterface, REFIID, riid, LPVOID*, ppv);
  117.     Co_AgnetFun0(m_pFacClass, ULONG, AddRef);
  118.     Co_AgnetFun0(m_pFacClass, ULONG, Release);

  119.     Co_AgnetFun1(m_pFacClass, HRESULT, SetPriority, LONG, nPriority);
  120.     Co_AgnetFun1(m_pFacClass, HRESULT, GetPriority, LONG *, pnPriority);
  121. };

  122. class CHtmlFilter_IInternetProtocol: public IInternetProtocol
  123. {
  124. public:
  125.     CHtmlFilter_IInternetProtocol(CoInterface * pFacClass)
  126.     {
  127.         m_pFacClass = pFacClass;
  128.     }
  129.     CoInterface        *m_pFacClass;

  130.     Co_AgnetFun2(m_pFacClass, HRESULT, QueryInterface, REFIID, riid, LPVOID*, ppv);
  131.     Co_AgnetFun0(m_pFacClass, ULONG, AddRef);
  132.     Co_AgnetFun0(m_pFacClass, ULONG, Release);

  133.     //    IInternetProtocolRoot
  134.     Co_AgnetFun5(m_pFacClass, HRESULT, Start, LPCWSTR, szUrl,IInternetProtocolSink *, pOIProtSink,
  135.         IInternetBindInfo *, pOIBindInfo, DWORD, grfPI, HANDLE_PTR, dwReserved);
  136.     Co_AgnetFun1(m_pFacClass, HRESULT, Continue, PROTOCOLDATA *, pProtocolData);
  137.     Co_AgnetFun2(m_pFacClass, HRESULT, Abort, HRESULT, hrReason, DWORD, dwOptions);
  138.     Co_AgnetFun1(m_pFacClass, HRESULT, Terminate, DWORD, dwOptions);
  139.     Co_AgnetFun0(m_pFacClass, HRESULT, Suspend);
  140.     Co_AgnetFun0(m_pFacClass, HRESULT, Resume);

  141.     //    IInternetProtocol
  142.     Co_AgnetFun3(m_pFacClass, HRESULT, Read, void *, pv, ULONG, cb, ULONG *, pcbRead);
  143.     Co_AgnetFun3(m_pFacClass, HRESULT, Seek, LARGE_INTEGER, dlibMove, DWORD, dwOrigin, ULARGE_INTEGER *, plibNewPosition);
  144.     Co_AgnetFun0(m_pFacClass, HRESULT, UnlockRequest);
  145.     Co_AgnetFun1(m_pFacClass, HRESULT, LockRequest, DWORD, dwOptions);
  146. };

  147. class CHtmlFilter_IInternetProtocolSink: public IInternetProtocolSink
  148. {
  149. public:
  150.     CHtmlFilter_IInternetProtocolSink(CoInterface *pFacClass)
  151.     {
  152.         m_pFacClass = pFacClass;
  153.     }
  154.     CoInterface        *m_pFacClass;

  155.     Co_AgnetFun2(m_pFacClass, HRESULT, QueryInterface, REFIID, riid, LPVOID*, ppv);
  156.     Co_AgnetFun0(m_pFacClass, ULONG, AddRef);
  157.     Co_AgnetFun0(m_pFacClass, ULONG, Release);

  158.     Co_AgnetFun3(m_pFacClass, HRESULT, ReportData, DWORD, grfBSCF, ULONG, ulProgress, ULONG, ulProgressMax);
  159.     Co_AgnetFun2(m_pFacClass, HRESULT, ReportProgress, ULONG, ulStatusCode, LPCWSTR, szStatusText);
  160.     Co_AgnetFun3(m_pFacClass, HRESULT, ReportResult, HRESULT, hrResult, DWORD, dwError, LPCWSTR, szResult);
  161.     Co_AgnetFun1(m_pFacClass, HRESULT, Switch, PROTOCOLDATA *, pProtocolData);
  162. };

  163. typedef enum _FilterType
  164. {
  165.     Filter_namespcae    = 0x1000,
  166.     Filter_Mime            = 0x2000
  167. }FilterType;

  168. class CHtmlFilter: public CoInterface
  169. {
  170. public:
  171.     CHtmlFilter(int nFilterType = Filter_Mime):
  172.      m_xIDispatch(this),
  173.      m_xIInternetProtocol(this),
  174.      m_xIInternetProtocolSink(this),
  175.      m_xIInternetPriority(this)
  176.     {
  177.         m_nRefCount = 0;
  178.         m_nFilterType = nFilterType;
  179.     }

  180.     virtual ~CHtmlFilter()
  181.     {

  182.     }

  183.     CHtmlFilter_IDispatch                m_xIDispatch;
  184.     CHtmlFilter_IInternetProtocol        m_xIInternetProtocol;
  185.     CHtmlFilter_IInternetProtocolSink    m_xIInternetProtocolSink;
  186.     CHtmlFilter_IInternetPriority        m_xIInternetPriority;


  187.     CComPtr<IInternetProtocol>        m_UrlMonProtocol;
  188.     CComPtr<IInternetProtocolSink>    m_UrlMonProtocolSink;
  189.     std::wstring                    m_sCacheFilePath;
  190.     std::wstring                    m_sCurUrl;
  191.     std::wstring                    m_sCurMime;

  192.     LONG            m_nRefCount;
  193.     int            m_nFilterType;

  194.     STDMETHOD(QueryInterface)(REFIID riid, LPVOID* ppv)
  195.     {
  196.         if(IsEqualIID(riid, IID_IUnknown))
  197.         {
  198.             *ppv = this;
  199.             AddRef();
  200.         }
  201.         else if(IsEqualIID(riid, IID_IDispatch))
  202.         {
  203.             *ppv = &m_xIDispatch;
  204.         }
  205.         else if(IsEqualIID(riid, IID_IInternetProtocol))
  206.         {
  207.             *ppv = &m_xIInternetProtocol;
  208.         }
  209.         else if(IsEqualIID(riid, IID_IInternetProtocolSink))
  210.         {
  211.             *ppv = &m_xIInternetProtocolSink;
  212.         }
  213.         else if(IsEqualIID(riid, IID_IInternetPriority))
  214.         {
  215.             *ppv = &m_xIInternetPriority;
  216.         }
  217.         else
  218.         {
  219.             assert(0);
  220.             return ResultFromScode(E_NOINTERFACE);
  221.         }
  222.         AddRef();
  223.         return S_OK;
  224.     }

  225.     STDMETHOD_(ULONG, AddRef)()
  226.     {
  227.         return InterlockedIncrement(&m_nRefCount);
  228.     }
  229.     STDMETHOD_(ULONG, Release)()
  230.     {
  231.         int        nRef;

  232.         nRef = InterlockedDecrement(&m_nRefCount);
  233.         if(nRef <= 0)
  234.         {
  235.             MEM_TRACE("delete: %08X/n", this);
  236.             delete this;
  237.         }

  238.         return nRef;
  239.     }
  240.     virtual /* [local] */ HRESULT STDMETHODCALLTYPE CreateInstance(
  241.         /* [unique][in] */ IUnknown *pUnkOuter,
  242.         /* [in] */ REFIID riid,
  243.         /* [iid_is][out] */ void **ppvObject)
  244.     {
  245.         CHtmlFilter        *pFacClass;
  246.         HRESULT            hr;

  247.         pFacClass = new CHtmlFilter(m_nFilterType);
  248.         MEM_TRACE("New: %08X/n", pFacClass);
  249.         hr = pFacClass->QueryInterface(riid, ppvObject);
  250.         if(FAILED(hr))
  251.             delete pFacClass;

  252.         return hr;
  253.     }

  254.     virtual /* [local] */ HRESULT STDMETHODCALLTYPE LockServer(
  255.     /* [in] */ BOOL fLock)
  256.     {
  257.         return S_OK;
  258.     }


  259. public:
  260.     //    IInternetProtocolRoot
  261.     STDMETHOD(Start)(LPCWSTR szUrl,    IInternetProtocolSink *pOIProtSink,    IInternetBindInfo *pOIBindInfo,
  262.         DWORD grfPI, HANDLE_PTR dwReserved)
  263.     {
  264. #define        OT_MAX        64

  265.         HRESULT        hr;
  266.         BINDINFO    BdInfo;
  267.         ULONG        uFetched;
  268.         LPOLESTR    pzUrl[OT_MAX+1];
  269.         DWORD        dwBdf;
  270.         PROTOCOLFILTERDATA    *pFiltDat;

  271.         m_UrlMonProtocolSink = pOIProtSink;

  272.         memset(pzUrl, 0, sizeof(pzUrl));
  273.         hr = pOIBindInfo->GetBindString(BINDSTRING_ACCEPT_MIMES, pzUrl, OT_MAX, &uFetched);
  274.         if(uFetched)
  275.         {
  276.             if(wcscmp(pzUrl[0], L"*/*") != 0)
  277.                 FLT_TRACE("BINDSTRING_ACCEPT_MIMES: %S/n", pzUrl[0]);
  278.         }

  279.         memset(pzUrl, 0, sizeof(pzUrl));
  280.         hr = pOIBindInfo->GetBindString(BINDSTRING_USER_AGENT, pzUrl, OT_MAX, &uFetched);
  281.         if(uFetched && pzUrl[0])
  282.             FLT_TRACE("BINDSTRING_USER_AGENT: %S/n", pzUrl[0]);
  283.         memset(pzUrl, 0, sizeof(pzUrl));
  284.         hr = pOIBindInfo->GetBindString(BINDSTRING_POST_COOKIE, pzUrl, OT_MAX, &uFetched);
  285.         if(uFetched && pzUrl[0])
  286.             FLT_TRACE("BINDSTRING_POST_COOKIE: %S/n", pzUrl[0]);
  287.         memset(pzUrl, 0, sizeof(pzUrl));
  288.         hr = pOIBindInfo->GetBindString(BINDSTRING_POST_DATA_MIME, pzUrl, OT_MAX, &uFetched);
  289.         if(uFetched && pzUrl[0])
  290.             FLT_TRACE("BINDSTRING_POST_DATA_MIME: %S/n", pzUrl[0]);

  291.         memset(&BdInfo, 0, sizeof(BdInfo));
  292.         BdInfo.cbSize = sizeof(BdInfo);
  293.         hr = pOIBindInfo->GetBindInfo(&dwBdf, &BdInfo);

  294.         if(m_nFilterType & Filter_namespcae)
  295.         {
  296.             CHAR            szCurUrl[1024];

  297.             FLT_TRACE("IInternetProtocolRoot::Start: %S/n", szUrl);
  298.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  299.             NStr::WChar2Char((WCHAR *)szUrl, szCurUrl, sizeof(szCurUrl));
  300.             if(NStr::StrTailCmp(szCurUrl, ".js") == 0 ||
  301.                 NStr::StrTailCmp(szCurUrl, ".jpg") == 0 ||
  302.                 NStr::StrTailCmp(szCurUrl, ".swf") == 0 ||
  303.                 0)
  304.             {
  305.                 m_UrlMonProtocolSink->ReportResult(S_OK, 0, NULL);
  306.                 m_UrlMonProtocol.Release();
  307.                 return S_OK;
  308.             }

  309.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
  310.         }

  311.         pFiltDat = (PROTOCOLFILTERDATA *)dwReserved;
  312.         hr = pOIProtSink->QueryInterface(IID_IInternetProtocol, (void**)&m_UrlMonProtocol);
  313.         ClearBuffer();

  314.         memset(pzUrl, 0, sizeof(pzUrl));
  315.         m_sCurMime = szUrl;

  316.         try
  317.         {
  318.             hr = pOIBindInfo->GetBindString(BINDSTRING_URL, pzUrl, OT_MAX, &uFetched);
  319.             m_sCurUrl = pzUrl[0];
  320.             FLT_TRACE("IInternetProtocolRoot::Start: %S, %S/n", szUrl, m_sCurUrl.c_str());
  321.         }
  322.         catch (CMemoryException* e)
  323.         {
  324.         }
  325.         catch (...)
  326.         {
  327.         }

  328.         return S_OK;
  329.     }
  330.     STDMETHOD(Continue)(PROTOCOLDATA * pProtocolData)
  331.     {
  332.         if(m_nFilterType & Filter_namespcae)
  333.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  334.         HRESULT hr;
  335.         hr = m_UrlMonProtocol->Continue(pProtocolData);
  336.         return hr;
  337.     }
  338.     STDMETHOD(Abort)(HRESULT hrReason, DWORD dwOptions)
  339.     {
  340.         if(m_nFilterType & Filter_namespcae)
  341.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  342.         HRESULT hr;
  343.         hr = m_UrlMonProtocol->Abort(hrReason, dwOptions);
  344.         return hr;
  345.     }
  346.     STDMETHOD(Terminate)(DWORD dwOptions)
  347.     {
  348.         if(m_nFilterType & Filter_namespcae)
  349.             return S_OK;

  350.         HRESULT hr;
  351.         ClearBuffer();
  352.         hr = m_UrlMonProtocol->Terminate(dwOptions);
  353.         return hr;
  354.     }
  355.     STDMETHOD(Suspend)()
  356.     {
  357.         if(m_nFilterType & Filter_namespcae)
  358.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  359.         HRESULT hr;
  360.         hr = m_UrlMonProtocol->Suspend();
  361.         return hr;
  362.     }
  363.     STDMETHOD(Resume)()
  364.     {
  365.         if(m_nFilterType & Filter_namespcae)
  366.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  367.         HRESULT hr;
  368.         hr = m_UrlMonProtocol->Resume();
  369.         return hr;
  370.     }

  371.     //    IInternetProtocol
  372.     ByteMem            m_ReadBuffer;
  373.     ByteMem            m_OneBuffer;
  374.     BOOL            m_bReadFull;

  375.     BOOL    ClearBuffer()
  376.     {
  377.         m_ReadBuffer.clear();
  378.         m_OneBuffer.resize(16*1024);
  379.         m_bReadFull = FALSE;
  380.         return TRUE;
  381.     }


  382.     //    <mce:script .... </script><!--
  383.     替换成空格
  384.     static BOOL WINAPI LoopFindPro(CHAR *pFind, VOID *pParam)
  385.     {
  386.         CHAR    *pEnd;
  387.         int        nSize;
  388.         CHAR    *pTailStr;

  389.         pTailStr = "
  390. // -->";
  391.         pEnd = StrStrIA(pFind, pTailStr);
  392.         if(pEnd == NULL)
  393.             return FALSE;

  394.         nSize = (int)(pEnd - pFind + strlen(pTailStr));
  395.         memset(pFind, ' ', nSize);
  396.         return TRUE;
  397.     }

  398.     BOOL    ProcessBuffer()
  399.     {
  400.         WCHAR        *pUrl;
  401.         CHAR        *pData;
  402.         DWORD        dwSize;

  403.         dwSize = (DWORD)m_ReadBuffer.size();
  404.         m_ReadBuffer.append((BYTE *)"", 1);    //    添加字符串结束标记
  405.         pData = (CHAR*)m_ReadBuffer.getptr();
  406.         pUrl = (WCHAR *)m_sCurUrl.c_str();
  407.         if(dwSize <= 0)
  408.             return TRUE;

  409.         if(wcsicmp(m_sCurMime.c_str(), L"text/html") != 0 &&
  410.             wcsicmp(m_sCurMime.c_str(), L"application/x-javascript") != 0 &&
  411.             wcsicmp(m_sCurMime.c_str(), L"text/css") != 0 &&
  412.             1)
  413.             return TRUE;

  414.         if(NStr::StrHeadCmp((WCHAR *)m_sCurUrl.c_str(), L"res://") == 0)
  415.         {
  416.             NStr::StrReplace(pData, "宋体", "雅黑");
  417.             return TRUE;
  418.         }

  419. //        return TRUE;
  420.         //    自己在这里处理
  421. //        NStr::LoopFindNoCaseString(LoopFindPro, pData, ", pData);    //简单去掉脚本元素
  422.         CHAR        szOld[64];
  423.         CHAR        szNew[64];
  424.         int            nCount;

  425.         nCount = 0;
  426.         nCount += NStr::StrReplace(pData, "天涯", "太阳");        //    普通编码

  427.         NStr::CodePageConvert(936, CP_UTF8, "天涯", szOld, sizeof(szOld));
  428.         NStr::CodePageConvert(936, CP_UTF8, "太阳", szNew, sizeof(szNew));
  429.         nCount += NStr::StrReplace(pData, szOld, szNew);        //    UTF8编码

  430.         NStr::String2HtmTranMean("天涯", szOld);
  431.         NStr::String2HtmTranMean("太阳", szNew);
  432.         nCount += NStr::StrReplace(pData, szOld, szNew);        //    Htm转义字符10进制

  433.         NStr::String2HtmTranMean("天涯", szOld, TRUE);
  434.         NStr::String2HtmTranMean("太阳", szNew, TRUE);
  435.         nCount += NStr::StrIReplace(pData, szOld, szNew);        //    Htm转义字符16进制


  436.         nCount += NStr::StrReplace(pData, "宋体", "雅黑");        //    普通编码

  437.         CHAR *pFind = strstr(pData, "宋体");
  438.         if(pFind)
  439.             TRACE("Find: 宋体/n");

  440.         FLT_TRACE("Replace: %d, %S/n", nCount, m_sCurUrl.c_str());

  441.         //    处理完毕
  442.         m_ReadBuffer.pop_back();            //    删除添加的字符串结束标记

  443.         return TRUE;
  444.     }

  445.     BOOL    RealFulltoBuffer()
  446.     {
  447.         HRESULT            hr;
  448.         DWORD            dwRead;

  449.         if(m_bReadFull)
  450.             return TRUE;

  451.         for(;;)
  452.         {
  453.             dwRead = 0;
  454.             hr = m_UrlMonProtocol->Read(m_OneBuffer.getptr(), (ULONG)m_OneBuffer.size(), &dwRead);
  455.             if(dwRead != 0)
  456.                 m_ReadBuffer.append(m_OneBuffer.getptr(), dwRead);

  457.             if(hr == S_OK)
  458.                 continue;
  459.             else if(hr == S_FALSE)
  460.                 break;
  461.             else
  462.                 return FALSE;
  463.         }

  464.         m_bReadFull = TRUE;
  465.         ProcessBuffer();
  466.         return TRUE;
  467.     }

  468.     BOOL    ReadFromBuffer(void *pv, ULONG cb, ULONG *pcbRead)
  469.     {
  470.         if(m_ReadBuffer.size() <= 0)
  471.             return FALSE;

  472.         m_ReadBuffer.pop_head(pv, cb, (int *)pcbRead);
  473.         return TRUE;
  474.     }

  475.     STDMETHOD(Read)(void *pv, ULONG cb, ULONG *pcbRead)
  476.     {
  477.         if(m_nFilterType & Filter_namespcae)
  478.             return S_OK;

  479. //        return m_UrlMonProtocol->Read(pv, cb, pcbRead);

  480.         BOOL        bRetVal;

  481.         bRetVal = RealFulltoBuffer();
  482.         if(bRetVal == FALSE)
  483.         {
  484.             *pcbRead = 0;
  485.             return E_PENDING;
  486.         }

  487.         bRetVal = ReadFromBuffer(pv, cb, pcbRead);
  488.         if(bRetVal == FALSE)
  489.             return S_FALSE;

  490.         return S_OK;
  491.     }

  492.     STDMETHOD(Seek)(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
  493.     {
  494.         if(m_nFilterType & Filter_namespcae)
  495.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  496.         HRESULT            hr;
  497.         hr = m_UrlMonProtocol->Seek(dlibMove, dwOrigin, plibNewPosition);
  498.         return hr;
  499.     }
  500.     STDMETHOD(UnlockRequest)()
  501.     {
  502.         if(m_nFilterType & Filter_namespcae)
  503.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  504.         HRESULT            hr;
  505.         hr = m_UrlMonProtocol->UnlockRequest();
  506.         return hr;
  507.     }
  508.     STDMETHOD(LockRequest)(DWORD dwOptions)
  509.     {
  510.         if(m_nFilterType & Filter_namespcae)
  511.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  512.         HRESULT            hr;
  513.         hr = m_UrlMonProtocol->LockRequest(dwOptions);
  514.         return hr;
  515.     }

  516. //    IInternetProtocolSink
  517.     STDMETHOD (ReportData)( DWORD grfBSCF, ULONG ulProgress, ULONG ulProgressMax)
  518.     {
  519.         if(m_nFilterType & Filter_namespcae)
  520.             return INET_E_USE_DEFAULT_PROTOCOLHANDLER;

  521.         HRESULT            hr;
  522.         hr = m_UrlMonProtocolSink->ReportData(grfBSCF, ulProgress, ulProgressMax);
  523.         return hr;
  524.     }
  525.     STDMETHOD (ReportProgress)( ULONG ulStatusCode, LPCWSTR szStatusText)
  526.     {
  527.         HRESULT            hr;

  528.         switch(ulStatusCode)
  529.         {
  530.         case BINDSTATUS_DECODING:
  531.             FLT_TRACE("ReportProgress-BINDSTATUS_DECODING: %S/n", szStatusText);
  532.             break;
  533.         case BINDSTATUS_MIMETYPEAVAILABLE:
  534.             FLT_TRACE("ReportProgress-BINDSTATUS_MIMETYPEAVAILABLE: %S/n", szStatusText);
  535.             break;
  536.         case BINDSTATUS_CACHEFILENAMEAVAILABLE:
  537.             FLT_TRACE("ReportProgress-BINDSTATUS_CACHEFILENAMEAVAILABLE: %S/n", szStatusText);
  538.             m_sCacheFilePath = szStatusText;
  539.             break;
  540.         default:
  541.             FLT_TRACE("ReportProgress-Unknow: %S/n", szStatusText);
  542.         }
  543.         hr = m_UrlMonProtocolSink->ReportProgress(ulStatusCode, szStatusText);
  544.         return hr;
  545.     }
  546.     STDMETHOD (ReportResult)( HRESULT hrResult, DWORD dwError, LPCWSTR szResult)
  547.     {
  548.         HRESULT            hr;
  549.         hr = m_UrlMonProtocolSink->ReportResult(hrResult, dwError, szResult);
  550.         return hr;
  551.     }
  552.     STDMETHOD (Switch)( PROTOCOLDATA *pProtocolData)
  553.     {
  554.         HRESULT            hr;
  555.         hr = m_UrlMonProtocolSink->Switch(pProtocolData);
  556.         return hr;
  557.     }

  558.     STDMETHOD (SetPriority)(LONG nPriority)
  559.     {
  560.         return S_OK;
  561.     }

  562.     STDMETHOD (GetPriority)(LONG * pnPriority)
  563.     {
  564.         return S_OK;
  565.     }

  566. };


  567. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

阅读(1909) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~