Web: Add new @location command, to follow HTTP redirects.

Useful to un-tinify URLs.
This commit is contained in:
Valentin Lorentz 2020-10-13 22:28:52 +02:00
parent d52e2aa829
commit 5195ff8e12
2 changed files with 22 additions and 0 deletions

View File

@ -275,6 +275,21 @@ class Web(callbacks.PluginRegexp):
finally:
fd.close()
@wrap(['httpUrl'])
@catch_web_errors
@fetch_sandbox
def location(self, irc, msg, args, url):
"""<url>
If the <url> is redirected to another page, returns the URL of that
page. This works even if there are multiple redirects.
Only HTTP urls are valid.
Useful to "un-tinify" URLs."""
timeout = self.registryValue('timeout')
(target, text) = utils.web.getUrlTargetAndContent(url, size=60,
timeout=timeout)
irc.reply(target)
_doctypeRe = re.compile(r'(<!DOCTYPE[^>]+>)', re.M)
@wrap(['httpUrl'])
@catch_web_errors

View File

@ -37,6 +37,13 @@ class WebTestCase(ChannelPluginTestCase):
self.assertError('headers ftp://ftp.cdrom.com/pub/linux')
self.assertNotError('headers http://www.slashdot.org/')
def testLocation(self):
self.assertError('location ftp://ftp.cdrom.com/pub/linux')
self.assertResponse(
'location http://limnoria.net/', 'https://limnoria.net/')
self.assertResponse(
'location https://www.limnoria.net/', 'https://limnoria.net/')
def testDoctype(self):
self.assertError('doctype ftp://ftp.cdrom.com/pub/linux')
self.assertNotError('doctype http://www.slashdot.org/')