2 # Copyright 2015 Google Inc. All Rights Reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from rules
import rule
22 class LogUrl(rule
.Rule
):
23 """Logs the request URL."""
25 def __init__(self
, url
, stop
=False):
26 r
"""Initializes with a url pattern.
29 url: a string regex, e.g. r'example\.com/id=(\d{6})'.
30 stop: boolean ApplyRule should_stop value, defaults to True.
32 self
._url
_re
= re
.compile(url
)
35 def IsType(self
, rule_type_name
):
36 """Returns True if the name matches this rule."""
37 return rule_type_name
== 'log_url'
39 def ApplyRule(self
, return_value
, request
, response
):
40 """Returns True if logged.
43 return_value: the prior log_url rule's return_value (if any).
44 request: the httparchive ArchivedHttpRequest.
45 response: the httparchive ArchivedHttpResponse.
47 A (should_stop, return_value) tuple, e.g. (False, True).
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
54 logging
.debug('url: %s', url
)
55 return self
._stop
, True
58 return _ToString(self
, ('url', self
._url
_re
.pattern
),
59 None if self
._stop
else ('stop', False))
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
))