Ejemplo n.º 1
0
def test_parse_param():
    uri_str = 'sip:[email protected]'
    uri = Uri(uri_str)
    fromto_hdr = FromToHeader('<sip:[email protected]>;Some=y')
    hp = HParams()
    hp.set_raw('Some', 'y')
    assert fromto_hdr.params == hp
    assert fromto_hdr.uri == uri
Ejemplo n.º 2
0
def test_find_raw_set():
    hparams = HParams()
    hparams.set('expires', 3, 'Expires', '3')
    hparams.set('q', QValue('0.1'), 'q', '0.1')
    hparams.set_raw('MyCustomParam', None)
    assert hparams.find_raw('expires') == '3'
    assert isinstance(hparams.find_raw('q1'), HParamNotFound)
    assert hparams.find_raw('q') == '0.1'
    assert hparams.find_raw('mycustoMParam') is None
Ejemplo n.º 3
0
def test_set_find():
    hparams = HParams()
    hparams.set('expires', 3, 'Expires', '3')
    assert hparams.assemble() == 'Expires=3'
    hparams.set('q', QValue('0.1'), 'q', '0.1')
    assert hparams.assemble() == 'Expires=3;q=0.1'
    hparams.set_raw('MyCustomParam', None)
    assert hparams.assemble() == 'Expires=3;q=0.1;MyCustomParam'
    assert hparams.find('expires') == 3
    assert hparams.find('Expires') == 3
    assert hparams.find('q') == QValue('0.1')
    assert isinstance(hparams.find('q1'), HParamNotFound)
    assert hparams.find_raw('expires') == '3'
Ejemplo n.º 4
0
 def do_parse_contact_params(string):
     try:
         hparams = HParams()
         params_list = parse_params(string, ';')
         for k, v in params_list:
             if k.lower() in (EXPIRES, Q):
                 hparams.set(k.lower(),
                             ContactHeader.parse_param(k.lower(), v), k, v)
             elif check_token(k.lower()):
                 hparams.set_raw(k, v)
             else:
                 raise ContactHeaderError(f'Key {k}={v} is not a token.')
         return hparams, ''
     except (ParserAUXError, HParamsError, ContactHeaderError) as e:
         raise ContactHeaderError(
             f'Cannot parse contact header {string}: {e}')
Ejemplo n.º 5
0
class FromToHeader(BaseSipHeader):
    DEFAULT_HOST = '127.0.0.1'

    def __init__(self, fromto=None):
        self.display_name = None
        self.uri = None
        self.params = HParams()
        if fromto is not None:
            self.display_name, self.uri, self.params = self.parse_fromto(
                fromto)

    def __eq__(self, other):
        if isinstance(other, FromToHeader):
            return self.display_name == other.display_name and self.uri == other.uri and self.params == other.params
        return NotImplemented

    def build(self, header_name):
        hdr = Header(header_name)
        hdr.add_value(self.assemble())
        return hdr

    @staticmethod
    def parse(fromto):
        ft = FromToHeader()
        ft.display_name, ft.uri, ft.params = FromToHeader.parse_fromto(fromto)
        return ft

    @staticmethod
    def parse_fromto(fromto):
        if isinstance(fromto, str):
            return FromToHeader.parse_string(fromto)
        elif isinstance(fromto, Header):
            return FromToHeader.parse_header(fromto)
        else:
            raise FromToError(
                f'Cannot parse fromto {fromto}: should be str or Header not {type(fromto)}'
            )

    @staticmethod
    def parse_header(fromto):
        if not fromto.values:
            raise FromToError(f'Cannot parse fromto {fromto}: no values')
        elif len(fromto.values) > 1:
            raise FromToError(f'Cannot parse fromto {fromto}: multiple values')
        else:
            return FromToHeader.parse_string(fromto.values[0])

    @staticmethod
    def parse_string(fromto):
        nameaddr = NameAddress(fromto)
        params = FromToHeader.parse_params(nameaddr.rest.strip())
        return nameaddr.display_name, nameaddr.uri, params

    @staticmethod
    def parse_params(params_str):
        params = HParams()
        if params_str.startswith(';'):
            params = HParams(params_str[1:])
            tag = params.find_raw(TAG)
            if not isinstance(tag, HParamNotFound):
                if not check_token(tag):
                    raise FromToError(
                        f'Cannot parse params {params_str}: tag value should be token.'
                    )
        return params

    @property
    def tag(self):
        tag = self.params.find_raw(TAG)
        if isinstance(tag, HParamNotFound):
            tag = None
        return tag

    @tag.setter
    def tag(self, value):
        self.params.set_raw(TAG, value)

    @property
    def tag_key(self):
        tag = self.tag
        if tag is None:
            return tag
        return tag.lower()

    def assemble(self):
        nameaddr = NameAddress.assemble(self.display_name, self.uri)
        params = self.params.assemble()
        if params:
            params = f';{params}'
        return f'{nameaddr}{params}'
