Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rose bush: view: default to escaping output HTML #1809

Merged
merged 4 commits into from Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/html/template/rose-bush/view-mode.html
@@ -1,10 +1,10 @@
<ul class="nav pull-right">
{% if mode != "text" -%}
<li class="active"><a>Normal</a></li>
{% if mode == "tags" -%}
<li><a href="{{script}}/view/{{user}}/{{suite}}?path={{path}}{%- if path_in_tar -%}&amp;path_in_tar={{path_in_tar}}{%- endif -%}">Text</a></li>
<li><a href="{{script}}/view/{{user}}/{{suite}}?path={{path}}{%- if path_in_tar -%}&amp;path_in_tar={{path_in_tar}}{%- endif -%}&amp;mode=text">Text</a></li>
{% else -%}
<li><a href="{{script}}/view/{{user}}/{{suite}}?path={{path}}{%- if path_in_tar -%}&amp;path_in_tar={{path_in_tar}}{%- endif -%}">Normal</a></li>
<li class="active"><a>Text</a></li>
<li><a href="{{script}}/view/{{user}}/{{suite}}?path={{path}}{%- if path_in_tar -%}&amp;path_in_tar={{path_in_tar}}{%- endif -%}&amp;mode=tags">Tags</a></li>
{% endif -%}
<li><a href="{{script}}/view/{{user}}/{{suite}}?path={{path}}{%- if path_in_tar -%}&amp;path_in_tar={{path_in_tar}}{%- endif -%}&amp;mode=download">Raw</a></li>
</ul>
16 changes: 15 additions & 1 deletion lib/python/rose/bush.py
Expand Up @@ -25,6 +25,7 @@
import jinja2
import mimetypes
import os
import re
import pwd
import shlex
import simplejson
Expand Down Expand Up @@ -493,7 +494,7 @@ def view(self, user, suite, path, path_in_tar=None, mode=None):
cherrypy.response.headers["Content-Type"] = mime
return cherrypy.lib.static.serve_file(f_name, mime)
s = open(f_name).read()
if mode == "text":
if mode == None:
s = jinja2.escape(s)
try:
lines = [unicode(line) for line in s.splitlines()]
Expand All @@ -518,6 +519,19 @@ def view(self, user, suite, path, path_in_tar=None, mode=None):
else:
file_content = self.suite_engine_proc.is_conf(path)
template = self.template_env.get_template("view.html")

# convert url(s) to hyperlinks if in text mode
if mode == None:
anchor = lambda url: '<a href="%s">%s</a>' % (url, url)
urlregex = '(http|https|ftp)://[^\s]+'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URL detection quite crude, expects urls to be space or new line terminated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth doing:

\b(?:https?|ftp)://\S+\b

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to use re.compile to pre-compile the regular expression to speed things up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that regex is far better.

for i in range(0, len(lines)):
line = lines[i]
match = re.search(urlregex, line)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do a substitution?

if match:
begining, end = match.span()
lines[i] = '%s'*3 % (line[:begining],
anchor(match.group(0)), line[end:])

data = {}
data.update(self._get_suite_logs_info(user, suite))
return template.render(
Expand Down