def handle_starttag(self, tag, attrs): attr_d = dict(attrs) title = attr_d.get('title', '').strip() if tag in self.link_types.keys(): url_attr, rels = self.link_types[tag] if not rels or attr_d.get("rel", None) in rels: target = attr_d.get(url_attr, "") if target: if "#" in target: target = target[:target.index('#')] for proc in self.link_procs: proc(self.base, target, tag, title) elif tag == 'base': self.base = attr_d.get('href', self.base) elif tag == 'meta' and \ attr_d.get('http-equiv', '').lower() == 'content-type': ct = attr_d.get('content', None) if ct: try: media_type, params = ct.split(";", 1) except ValueError: media_type, params = ct, '' media_type = media_type.lower() param_dict = {} for param in rh.split_string( params, syntax.PARAMETER, "\s*;\s*" ): try: a, v = param.split("=", 1) param_dict[a.lower()] = rh.unquote_string(v) except ValueError: param_dict[param.lower()] = None self.doc_enc = param_dict.get('charset', self.doc_enc)
def handle_starttag(self, tag: str, attrs: List[Tuple[str, str]]) -> None: attr_d = dict(attrs) title = attr_d.get('title', '').strip() if tag in self.link_types: url_attr, rels = self.link_types[tag] if not rels or attr_d.get("rel", None) in rels: target = attr_d.get(url_attr, "") if target: if "#" in target: target = target[:target.index('#')] for proc in self.link_procs: proc(self.message.base_uri, target, tag, title) elif tag == 'base': self.message.base_uri = attr_d.get('href', self.message.base_uri) elif tag == 'meta' and attr_d.get('http-equiv', '').lower() == 'content-type': ct = attr_d.get('content', None) if ct: try: media_type, params = ct.split(";", 1) except ValueError: media_type, params = ct, '' media_type = media_type.lower() param_dict = {} for param in headers.split_string(params, rfc7231.parameter, r"\s*;\s*"): try: a, v = param.split("=", 1) param_dict[a.lower()] = headers.unquote_string(v) except ValueError: param_dict[param.lower()] = None self.message.character_encoding = param_dict.get( 'charset', self.message.character_encoding)
def handle_starttag(self, tag: str, attrs: List[Tuple[str, str]]) -> None: attr_d = dict(attrs) title = attr_d.get('title', '').strip() if tag in self.link_types: url_attr, rels = self.link_types[tag] if not rels or attr_d.get("rel", None) in rels: target = attr_d.get(url_attr, "") if target: if "#" in target: target = target[:target.index('#')] for proc in self.link_procs: proc(self.message.base_uri, target, tag, title) elif tag == 'base': self.message.base_uri = attr_d.get('href', self.message.base_uri) elif tag == 'meta' and attr_d.get('http-equiv', '').lower() == 'content-type': ct = attr_d.get('content', None) if ct: try: media_type, params = ct.split(";", 1) except ValueError: media_type, params = ct, '' media_type = media_type.lower() param_dict = {} for param in headers.split_string(params, rfc7231.parameter, r"\s*;\s*"): try: a, v = param.split("=", 1) param_dict[a.lower()] = headers.unquote_string(v) except ValueError: param_dict[param.lower()] = None self.message.character_encoding = param_dict.get('charset', self.message.character_encoding)
def parse(self, field_value: str, add_note: AddNoteMethodType) -> Tuple[str, str]: try: attr, attr_val = field_value.split("=", 1) attr_val = headers.unquote_string(attr_val) except ValueError: attr = field_value attr_val = None return (attr.lower(), attr_val)
def parse(subject, value, red): try: attr, attr_val = value.split("=", 1) attr_val = rh.unquote_string(attr_val) except ValueError: attr = value attr_val = None return (attr.lower(), attr_val)
def test_unquote_string(self): i = 0 for (instr, expected_str, expected_notes) in [ ('foo', 'foo', []), ('"foo"', 'foo', []), (r'"fo\"o"', 'fo"o', []), (r'"f\"o\"o"', 'f"o"o', []), (r'"fo\\o"', r'fo\o', []), (r'"f\\o\\o"', r'f\o\o', []), (r'"fo\o"', 'foo', []), ]: out_str = headers.unquote_string(str(instr)) self.assertEqual(expected_str, out_str, "[%s] %s != %s" % (i, str(expected_str), str(out_str))) i += 1
def parse(self, field_value: str, add_note: AddNoteMethodType) -> Tuple[str, str]: try: directive_name, directive_val = field_value.split("=", 1) directive_val = headers.unquote_string(directive_val) except ValueError: directive_name = field_value directive_val = None directive_name = directive_name.lower() if directive_name in ["max-age", "s-maxage"]: try: directive_val = int(directive_val) # type: ignore except (ValueError, TypeError): add_note(BAD_CC_SYNTAX, bad_cc_attr=directive_name) raise ValueError return (directive_name, directive_val)
def test_unquote_string(self): i = 0 for (instr, expected_str, expected_notes) in [ ('foo', 'foo', []), ('"foo"', 'foo', []), (r'"fo\"o"', 'fo"o', []), (r'"f\"o\"o"', 'f"o"o', []), (r'"fo\\o"', r'fo\o', []), (r'"f\\o\\o"', r'f\o\o', []), (r'"fo\o"', 'foo', []), ]: out_str = headers.unquote_string(str(instr)) self.assertEqual( expected_str, out_str, "[%s] %s != %s" % (i, str(expected_str), str(out_str))) i += 1
def parse(self, field_value: str, add_note: AddNoteMethodType) -> Tuple[str, str]: try: directive_name, directive_val = field_value.split("=", 1) directive_val = headers.unquote_string(directive_val) except ValueError: directive_name = field_value directive_val = None directive_name = directive_name.lower() if directive_name in ['max-age', 's-maxage']: try: directive_val = int(directive_val) # type: ignore except (ValueError, TypeError): add_note(BAD_CC_SYNTAX, bad_cc_attr=directive_name) raise ValueError return (directive_name, directive_val)
def parse(subject, value, red): try: directive_name, directive_val = value.split("=", 1) directive_val = rh.unquote_string(directive_val) except ValueError: directive_name = value directive_val = None directive_name = directive_name.lower() # TODO: warn on upper-cased directives? if directive_name in ['max-age', 's-maxage']: try: directive_val = int(directive_val) except (ValueError, TypeError): red.add_note(subject, rs.BAD_CC_SYNTAX, bad_cc_attr=directive_name ) return None return (directive_name, directive_val)
def test_unquote_string(self): i = 0 for (instr, expected_str, expected_notes) in [ ('foo', 'foo', []), ('"foo"', 'foo', []), (r'"fo\"o"', 'fo"o', []), (r'"f\"o\"o"', 'f"o"o', []), (r'"fo\\o"', r'fo\o', []), (r'"f\\o\\o"', r'f\o\o', []), (r'"fo\o"', 'foo', []), ]: self.red.__init__() out_str = headers.unquote_string(unicode(instr)) diff = set( [n.__name__ for n in expected_notes]).symmetric_difference( set(self.red.note_classes) ) self.assertEqual(len(diff), 0, "[%s] Mismatched notes: %s" % (i, diff) ) self.assertEqual(expected_str, out_str, "[%s] %s != %s" % (i, str(expected_str), str(out_str))) i += 1
def parse(subject, value, red): if value[:2] == 'W/': return (True, rh.unquote_string(value[2:])) else: return (False, rh.unquote_string(value))
def parse(self, field_value: str, add_note: AddNoteMethodType) -> Tuple[bool, str]: if field_value[:2] == 'W/': return (True, headers.unquote_string(field_value[2:])) else: return (False, headers.unquote_string(field_value))