Package dns :: Package rdtypes :: Package ANY :: Module URI |
[hide private]
[frames] | no frames]

# Source Code for Module dns.rdtypes.ANY.URI

     1  # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
     2  # Copyright (C) 2015 Red Hat, Inc.
     3  #
     4  # Permission to use, copy, modify, and distribute this software and its
     5  # documentation for any purpose with or without fee is hereby granted,
     6  # provided that the above copyright notice and this permission notice
     7  # appear in all copies.
     8  #
     9  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
    10  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    11  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
    12  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    13  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    14  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
    15  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    16
    17  import struct
    18
    19  import dns.exception
    20  import dns.rdata
    21  import dns.name
    22  from dns._compat import text_type
    23
    24
    25 -class URI(dns.rdata.Rdata):
    26
    27      """URI record
    28
    29      @ivar priority: the priority
    30      @type priority: int
    31      @ivar weight: the weight
    32      @type weight: int
    33      @ivar target: the target host
    34      @type target: dns.name.Name object
    35      @see: draft-faltstrom-uri-13"""
    36
    37      __slots__ = ['priority', 'weight', 'target']
    38
    39 -    def __init__(self, rdclass, rdtype, priority, weight, target):
    40          super(URI, self).__init__(rdclass, rdtype)
    41          self.priority = priority
    42          self.weight = weight
    43          if len(target) < 1:
    44              raise dns.exception.SyntaxError("URI target cannot be empty")
    45          if isinstance(target, text_type):
    46              self.target = target.encode()
    47          else:
    48              self.target = target
    49
    50 -    def to_text(self, origin=None, relativize=True, **kw):
    51          return '%d %d "%s"' % (self.priority, self.weight,
    52                                 self.target.decode())
    53
    54      @classmethod
    55 -    def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
    56          priority = tok.get_uint16()
    57          weight = tok.get_uint16()
    58          target = tok.get().unescape()
    59          if not (target.is_quoted_string() or target.is_identifier()):
    60              raise dns.exception.SyntaxError("URI target must be a string")
    61          tok.get_eol()
    62          return cls(rdclass, rdtype, priority, weight, target.value)
    63
    64 -    def to_wire(self, file, compress=None, origin=None):
    65          two_ints = struct.pack("!HH", self.priority, self.weight)
    66          file.write(two_ints)
    67          file.write(self.target)
    68
    69      @classmethod
    70 -    def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
    71          if rdlen < 5:
    72              raise dns.exception.FormError('URI RR is shorter than 5 octets')
    73
    74          (priority, weight) = struct.unpack('!HH', wire[current: current + 4])
    75          current += 4
    76          rdlen -= 4
    77          target = wire[current: current + rdlen]
    78          current += rdlen
    79
    80          return cls(rdclass, rdtype, priority, weight, target)
    81