LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
138 WBPassthruSink::WBPassthruSink() | 138 WBPassthruSink::WBPassthruSink() |
139 : m_currentPositionOfSentPage(0) | 139 : m_currentPositionOfSentPage(0) |
140 , m_contentType(ContentType::CONTENT_TYPE_OTHER) | 140 , m_contentType(ContentType::CONTENT_TYPE_OTHER) |
141 , m_isCustomResponse(false) | 141 , m_isCustomResponse(false) |
142 { | 142 { |
143 } | 143 } |
144 | 144 |
145 ContentType WBPassthruSink::GetContentTypeFromMimeType(const std::wstring& mimeT
ype) | 145 namespace |
146 { | 146 { |
147 if (mimeType.find(L"image/") != std::wstring::npos) | 147 /** |
148 { | 148 * Heuristic to infer an ABP content type from the media type list of an Accep
t: header field. |
149 return ContentType::CONTENT_TYPE_IMAGE; | 149 * |
150 } | 150 * Known IE Accept: strings for documents and images: |
151 if (mimeType.find(L"text/css") != std::wstring::npos) | 151 * text/html, application/xhtml+xml, image/jxr, *\/* |
152 { | 152 * image/png, image/svg+xml, image/jxr, image/*;q=0.8, *\/*;q = 0.5 |
153 return ContentType::CONTENT_TYPE_STYLESHEET; | 153 * In IE9 the image/jxr part is missing. |
154 } | 154 */ |
155 if ((mimeType.find(L"application/javascript") != std::wstring::npos) || (mimeT
ype.find(L"application/json") != std::wstring::npos)) | 155 ContentType InferContentTypeFromMediaTypeList(const std::wstring& mediaTypeLis
t) |
156 { | 156 { |
157 return ContentType::CONTENT_TYPE_SCRIPT; | 157 if ((mediaTypeList.find(L"text/html") != std::wstring::npos) || |
158 } | 158 (mediaTypeList.find(L"application/xhtml+xml") != std::wstring::npos)) |
159 if (mimeType.find(L"application/x-shockwave-flash") != std::wstring::npos) | 159 { |
160 { | 160 return ContentType::CONTENT_TYPE_SUBDOCUMENT; |
161 return ContentType::CONTENT_TYPE_OBJECT; | 161 } |
162 } | 162 if (mediaTypeList.find(L"image/") != std::wstring::npos) |
163 if (mimeType.find(L"text/html") != std::wstring::npos) | 163 { |
164 { | 164 return ContentType::CONTENT_TYPE_IMAGE; |
165 return ContentType::CONTENT_TYPE_SUBDOCUMENT; | 165 } |
166 } | 166 if (mediaTypeList.find(L"text/css") != std::wstring::npos) |
167 // It is important to have this check last, since it is rather generic, and mi
ght overlay text/html, for example | 167 { |
168 if (mimeType.find(L"xml") != std::wstring::npos) | 168 return ContentType::CONTENT_TYPE_STYLESHEET; |
169 { | 169 } |
170 return ContentType::CONTENT_TYPE_XMLHTTPREQUEST; | 170 if ((mediaTypeList.find(L"application/javascript") != std::wstring::npos) ||
(mediaTypeList.find(L"application/json") != std::wstring::npos)) |
171 } | 171 { |
172 | 172 return ContentType::CONTENT_TYPE_SCRIPT; |
173 return ContentType::CONTENT_TYPE_OTHER; | 173 } |
174 } | 174 if (mediaTypeList.find(L"application/x-shockwave-flash") != std::wstring::np
os) |
175 | 175 { |
176 ContentType WBPassthruSink::GetContentTypeFromURL(const std::wstring& src) | 176 return ContentType::CONTENT_TYPE_OBJECT; |
177 { | 177 } |
178 std::wstring schemeAndHierarchicalPart = GetSchemeAndHierarchicalPart(src); | 178 // It is important to have this check last, since it is rather generic, and
might overlay text/html, for example |
179 auto contentType = GetContentTypeFromString(schemeAndHierarchicalPart); | 179 if (mediaTypeList.find(L"xml") != std::wstring::npos) |
180 if (contentType == ContentType::CONTENT_TYPE_OTHER && | 180 { |
181 AdblockPlus::IE::InstalledMajorVersion() == 8) | 181 return ContentType::CONTENT_TYPE_XMLHTTPREQUEST; |
182 { | 182 } |
183 std::wstring queryString = GetQueryString(src); | 183 return ContentType::CONTENT_TYPE_OTHER; |
184 wchar_t* nextToken = nullptr; | 184 } |
185 const wchar_t* token = wcstok_s(&queryString[0], L"&=", &nextToken); | 185 |
186 while (token != nullptr) | 186 ContentType InferContentTypeFromUrl(const std::wstring& src) |
187 { | 187 { |
188 contentType = GetContentTypeFromString(token); | 188 std::wstring schemeAndHierarchicalPart = GetSchemeAndHierarchicalPart(src); |
189 if (contentType != ContentType::CONTENT_TYPE_OTHER) | 189 auto contentType = GetContentTypeFromString(schemeAndHierarchicalPart); |
| 190 if (contentType == ContentType::CONTENT_TYPE_OTHER && |
| 191 AdblockPlus::IE::InstalledMajorVersion() == 8) |
| 192 { |
| 193 std::wstring queryString = GetQueryString(src); |
| 194 wchar_t* nextToken = nullptr; |
| 195 const wchar_t* token = wcstok_s(&queryString[0], L"&=", &nextToken); |
| 196 while (token != nullptr) |
190 { | 197 { |
191 return contentType; | 198 contentType = GetContentTypeFromString(token); |
| 199 if (contentType != ContentType::CONTENT_TYPE_OTHER) |
| 200 { |
| 201 return contentType; |
| 202 } |
| 203 token = wcstok_s(nullptr, L"&=", &nextToken); |
192 } | 204 } |
193 token = wcstok_s(nullptr, L"&=", &nextToken); | 205 } |
194 } | 206 return contentType; |
195 } | 207 } |
196 return contentType; | 208 |
197 } | 209 ContentType InferContentType(const std::wstring& mediaTypeList, const std::wst
ring& domain, const std::wstring& src) |
198 | 210 { |
199 ContentType WBPassthruSink::GetContentType(const std::wstring& mimeType, const s
td::wstring& domain, const std::wstring& src) | 211 // No referer or mime type |
200 { | 212 // BINDSTRING_XDR_ORIGIN works only for IE v8+ |
201 // No referer or mime type | 213 if (mediaTypeList.empty() && domain.empty() && AdblockPlus::IE::InstalledMaj
orVersion() >= 8) |
202 // BINDSTRING_XDR_ORIGIN works only for IE v8+ | 214 { |
203 if (mimeType.empty() && domain.empty() && AdblockPlus::IE::InstalledMajorVersi
on() >= 8) | 215 return ContentType::CONTENT_TYPE_XMLHTTPREQUEST; |
204 { | 216 } |
205 return ContentType::CONTENT_TYPE_XMLHTTPREQUEST; | 217 ContentType contentType = InferContentTypeFromMediaTypeList(mediaTypeList); |
206 } | 218 if (contentType == ContentType::CONTENT_TYPE_OTHER) |
207 ContentType contentType = GetContentTypeFromMimeType(mimeType); | 219 { |
208 if (contentType == ContentType::CONTENT_TYPE_OTHER) | 220 contentType = InferContentTypeFromUrl(src); |
209 { | 221 } |
210 contentType = GetContentTypeFromURL(src); | 222 return contentType; |
211 } | 223 } |
212 return contentType; | |
213 } | 224 } |
214 | 225 |
215 ////////////////////////////////////////////////////////////////////////////////
//////// | 226 ////////////////////////////////////////////////////////////////////////////////
//////// |
216 //WBPassthruSink | 227 //WBPassthruSink |
217 //Monitor and/or cancel every request and responde | 228 //Monitor and/or cancel every request and responde |
218 //WB makes, including images, sounds, scripts, etc | 229 //WB makes, including images, sounds, scripts, etc |
219 ////////////////////////////////////////////////////////////////////////////////
//////// | 230 ////////////////////////////////////////////////////////////////////////////////
//////// |
220 HRESULT WBPassthruSink::OnStart(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSin
k, | 231 HRESULT WBPassthruSink::OnStart(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSin
k, |
221 IInternetBindInfo *pOIBindInfo, DWORD grfPI, HAN
DLE_PTR dwReserved, | 232 IInternetBindInfo *pOIBindInfo, DWORD grfPI, HAN
DLE_PTR dwReserved, |
222 IInternetProtocol* pTargetProtocol) | 233 IInternetProtocol* pTargetProtocol) |
223 { | 234 { |
224 m_pTargetProtocol = pTargetProtocol; | 235 m_pTargetProtocol = pTargetProtocol; |
225 return BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved,
pTargetProtocol); | 236 return BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved,
pTargetProtocol); |
226 } | 237 } |
227 | 238 |
228 HRESULT WBPassthruSink::OnRead(void* pv, ULONG cb, ULONG* pcbRead) | 239 HRESULT WBPassthruSink::OnRead(void* pv, ULONG cb, ULONG* pcbRead) |
229 { | 240 { |
230 if (!pv || !pcbRead) | 241 if (!pv || !pcbRead) |
231 { | 242 { |
232 return E_POINTER; | 243 return E_POINTER; |
233 } | 244 } |
234 *pcbRead = 0; | 245 *pcbRead = 0; |
235 | 246 |
236 if (PassthroughAPP::CustomSinkStartPolicy<WBPassthru, WBPassthruSink>::GetProt
ocol(this)->m_shouldSupplyCustomContent) | 247 if (PassthroughAPP::CustomSinkStartPolicy<WbPassthroughProtocol, WBPassthruSin
k>::GetProtocol(this)->m_shouldSupplyCustomContent) |
237 { | 248 { |
238 ULONG blockedByABPPageSize = static_cast<ULONG>(g_blockedByABPPage.size()); | 249 ULONG blockedByABPPageSize = static_cast<ULONG>(g_blockedByABPPage.size()); |
239 auto positionGrow = std::min<ULONG>(cb, static_cast<ULONG>(blockedByABPPageS
ize - m_currentPositionOfSentPage)); | 250 auto positionGrow = std::min<ULONG>(cb, static_cast<ULONG>(blockedByABPPageS
ize - m_currentPositionOfSentPage)); |
240 if (positionGrow == 0) { | 251 if (positionGrow == 0) { |
241 return S_FALSE; | 252 return S_FALSE; |
242 } | 253 } |
243 std::copy(g_blockedByABPPage.begin(), g_blockedByABPPage.begin() + positionG
row, | 254 std::copy(g_blockedByABPPage.begin(), g_blockedByABPPage.begin() + positionG
row, |
244 stdext::make_checked_array_iterator(static_cast<char*>(pv), cb)); | 255 stdext::make_checked_array_iterator(static_cast<char*>(pv), cb)); |
245 *pcbRead = positionGrow; | 256 *pcbRead = positionGrow; |
246 m_currentPositionOfSentPage += positionGrow; | 257 m_currentPositionOfSentPage += positionGrow; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 QueryServiceFromClient(&httpNegotiate); | 347 QueryServiceFromClient(&httpNegotiate); |
337 // This fills the pszAdditionalHeaders with more headers. One of which is the
Referer header, which we need. | 348 // This fills the pszAdditionalHeaders with more headers. One of which is the
Referer header, which we need. |
338 // There doesn't seem to be any other way to get this header before the reques
t has been made. | 349 // There doesn't seem to be any other way to get this header before the reques
t has been made. |
339 HRESULT nativeHr = httpNegotiate ? httpNegotiate->BeginningTransaction(szURL,
szHeaders, dwReserved, pszAdditionalHeaders) : S_OK; | 350 HRESULT nativeHr = httpNegotiate ? httpNegotiate->BeginningTransaction(szURL,
szHeaders, dwReserved, pszAdditionalHeaders) : S_OK; |
340 | 351 |
341 if (pszAdditionalHeaders && *pszAdditionalHeaders) | 352 if (pszAdditionalHeaders && *pszAdditionalHeaders) |
342 { | 353 { |
343 m_boundDomain = ExtractHttpHeader<std::wstring>(*pszAdditionalHeaders, L"Ref
erer:", L"\n"); | 354 m_boundDomain = ExtractHttpHeader<std::wstring>(*pszAdditionalHeaders, L"Ref
erer:", L"\n"); |
344 } | 355 } |
345 m_boundDomain = TrimString(m_boundDomain); | 356 m_boundDomain = TrimString(m_boundDomain); |
346 m_contentType = GetContentType(ToUtf16String(ExtractHttpAcceptHeader(m_spTarge
tProtocol)), m_boundDomain, src); | 357 m_contentType = InferContentType(ToUtf16String(ExtractHttpAcceptHeader(m_spTar
getProtocol)), m_boundDomain, src); |
347 | 358 |
348 CPluginTab* tab = CPluginClass::GetTabCurrentThread(); | 359 CPluginTab* tab = CPluginClass::GetTabForCurrentThread(); |
349 CPluginClient* client = CPluginClient::GetInstance(); | 360 CPluginClient* client = CPluginClient::GetInstance(); |
350 | 361 |
351 if (tab && client) | 362 if (tab && client) |
352 { | 363 { |
353 std::wstring documentUrl = tab->GetDocumentUrl(); | 364 std::wstring documentUrl = tab->GetDocumentUrl(); |
354 // Page is identical to document => don't block | 365 // Page is identical to document => don't block |
355 if (documentUrl == src) | 366 if (documentUrl == src) |
356 { | 367 { |
357 return nativeHr; | 368 return nativeHr; |
358 } | 369 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 if (m_isCustomResponse) | 421 if (m_isCustomResponse) |
411 { | 422 { |
412 // Don't notify the client about aborting of the operation, thus don't call
BaseClass::ReportResult. | 423 // Don't notify the client about aborting of the operation, thus don't call
BaseClass::ReportResult. |
413 // Current method is called by the original protocol implementation and we a
re intercepting the | 424 // Current method is called by the original protocol implementation and we a
re intercepting the |
414 // call here and eating it, we will call the proper ReportResult later by ou
rself. | 425 // call here and eating it, we will call the proper ReportResult later by ou
rself. |
415 return S_OK; | 426 return S_OK; |
416 } | 427 } |
417 return BaseClass::ReportResult(hrResult, dwError, szResult); | 428 return BaseClass::ReportResult(hrResult, dwError, szResult); |
418 } | 429 } |
419 | 430 |
420 | 431 HRESULT WbPassthroughSinkStartPolicy::OnStart(LPCWSTR szUrl, |
421 WBPassthru::WBPassthru() | 432 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo, |
422 : m_shouldSupplyCustomContent(false) | 433 DWORD grfPI, HANDLE_PTR dwReserved, |
423 { | 434 IInternetProtocol* pTargetProtocol) |
424 } | 435 { |
425 | 436 HRESULT hr = BaseClass::OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwRese
rved, pTargetProtocol); |
426 STDMETHODIMP WBPassthru::Start(LPCWSTR szUrl, IInternetProtocolSink *pOIProtSink
, | 437 WBPassthruSink* pSink = GetSink(); |
| 438 if (hr == E_ABORT && pSink->m_isCustomResponse) |
| 439 { |
| 440 GetProtocol(pSink)->m_shouldSupplyCustomContent = true; |
| 441 pSink->m_spInternetProtocolSink->ReportProgress(BINDSTATUS_MIMETYPEAVAILABLE
, L"text/html"); |
| 442 pSink->m_spInternetProtocolSink->ReportData(BSCF_FIRSTDATANOTIFICATION, 0, s
tatic_cast<ULONG>(g_blockedByABPPage.size())); |
| 443 return S_OK; |
| 444 } |
| 445 return hr; |
| 446 } |
| 447 |
| 448 STDMETHODIMP WbPassthroughProtocol::Start(LPCWSTR szUrl, IInternetProtocolSink *
pOIProtSink, |
427 IInternetBindInfo *pOIBindInfo, DWORD grfPI, HANDLE_PTR dwReserved) | 449 IInternetBindInfo *pOIBindInfo, DWORD grfPI, HANDLE_PTR dwReserved) |
428 { | 450 { |
429 ATLASSERT(m_spInternetProtocol != 0); | 451 ATLASSERT(m_spInternetProtocol != 0); |
430 if (!m_spInternetProtocol) | 452 if (!m_spInternetProtocol) |
431 { | 453 { |
432 return E_UNEXPECTED; | 454 return E_UNEXPECTED; |
433 } | 455 } |
434 | 456 |
435 return OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, m_spInterne
tProtocol); | 457 return OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, m_spInterne
tProtocol); |
436 } | 458 } |
437 | 459 |
438 STDMETHODIMP WBPassthru::Read(/* [in, out] */ void *pv,/* [in] */ ULONG cb,/* [o
ut] */ ULONG *pcbRead) | 460 STDMETHODIMP WbPassthroughProtocol::Read(/* [in, out] */ void *pv,/* [in] */ ULO
NG cb,/* [out] */ ULONG *pcbRead) |
439 { | 461 { |
440 WBPassthruSink* pSink = GetSink(); | 462 WBPassthruSink* pSink = GetSink(); |
441 return pSink->OnRead(pv, cb, pcbRead); | 463 return pSink->OnRead(pv, cb, pcbRead); |
442 } | 464 } |
LEFT | RIGHT |