Merge lp:~stefanor/ibid/isitup-redirect-599410-0.1 into lp:~ibid-core/ibid/old-release-0.1-1.6

Proposed by Stefano Rivera
Status: Merged
Approved by: Max Rabkin
Approved revision: 911
Merged at revision: 962
Proposed branch: lp:~stefanor/ibid/isitup-redirect-599410-0.1
Merge into: lp:~ibid-core/ibid/old-release-0.1-1.6
Diff against target: 84 lines (+19/-15)
1 file modified
ibid/plugins/network.py (+19/-15)
To merge this branch: bzr merge lp:~stefanor/ibid/isitup-redirect-599410-0.1
Reviewer Review Type Date Requested Status
Max Rabkin Approve
Jonathan Hitchcock Approve
Review via email: mp+37502@code.launchpad.net

Commit message

Follow redirects in "is it up"

To post a comment you must log in.
Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve
Revision history for this message
Max Rabkin (max-rabkin) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid/plugins/network.py'
2--- ibid/plugins/network.py 2010-04-26 09:31:23 +0000
3+++ ibid/plugins/network.py 2010-10-04 17:41:27 +0000
4@@ -230,7 +230,7 @@
5 u'Timeout for HTTP connections in seconds', 15)
6 sites = DictOption('sites', u'Mapping of site names to domains', {})
7 max_hops = IntOption('max_hops',
8- u'Maximum hops in get/head when receiving a 30[12]', 3)
9+ u'Maximum number of http redirects to follow', 5)
10 whensitup_delay = IntOption('whensitup_delay',
11 u'Initial delay between whensitup attempts in seconds', 60)
12 whensitup_factor = FloatOption('whensitup_factor',
13@@ -255,8 +255,7 @@
14 reply = u'%s %s' % (status, reason)
15
16 hops = 0
17- while status in (301, 302) and self._get_header(headers,
18- 'location'):
19+ while 300 <= status < 400 and self._get_header(headers, 'location'):
20 location = self._get_header(headers, 'location')
21 status, reason, data, headers = self._request(location, 'GET')
22 if hops >= self.max_hops:
23@@ -301,7 +300,7 @@
24 url += '/'
25 return url
26
27- def _isitup(self, url):
28+ def _isitup(self, url, return_status=False, redirects=0):
29 valid_url = self._makeurl(url)
30 if Resolver is not None:
31 r = Resolver()
32@@ -309,29 +308,33 @@
33 try:
34 r.query(host)
35 except NoAnswer:
36- return False, u'No DNS A/CNAME-records for that domain'
37+ return (False, valid_url,
38+ u'No DNS A/CNAME-records for that domain')
39 except NXDOMAIN:
40- return False, u'No such domain'
41+ return False, valid_url, u'No such domain'
42
43 try:
44 status, reason, data, headers = self._request(valid_url, 'HEAD')
45- # If the URL is only a hostname, we consider 400 series errors to
46- # mean up. Full URLs are checked for a sensible response code
47- if url == urlparse(url).path and '/' not in url:
48- up = status < 500
49- else:
50- up = status < 400
51+ if 300 <= status < 400 and self._get_header(headers, 'location'):
52+ if redirects > self.max_hops:
53+ return False, valid_url, u'Redirect limit reached'
54+ return self._isitup(self._get_header(headers, 'location'),
55+ return_status, redirects + 1)
56+
57+ up = status < 300
58+ if return_status:
59 reason = u'%(status)d %(reason)s' % {
60 u'status': status,
61 u'reason': reason,
62 }
63- return up, reason
64+ return up, valid_url, reason
65 except HTTPException:
66- return False, u'Server is not responding'
67+ return False, valid_url, u'Server is not responding'
68
69 @match(r'^is\s+(\S+)\s+(up|down)$')
70 def isit(self, event, url, type):
71- up, reason = self._isitup(url)
72+ host_only = (url == urlparse(url).path and '/' not in url)
73+ up, url, reason = self._isitup(url, return_status=not host_only)
74 if up:
75 if type.lower() == 'up':
76 event.addresponse(u'Yes, %s is up', url)
77@@ -359,6 +362,7 @@
78 event.addresponse(u"Sorry, it appears %s is never coming up. "
79 u"I'm not going to check any more.",
80 self._makeurl(url))
81+ return
82 delay *= self.whensitup_factor
83 delay = max(delay, self.whensitup_maxdelay)
84 ibid.dispatcher.call_later(delay, self._whensitup, event, url, delay,

Subscribers

People subscribed via source and target branches