Ejemplo n.º 6
0
def test_set_raw():
    hparams = HParams()
    hparams.set_raw('a', 'b')
    hparams.set_raw('a', 'c')
    assert hparams.assemble() == 'a=c'
    assert hparams.find_raw('A') == 'c'
Ejemplo n.º 7
0
class ContactHeader(object):
    def __init__(self, contact_string=None):
        self.display_name = None
        self.uri = Uri()
        self.uri.parser_impl = SIPUriParserUnicode()
        self.params = HParams()
        self.rest = None
        if contact_string is not None:
            self.display_name, self.uri, self.params, self.rest = self.parse_hdr(
                contact_string)

    def parse_contact_params(self, contact_params):
        if contact_params.startswith(';'):
            return self.do_parse_contact_params(contact_params[1:])
        return HParams(), ''

    @staticmethod
    def do_parse_contact_params(string):
        try:
            hparams = HParams()
            params_list = parse_params(string, ';')
            for k, v in params_list:
                if k.lower() in (EXPIRES, Q):
                    hparams.set(k.lower(),
                                ContactHeader.parse_param(k.lower(), v), k, v)
                elif check_token(k.lower()):
                    hparams.set_raw(k, v)
                else:
                    raise ContactHeaderError(f'Key {k}={v} is not a token.')
            return hparams, ''
        except (ParserAUXError, HParamsError, ContactHeaderError) as e:
            raise ContactHeaderError(
                f'Cannot parse contact header {string}: {e}')

    def parse_hdr(self, string):
        try:
            nameaddr = NameAddress(string)
            display_name = nameaddr.display_name
            uri = nameaddr.uri
        except NameAddressError as e:
            raise ContactHeaderError(
                f'Cannot parse contact header {string} nameaddress part: {e}')
        contact_params, rest = self.parse_contact_params(nameaddr.rest)
        return display_name, uri, contact_params, rest

    def get_expires(self, default=None):
        expires = self.params.find(EXPIRES)
        if isinstance(expires, HParamNotFound):
            return default
        return expires

    def set_expires(self, expires_val):
        if is_integer(expires_val):
            self.params.set(EXPIRES, expires_val, EXPIRES, expires_val)
        else:
            raise ContactHeaderError(
                f'Cannot set expires parameter {expires_val}: not an integer')

    def get_qvalue(self, default=None):
        qval = self.params.find('q')
        if isinstance(qval, HParamNotFound):
            return default
        return qval

    def set_qvalue(self, qvalue):
        self.params.set(Q, qvalue, Q, qvalue)

    @staticmethod
    def parse_param(param_name, param_value):
        if param_name == EXPIRES:
            try:
                return int(param_value)
            except ValueError:
                raise ContactHeaderError(
                    f'Cannot parse expires param: value {param_value} is not int.'
                )
        if param_name == Q:
            try:
                return QValue(param_value)
            except QValueError as e:
                raise ContactHeaderError(f'Cannot parse q param: {e}')

    def __eq__(self, other):
        if isinstance(other, ContactHeader):
            return self.display_name == other.display_name and self.uri == other.uri and self.params == other.params
        return NotImplemented

    def assemble(self):
        assembled_hparams = self.params.assemble()
        if assembled_hparams:
            assembled_hparams = f';{assembled_hparams}'
        return f'{NameAddress.assemble(self.display_name, self.uri)}{assembled_hparams}'

    def set_param(self, name, value):
        if name.lower() in (EXPIRES, Q):
            try:
                parsed_value = self.parse_param(name, value)
                self.params.set(name.lower(), parsed_value, name, value)
            except ContactHeaderError as e:
                raise ContactHeaderError(
                    f'Cannot set param {name}={value}: {e}')
        elif check_token(name.lower()):
            self.params.set_raw(name, value)
        else:
            raise ContactHeaderError(
                f'Cannot set param {name}={value}: invalid param.')

    def get_param(self, name):
        return self.params.find_raw(name)

    def __repr__(self):
        return self.assemble()