# Path: sippy/SipHeader.py
# class SipHeader(object):
#     name = None
#     body = None
# 
#     def __init__(self, s = None, name = None, body = None, bodys = None, fixname = False):
#         if s != None:
#             name, bodys = [x.strip() for x in s.split(':', 1)]
#         if name != None:
#             self.name = name.lower()
#         if body == None:
#             try:
#                 try:
#                     body = hf_types[self.name](bodys)
#                 except KeyError:
#                     body = SipGenericHF(bodys, name)
#             except ESipHeaderCSV as einst:
#                 einst.name = self.name
#                 raise einst
#         self.body = body
#         # If no name is provided use canonic name from the body-specific
#         # class.
#         if self.name == None or fixname:
#             self.name = body.hf_names[0]
# 
#     def __str__(self):
#         return str(self.body.getCanName(self.name)) + ': ' + str(self.body)
# 
#     def localStr(self, local_addr = None, local_port = None, compact = False):
#         return str(self.body.getCanName(self.name, compact)) + ': ' + \
#           self.body.localStr(local_addr, local_port)
# 
#     def getBody(self):
#         if not self.body.parsed:
#             self.body.parse()
#         return self.body
# 
#     def getBCopy(self):
#         return self.body.getCopy()
# 
#     def getCopy(self):
#         cself = self.__class__(name = self.name, body = self.body.getCopy())
#         return cself 
# 
# Path: sippy/SipContentLength.py
# class SipContentLength(SipNumericHF):
#     hf_names = ('content-length', 'l')
# 
#     def getCanName(self, name, compact = False):
#         if compact:
#             return 'l'
#         return 'Content-Length'
# 
# Path: sippy/SipContentType.py
# class SipContentType(SipGenericHF):
#     hf_names = ('content-type', 'c')
# 
#     def __init__(self, body):
#         SipGenericHF.__init__(self, body)
#         self.parsed = True
# 
#     def getCanName(self, name, compact = False):
#         if compact:
#             return 'c'
#         return 'Content-Type'
# 
# Path: sippy/MsgBody.py
# class MsgBody(object):
#     content = None
#     mtype = None
#     needs_update = True
#     parsed = False
# 
#     def __init__(self, content = None, mtype = 'application/sdp', cself = None):
#         if content != None:
#             self.mtype = mtype
#             self.content = content
#         else:
#             if type(cself.content) in str_types:
#                 self.content = cself.content
#             else:
#                 self.content = cself.content.getCopy()
#             self.mtype = cself.mtype
#             self.parsed = True
# 
#     def parse(self):
#         if not self.parsed:
#             if self.mtype in b_types:
#                 self.content = b_types[self.mtype](self.content)
#             self.parsed = True
# 
#     def __str__(self):
#         return str(self.content)
# 
#     def localStr(self, local_addr = None, local_port = None):
#         if type(self.content) in str_types:
#             return self.content
#         return self.content.localStr(local_addr, local_port)
# 
#     def getType(self):
#         return self.mtype
# 
#     def getCopy(self):
#         if not self.parsed:
#             return MsgBody(self.content)
#         return MsgBody(cself = self)
# 
# Path: sippy/ESipHeaderCSV.py
# class ESipHeaderCSV(Exception):
#     def __init__(self, name, bodys):
#         Exception.__init__(self)
#         self.name = name
#         self.bodys = bodys
# 
# Path: sippy/ESipHeaderIgnore.py
# class ESipHeaderIgnore(Exception):
#     def __init__(self):
#         Exception.__init__(self)
# 
# Path: sippy/ESipParseException.py
# class ESipParseException(Exception):
#     sip_response = None
#     arg = None
# 
#     def __init__(self, arg, sip_response = None):
#         self.arg = arg
#         self.sip_response = sip_response
# 
#     def __str__(self):
#         return str(self.arg)


from sippy.SipHeader import SipHeader
from sippy.SipContentLength import SipContentLength
from sippy.SipContentType import SipContentType
from sippy.MsgBody import MsgBody
from sippy.ESipHeaderCSV import ESipHeaderCSV
from sippy.ESipHeaderIgnore import ESipHeaderIgnore
from sippy.ESipParseException import ESipParseException
    def __init__(self, buf = None):
        self.headers = []
        if buf == None:
            return
        # Locate a body
        self.__mbody = None
        for bdel in ('\r\n\r\n', '\r\r', '\n\n'):
            boff = buf.find(bdel)
            if boff != -1:
                self.__mbody = buf[boff + len(bdel):]
                buf = buf[:boff]
                if len(self.__mbody) == 0:
                    self.__mbody = None
                break
        # Split message into lines and put aside start line
        lines = buf.splitlines()
        self.setSL(lines[0])
        i = 2
        while i < len(lines):
            if lines[i][0] in (' ', '\t'):
                lines[i - 1] += ' ' + lines[i].strip()
                del lines[i]
            else:
                i += 1
        # Parse headers
        self.__content_type = None
        self.__content_length = None
        header_names = []
        for line in lines[1:]:
            try:

