コード例 #1
0
ファイル: link_parse.py プロジェクト: ElijahLynn/redbot
 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)
コード例 #2
0
 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)
コード例 #3
0
ファイル: link_parse.py プロジェクト: optionalg/redbot
 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)
コード例 #4
0
ファイル: keep_alive.py プロジェクト: mnot/redbot
 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)
コード例 #5
0
ファイル: keep_alive.py プロジェクト: ElijahLynn/redbot
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)
コード例 #6
0
ファイル: keep_alive.py プロジェクト: jugglinmike/redbot
 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)
コード例 #7
0
ファイル: unit_tests.py プロジェクト: mnot/redbot
 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
コード例 #8
0
ファイル: cache_control.py プロジェクト: mnot/redbot
 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)
コード例 #9
0
 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
コード例 #10
0
 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)
コード例 #11
0
ファイル: cache_control.py プロジェクト: ElijahLynn/redbot
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)
コード例 #12
0
ファイル: unit_tests.py プロジェクト: ElijahLynn/redbot
 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
コード例 #13
0
ファイル: etag.py プロジェクト: Sylvhem/redbot
def parse(subject, value, red):
    if value[:2] == 'W/':
        return (True, rh.unquote_string(value[2:]))
    else:
        return (False, rh.unquote_string(value))
コード例 #14
0
ファイル: etag.py プロジェクト: jugglinmike/redbot
 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))
コード例 #15
0
ファイル: etag.py プロジェクト: optionalg/redbot
 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))