0008-misc-move-bot-user-agent-check-into-request-objects-.patch
wcs/qommon/http_request.py | ||
---|---|---|
16 | 16 | |
17 | 17 |
import base64 |
18 | 18 |
import copy |
19 |
import os |
|
19 | 20 |
import re |
20 | 21 |
import time |
21 | 22 | |
... | ... | |
204 | 205 |
return not (self.is_in_backoffice() or self.is_api_url()) |
205 | 206 | |
206 | 207 |
def is_from_bot(self): |
207 |
from .logger import BotFilter |
|
208 | ||
209 |
return BotFilter.is_bot(request=self) |
|
208 |
botfile = os.path.join(get_publisher().data_dir, 'webbots') |
|
209 |
user_agent = self.get_environ('HTTP_USER_AGENT', '') |
|
210 |
with open(botfile) as fd: |
|
211 |
for bot_ua_string in [x.strip() for x in fd.readlines()]: |
|
212 |
if bot_ua_string in user_agent: |
|
213 |
return True |
|
214 |
return False |
|
210 | 215 | |
211 | 216 |
def is_from_application(self): |
212 | 217 |
# detect calls made from other applications or debug tools |
wcs/qommon/logger.py | ||
---|---|---|
44 | 44 |
) |
45 | 45 | |
46 | 46 | |
47 |
class BotFilter(logging.Filter): |
|
48 |
def filter(self, record): |
|
49 |
if record.levelno >= logging.ERROR: |
|
50 |
# always log errors |
|
51 |
return 1 |
|
52 |
if self.is_bot(): |
|
53 |
return 0 |
|
54 |
return 1 |
|
55 | ||
56 |
@classmethod |
|
57 |
def is_bot(cls, request=None): |
|
58 |
if request is None: |
|
59 |
request = get_request() |
|
60 |
botfile = os.path.join(get_publisher().data_dir, 'webbots') |
|
61 |
if request and os.path.exists(botfile): |
|
62 |
user_agent = request.get_environ('HTTP_USER_AGENT', '') |
|
63 |
with open(botfile) as fd: |
|
64 |
for bot_ua_string in [x.strip() for x in fd.readlines()]: |
|
65 |
if bot_ua_string in user_agent: |
|
66 |
return True |
|
67 |
return False |
|
68 | ||
69 | ||
70 | 47 |
class Formatter(logging.Formatter): |
71 | 48 |
def format(self, record): |
72 | 49 |
request = get_request() |
73 |
- |