Left: | ||
Right: |
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-2015 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 |
(...skipping 20 matching lines...) Expand all Loading... | |
31 | 31 |
32 namespace | 32 namespace |
33 { | 33 { |
34 const std::string g_blockedByABPPage = "<!DOCTYPE html>" | 34 const std::string g_blockedByABPPage = "<!DOCTYPE html>" |
35 "<html>" | 35 "<html>" |
36 "<body>" | 36 "<body>" |
37 "<!-- blocked by AdblockPlus -->" | 37 "<!-- blocked by AdblockPlus -->" |
38 "</body>" | 38 "</body>" |
39 "</html>"; | 39 "</html>"; |
40 | 40 |
41 template <typename T> | |
42 T ASCIIStringToLower(const T& text) | |
43 { | |
44 T textlower; | |
45 std::transform(text.begin(), text.end(), std::back_inserter(textlower), | |
46 [](T::value_type ch) | |
47 { | |
48 return std::tolower(ch, std::locale()); | |
49 } | |
50 ); | |
51 return textlower; | |
52 } | |
53 | |
54 typedef AdblockPlus::FilterEngine::ContentType ContentType; | |
55 | |
41 template <class T> | 56 template <class T> |
42 T ExtractHttpHeader(const T& allHeaders, const T& targetHeaderNameWithColon, c onst T& delimiter) | 57 T ExtractHttpHeader(const T& allHeaders, const T& targetHeaderNameWithColon, c onst T& delimiter) |
43 { | 58 { |
44 auto targetHeaderBeginsAt = allHeaders.find(targetHeaderNameWithColon); | 59 const T allHeadersLower = ASCIIStringToLower(allHeaders); |
60 auto targetHeaderBeginsAt = allHeadersLower.find(ASCIIStringToLower(targetHe aderNameWithColon)); | |
45 if (targetHeaderBeginsAt == T::npos) | 61 if (targetHeaderBeginsAt == T::npos) |
46 { | 62 { |
47 return T(); | 63 return T(); |
48 } | 64 } |
49 targetHeaderBeginsAt += targetHeaderNameWithColon.length(); | 65 targetHeaderBeginsAt += targetHeaderNameWithColon.length(); |
50 auto targetHeaderEndsAt = allHeaders.find(delimiter, targetHeaderBeginsAt); | 66 auto targetHeaderEndsAt = allHeadersLower.find(ASCIIStringToLower(delimiter) , targetHeaderBeginsAt); |
51 if (targetHeaderEndsAt == T::npos) | 67 if (targetHeaderEndsAt == T::npos) |
52 { | 68 { |
53 return T(); | 69 return T(); |
54 } | 70 } |
55 return allHeaders.substr(targetHeaderBeginsAt, targetHeaderEndsAt - targetHe aderBeginsAt); | 71 return allHeaders.substr(targetHeaderBeginsAt, targetHeaderEndsAt - targetHe aderBeginsAt); |
56 } | 72 } |
57 | 73 |
58 std::string ExtractHttpAcceptHeader(IInternetProtocol* internetProtocol) | 74 std::string ExtractHttpAcceptHeader(IInternetProtocol* internetProtocol) |
59 { | 75 { |
60 // Despite there being HTTP_QUERY_ACCEPT and other query info flags, they do n't work here, | 76 // Despite there being HTTP_QUERY_ACCEPT and other query info flags, they do n't work here, |
(...skipping 20 matching lines...) Expand all Loading... | |
81 } | 97 } |
82 return ExtractHttpHeader<std::string>(buf, "Accept:", "\r\n"); | 98 return ExtractHttpHeader<std::string>(buf, "Accept:", "\r\n"); |
83 } | 99 } |
84 | 100 |
85 bool IsXmlHttpRequest(const std::wstring& additionalHeaders) | 101 bool IsXmlHttpRequest(const std::wstring& additionalHeaders) |
86 { | 102 { |
87 auto requestedWithHeader = ExtractHttpHeader<std::wstring>(additionalHeaders , L"X-Requested-With:", L"\n"); | 103 auto requestedWithHeader = ExtractHttpHeader<std::wstring>(additionalHeaders , L"X-Requested-With:", L"\n"); |
88 return TrimString(requestedWithHeader) == L"XMLHttpRequest"; | 104 return TrimString(requestedWithHeader) == L"XMLHttpRequest"; |
89 } | 105 } |
90 | 106 |
91 int GetContentTypeFromString(const std::wstring& value) | 107 ContentType GetContentTypeFromString(const std::wstring& value) |
92 { | 108 { |
93 auto lastDotIt = std::find(value.rbegin(), value.rend(), L'.').base(); | 109 auto lastDotPos = value.rfind(L'.'); |
Eric
2015/02/02 18:41:58
It's much clearer just to call 'rfind':
auto
sergei
2015/02/12 14:44:06
Good point!
fixed
| |
94 if (lastDotIt == value.end()) | 110 if (lastDotPos == std::wstring::npos) |
95 return CFilter::contentTypeAny; | 111 return ContentType::CONTENT_TYPE_OTHER; |
96 | 112 |
97 std::wstring ext(lastDotIt, value.end()); | 113 std::wstring ext = ASCIIStringToLower(value.substr(lastDotPos + 1)); |
98 if (ext == L"jpg" || ext == L"gif" || ext == L"png" || ext == L"jpeg") | 114 if (ext == L"jpg" || ext == L"gif" || ext == L"png" || ext == L"jpeg") |
99 { | 115 { |
100 return CFilter::contentTypeImage; | 116 return ContentType::CONTENT_TYPE_IMAGE; |
101 } | 117 } |
102 else if (ext == L"css") | 118 else if (ext == L"css") |
103 { | 119 { |
104 return CFilter::contentTypeStyleSheet; | 120 return ContentType::CONTENT_TYPE_STYLESHEET; |
105 } | 121 } |
106 else if (ext == L"js") | 122 else if (ext == L"js") |
107 { | 123 { |
108 return CFilter::contentTypeScript; | 124 return ContentType::CONTENT_TYPE_SCRIPT; |
109 } | 125 } |
110 else if (ext == L"xml") | 126 else if (ext == L"xml") |
111 { | 127 { |
112 return CFilter::contentTypeXmlHttpRequest; | 128 return ContentType::CONTENT_TYPE_XMLHTTPREQUEST; |
113 } | 129 } |
114 else if (ext == L"swf") | 130 else if (ext == L"swf") |
115 { | 131 { |
116 return CFilter::contentTypeObject; | 132 return ContentType::CONTENT_TYPE_OBJECT; |
117 } | 133 } |
118 else if (ext == L"jsp" || ext == L"php" || ext == L"html") | 134 else if (ext == L"jsp" || ext == L"php" || ext == L"html") |
119 { | 135 { |
120 return CFilter::contentTypeSubdocument; | 136 return ContentType::CONTENT_TYPE_SUBDOCUMENT; |
121 } | 137 } |
122 return CFilter::contentTypeAny; | 138 return ContentType::CONTENT_TYPE_OTHER; |
123 } | 139 } |
124 } | 140 } |
125 | 141 |
126 WBPassthruSink::WBPassthruSink() | 142 WBPassthruSink::WBPassthruSink() |
127 : m_currentPositionOfSentPage(0) | 143 : m_currentPositionOfSentPage(0) |
128 , m_contentType(CFilter::EContentType::contentTypeAny) | 144 , m_contentType(ContentType::CONTENT_TYPE_OTHER) |
129 , m_isCustomResponse(false) | 145 , m_isCustomResponse(false) |
130 { | 146 { |
131 } | 147 } |
132 | 148 |
133 int WBPassthruSink::GetContentTypeFromMimeType(const CString& mimeType) | 149 ContentType WBPassthruSink::GetContentTypeFromMimeType(const CString& mimeType) |
134 { | 150 { |
135 if (mimeType.Find(L"image/") >= 0) | 151 if (mimeType.Find(L"image/") >= 0) |
136 { | 152 { |
137 return CFilter::contentTypeImage; | 153 return ContentType::CONTENT_TYPE_IMAGE; |
138 } | 154 } |
139 if (mimeType.Find(L"text/css") >= 0) | 155 if (mimeType.Find(L"text/css") >= 0) |
140 { | 156 { |
141 return CFilter::contentTypeStyleSheet; | 157 return ContentType::CONTENT_TYPE_STYLESHEET; |
142 } | 158 } |
143 if ((mimeType.Find(L"application/javascript") >= 0) || (mimeType.Find(L"applic ation/json") >= 0)) | 159 if ((mimeType.Find(L"application/javascript") >= 0) || (mimeType.Find(L"applic ation/json") >= 0)) |
144 { | 160 { |
145 return CFilter::contentTypeScript; | 161 return ContentType::CONTENT_TYPE_SCRIPT; |
146 } | 162 } |
147 if (mimeType.Find(L"application/x-shockwave-flash") >= 0) | 163 if (mimeType.Find(L"application/x-shockwave-flash") >= 0) |
148 { | 164 { |
149 return CFilter::contentTypeObject; | 165 return ContentType::CONTENT_TYPE_OBJECT; |
150 } | 166 } |
151 if (mimeType.Find(L"text/html") >= 0) | 167 if (mimeType.Find(L"text/html") >= 0) |
152 { | 168 { |
153 return CFilter::contentTypeSubdocument; | 169 return ContentType::CONTENT_TYPE_SUBDOCUMENT; |
154 } | 170 } |
155 // It is important to have this check last, since it is rather generic, and mi ght overlay text/html, for example | 171 // It is important to have this check last, since it is rather generic, and mi ght overlay text/html, for example |
156 if (mimeType.Find(L"xml") >= 0) | 172 if (mimeType.Find(L"xml") >= 0) |
157 { | 173 { |
158 return CFilter::contentTypeXmlHttpRequest; | 174 return ContentType::CONTENT_TYPE_XMLHTTPREQUEST; |
159 } | 175 } |
160 | 176 |
161 return CFilter::contentTypeAny; | 177 return ContentType::CONTENT_TYPE_OTHER; |
162 } | 178 } |
163 | 179 |
164 int WBPassthruSink::GetContentTypeFromURL(const std::wstring& src) | 180 ContentType WBPassthruSink::GetContentTypeFromURL(const std::wstring& src) |
165 { | 181 { |
166 // http://en.wikipedia.org/wiki/URI_scheme | 182 std::wstring schemeAndHierarchicalPart = GetSchemeAndHierarchicalPart(src); |
167 auto schemeAndHierarchicalPartEndsAt = src.find(L'?'); | |
168 if (schemeAndHierarchicalPartEndsAt == std::wstring::npos) | |
169 { | |
170 schemeAndHierarchicalPartEndsAt = src.find(L'#'); | |
171 } | |
172 std::wstring schemeAndHierarchicalPart = src.substr(0, schemeAndHierarchicalPa rtEndsAt); | |
173 auto contentType = GetContentTypeFromString(schemeAndHierarchicalPart); | 183 auto contentType = GetContentTypeFromString(schemeAndHierarchicalPart); |
174 if (contentType != CFilter::contentTypeAny) | 184 if (contentType == ContentType::CONTENT_TYPE_OTHER && |
175 { | 185 AdblockPlus::IE::InstalledMajorVersion() == 8) |
Eric
2015/02/02 18:41:58
As it's already been mentioned in the comments, it
sergei
2015/02/12 14:44:06
Actually, I'm not sure that it's a problem of only
| |
176 return contentType; | 186 { |
177 } | 187 std::wstring queryString = GetQueryString(src); |
178 | 188 wchar_t* nextToken = nullptr; |
179 ProcessQueryStringParameters(GetQueryString(src), | 189 const wchar_t* token = wcstok_s(&queryString[0], L"&=", &nextToken); |
180 [&contentType](const std::wstring& name, const std::wstring& value)->bool | 190 while (token != nullptr) |
181 { | 191 { |
182 if (!value.empty()) | 192 contentType = GetContentTypeFromString(token); |
193 if (contentType != ContentType::CONTENT_TYPE_OTHER) | |
183 { | 194 { |
184 contentType = GetContentTypeFromString(value); | 195 return contentType; |
185 if (contentType != CFilter::contentTypeAny) | |
186 { | |
187 return false; | |
188 } | |
189 } | 196 } |
190 contentType = GetContentTypeFromString(name); | 197 token = wcstok_s(nullptr, L"&=", &nextToken); |
191 if (contentType != CFilter::contentTypeAny) | 198 } |
192 { | 199 } |
193 return false; | |
194 } | |
195 return true; | |
196 }); | |
197 | |
198 return contentType; | 200 return contentType; |
199 } | 201 } |
200 | 202 |
201 int WBPassthruSink::GetContentType(const CString& mimeType, const std::wstring& domain, const std::wstring& src) | 203 ContentType WBPassthruSink::GetContentType(const CString& mimeType, const std::w string& domain, const std::wstring& src) |
202 { | 204 { |
203 // No referer or mime type | 205 // No referer or mime type |
204 // BINDSTRING_XDR_ORIGIN works only for IE v8+ | 206 // BINDSTRING_XDR_ORIGIN works only for IE v8+ |
205 if (mimeType.IsEmpty() && domain.empty() && AdblockPlus::IE::InstalledMajorVer sion() >= 8) | 207 if (mimeType.IsEmpty() && domain.empty() && AdblockPlus::IE::InstalledMajorVer sion() >= 8) |
206 { | 208 { |
207 return CFilter::contentTypeXmlHttpRequest; | 209 return ContentType::CONTENT_TYPE_XMLHTTPREQUEST; |
208 } | 210 } |
209 int contentType = GetContentTypeFromMimeType(mimeType); | 211 ContentType contentType = GetContentTypeFromMimeType(mimeType); |
210 if (contentType == CFilter::contentTypeAny) | 212 if (contentType == ContentType::CONTENT_TYPE_OTHER) |
211 { | 213 { |
212 contentType = GetContentTypeFromURL(src); | 214 contentType = GetContentTypeFromURL(src); |
213 } | 215 } |
214 return contentType; | 216 return contentType; |
215 } | 217 } |
216 | 218 |
217 //////////////////////////////////////////////////////////////////////////////// //////// | 219 //////////////////////////////////////////////////////////////////////////////// //////// |
218 //WBPassthruSink | 220 //WBPassthruSink |
219 //Monitor and/or cancel every request and responde | 221 //Monitor and/or cancel every request and responde |
220 //WB makes, including images, sounds, scripts, etc | 222 //WB makes, including images, sounds, scripts, etc |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
320 return false; | 322 return false; |
321 } | 323 } |
322 | 324 |
323 STDMETHODIMP WBPassthruSink::BeginningTransaction(LPCWSTR szURL, LPCWSTR szHeade rs, DWORD dwReserved, LPWSTR* pszAdditionalHeaders) | 325 STDMETHODIMP WBPassthruSink::BeginningTransaction(LPCWSTR szURL, LPCWSTR szHeade rs, DWORD dwReserved, LPWSTR* pszAdditionalHeaders) |
324 { | 326 { |
325 if (!szURL) | 327 if (!szURL) |
326 { | 328 { |
327 return E_POINTER; | 329 return E_POINTER; |
328 } | 330 } |
329 std::wstring src = szURL; | 331 std::wstring src = szURL; |
332 UnescapeUrl(src); | |
330 DEBUG_GENERAL(ToCString(src)); | 333 DEBUG_GENERAL(ToCString(src)); |
331 | 334 |
332 std::string acceptHeader = ExtractHttpAcceptHeader(m_spTargetProtocol); | 335 std::string acceptHeader = ExtractHttpAcceptHeader(m_spTargetProtocol); |
333 | 336 |
334 if (pszAdditionalHeaders) | 337 if (pszAdditionalHeaders) |
335 { | 338 { |
336 *pszAdditionalHeaders = nullptr; | 339 *pszAdditionalHeaders = nullptr; |
337 } | 340 } |
338 | 341 |
339 CComPtr<IHttpNegotiate> httpNegotiate; | 342 CComPtr<IHttpNegotiate> httpNegotiate; |
340 QueryServiceFromClient(&httpNegotiate); | 343 QueryServiceFromClient(&httpNegotiate); |
341 // This fills the pszAdditionalHeaders with more headers. One of which is the Referer header, which we need. | 344 // This fills the pszAdditionalHeaders with more headers. One of which is the Referer header, which we need. |
342 // There doesn't seem to be any other way to get this header before the reques t has been made. | 345 // There doesn't seem to be any other way to get this header before the reques t has been made. |
343 HRESULT nativeHr = httpNegotiate ? httpNegotiate->BeginningTransaction(szURL, szHeaders, dwReserved, pszAdditionalHeaders) : S_OK; | 346 HRESULT nativeHr = httpNegotiate ? httpNegotiate->BeginningTransaction(szURL, szHeaders, dwReserved, pszAdditionalHeaders) : S_OK; |
344 | 347 |
345 if (pszAdditionalHeaders && *pszAdditionalHeaders) | 348 if (pszAdditionalHeaders && *pszAdditionalHeaders) |
346 { | 349 { |
347 m_boundDomain = ExtractHttpHeader<std::wstring>(*pszAdditionalHeaders, L"Ref erer:", L"\n"); | 350 m_boundDomain = ExtractHttpHeader<std::wstring>(*pszAdditionalHeaders, L"Ref erer:", L"\n"); |
348 } | 351 } |
349 m_boundDomain = TrimString(m_boundDomain); | 352 m_boundDomain = TrimString(m_boundDomain); |
350 m_contentType = GetContentType(ATL::CString(acceptHeader.c_str()), m_boundDoma in, src); | 353 m_contentType = GetContentType(ATL::CString(acceptHeader.c_str()), m_boundDoma in, src); |
354 | |
351 CPluginTab* tab = CPluginClass::GetTab(::GetCurrentThreadId()); | 355 CPluginTab* tab = CPluginClass::GetTab(::GetCurrentThreadId()); |
352 CPluginClient* client = CPluginClient::GetInstance(); | 356 CPluginClient* client = CPluginClient::GetInstance(); |
353 | 357 |
354 if (tab && client) | 358 if (tab && client) |
355 { | 359 { |
356 std::wstring documentUrl = tab->GetDocumentUrl(); | 360 std::wstring documentUrl = tab->GetDocumentUrl(); |
357 // Page is identical to document => don't block | 361 // Page is identical to document => don't block |
358 if (documentUrl == src) | 362 if (documentUrl == src) |
359 { | 363 { |
360 return nativeHr; | 364 return nativeHr; |
361 } | 365 } |
362 else if (CPluginSettings::GetInstance()->IsPluginEnabled() && !client->IsWhi telistedUrl(documentUrl)) | 366 else if (CPluginSettings::GetInstance()->IsPluginEnabled() && !client->IsWhi telistedUrl(documentUrl)) |
363 { | 367 { |
364 if (tab->IsFrameCached(src)) | 368 if (tab->IsFrameCached(src)) |
365 { | 369 { |
366 m_contentType = CFilter::contentTypeSubdocument; | 370 m_contentType = ContentType::CONTENT_TYPE_SUBDOCUMENT; |
367 } | 371 } |
368 } | 372 } |
369 } | 373 } |
370 | 374 |
371 if (IsFlashRequest(pszAdditionalHeaders)) | 375 if (IsFlashRequest(pszAdditionalHeaders)) |
372 { | 376 { |
373 m_contentType = CFilter::EContentType::contentTypeObjectSubrequest; | 377 m_contentType = ContentType::CONTENT_TYPE_OBJECT_SUBREQUEST; |
374 } | 378 } |
375 | 379 |
376 if (pszAdditionalHeaders && *pszAdditionalHeaders && IsXmlHttpRequest(*pszAddi tionalHeaders)) | 380 if (pszAdditionalHeaders && *pszAdditionalHeaders && IsXmlHttpRequest(*pszAddi tionalHeaders)) |
377 { | 381 { |
378 m_contentType = CFilter::EContentType::contentTypeXmlHttpRequest; | 382 m_contentType = ContentType::CONTENT_TYPE_XMLHTTPREQUEST; |
379 } | 383 } |
380 | 384 |
381 if (client->ShouldBlock(szURL, m_contentType, m_boundDomain, /*debug flag but must be set*/true)) | 385 if (client->ShouldBlock(szURL, m_contentType, m_boundDomain, /*debug flag but must be set*/true)) |
382 { | 386 { |
383 // NOTE: Feeding custom HTML to Flash, instead of original object subrequest | 387 // NOTE: Feeding custom HTML to Flash, instead of original object subrequest |
384 // doesn't have much sense. It also can manifest in unwanted result» | 388 // doesn't have much sense. It also can manifest in unwanted result |
385 // like video being blocked (See https://issues.adblockplus.org/ticket/1669) » | 389 // like video being blocked (See https://issues.adblockplus.org/ticket/1669) |
386 // So we report blocked object subrequests as failed, not just empty HTML. | 390 // So we report blocked object subrequests as failed, not just empty HTML. |
387 m_isCustomResponse = m_contentType != CFilter::contentTypeObjectSubrequest; | 391 m_isCustomResponse = m_contentType != ContentType::CONTENT_TYPE_OBJECT_SUBRE QUEST; |
388 return E_ABORT; | 392 return E_ABORT; |
389 } | 393 } |
390 return nativeHr; | 394 return nativeHr; |
391 } | 395 } |
392 | 396 |
393 STDMETHODIMP WBPassthruSink::OnResponse(DWORD dwResponseCode, LPCWSTR szResponse Headers, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders) | 397 STDMETHODIMP WBPassthruSink::OnResponse(DWORD dwResponseCode, LPCWSTR szResponse Headers, LPCWSTR szRequestHeaders, LPWSTR *pszAdditionalRequestHeaders) |
394 { | 398 { |
395 if (pszAdditionalRequestHeaders) | 399 if (pszAdditionalRequestHeaders) |
396 { | 400 { |
397 *pszAdditionalRequestHeaders = 0; | 401 *pszAdditionalRequestHeaders = 0; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
436 } | 440 } |
437 | 441 |
438 return OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, m_spInterne tProtocol); | 442 return OnStart(szUrl, pOIProtSink, pOIBindInfo, grfPI, dwReserved, m_spInterne tProtocol); |
439 } | 443 } |
440 | 444 |
441 STDMETHODIMP WBPassthru::Read(/* [in, out] */ void *pv,/* [in] */ ULONG cb,/* [o ut] */ ULONG *pcbRead) | 445 STDMETHODIMP WBPassthru::Read(/* [in, out] */ void *pv,/* [in] */ ULONG cb,/* [o ut] */ ULONG *pcbRead) |
442 { | 446 { |
443 WBPassthruSink* pSink = GetSink(); | 447 WBPassthruSink* pSink = GetSink(); |
444 return pSink->OnRead(pv, cb, pcbRead); | 448 return pSink->OnRead(pv, cb, pcbRead); |
445 } | 449 } |
LEFT | RIGHT |