# Path: sippy/SipHeader.py
# class SipHeader(object):
#     name = None
#     body = None
# 
#     def __init__(self, s = None, name = None, body = None, bodys = None, fixname = False):
#         if s != None:
#             name, bodys = [x.strip() for x in s.split(':', 1)]
#         if name != None:
#             self.name = name.lower()
#         if body == None:
#             try:
#                 try:
#                     body = hf_types[self.name](bodys)
#                 except KeyError:
#                     body = SipGenericHF(bodys, name)
#             except ESipHeaderCSV as einst:
#                 einst.name = self.name
#                 raise einst
#         self.body = body
#         # If no name is provided use canonic name from the body-specific
#         # class.
#         if self.name == None or fixname:
#             self.name = body.hf_names[0]
# 
#     def __str__(self):
#         return str(self.body.getCanName(self.name)) + ': ' + str(self.body)
# 
#     def localStr(self, local_addr = None, local_port = None, compact = False):
#         return str(self.body.getCanName(self.name, compact)) + ': ' + \
#           self.body.localStr(local_addr, local_port)
# 
#     def getBody(self):
#         if not self.body.parsed:
#             self.body.parse()
#         return self.body
# 
#     def getBCopy(self):
#         return self.body.getCopy()
# 
#     def getCopy(self):
#         cself = self.__class__(name = self.name, body = self.body.getCopy())
#         return cself 
# 
# Path: sippy/SipContentLength.py
# class SipContentLength(SipNumericHF):
#     hf_names = ('content-length', 'l')
# 
#     def getCanName(self, name, compact = False):
#         if compact:
#             return 'l'
#         return 'Content-Length'
# 
# Path: sippy/SipContentType.py
# class SipContentType(SipGenericHF):
#     hf_names = ('content-type', 'c')
# 
#     def __init__(self, body):
#         SipGenericHF.__init__(self, body)
#         self.parsed = True
# 
#     def getCanName(self, name, compact = False):
#         if compact:
#             return 'c'
#         return 'Content-Type'
# 
# Path: sippy/MsgBody.py
# class MsgBody(object):
#     content = None
#     mtype = None
#     needs_update = True
#     parsed = False
# 
#     def __init__(self, content = None, mtype = 'application/sdp', cself = None):
#         if content != None:
#             self.mtype = mtype
#             self.content = content
#         else:
#             if type(cself.content) in str_types:
#                 self.content = cself.content
#             else:
#                 self.content = cself.content.getCopy()
#             self.mtype = cself.mtype
#             self.parsed = True
# 
#     def parse(self):
#         if not self.parsed:
#             if self.mtype in b_types:
#                 self.content = b_types[self.mtype](self.content)
#             self.parsed = True
# 
#     def __str__(self):
#         return str(self.content)
# 
#     def localStr(self, local_addr = None, local_port = None):
#         if type(self.content) in str_types:
#             return self.content
#         return self.content.localStr(local_addr, local_port)
# 
#     def getType(self):
#         return self.mtype
# 
#     def getCopy(self):
#         if not self.parsed:
#             return MsgBody(self.content)
#         return MsgBody(cself = self)
# 
# Path: sippy/ESipHeaderCSV.py
# class ESipHeaderCSV(Exception):
#     def __init__(self, name, bodys):
#         Exception.__init__(self)
#         self.name = name
#         self.bodys = bodys
# 
# Path: sippy/ESipHeaderIgnore.py
# class ESipHeaderIgnore(Exception):
#     def __init__(self):
#         Exception.__init__(self)
# 
# Path: sippy/ESipParseException.py
# class ESipParseException(Exception):
#     sip_response = None
#     arg = None
# 
#     def __init__(self, arg, sip_response = None):
#         self.arg = arg
#         self.sip_response = sip_response
# 
#     def __str__(self):
#         return str(self.arg)


# Path: sippy/SipMsg.py
from sippy.SipHeader import SipHeader
from sippy.SipContentLength import SipContentLength
from sippy.SipContentType import SipContentType
from sippy.MsgBody import MsgBody
from sippy.ESipHeaderCSV import ESipHeaderCSV
from sippy.ESipHeaderIgnore import ESipHeaderIgnore
from sippy.ESipParseException import ESipParseException

    def __init__(self, buf = None):
        self.headers = []
        if buf == None:
            return
        # Locate a body
        self.__mbody = None
        for bdel in ('\r\n\r\n', '\r\r', '\n\n'):
            boff = buf.find(bdel)
            if boff != -1:
                self.__mbody = buf[boff + len(bdel):]
                buf = buf[:boff]
                if len(self.__mbody) == 0:
                    self.__mbody = None
                break
        # Split message into lines and put aside start line
        lines = buf.splitlines()
        self.setSL(lines[0])
        i = 2
        while i < len(lines):
            if lines[i][0] in (' ', '\t'):
                lines[i - 1] += ' ' + lines[i].strip()
                del lines[i]
            else:
                i += 1
        # Parse headers
        self.__content_type = None
        self.__content_length = None
        header_names = []
        for line in lines[1:]:
            try:

                header = SipHeader(line, fixname = True)