def format_body_sample(self, red): """show the stored body sample""" if not hasattr(red, "body_sample"): return "" try: uni_sample = unicode(red.body_sample, red.res_body_enc, 'ignore') except LookupError: uni_sample = unicode(red.body_sample, 'utf-8', 'ignore') safe_sample = e_html(uni_sample) message = "" for tag, link_set in red.links.items(): for link in link_set: def link_to(matchobj): try: qlink = urljoin(red.base_uri, link) except ValueError, why: pass # TODO: pass link problem upstream? # e.g., ValueError("Invalid IPv6 URL") return r"%s<a href='%s' class='nocode'>%s</a>%s" % ( matchobj.group(1), u"?uri=%s" % e_query_arg(qlink), e_html(link), matchobj.group(1) ) safe_sample = re.sub(r"(['\"])%s\1" % \ re.escape(link), link_to, safe_sample)
def format_body_sample(self, state): """show the stored body sample""" try: uni_sample = unicode(self.body_sample, state.response.character_encoding, 'ignore' ) except LookupError: uni_sample = unicode(self.body_sample, 'utf-8', 'ignore') safe_sample = e_html(uni_sample) message = "" for tag, link_set in state.links.items(): for link in link_set: def link_to(matchobj): try: qlink = urljoin(state.response.base_uri, link) except ValueError, why: pass # TODO: pass link problem upstream? # e.g., ValueError("Invalid IPv6 URL") return r"%s<a href='%s' class='nocode'>%s</a>%s" % ( matchobj.group(1), u"?uri=%s&req_hdr=Referer%%3A%s" % ( e_query_arg(qlink), e_query_arg(state.response.base_uri) ), e_html(link), matchobj.group(1) ) safe_sample = re.sub(r"(['\"])%s\1" % \ re.escape(link), link_to, safe_sample)
def start_output(self): extra_title = u" <span class='save'>" if self.kw.get('is_saved', None): extra_title += u" saved " if self.kw.get('check_type', None): extra_title += "%s response" % e_html(self.kw['check_type']) extra_title += u"</span>" if self.kw.get('is_blank', None): extra_body_class = u"blank" else: extra_body_class = u"" if self.kw.get('descend', False): descend = u"&descend=True" else: descend = u'' self.output(html_header.__doc__ % { u'static': static_root, u'version': __version__, u'html_uri': e_html(self.uri), u'js_uri': e_js(self.uri), u'config': urllib.quote(json.dumps({ u'redbot_uri': self.uri, u'redbot_req_hdrs': self.req_hdrs, u'redbot_version': __version__ }, ensure_ascii=False)), u'js_req_hdrs': u", ".join([u'["%s", "%s"]' % ( e_js(n), e_js(v)) for n,v in self.req_hdrs]), u'extra_js': self.format_extra(u'.js'), u'test_id': self.kw.get('test_id', u""), u'extra_title': extra_title, u'extra_body_class': extra_body_class, u'descend': descend })
def format_category(self, category, state): """ For a given category, return all of the non-detail notes in it as an HTML list. """ notes = [note for note in state.notes if note.category == category] if not notes: return nl out = [] if [note for note in notes]: out.append(u"<h3>%s</h3>\n<ul>\n" % category) for note in notes: out.append( u"""\ <li class='%s note' data-subject='%s' data-name='noteid-%s'> <span>%s</span> </li>""" % ( note.level, e_html(note.subject), id(note), e_html(note.show_summary(self.lang)) ) ) self.hidden_text.append( ("noteid-%s" % id(note), note.show_text(self.lang)) ) if hasattr(state, "subreqs"): subreq = state.subreqs.get(note.subrequest, None) smsgs = [note for note in getattr(subreq, "notes", []) if \ note.level in [rs.l.BAD]] if smsgs: out.append(u"<ul>") for sm in smsgs: out.append(u"""\ <li class='%s note' data-subject='%s' name='msgid-%s'> <span>%s</span> </li>""" % ( sm.level, e_html(sm.subject), id(sm), e_html(sm.show_summary(self.lang)) ) ) self.hidden_text.append( (u"msgid-%s" % id(sm), sm.show_text(self.lang)) ) out.append(u"</ul>") out.append(u"</ul>\n") return nl.join(out)
def format_category(self, category, red): """ For a given category, return all of the non-detail messages in it as an HTML list. """ messages = [msg for msg in red.messages if msg.category == category] if not messages: return nl out = [] if [msg for msg in messages]: out.append(u"<h3>%s</h3>\n<ul>\n" % category) for m in messages: out.append( u"""\ <li class='%s msg' data-subject='%s' data-name='msgid-%s'> <span>%s</span> </li>""" % ( m.level, e_html(m.subject), id(m), e_html(m.show_summary(self.lang)) ) ) self.hidden_text.append( ("msgid-%s" % id(m), m.show_text(self.lang)) ) subreq = red.subreqs.get(m.subrequest, None) smsgs = [msg for msg in getattr(subreq, "messages", []) if \ msg.level in [rs.l.BAD]] if smsgs: out.append(u"<ul>") for sm in smsgs: out.append(u"""\ <li class='%s msg' data-subject='%s' name='msgid-%s'> <span>%s</span> </li>""" % ( sm.level, e_html(sm.subject), id(sm), e_html(sm.show_summary(self.lang)) ) ) self.hidden_text.append( ("msgid-%s" % id(sm), sm.show_text(self.lang)) ) out.append(u"</ul>") out.append(u"</ul>\n") return nl.join(out)
def format_response(self, state): "Return the HTTP response line and headers as HTML" offset = 0 headers = [] for (name, value) in state.response.headers: offset += 1 headers.append(self.format_header(name, value, offset)) return \ u" <span class='status'>HTTP/%s %s %s</span>\n" % ( e_html(state.response.version), e_html(state.response.status_code), e_html(state.response.status_phrase) ) + \ nl.join(headers)
def format_response(self, red): "Return the HTTP response line and headers as HTML" offset = 0 headers = [] for (name, value) in red.res_hdrs: offset += 1 headers.append(self.format_header(name, value, offset)) return \ u" <span class='status'>HTTP/%s %s %s</span>\n" % ( e_html(str(red.res_version)), e_html(str(red.res_status)), e_html(red.res_phrase) ) + \ nl.join(headers)
def start_output(self): if self.kw.get('is_saved', None): extra_title = " <span class='save'>saved results for...</span>" else: extra_title = "" if self.kw.get('is_blank', None): extra_body_class = "blank" else: extra_body_class = "" if self.kw.get('descend', False): descend = "&descend=True" else: descend = '' self.output(html_header.__doc__ % { 'static': static_root, 'version': droid.__version__, 'html_uri': e_html(self.uri), 'js_uri': e_js(self.uri), 'config': urllib.quote(json.dumps({ 'redbot_uri': self.uri, 'redbot_req_hdrs': self.req_hdrs, 'redbot_version': droid.__version__ })), 'js_req_hdrs': ", ".join(['["%s", "%s"]' % ( e_js(n), e_js(v)) for n,v in self.req_hdrs]), 'extra_js': self.format_extra('.js'), 'test_id': self.kw.get('test_id', ""), 'extra_title': extra_title, 'extra_body_class': extra_body_class, 'descend': descend })
def format_problems(self): out = ['<br /><h2>Problems</h2><ol>'] for m in self.problems: out.append(u"""\ <li class='%s %s msg' name='msgid-%s'><span>%s</span></li>""" % ( m.level, e_html(m.subject), id(m), e_html(m.summary[self.lang] % m.vars) ) ) self.hidden_text.append( ("msgid-%s" % id(m), m.text[self.lang] % m.vars) ) out.append(u"</ol>\n") return nl.join(out)
def format_header(self, name, value, offset): "Return an individual HTML header as HTML" token_name = "header-%s" % name.lower() py_name = "HDR_" + name.upper().replace("-", "_") if hasattr(defns, py_name) and token_name not in \ [i[0] for i in self.hidden_text]: defn = getattr(defns, py_name)[self.lang] % { 'field_name': name, } self.hidden_text.append((token_name, defn)) return u"""\ <span data-offset='%s' data-name='%s' class='hdr'>%s:%s</span>""" % ( offset, e_html(name.lower()), e_html(name), self.header_presenter.Show(name, value) )
def status(self, message): "Update the status bar of the browser" self.output(u""" <script> <!-- %3.3f window.status="%s"; --> </script> """ % (thor.time() - self.start, e_html(message)))
def BARE_URI(self, name, value): "Present a bare URI header value" value = value.rstrip() svalue = value.lstrip() space = len(value) - len(svalue) return u"%s<a href='?uri=%s'>%s</a>" % ( " " * space, e_query_arg(urljoin(self.URI, svalue)), self.I(e_html(svalue), len(name)) )
def format_category(self, category, state): """ For a given category, return all of the non-detail notes in it as an HTML list. """ notes = [note for note in state.notes if note.category == category] if not notes: return nl out = [] out.append(u"<h3>%s\n" % category) if category in self.note_responses.keys(): for check_type in self.note_responses[category]: if not state.subreqs.has_key(check_type): continue out.append(u'<span class="req_link"> (<a href="?%s">%s response</a>' % \ (self.req_qs(check_type=check_type), check_type) ) smsgs = [note for note in getattr(state.subreqs[check_type], "notes", []) if \ note.level in [rs.l.BAD]] if len(smsgs) == 1: out.append(" - %i warning\n" % len(smsgs)) elif smsgs: out.append(" - %i warnings\n" % len(smsgs)) out.append(u')</span>\n') out.append(u"</h3>\n") out.append(u"<ul>\n") for note in notes: out.append( u"""\ <li class='%s note' data-subject='%s' data-name='noteid-%s'> <span>%s</span> </li>""" % ( note.level, e_html(note.subject), id(note), e_html(note.show_summary(self.lang)), ) ) self.hidden_text.append( ("noteid-%s" % id(note), note.show_text(self.lang)) ) out.append(u"</ul>\n") return nl.join(out)
def BARE_URI(self, name, value): "Present a bare URI header value" value = value.rstrip() svalue = value.lstrip() space = len(value) - len(svalue) return u"%s<a href='?%s'>%s</a>" % ( " " * space, self.formatter.req_qs(svalue, use_stored=False), self.I(e_html(svalue), len(name)) )
def Show(self, name, value): """ Return the given header name/value pair after presentation processing. """ name = name.lower() name_token = name.replace('-', '_') if name_token[0] != "_" and hasattr(self, name_token): return getattr(self, name_token)(name, value) else: return self.I(e_html(value), len(name))
def start_output(self): if self.kw.get('is_saved', None): extra_title = " <span class='save'>saved results for...</span>" else: extra_title = "" if self.kw.get('is_blank', None): extra_body_class = "blank" else: extra_body_class = "" if self.kw.get('descend', False): descend = "&descend=True" else: descend = '' self.output( html_header.__doc__ % { 'static': static_root, 'version': droid.__version__, 'html_uri': e_html(self.uri), 'js_uri': e_js(self.uri), 'config': urllib.quote( json.dumps({ 'redbot_uri': self.uri, 'redbot_req_hdrs': self.req_hdrs, 'redbot_version': droid.__version__ })), 'js_req_hdrs': ", ".join([ '["%s", "%s"]' % (e_js(n), e_js(v)) for n, v in self.req_hdrs ]), 'extra_js': self.format_extra('.js'), 'test_id': self.kw.get('test_id', ""), 'extra_title': extra_title, 'extra_body_class': extra_body_class, 'descend': descend })
def format_footer(self): "page footer" return u"""\ <br /> <div class="footer"> <p class="version">this is RED %(version)s.</p> <p class="navigation"> <a href="https://REDbot.org/about/">about</a> | <script type="text/javascript"> document.write('<a href="#help" id="help"><strong>help</strong></a> |') </script> <a href="https://REDbot.org/project">project</a> | <span class="help">Drag the bookmarklet to your bookmark bar - it makes checking easy!</span> <a href="javascript:location%%20=%%20'%(baseuri)s?uri='+encodeURIComponent(location);%%20void%%200" title="drag me to your toolbar to use RED any time.">RED</a> bookmarklet </p> </div> """ % { 'baseuri': e_html(self.ui_uri), 'version': __version__, }
def format_droid(self, red): out = [u'<tr class="droid %s">'] m = 50 if red.parsed_hdrs.get('content-type', [""])[0][:6] == 'image/': cl = " class='preview'" else: cl = "" if len(red.uri) > m: out.append(u"""\ <td class="uri"> <a href="%s" title="%s"%s>%s<span class="fade1">%s</span><span class="fade2">%s</span><span class="fade3">%s</span> </a> </td>""" % ( u"?%s" % self.req_qs(red.uri), e_html(red.uri), cl, e_html(red.uri[:m-2]), e_html(red.uri[m-2]), e_html(red.uri[m-1]), e_html(red.uri[m]), ) ) else: out.append( u'<td class="uri"><a href="%s" title="%s"%s>%s</a></td>' % ( u"?%s" % self.req_qs(red.uri), e_html(red.uri), cl, e_html(red.uri) ) ) if red.res_complete: if red.res_status in ['301', '302', '303', '307'] and \ red.parsed_hdrs.has_key('location'): out.append( u'<td><a href="?descend=True&%s">%s</a></td>' % ( self.req_qs(red.parsed_hdrs['location']), red.res_status ) ) elif red.res_status in ['400', '404', '410']: out.append(u'<td class="bad">%s</td>' % red.res_status) else: out.append(u'<td>%s</td>' % red.res_status) # pconn out.append(self.format_yes_no(red.store_shared)) out.append(self.format_yes_no(red.store_private)) out.append(self.format_time(red.age)) out.append(self.format_time(red.freshness_lifetime)) out.append(self.format_yes_no(red.stale_serveable)) out.append(self.format_yes_no(red.ims_support)) out.append(self.format_yes_no(red.inm_support)) if red.gzip_support: out.append(u"<td>%s%%</td>" % red.gzip_savings) else: out.append(self.format_yes_no(red.gzip_support)) out.append(self.format_yes_no(red.partial_support)) problems = [m for m in red.messages if \ m.level in [rs.l.WARN, rs.l.BAD]] # TODO: problems += sum([m[2].messages for m in red.messages if # m[2] != None], []) out.append(u"<td>") pr_enum = [] for problem in problems: if problem not in self.problems: self.problems.append(problem) pr_enum.append(self.problems.index(problem)) # add the problem number to the <tr> so we can highlight out[0] = out[0] % u" ".join(["%d" % p for p in pr_enum]) # append the actual problem numbers to the final <td> for p in pr_enum: m = self.problems[p] out.append("<span class='prob_num'> %s <span class='hidden'>%s</span></span>" % ( p + 1, e_html(m.show_summary(self.lang)) ) ) else: if red.res_error == None: err = "response incomplete" else: err = red.res_error.desc or 'unknown problem' out.append('<td colspan="11">%s' % err) out.append(u"</td>") out.append(u'</tr>') return nl.join(out)