OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright 2015 Google Inc. All Rights Reserved. | |
3 # | |
4 # Licensed under the Apache License, Version 2.0 (the "License"); | |
5 # you may not use this file except in compliance with the License. | |
6 # You may obtain a copy of the License at | |
7 # | |
8 # http://www.apache.org/licenses/LICENSE-2.0 | |
9 # | |
10 # Unless required by applicable law or agreed to in writing, software | |
11 # distributed under the License is distributed on an "AS IS" BASIS, | |
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 # See the License for the specific language governing permissions and | |
14 # limitations under the License. | |
15 | |
16 import logging | |
17 import re | |
18 | |
19 from rules import rule | |
20 | |
21 | |
22 class LogUrl(rule.Rule): | |
23 """Logs the request URL.""" | |
24 | |
25 def __init__(self, url, stop=False): | |
26 r"""Initializes with a url pattern. | |
27 | |
28 Args: | |
29 url: a string regex, e.g. r'example\.com/id=(\d{6})'. | |
30 stop: boolean ApplyRule should_stop value, defaults to True. | |
31 """ | |
32 self._url_re = re.compile(url) | |
33 self._stop = stop | |
34 | |
35 def IsType(self, rule_type_name): | |
36 """Returns True if the name matches this rule.""" | |
37 return rule_type_name == 'log_url' | |
38 | |
39 def ApplyRule(self, return_value, request, response): | |
40 """Returns True if logged. | |
41 | |
42 Args: | |
43 return_value: the prior log_url rule's return_value (if any). | |
44 request: the httparchive ArchivedHttpRequest. | |
45 response: the httparchive ArchivedHttpResponse. | |
46 Returns: | |
47 A (should_stop, return_value) tuple, e.g. (False, True). | |
48 """ | |
49 del response # unused. | |
50 url = '%s%s' % (request.host, request.full_path) | |
51 if not self._url_re.match(url): | |
52 return False, return_value | |
53 | |
54 logging.debug('url: %s', url) | |
55 return self._stop, True | |
56 | |
57 def __str__(self): | |
58 return _ToString(self, ('url', self._url_re.pattern), | |
59 None if self._stop else ('stop', False)) | |
60 | |
61 def __repr__(self): | |
62 return str(self) | |
63 | |
64 | |
65 def _ToString(obj, *items): | |
66 pkg = (obj.__module__[:obj.__module__.rfind('.') + 1] | |
67 if '.' in obj.__module__ else '') | |
68 clname = obj.__class__.__name__ | |
69 args = [('%s=r\'%s\'' % item if isinstance(item[1], basestring) | |
70 else '%s=%s' % item) for item in items if item] | |
71 return '%s%s(%s)' % (pkg, clname, ', '.join(args)) | |
OLD | NEW |