Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/plugin/PluginWbPassThrough.cpp

Issue 6390087684194304: [superseded] Issue 1265 - Improve detection of mime type of the expected response (Closed)
Patch Set: Fix missed support of other blocked types Created Sept. 12, 2014, 7:54 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/plugin/PluginWbPassThrough.h ('k') | src/plugin/SinkPolicy.inl » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/plugin/PluginWbPassThrough.cpp
===================================================================
--- a/src/plugin/PluginWbPassThrough.cpp
+++ b/src/plugin/PluginWbPassThrough.cpp
@@ -9,11 +9,9 @@
#include "PluginSettings.h"
#include "PluginClass.h"
#include "PluginSystem.h"
-
+#include <WinInet.h>
#include "wtypes.h"
-EXTERN_C IMAGE_DOS_HEADER __ImageBase;
-
namespace
{
std::string g_myPageBlocked = "<!DOCTYPE html>"
@@ -24,7 +22,13 @@
"</html>";
}
-int WBPassthruSink::GetContentTypeFromMimeType(CString mimeType)
+WBPassthruSink::WBPassthruSink()
+ : m_currentPositionOfSentPage(0)
+ , m_contentType(CFilter::EContentType::contentTypeAny)
+{
+}
+
+int WBPassthruSink::GetContentTypeFromMimeType(const CString& mimeType)
{
if (mimeType.Find(L"image/") >= 0)
{
@@ -55,7 +59,7 @@
return CFilter::contentTypeAny;
}
-int WBPassthruSink::GetContentTypeFromURL(CString src)
+int WBPassthruSink::GetContentTypeFromURL(const CString& src)
{
CString srcExt = src;
@@ -93,14 +97,10 @@
{
return CFilter::contentTypeSubdocument;
}
- else
- {
- return CFilter::contentTypeAny & ~CFilter::contentTypeSubdocument;
- }
-
+ return CFilter::contentTypeAny;
}
-int WBPassthruSink::GetContentType(CString mimeType, CString domain, CString src)
+int WBPassthruSink::GetContentType(const CString& mimeType, const CString& domain, const CString& src)
{
// No referer or mime type
// BINDSTRING_XDR_ORIGIN works only for IE v8+
@@ -127,36 +127,32 @@
{
m_pTargetProtocol = pTargetProtocol;
bool isBlocked = false;
- m_currentPositionOfSentPage = 0;
- CString src;
- src.Append(szUrl);
+ CString src = szUrl;
DEBUG_GENERAL(src);
CPluginClient::UnescapeUrl(src);
- CString boundDomain;
CString mimeType;
- LPOLESTR mime[10];
if (pOIBindInfo)
{
ULONG resLen = 0;
// Apparently IE will report random mime type if there's more then 1 in the list.
// So we get the whole list and just use the first one (top priority one)
+ LPOLESTR mime[10];
pOIBindInfo->GetBindString(BINDSTRING_ACCEPT_MIMES, mime, 10, &resLen);
if (mime && resLen > 0)
{
mimeType.SetString(mime[0]);
}
- LPOLESTR bindToObject = 0;
+ LPOLESTR bindToObject = nullptr;
pOIBindInfo->GetBindString(BINDSTRING_FLAG_BIND_TO_OBJECT, &bindToObject, 1, &resLen);
- LPOLESTR domainRetrieved = 0;
+ LPOLESTR domainRetrieved = nullptr;
if (resLen == 0 || wcscmp(bindToObject, L"FALSE") == 0)
- {
+ {
HRESULT hr = pOIBindInfo->GetBindString(BINDSTRING_XDR_ORIGIN, &domainRetrieved, 1, &resLen);
-
if ((hr == S_OK) && domainRetrieved && (resLen > 0))
{
- boundDomain.SetString(domainRetrieved);
+ m_boundDomain = domainRetrieved;
}
}
}
@@ -166,12 +162,9 @@
ULONG len2 = 2048;
#ifdef SUPPORT_FILTER
- int contentType = CFilter::contentTypeAny;
-
CPluginTab* tab = CPluginClass::GetTab(::GetCurrentThreadId());
CPluginClient* client = CPluginClient::GetInstance();
-
if (tab && client)
{
CString documentUrl = tab->GetDocumentUrl();
@@ -182,104 +175,69 @@
}
else if (CPluginSettings::GetInstance()->IsPluginEnabled() && !client->IsWhitelistedUrl(std::wstring(documentUrl)))
{
- boundDomain = tab->GetDocumentUrl();
-
- contentType = CFilter::contentTypeAny;
-
+ m_boundDomain = tab->GetDocumentUrl();
+ m_contentType = CFilter::contentTypeAny;
#ifdef SUPPORT_FRAME_CACHING
- if ((tab != 0) && (tab->IsFrameCached(src)))
+ if (nullptr != tab && tab->IsFrameCached(src))
{
- contentType = CFilter::contentTypeSubdocument;
+ m_contentType = CFilter::contentTypeSubdocument;
}
else
#endif // SUPPORT_FRAME_CACHING
- contentType = GetContentType(mimeType, boundDomain, src);
- if (client->ShouldBlock(src, contentType, boundDomain, true))
{
- isBlocked = true;
-
- DEBUG_BLOCKER("Blocker::Blocking Http-request:" + src);
+ m_contentType = GetContentType(mimeType, m_boundDomain, src);
}
}
- if (!isBlocked)
- {
- DEBUG_BLOCKER("Blocker::Ignoring Http-request:" + src)
- }
+
}
-
- if (tab == NULL)
+ if (nullptr == tab)
Oleksandr 2014/10/01 09:15:39 I personally don't like yoda conditions, and I thi
{
- contentType = GetContentType(mimeType, boundDomain, src);
- if (client->ShouldBlock(src, contentType, boundDomain, true))
- {
- isBlocked = true;
- }
+ m_contentType = GetContentType(mimeType, m_boundDomain, src);
}
-#ifdef _DEBUG
- CString type;
+ if (nullptr != client
+ && CFilter::EContentType::contentTypeAny != m_contentType
+ && client->ShouldBlock(src, m_contentType, m_boundDomain, true))
Oleksandr 2014/10/01 09:15:39 Comment about yoda conditions applies here as well
+ {
+ isBlocked = true;
+ }
- if (contentType == CFilter::contentTypeDocument) type = "DOCUMENT";
- else if (contentType == CFilter::contentTypeObject) type = "OBJECT";
- else if (contentType == CFilter::contentTypeImage) type = "IMAGE";
- else if (contentType == CFilter::contentTypeScript) type = "SCRIPT";
- else if (contentType == CFilter::contentTypeOther) type = "OTHER";
- else if (contentType == CFilter::contentTypeUnknown) type = "OTHER";
- else if (contentType == CFilter::contentTypeSubdocument) type = "SUBDOCUMENT";
- else if (contentType == CFilter::contentTypeStyleSheet) type = "STYLESHEET";
- else type = "OTHER";
-
- if (isBlocked)
+ // For IE6 and earlier there is iframe back button issue, so avoid it.
+ if (isBlocked && client->GetIEVersion() > 6)
{
- CPluginDebug::DebugResultBlocking(type, src, boundDomain);
- }
- else
- {
- CPluginDebug::DebugResultIgnoring(type, src, boundDomain);
- }
-#endif
-
- //Fixes the iframe back button issue
- if (client->GetIEVersion() > 6)
- {
- if (contentType == CFilter::contentTypeImage && isBlocked)
+ handled = true;
+ if (CFilter::EContentType::contentTypeImage == m_contentType)
{
BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, pTargetProtocol);
- handled = true;
// IE shows a cross that img is not loaded
return INET_E_REDIRECT_FAILED;
}
- if (contentType == CFilter::contentTypeSubdocument && isBlocked)
+ if (CFilter::EContentType::contentTypeSubdocument == m_contentType)
{
PassthroughAPP::CustomSinkStartPolicy<WBPassthru, WBPassthruSink>::GetProtocol(this)->m_shouldSupplyCustomContent = true;
BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, pTargetProtocol);
-
m_spInternetProtocolSink->ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE, L"text/html");
m_spInternetProtocolSink->ReportData(BSCF_FIRSTDATANOTIFICATION, 0, static_cast<ULONG>(g_myPageBlocked.size()));
- handled = true;
return S_OK;
}
- if (contentType == CFilter::contentTypeScript && isBlocked)
+ if (CFilter::EContentType::contentTypeScript == m_contentType)
{
BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, pTargetProtocol);
m_spInternetProtocolSink->ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE, L"text/javascript");
m_spInternetProtocolSink->ReportResult(INET_E_REDIRECTING, 301, L"data:");
- handled = true;
return INET_E_REDIRECT_FAILED;
}
- if (contentType == CFilter::contentTypeXmlHttpRequest && isBlocked)
+ if (CFilter::EContentType::contentTypeXmlHttpRequest == m_contentType)
Oleksandr 2014/10/01 09:15:39 Looks like we don't need a special case for conten
{
BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, pTargetProtocol);
m_spInternetProtocolSink->ReportResult(INET_E_REDIRECTING, 301, L"data:");
- handled = true;
return INET_E_REDIRECT_FAILED;
}
- if (isBlocked)
+ if (CFilter::EContentType::contentTypeAny != m_contentType)
{
BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, pTargetProtocol);
- m_spInternetProtocolSink->ReportResult(S_FALSE, 0, L"");
- handled = true;
+ m_spInternetProtocolSink->ReportResult(INET_E_REDIRECTING, 301, L"data:");
Oleksandr 2014/10/01 09:15:39 I don't think redirecting all requests to 'data:'
return INET_E_REDIRECT_FAILED;
}
}
@@ -288,7 +246,6 @@
return isBlocked ? S_FALSE : BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, pTargetProtocol);
}
-
HRESULT WBPassthruSink::OnRead(void* pv, ULONG cb, ULONG* pcbRead)
{
if (nullptr == pv)
@@ -354,13 +311,60 @@
return m_spInternetProtocolSink ? m_spInternetProtocolSink->Switch(pProtocolData) : E_UNEXPECTED;
}
-STDMETHODIMP WBPassthruSink::BeginningTransaction(LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR *pszAdditionalHeaders)
+STDMETHODIMP WBPassthruSink::BeginningTransaction(LPCWSTR szURL, LPCWSTR szHeaders, DWORD dwReserved, LPWSTR* pszAdditionalHeaders)
{
if (pszAdditionalHeaders)
{
- *pszAdditionalHeaders = 0;
+ *pszAdditionalHeaders = nullptr;
}
+ CPluginClient* client = nullptr;
+ if (CFilter::EContentType::contentTypeAny == m_contentType && (client = CPluginClient::GetInstance()))
+ {
+ auto acceptHeader = [&]() -> std::string
+ {
+ ATL::CComPtr<IWinInetHttpInfo> winInetHttpInfo;
+ HRESULT hr = m_spTargetProtocol->QueryInterface(&winInetHttpInfo);
+ if(FAILED(hr))
+ {
+ return "";
+ }
+ DWORD size = 0;
+ DWORD flags = 0;
+ hr = winInetHttpInfo->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF | HTTP_QUERY_FLAG_REQUEST_HEADERS,
+ /*buffer*/nullptr, /* get size */&size, &flags, /*reserved*/ 0);
+ if(FAILED(hr))
+ {
+ return "";
+ }
+ std::string buf(size, '\0');
+ hr = winInetHttpInfo->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF | HTTP_QUERY_FLAG_REQUEST_HEADERS,
+ &buf[0], &size, &flags, 0);
+ if(FAILED(hr))
+ {
+ return "";
+ }
+ char acceptHeader[] = "Accept:";
+ auto acceptHeaderBeginsAt = buf.find(acceptHeader);
+ if (std::string::npos == acceptHeaderBeginsAt)
+ {
+ return "";
+ }
+ acceptHeaderBeginsAt += sizeof(acceptHeader);
+ auto acceptHeaderEndsAt = buf.find("\n", acceptHeaderBeginsAt);
+ if (std::string::npos == acceptHeaderEndsAt)
+ {
+ return "";
+ }
+ return buf.substr(acceptHeaderBeginsAt, acceptHeaderEndsAt - acceptHeaderBeginsAt);
+ }();
+ m_contentType = GetContentTypeFromMimeType(ATL::CString(acceptHeader.c_str()));
+ bool isBlocked = client->ShouldBlock(szURL, m_contentType, m_boundDomain, /*debug flag but must be set*/true);
+ if (isBlocked)
+ {
+ return E_ABORT;
+ }
+ }
CComPtr<IHttpNegotiate> spHttpNegotiate;
QueryServiceFromClient(&spHttpNegotiate);
return spHttpNegotiate ? spHttpNegotiate->BeginningTransaction(szURL, szHeaders,dwReserved, pszAdditionalHeaders) : S_OK;
« no previous file with comments | « src/plugin/PluginWbPassThrough.h ('k') | src/plugin/SinkPolicy.inl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld