diff --git a/doc/changelog.txt b/doc/changelog.txt index e4d32354..9e3ea161 100644 --- a/doc/changelog.txt +++ b/doc/changelog.txt @@ -11,6 +11,8 @@ Fixes: - checking: Work around a Python 2.7 regression in parsing certain URLs with paths starting with a digit. - cmdline: Fix filename completion if path starts with ~ +- cgi: Prevent encoding errors printing to sys.stdout using an + encoding wrapper. Changes: - checking: Use HTTP GET requests to work around buggy IIS servers diff --git a/linkcheck/lc_cgi.py b/linkcheck/lc_cgi.py index f4eedb1a..42d1396a 100644 --- a/linkcheck/lc_cgi.py +++ b/linkcheck/lc_cgi.py @@ -25,7 +25,7 @@ import re import time import urlparse import types -from . import configuration, strformat, checker, director +from . import configuration, strformat, checker, director, logger from . import add_intern_pattern, get_link_pat, init_i18n from . import url as urlutil @@ -45,16 +45,20 @@ class LCFormError (StandardError): pass -def startoutput (out=sys.stdout): +def startoutput (out=None): """Print leading HTML headers to given output stream.""" + if out is None: + out = logger.get_stdout_writer() out.write("Content-type: text/html\r\n" "Cache-Control: no-cache\r\n" "Pragma: no-cache\r\n" "\r\n") -def checklink (out=sys.stdout, form=None, env=os.environ): +def checklink (out=None, form=None, env=os.environ): """Main cgi function, check the given links and print out the result.""" + if out is None: + out = logger.get_stdout_writer() if form is None: form = {} try: diff --git a/tests/test_cgi.py b/tests/test_cgi.py index c304ecc8..be6252f2 100644 --- a/tests/test_cgi.py +++ b/tests/test_cgi.py @@ -39,18 +39,14 @@ class TestCgi (unittest.TestCase): """ def test_form_valid_url (self): - """ - Check url validity. - """ + # Check url validity. form = {"url": Store("http://www.heise.de/"), - "level": Store("0"), + "level": Store("1"), } linkcheck.lc_cgi.checkform(form) def test_form_empty_url (self): - """ - Check with empty url. - """ + # Check with empty url. form = {"url": Store(""), "level": Store("0"), } @@ -58,9 +54,7 @@ class TestCgi (unittest.TestCase): linkcheck.lc_cgi.checkform, form) def test_form_default_url (self): - """ - Check with default url. - """ + # Check with default url. form = {"url": Store("http://"), "level": Store("0"), } @@ -68,11 +62,15 @@ class TestCgi (unittest.TestCase): linkcheck.lc_cgi.checkform, form) def test_form_invalid_url (self): - """ - Check url (in)validity. - """ + # Check url (in)validity. form = {"url": Store("http://www.foo bar/"), "level": Store("0"), } self.assertRaises(linkcheck.lc_cgi.LCFormError, linkcheck.lc_cgi.checkform, form) + + def test_checklink (self): + form = {"url": Store("http://www.heise.de/"), + "level": Store("0"), + } + linkcheck.lc_cgi.checklink(form=form)