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

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

Commit message

Follow redirects in "is it up"

To post a comment you must log in.
910. By Stefano Rivera

Actually give up waiting for a service to come up

Revision history for this message
Jonathan Hitchcock (vhata) wrote :

"More than max_hops" is not the same as "infinite redirect".

(And I don't think that "redirects" are the same as "hops".)

review: Needs Fixing
Revision history for this message
Stefano Rivera (stefanor) wrote :

> And I don't think that "redirects" are the same as "hops".

Agreed with you on that, didn't want to rename it in 0.1, I suppose I should split this into a separate branch for 0.2

> "More than max_hops" is not the same as "infinite redirect".

"Redirection limit reached"?

Revision history for this message
Jonathan Hitchcock (vhata) wrote :

> > "More than max_hops" is not the same as "infinite redirect".
>
> "Redirection limit reached"?

Works for me.

911. By Stefano Rivera

Response: infinite redircect loop -> Redirect limit reached

Revision history for this message
Stefano Rivera (stefanor) wrote :

r912

912. By Stefano Rivera

Rename max_hops to redirect_limit

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

Subscribers

People subscribed via source and target branches