Skip to content

Commit

Permalink
[middleware][cleanup] Remove unused HtmlValidationMiddleware (#3871)
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

- `HtmlValidationMiddleware` was commented out in `settings.py` 9 years ago and was never used. It makes sense to remove the related dead code.

## How was this patch tested?

- Manually
  • Loading branch information
Harshg999 authored Oct 21, 2024
1 parent ccecafa commit 16c940e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 109 deletions.
106 changes: 0 additions & 106 deletions desktop/core/src/desktop/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,112 +491,6 @@ def _get_allowed(self, request, response=None):
return allowed


try:
import tidylib
_has_tidylib = True
except Exception as ex:
# The exception type is not ImportError. It's actually an OSError.
logging.warn("Failed to import tidylib (for debugging). Is libtidy installed?")
_has_tidylib = False


class HtmlValidationMiddleware(MiddlewareMixin):
"""
If configured, validate output html for every response.
"""
def __init__(self, get_response):
self.get_response = get_response
self._logger = logging.getLogger('HtmlValidationMiddleware')

if not _has_tidylib:
logging.error("HtmlValidationMiddleware not activatived: Failed to import tidylib.")
return

# Things that we don't care about
self._to_ignore = (
re.compile('- Warning: <.*> proprietary attribute "data-'),
re.compile('- Warning: trimming empty'),
re.compile('- Info:'),
)

# Find the directory to write tidy html output
try:
self._outdir = os.path.join(tempfile.gettempdir(), 'hue_html_validation')
if not os.path.isdir(self._outdir):
os.mkdir(self._outdir, 0o755)
except Exception as ex:
self._logger.exception('Failed to get temp directory: %s', (ex,))
self._outdir = tempfile.mkdtemp(prefix='hue_html_validation-')

# Options to pass to libtidy. See
# http://tidy.sourceforge.net/docs/quickref.html
self._options = {
'show-warnings': 1,
'output-html': 0,
'output-xhtml': 1,
'char-encoding': 'utf8',
'output-encoding': 'utf8',
'indent': 1,
'wrap': 0,
}

def process_response(self, request, response):

if not _has_tidylib or not self._is_html(request, response):
return response

html, errors = tidylib.tidy_document(response.content,
self._options,
keep_doc=True)
if not errors:
return response

# Filter out what we care about
err_list = errors.rstrip().split('\n')
err_list = self._filter_warnings(err_list)
if not err_list:
return response

try:
fn = resolve(request.path)[0]
fn_name = '%s.%s' % (fn.__module__, fn.__name__)
except Exception:
LOG.exception('failed to resolve url')
fn_name = '<unresolved_url>'

# Write the two versions of html out for offline debugging
filename = os.path.join(self._outdir, fn_name)

result = "HTML tidy result: %s [%s]:" \
"\n\t%s" \
"\nPlease see %s.orig %s.tidy\n-------" % \
(request.path, fn_name, '\n\t'.join(err_list), filename, filename)

file(filename + '.orig', 'w').write(i18n.smart_str(response.content))
file(filename + '.tidy', 'w').write(i18n.smart_str(html))
file(filename + '.info', 'w').write(i18n.smart_str(result))

self._logger.error(result)

return response

def _filter_warnings(self, err_list):
"""A hacky way to filter out things that we don't care about."""
res = []
for err in err_list:
for ignore in self._to_ignore:
if ignore.search(err):
break
else:
res.append(err)
return res

def _is_html(self, request, response):
return not is_ajax(request) and \
'html' in response['Content-Type'] and \
200 <= response.status_code < 300


class ProxyMiddleware(MiddlewareMixin):

def __init__(self, get_response):
Expand Down
3 changes: 0 additions & 3 deletions desktop/core/src/desktop/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@
'crequest.middleware.CrequestMiddleware',
]

# if os.environ.get(ENV_DESKTOP_DEBUG):
# MIDDLEWARE.append('desktop.middleware.HtmlValidationMiddleware')
# logging.debug("Will try to validate generated HTML.")

ROOT_URLCONF = 'desktop.urls'

Expand Down

0 comments on commit 16c940e

Please sign in to comment.