Ticket #3583: sip-messageparser-3583-patch.txt

File sip-messageparser-3583-patch.txt, 55.2 KB (added by wooshie, 10 years ago)

Replaced response codes with corresponding constants, added missing ones.

Line 
1Index: twisted/protocols/sip.py
2===================================================================
3--- twisted/protocols/sip.py    (revision 29927)
4+++ twisted/protocols/sip.py    (working copy)
5@@ -94,7 +94,7 @@
6     488: "Not Acceptable Here",
7     491: "Request Pending",
8     493: "Undecipherable",
9-   
10+
11     500: "Internal Server Error",
12     501: "Not Implemented",
13     502: "Bad Gateway", # no donut
14@@ -102,7 +102,7 @@
15     504: "Server Time-out",
16     505: "SIP Version not supported",
17     513: "Message Too Large",
18-   
19+
20     600: "Busy Everywhere",
21     603: "Decline",
22     604: "Does not exist anywhere",
23@@ -110,8 +110,13 @@
24 }
25 
26 #SIP symbolic error codes
27+OK = 200
28 BAD_REQUEST = 400
29+UNAUTHORIZED = 401
30+NOT_FOUND = 404
31 UNSUPPORTED_URI = 416
32+INTERNAL_SERVER_ERROR = 500
33+NOT_IMPLEMENTED = 501
34 
35 specialCases = {
36     'cseq': 'CSeq',
37@@ -119,41 +124,41 @@
38     'www-authenticate': 'WWW-Authenticate',
39 }
40 
41-def dashCapitalize(s):
42+def dashCapitalize( s ):
43     ''' Capitalize a string, making sure to treat - as a word seperator '''
44-    return '-'.join([ x.capitalize() for x in s.split('-')])
45+    return '-'.join( [ x.capitalize() for x in s.split( '-' )] )
46 
47-def unq(s):
48+def unq( s ):
49     if s[0] == s[-1] == '"':
50         return s[1:-1]
51     return s
52 
53-def DigestCalcHA1(
54+def DigestCalcHA1(
55     pszAlg,
56     pszUserName,
57     pszRealm,
58     pszPassword,
59     pszNonce,
60     pszCNonce,
61-):
62+ ):
63     m = md5()
64-    m.update(pszUserName)
65-    m.update(":")
66-    m.update(pszRealm)
67-    m.update(":")
68-    m.update(pszPassword)
69+    m.update( pszUserName )
70+    m.update( ":" )
71+    m.update( pszRealm )
72+    m.update( ":" )
73+    m.update( pszPassword )
74     HA1 = m.digest()
75     if pszAlg == "md5-sess":
76         m = md5()
77-        m.update(HA1)
78-        m.update(":")
79-        m.update(pszNonce)
80-        m.update(":")
81-        m.update(pszCNonce)
82+        m.update( HA1 )
83+        m.update( ":" )
84+        m.update( pszNonce )
85+        m.update( ":" )
86+        m.update( pszCNonce )
87         HA1 = m.digest()
88-    return HA1.encode('hex')
89+    return HA1.encode( 'hex' )
90 
91-def DigestCalcResponse(
92+def DigestCalcResponse(
93     HA1,
94     pszNonce,
95     pszNonceCount,
96@@ -162,37 +167,37 @@
97     pszMethod,
98     pszDigestUri,
99     pszHEntity,
100-):
101+ ):
102     m = md5()
103-    m.update(pszMethod)
104-    m.update(":")
105-    m.update(pszDigestUri)
106+    m.update( pszMethod )
107+    m.update( ":" )
108+    m.update( pszDigestUri )
109     if pszQop == "auth-int":
110-        m.update(":")
111-        m.update(pszHEntity)
112-    HA2 = m.digest().encode('hex')
113-   
114+        m.update( ":" )
115+        m.update( pszHEntity )
116+    HA2 = m.digest().encode( 'hex' )
117+
118     m = md5()
119-    m.update(HA1)
120-    m.update(":")
121-    m.update(pszNonce)
122-    m.update(":")
123+    m.update( HA1 )
124+    m.update( ":" )
125+    m.update( pszNonce )
126+    m.update( ":" )
127     if pszNonceCount and pszCNonce: # pszQop:
128-        m.update(pszNonceCount)
129-        m.update(":")
130-        m.update(pszCNonce)
131-        m.update(":")
132-        m.update(pszQop)
133-        m.update(":")
134-    m.update(HA2)
135-    hash = m.digest().encode('hex')
136+        m.update( pszNonceCount )
137+        m.update( ":" )
138+        m.update( pszCNonce )
139+        m.update( ":" )
140+        m.update( pszQop )
141+        m.update( ":" )
142+    m.update( HA2 )
143+    hash = m.digest().encode( 'hex' )
144     return hash
145 
146 class Via:
147     """A SIP Via header."""
148 
149-    def __init__(self, host, port=PORT, transport="UDP", ttl=None, hidden=False,
150-                 received=None, rport=None, branch=None, maddr=None):
151+    def __init__( self, host, port=PORT, transport="UDP", ttl=None, hidden=False,
152+                 received=None, rport=None, branch=None, maddr=None ):
153         self.transport = transport
154         self.host = host
155         self.port = port
156@@ -203,63 +208,63 @@
157         self.branch = branch
158         self.maddr = maddr
159 
160-    def toString(self):
161-        s = "SIP/2.0/%s %s:%s" % (self.transport, self.host, self.port)
162+    def toString( self ):
163+        s = "SIP/2.0/%s %s:%s" % ( self.transport, self.host, self.port )
164         if self.hidden:
165             s += ";hidden"
166         for n in "ttl", "branch", "maddr", "received", "rport":
167-            value = getattr(self, n)
168+            value = getattr( self, n )
169             if value == True:
170                 s += ";" + n
171             elif value != None:
172-                s += ";%s=%s" % (n, value)
173+                s += ";%s=%s" % ( n, value )
174         return s
175 
176 
177-def parseViaHeader(value):
178+def parseViaHeader( value ):
179     """Parse a Via header, returning Via class instance."""
180-    parts = value.split(";")
181+    parts = value.split( ";" )
182     sent, params = parts[0], parts[1:]
183-    protocolinfo, by = sent.split(" ", 1)
184+    protocolinfo, by = sent.split( " ", 1 )
185     by = by.strip()
186     result = {}
187-    pname, pversion, transport = protocolinfo.split("/")
188+    pname, pversion, transport = protocolinfo.split( "/" )
189     if pname != "SIP" or pversion != "2.0":
190         raise ValueError, "wrong protocol or version: %r" % value
191     result["transport"] = transport
192     if ":" in by:
193-        host, port = by.split(":")
194-        result["port"] = int(port)
195+        host, port = by.split( ":" )
196+        result["port"] = int( port )
197         result["host"] = host
198     else:
199         result["host"] = by
200     for p in params:
201         # it's the comment-striping dance!
202-        p = p.strip().split(" ", 1)
203-        if len(p) == 1:
204+        p = p.strip().split( " ", 1 )
205+        if len( p ) == 1:
206             p, comment = p[0], ""
207         else:
208             p, comment = p
209         if p == "hidden":
210             result["hidden"] = True
211             continue
212-        parts = p.split("=", 1)
213-        if len(parts) == 1:
214+        parts = p.split( "=", 1 )
215+        if len( parts ) == 1:
216             name, value = parts[0], True
217         else:
218             name, value = parts
219-            if name in ("rport", "ttl"):
220-                value = int(value)
221+            if name in ( "rport", "ttl" ):
222+                value = int( value )
223         result[name] = value
224-    return Via(**result)
225+    return Via( **result )
226 
227 
228 class URL:
229     """A SIP URL."""
230 
231-    def __init__(self, host, username=None, password=None, port=None,
232+    def __init__( self, host, username=None, password=None, port=None,
233                  transport=None, usertype=None, method=None,
234-                 ttl=None, maddr=None, tag=None, other=None, headers=None):
235+                 ttl=None, maddr=None, tag=None, other=None, headers=None ):
236         self.username = username
237         self.host = host
238         self.password = password
239@@ -279,64 +284,64 @@
240         else:
241             self.headers = headers
242 
243-    def toString(self):
244+    def toString( self ):
245         l = []; w = l.append
246-        w("sip:")
247+        w( "sip:" )
248         if self.username != None:
249-            w(self.username)
250+            w( self.username )
251             if self.password != None:
252-                w(":%s" % self.password)
253-            w("@")
254-        w(self.host)
255+                w( ":%s" % self.password )
256+            w( "@" )
257+        w( self.host )
258         if self.port != None:
259-            w(":%d" % self.port)
260+            w( ":%d" % self.port )
261         if self.usertype != None:
262-            w(";user=%s" % self.usertype)
263-        for n in ("transport", "ttl", "maddr", "method", "tag"):
264-            v = getattr(self, n)
265+            w( ";user=%s" % self.usertype )
266+        for n in ( "transport", "ttl", "maddr", "method", "tag" ):
267+            v = getattr( self, n )
268             if v != None:
269-                w(";%s=%s" % (n, v))
270+                w( ";%s=%s" % ( n, v ) )
271         for v in self.other:
272-            w(";%s" % v)
273+            w( ";%s" % v )
274         if self.headers:
275-            w("?")
276-            w("&".join([("%s=%s" % (specialCases.get(h) or dashCapitalize(h), v)) for (h, v) in self.headers.items()]))
277-        return "".join(l)
278+            w( "?" )
279+            w( "&".join( [( "%s=%s" % ( specialCases.get( h ) or dashCapitalize( h ), v ) ) for ( h, v ) in self.headers.items()] ) )
280+        return "".join( l )
281 
282-    def __str__(self):
283+    def __str__( self ):
284         return self.toString()
285-   
286-    def __repr__(self):
287-        return '<URL %s:%s@%s:%r/%s>' % (self.username, self.password, self.host, self.port, self.transport)
288 
289+    def __repr__( self ):
290+        return '<URL %s:%s@%s:%r/%s>' % ( self.username, self.password, self.host, self.port, self.transport )
291 
292-def parseURL(url, host=None, port=None):
293+
294+def parseURL( url, host=None, port=None ):
295     """Return string into URL object.
296 
297     URIs are of of form 'sip:user@example.com'.
298     """
299     d = {}
300-    if not url.startswith("sip:"):
301-        raise ValueError("unsupported scheme: " + url[:4])
302-    parts = url[4:].split(";")
303+    if not url.startswith( "sip:" ):
304+        raise ValueError( "unsupported scheme: " + url[:4] )
305+    parts = url[4:].split( ";" )
306     userdomain, params = parts[0], parts[1:]
307-    udparts = userdomain.split("@", 1)
308-    if len(udparts) == 2:
309+    udparts = userdomain.split( "@", 1 )
310+    if len( udparts ) == 2:
311         userpass, hostport = udparts
312-        upparts = userpass.split(":", 1)
313-        if len(upparts) == 1:
314+        upparts = userpass.split( ":", 1 )
315+        if len( upparts ) == 1:
316             d["username"] = upparts[0]
317         else:
318             d["username"] = upparts[0]
319             d["password"] = upparts[1]
320     else:
321         hostport = udparts[0]
322-    hpparts = hostport.split(":", 1)
323-    if len(hpparts) == 1:
324+    hpparts = hostport.split( ":", 1 )
325+    if len( hpparts ) == 1:
326         d["host"] = hpparts[0]
327     else:
328         d["host"] = hpparts[0]
329-        d["port"] = int(hpparts[1])
330+        d["port"] = int( hpparts[1] )
331     if host != None:
332         d["host"] = host
333     if port != None:
334@@ -344,27 +349,27 @@
335     for p in params:
336         if p == params[-1] and "?" in p:
337             d["headers"] = h = {}
338-            p, headers = p.split("?", 1)
339-            for header in headers.split("&"):
340-                k, v = header.split("=")
341+            p, headers = p.split( "?", 1 )
342+            for header in headers.split( "&" ):
343+                k, v = header.split( "=" )
344                 h[k] = v
345-        nv = p.split("=", 1)
346-        if len(nv) == 1:
347-            d.setdefault("other", []).append(p)
348+        nv = p.split( "=", 1 )
349+        if len( nv ) == 1:
350+            d.setdefault( "other", [] ).append( p )
351             continue
352         name, value = nv
353         if name == "user":
354             d["usertype"] = value
355-        elif name in ("transport", "ttl", "maddr", "method", "tag"):
356+        elif name in ( "transport", "ttl", "maddr", "method", "tag" ):
357             if name == "ttl":
358-                value = int(value)
359+                value = int( value )
360             d[name] = value
361         else:
362-            d.setdefault("other", []).append(p)
363-    return URL(**d)
364+            d.setdefault( "other", [] ).append( p )
365+    return URL( **d )
366 
367 
368-def cleanRequestURL(url):
369+def cleanRequestURL( url ):
370     """Clean a URL from a Request line."""
371     url.transport = None
372     url.maddr = None
373@@ -372,30 +377,30 @@
374     url.headers = {}
375 
376 
377-def parseAddress(address, host=None, port=None, clean=0):
378+def parseAddress( address, host=None, port=None, clean=0 ):
379     """Return (name, uri, params) for From/To/Contact header.
380 
381     @param clean: remove unnecessary info, usually for From and To headers.
382     """
383     address = address.strip()
384     # simple 'sip:foo' case
385-    if address.startswith("sip:"):
386-        return "", parseURL(address, host=host, port=port), {}
387+    if address.startswith( "sip:" ):
388+        return "", parseURL( address, host=host, port=port ), {}
389     params = {}
390-    name, url = address.split("<", 1)
391+    name, url = address.split( "<", 1 )
392     name = name.strip()
393-    if name.startswith('"'):
394+    if name.startswith( '"' ):
395         name = name[1:]
396-    if name.endswith('"'):
397+    if name.endswith( '"' ):
398         name = name[:-1]
399-    url, paramstring = url.split(">", 1)
400-    url = parseURL(url, host=host, port=port)
401+    url, paramstring = url.split( ">", 1 )
402+    url = parseURL( url, host=host, port=port )
403     paramstring = paramstring.strip()
404     if paramstring:
405-        for l in paramstring.split(";"):
406+        for l in paramstring.split( ";" ):
407             if not l:
408                 continue
409-            k, v = l.split("=")
410+            k, v = l.split( "=" )
411             params[k] = v
412     if clean:
413         # rfc 2543 6.21
414@@ -406,16 +411,16 @@
415     return name, url, params
416 
417 
418-class SIPError(Exception):
419-    def __init__(self, code, phrase=None):
420+class SIPError( Exception ):
421+    def __init__( self, code, phrase=None ):
422         if phrase is None:
423             phrase = statusCodes[code]
424-        Exception.__init__(self, "SIP error (%d): %s" % (code, phrase))
425+        Exception.__init__( self, "SIP error (%d): %s" % ( code, phrase ) )
426         self.code = code
427         self.phrase = phrase
428 
429 
430-class RegistrationError(SIPError):
431+class RegistrationError( SIPError ):
432     """Registration was not possible."""
433 
434 
435@@ -423,78 +428,78 @@
436     """A SIP message."""
437 
438     length = None
439-   
440-    def __init__(self):
441+
442+    def __init__( self ):
443         self.headers = util.OrderedDict() # map name to list of values
444         self.body = ""
445         self.finished = 0
446-   
447-    def addHeader(self, name, value):
448+
449+    def addHeader( self, name, value ):
450         name = name.lower()
451-        name = longHeaders.get(name, name)
452+        name = longHeaders.get( name, name )
453         if name == "content-length":
454-            self.length = int(value)
455-        self.headers.setdefault(name,[]).append(value)
456+            self.length = int( value )
457+        self.headers.setdefault( name, [] ).append( value )
458 
459-    def bodyDataReceived(self, data):
460+    def bodyDataReceived( self, data ):
461         self.body += data
462-   
463-    def creationFinished(self):
464-        if (self.length != None) and (self.length != len(self.body)):
465+
466+    def creationFinished( self ):
467+        if ( self.length != None ) and ( self.length != len( self.body ) ):
468             raise ValueError, "wrong body length"
469         self.finished = 1
470 
471-    def toString(self):
472+    def toString( self ):
473         s = "%s\r\n" % self._getHeaderLine()
474         for n, vs in self.headers.items():
475             for v in vs:
476-                s += "%s: %s\r\n" % (specialCases.get(n) or dashCapitalize(n), v)
477+                s += "%s: %s\r\n" % ( specialCases.get( n ) or dashCapitalize( n ), v )
478         s += "\r\n"
479         s += self.body
480         return s
481 
482-    def _getHeaderLine(self):
483+    def _getHeaderLine( self ):
484         raise NotImplementedError
485 
486 
487-class Request(Message):
488+class Request( Message ):
489     """A Request for a URI"""
490 
491 
492-    def __init__(self, method, uri, version="SIP/2.0"):
493-        Message.__init__(self)
494+    def __init__( self, method, uri, version="SIP/2.0" ):
495+        Message.__init__( self )
496         self.method = method
497-        if isinstance(uri, URL):
498+        if isinstance( uri, URL ):
499             self.uri = uri
500         else:
501-            self.uri = parseURL(uri)
502-            cleanRequestURL(self.uri)
503-   
504-    def __repr__(self):
505-        return "<SIP Request %d:%s %s>" % (id(self), self.method, self.uri.toString())
506+            self.uri = parseURL( uri )
507+            cleanRequestURL( self.uri )
508+
509+    def __repr__( self ):
510+        return "<SIP Request %d:%s %s>" % ( id( self ), self.method, self.uri.toString() )
511 
512-    def _getHeaderLine(self):
513-        return "%s %s SIP/2.0" % (self.method, self.uri.toString())
514+    def _getHeaderLine( self ):
515+        return "%s %s SIP/2.0" % ( self.method, self.uri.toString() )
516 
517 
518-class Response(Message):
519+class Response( Message ):
520     """A Response to a URI Request"""
521 
522-    def __init__(self, code, phrase=None, version="SIP/2.0"):
523-        Message.__init__(self)
524+    def __init__( self, code, phrase=None, version="SIP/2.0" ):
525+        Message.__init__( self )
526         self.code = code
527         if phrase == None:
528             phrase = statusCodes[code]
529         self.phrase = phrase
530 
531-    def __repr__(self):
532-        return "<SIP Response %d:%s>" % (id(self), self.code)
533+    def __repr__( self ):
534+        return "<SIP Response %d:%s>" % ( id( self ), self.code )
535 
536-    def _getHeaderLine(self):
537-        return "SIP/2.0 %s %s" % (self.code, self.phrase)
538+    def _getHeaderLine( self ):
539+        return "SIP/2.0 %s %s" % ( self.code, self.phrase )
540 
541 
542-class MessagesParser(basic.LineReceiver):
543+class MessagesParser( basic.LineReceiver ):
544     """
545     A SIP messages parser.
546 
547@@ -517,15 +522,15 @@
548     acceptResponses = 1
549     acceptRequests = 1
550     state = "firstline" # or "headers", "body"
551-    _multiheaders = ['accept','accept-encoding', 'accept-language',
552+    _multiheaders = ['accept', 'accept-encoding', 'accept-language',
553                     'alert-info', 'allow', 'authentication-info',
554-                    'call-info',  'content-encoding', 'content-language',
555-                    'error-info', 'in-reply-to', 'proxy-require',  'require',
556+                    'call-info', 'content-encoding', 'content-language',
557+                    'error-info', 'in-reply-to', 'proxy-require', 'require',
558                     'supported', 'unsupported', 'via', 'warning']
559     _multiAddressHeaders = ['route', 'record-route', 'contact']
560 
561 
562-    def __init__(self, messageReceivedCallback):
563+    def __init__( self, messageReceivedCallback ):
564         """
565         @param messageReceivedCallback: A one-argument callable, to be invoked
566         when a complete message has been parsed.
567@@ -534,25 +539,25 @@
568         self.reset()
569 
570 
571-    def reset(self, remainingData=""):
572+    def reset( self, remainingData="" ):
573         """
574         Prepare the parser to accept a new message.
575         """
576         self.state = "firstline"
577         self.bodyReceived = 0 # how much of the body we received
578         self.message = None
579-        self.setLineMode(remainingData)
580+        self.setLineMode( remainingData )
581 
582 
583-    def invalidMessage(self):
584+    def invalidMessage( self ):
585         """
586         Raise an exception, indicating failure to parse a valid SIP message.
587         """
588         self.dataDone()
589-        raise SIPError(400)
590+        raise SIPError( BAD_REQUEST )
591 
592 
593-    def dataDone(self):
594+    def dataDone( self ):
595         """
596         Signal the end of the message if a complete message has been received,
597         and reset internal state to prepare for a new message.
598@@ -572,11 +577,11 @@
599             msg = self.message
600             self.reset()
601             # RFC 3261, section 18.3
602-            if isinstance(msg, Request):
603-                raise SIPError(BAD_REQUEST)
604+            if isinstance( msg, Request ):
605+                raise SIPError( BAD_REQUEST )
606 
607 
608-    def lineLengthExceeded(self, line):
609+    def lineLengthExceeded( self, line ):
610         """
611         Raise L{SIPError} if lines longer than L{LineReceiver.MAX_LENGTH} are
612         received.
613@@ -584,30 +589,30 @@
614         self.invalidMessage()
615 
616 
617-    def lineReceived(self, line):
618+    def lineReceived( self, line ):
619         """
620         Handle a single SIP message line.
621         """
622         if self.state == "firstline":
623-            while line.startswith("\n") or line.startswith("\r"):
624+            while line.startswith( "\n" ) or line.startswith( "\r" ):
625                 line = line[1:]
626             if not line:
627                 return
628             try:
629-                a, b, c = line.split(" ", 2)
630+                a, b, c = line.split( " ", 2 )
631             except ValueError:
632                 self.invalidMessage()
633             if a == "SIP/2.0" and self.acceptResponses:
634                 # response
635                 try:
636-                    code = int(b)
637+                    code = int( b )
638                 except ValueError:
639                     self.invalidMessage()
640-                self.message = Response(code, c)
641+                self.message = Response( code, c )
642             elif c == "SIP/2.0" and self.acceptRequests:
643-                if not b.startswith("sip:"):
644-                    raise SIPError(416)
645-                self.message = Request(a, b)
646+                if not b.startswith( "sip:" ):
647+                    raise SIPError( 416 )
648+                self.message = Request( a, b )
649             else:
650                 self.invalidMessage()
651                 return
652@@ -625,7 +630,7 @@
653                 #new header
654                 if self.prevline:
655                     try:
656-                        self._processHeaderLine(self.prevline)
657+                        self._processHeaderLine( self.prevline )
658                     except ValueError:
659                         self.invalidMessage()
660                 self.prevline = line
661@@ -636,7 +641,7 @@
662             # from the connection sending us data.
663             self.state = "body"
664             try:
665-                self._processHeaderLine(self.prevline)
666+                self._processHeaderLine( self.prevline )
667             except ValueError:
668                 self.invalidMessage()
669                 return
670@@ -646,111 +651,111 @@
671             self.setRawMode()
672 
673 
674-    def _splitMultiHeader(self, s):
675+    def _splitMultiHeader( self, s ):
676         """
677         Split a header on commas, ignoring commas in quotes and escaped quotes.
678         """
679         headers = []
680         last = 0
681         quoted = False
682-        for i in xrange(len(s)):
683+        for i in xrange( len( s ) ):
684             if s[i] == '"':
685                 quoted = ~quoted
686                 if i == 0:
687                     continue
688-                j = i-1
689+                j = i - 1
690                 while s[j] == '\\':
691                     quoted = not quoted
692-                    j = j-1
693+                    j = j - 1
694             if not quoted and s[i] == ',':
695-                headers.append(s[last:i])
696-                last = i+1
697-        headers.append(s[last:])
698+                headers.append( s[last:i] )
699+                last = i + 1
700+        headers.append( s[last:] )
701         return headers
702 
703 
704-    def _processHeaderLine(self, line):
705+    def _processHeaderLine( self, line ):
706         """
707         Parse a single SIP header.
708         """
709-        name, value = line.split(":", 1)
710+        name, value = line.split( ":", 1 )
711         name, value = name.rstrip().lower(), value.lstrip()
712 
713         if name in self._multiheaders:
714-            multi = value.split(',')
715+            multi = value.split( ',' )
716             if multi:
717                 for v in multi:
718-                    self.message.addHeader(name, v.strip())
719+                    self.message.addHeader( name, v.strip() )
720             else:
721-                self.message.addHeader(v)
722+                self.message.addHeader( v )
723         elif name in self._multiAddressHeaders:
724-            for val in self._splitMultiHeader(value):
725-                self.message.addHeader(name, val)
726+            for val in self._splitMultiHeader( value ):
727+                self.message.addHeader( name, val )
728         else:
729-            self.message.addHeader(name, value)
730+            self.message.addHeader( name, value )
731 
732 
733-    def messageDone(self, remainingData=""):
734+    def messageDone( self, remainingData="" ):
735         """
736         Invoke the C{messageReceived} callback with the completed message
737         object and prepare to receive a new message.
738         """
739         assert self.state == "body"
740         self.message.creationFinished()
741-        self.messageReceived(self.message)
742-        self.reset(remainingData)
743+        self.messageReceived( self.message )
744+        self.reset( remainingData )
745 
746 
747-    def rawDataReceived(self, data):
748+    def rawDataReceived( self, data ):
749         """
750         Handle message body data.
751         """
752         if self.message.length == None:
753-            self.message.bodyDataReceived(data)
754+            self.message.bodyDataReceived( data )
755         else:
756-            dataLen = len(data)
757+            dataLen = len( data )
758             expectedLen = self.message.length - self.bodyReceived
759             if dataLen > expectedLen:
760-                self.message.bodyDataReceived(data[:expectedLen])
761-                self.messageDone(data[expectedLen:])
762+                self.message.bodyDataReceived( data[:expectedLen] )
763+                self.messageDone( data[expectedLen:] )
764                 return
765             else:
766                 self.bodyReceived += dataLen
767-                self.message.bodyDataReceived(data)
768+                self.message.bodyDataReceived( data )
769                 if self.bodyReceived == self.message.length:
770                     self.messageDone()
771 
772 
773-class Base(protocol.DatagramProtocol):
774+class Base( protocol.DatagramProtocol ):
775     """Base class for SIP clients and servers."""
776-   
777+
778     PORT = PORT
779     debug = False
780-   
781-    def __init__(self):
782+
783+    def __init__( self ):
784         self.messages = []
785-        self.parser = MessagesParser(self.addMessage)
786+        self.parser = MessagesParser( self.addMessage )
787 
788-    def addMessage(self, msg):
789-        self.messages.append(msg)
790+    def addMessage( self, msg ):
791+        self.messages.append( msg )
792 
793-    def datagramReceived(self, data, addr):
794-        self.parser.dataReceived(data)
795+    def datagramReceived( self, data, addr ):
796+        self.parser.dataReceived( data )
797         self.parser.dataDone()
798         for m in self.messages:
799-            self._fixupNAT(m, addr)
800+            self._fixupNAT( m, addr )
801             if self.debug:
802-                log.msg("Received %r from %r" % (m.toString(), addr))
803-            if isinstance(m, Request):
804-                self.handle_request(m, addr)
805+                log.msg( "Received %r from %r" % ( m.toString(), addr ) )
806+            if isinstance( m, Request ):
807+                self.handle_request( m, addr )
808             else:
809-                self.handle_response(m, addr)
810+                self.handle_response( m, addr )
811         self.messages[:] = []
812 
813-    def _fixupNAT(self, message, (srcHost, srcPort)):
814+    def _fixupNAT( self, message, ( srcHost, srcPort ) ):
815         # RFC 2543 6.40.2,
816-        senderVia = parseViaHeader(message.headers["via"][0])
817-        if senderVia.host != srcHost:           
818+        senderVia = parseViaHeader( message.headers["via"][0] )
819+        if senderVia.host != srcHost:
820             senderVia.received = srcHost
821             if senderVia.port != srcPort:
822                 senderVia.rport = srcPort
823@@ -760,38 +765,38 @@
824             senderVia.rport = srcPort
825             message.headers["via"][0] = senderVia.toString()
826 
827-    def deliverResponse(self, responseMessage):
828+    def deliverResponse( self, responseMessage ):
829         """Deliver response.
830 
831         Destination is based on topmost Via header."""
832-        destVia = parseViaHeader(responseMessage.headers["via"][0])
833+        destVia = parseViaHeader( responseMessage.headers["via"][0] )
834         # XXX we don't do multicast yet
835         host = destVia.received or destVia.host
836         port = destVia.rport or destVia.port or self.PORT
837-        destAddr = URL(host=host, port=port)
838-        self.sendMessage(destAddr, responseMessage)
839+        destAddr = URL( host=host, port=port )
840+        self.sendMessage( destAddr, responseMessage )
841 
842-    def responseFromRequest(self, code, request):
843+    def responseFromRequest( self, code, request ):
844         """Create a response to a request message."""
845-        response = Response(code)
846-        for name in ("via", "to", "from", "call-id", "cseq"):
847-            response.headers[name] = request.headers.get(name, [])[:]
848+        response = Response( code )
849+        for name in ( "via", "to", "from", "call-id", "cseq" ):
850+            response.headers[name] = request.headers.get( name, [] )[:]
851 
852         return response
853 
854-    def sendMessage(self, destURL, message):
855+    def sendMessage( self, destURL, message ):
856         """Send a message.
857 
858         @param destURL: C{URL}. This should be a *physical* URL, not a logical one.
859         @param message: The message to send.
860         """
861-        if destURL.transport not in ("udp", None):
862+        if destURL.transport not in ( "udp", None ):
863             raise RuntimeError, "only UDP currently supported"
864         if self.debug:
865-            log.msg("Sending %r to %r" % (message.toString(), destURL))
866-        self.transport.write(message.toString(), (destURL.host, destURL.port or self.PORT))
867+            log.msg( "Sending %r to %r" % ( message.toString(), destURL ) )
868+        self.transport.write( message.toString(), ( destURL.host, destURL.port or self.PORT ) )
869 
870-    def handle_request(self, message, addr):
871+    def handle_request( self, message, addr ):
872         """Override to define behavior for requests received
873 
874         @type message: C{Message}
875@@ -799,7 +804,7 @@
876         """
877         raise NotImplementedError
878 
879-    def handle_response(self, message, addr):
880+    def handle_response( self, message, addr ):
881         """Override to define behavior for responses received.
882         
883         @type message: C{Message}
884@@ -808,41 +813,41 @@
885         raise NotImplementedError
886 
887 
888-class IContact(Interface):
889+class IContact( Interface ):
890     """A user of a registrar or proxy"""
891 
892 
893 class Registration:
894-    def __init__(self, secondsToExpiry, contactURL):
895+    def __init__( self, secondsToExpiry, contactURL ):
896         self.secondsToExpiry = secondsToExpiry
897         self.contactURL = contactURL
898 
899-class IRegistry(Interface):
900+class IRegistry( Interface ):
901     """Allows registration of logical->physical URL mapping."""
902 
903-    def registerAddress(domainURL, logicalURL, physicalURL):
904+    def registerAddress( domainURL, logicalURL, physicalURL ):
905         """Register the physical address of a logical URL.
906 
907         @return: Deferred of C{Registration} or failure with RegistrationError.
908         """
909 
910-    def unregisterAddress(domainURL, logicalURL, physicalURL):
911+    def unregisterAddress( domainURL, logicalURL, physicalURL ):
912         """Unregister the physical address of a logical URL.
913 
914         @return: Deferred of C{Registration} or failure with RegistrationError.
915         """
916 
917-    def getRegistrationInfo(logicalURL):
918+    def getRegistrationInfo( logicalURL ):
919         """Get registration info for logical URL.
920 
921         @return: Deferred of C{Registration} object or failure of LookupError.
922         """
923 
924 
925-class ILocator(Interface):
926+class ILocator( Interface ):
927     """Allow looking up physical address for logical URL."""
928 
929-    def getAddress(logicalURL):
930+    def getAddress( logicalURL ):
931         """Return physical URL of server for logical URL of user.
932 
933         @param logicalURL: a logical C{URL}.
934@@ -850,14 +855,14 @@
935         """
936 
937 
938-class Proxy(Base):
939+class Proxy( Base ):
940     """SIP proxy."""
941-   
942+
943     PORT = PORT
944 
945     locator = None # object implementing ILocator
946-   
947-    def __init__(self, host=None, port=PORT):
948+
949+    def __init__( self, host=None, port=PORT ):
950         """Create new instance.
951 
952         @param host: our hostname/IP as set in Via headers.
953@@ -865,32 +870,32 @@
954         """
955         self.host = host or socket.getfqdn()
956         self.port = port
957-        Base.__init__(self)
958-       
959-    def getVia(self):
960+        Base.__init__( self )
961+
962+    def getVia( self ):
963         """Return value of Via header for this proxy."""
964-        return Via(host=self.host, port=self.port)
965+        return Via( host=self.host, port=self.port )
966 
967-    def handle_request(self, message, addr):
968+    def handle_request( self, message, addr ):
969         # send immediate 100/trying message before processing
970         #self.deliverResponse(self.responseFromRequest(100, message))
971-        f = getattr(self, "handle_%s_request" % message.method, None)
972+        f = getattr( self, "handle_%s_request" % message.method, None )
973         if f is None:
974             f = self.handle_request_default
975         try:
976-            d = f(message, addr)
977+            d = f( message, addr )
978         except SIPError, e:
979-            self.deliverResponse(self.responseFromRequest(e.code, message))
980+            self.deliverResponse( self.responseFromRequest( e.code, message ) )
981         except:
982             log.err()
983-            self.deliverResponse(self.responseFromRequest(500, message))
984+            self.deliverResponse( self.responseFromRequest( INTERNAL_SERVER_ERROR, message ) )
985         else:
986             if d is not None:
987-                d.addErrback(lambda e:
988-                    self.deliverResponse(self.responseFromRequest(e.code, message))
989+                d.addErrback( lambda e:
990+                    self.deliverResponse( self.responseFromRequest( e.code, message ) )
991                 )
992-       
993-    def handle_request_default(self, message, (srcHost, srcPort)):
994+
995+    def handle_request_default( self, message, ( srcHost, srcPort ) ):
996         """Default request handler.
997         
998         Default behaviour for OPTIONS and unknown methods for proxies
999@@ -899,70 +904,70 @@
1000         Since at the moment we are stateless proxy, thats basically
1001         everything.
1002         """
1003-        def _mungContactHeader(uri, message):
1004-            message.headers['contact'][0] = uri.toString()           
1005-            return self.sendMessage(uri, message)
1006-       
1007+        def _mungContactHeader( uri, message ):
1008+            message.headers['contact'][0] = uri.toString()
1009+            return self.sendMessage( uri, message )
1010+
1011         viaHeader = self.getVia()
1012         if viaHeader.toString() in message.headers["via"]:
1013             # must be a loop, so drop message
1014-            log.msg("Dropping looped message.")
1015+            log.msg( "Dropping looped message." )
1016             return
1017 
1018-        message.headers["via"].insert(0, viaHeader.toString())
1019-        name, uri, tags = parseAddress(message.headers["to"][0], clean=1)
1020+        message.headers["via"].insert( 0, viaHeader.toString() )
1021+        name, uri, tags = parseAddress( message.headers["to"][0], clean=1 )
1022 
1023         # this is broken and needs refactoring to use cred
1024-        d = self.locator.getAddress(uri)
1025-        d.addCallback(self.sendMessage, message)
1026-        d.addErrback(self._cantForwardRequest, message)
1027-   
1028-    def _cantForwardRequest(self, error, message):
1029-        error.trap(LookupError)
1030+        d = self.locator.getAddress( uri )
1031+        d.addCallback( self.sendMessage, message )
1032+        d.addErrback( self._cantForwardRequest, message )
1033+
1034+    def _cantForwardRequest( self, error, message ):
1035+        error.trap( LookupError )
1036         del message.headers["via"][0] # this'll be us
1037-        self.deliverResponse(self.responseFromRequest(404, message))
1038-   
1039-    def deliverResponse(self, responseMessage):
1040+        self.deliverResponse( self.responseFromRequest( NOT_FOUND, message ) )
1041+
1042+    def deliverResponse( self, responseMessage ):
1043         """Deliver response.
1044 
1045         Destination is based on topmost Via header."""
1046-        destVia = parseViaHeader(responseMessage.headers["via"][0])
1047+        destVia = parseViaHeader( responseMessage.headers["via"][0] )
1048         # XXX we don't do multicast yet
1049         host = destVia.received or destVia.host
1050         port = destVia.rport or destVia.port or self.PORT
1051-       
1052-        destAddr = URL(host=host, port=port)
1053-        self.sendMessage(destAddr, responseMessage)
1054 
1055-    def responseFromRequest(self, code, request):
1056+        destAddr = URL( host=host, port=port )
1057+        self.sendMessage( destAddr, responseMessage )
1058+
1059+    def responseFromRequest( self, code, request ):
1060         """Create a response to a request message."""
1061-        response = Response(code)
1062-        for name in ("via", "to", "from", "call-id", "cseq"):
1063-            response.headers[name] = request.headers.get(name, [])[:]
1064+        response = Response( code )
1065+        for name in ( "via", "to", "from", "call-id", "cseq" ):
1066+            response.headers[name] = request.headers.get( name, [] )[:]
1067         return response
1068-   
1069-    def handle_response(self, message, addr):
1070+
1071+    def handle_response( self, message, addr ):
1072         """Default response handler."""
1073-        v = parseViaHeader(message.headers["via"][0])
1074-        if (v.host, v.port) != (self.host, self.port):
1075+        v = parseViaHeader( message.headers["via"][0] )
1076+        if ( v.host, v.port ) != ( self.host, self.port ):
1077             # we got a message not intended for us?
1078             # XXX note this check breaks if we have multiple external IPs
1079             # yay for suck protocols
1080-            log.msg("Dropping incorrectly addressed message")
1081+            log.msg( "Dropping incorrectly addressed message" )
1082             return
1083         del message.headers["via"][0]
1084         if not message.headers["via"]:
1085             # this message is addressed to us
1086-            self.gotResponse(message, addr)
1087+            self.gotResponse( message, addr )
1088             return
1089-        self.deliverResponse(message)
1090-   
1091-    def gotResponse(self, message, addr):
1092+        self.deliverResponse( message )
1093+
1094+    def gotResponse( self, message, addr ):
1095         """Called with responses that are addressed at this server."""
1096         pass
1097 
1098-class IAuthorizer(Interface):
1099-    def getChallenge(peer):
1100+class IAuthorizer( Interface ):
1101+    def getChallenge( peer ):
1102         """Generate a challenge the client may respond to.
1103         
1104         @type peer: C{tuple}
1105@@ -971,118 +976,118 @@
1106         @rtype: C{str}
1107         @return: The challenge string
1108         """
1109-   
1110-    def decode(response):
1111+
1112+    def decode( response ):
1113         """Create a credentials object from the given response.
1114         
1115         @type response: C{str}
1116         """
1117-
1118+
1119 class BasicAuthorizer:
1120     """Authorizer for insecure Basic (base64-encoded plaintext) authentication.
1121     
1122     This form of authentication is broken and insecure.  Do not use it.
1123     """
1124 
1125-    implements(IAuthorizer)
1126-   
1127-    def getChallenge(self, peer):
1128+    implements( IAuthorizer )
1129+
1130+    def getChallenge( self, peer ):
1131         return None
1132-   
1133-    def decode(self, response):
1134+
1135+    def decode( self, response ):
1136         # At least one SIP client improperly pads its Base64 encoded messages
1137-        for i in range(3):
1138+        for i in range( 3 ):
1139             try:
1140-                creds = (response + ('=' * i)).decode('base64')
1141+                creds = ( response + ( '=' * i ) ).decode( 'base64' )
1142             except:
1143                 pass
1144             else:
1145                 break
1146         else:
1147             # Totally bogus
1148-            raise SIPError(400)
1149-        p = creds.split(':', 1)
1150-        if len(p) == 2:
1151-            return cred.credentials.UsernamePassword(*p)
1152-        raise SIPError(400)
1153+            raise SIPError( BAD_REQUEST )
1154+        p = creds.split( ':', 1 )
1155+        if len( p ) == 2:
1156+            return cred.credentials.UsernamePassword( *p )
1157+        raise SIPError( BAD_REQUEST )
1158 
1159 
1160-class DigestedCredentials(cred.credentials.UsernameHashedPassword):
1161+class DigestedCredentials( cred.credentials.UsernameHashedPassword ):
1162     """Yet Another Simple Digest-MD5 authentication scheme"""
1163-   
1164-    def __init__(self, username, fields, challenges):
1165+
1166+    def __init__( self, username, fields, challenges ):
1167         self.username = username
1168         self.fields = fields
1169         self.challenges = challenges
1170-   
1171-    def checkPassword(self, password):
1172+
1173+    def checkPassword( self, password ):
1174         method = 'REGISTER'
1175-        response = self.fields.get('response')
1176-        uri = self.fields.get('uri')
1177-        nonce = self.fields.get('nonce')
1178-        cnonce = self.fields.get('cnonce')
1179-        nc = self.fields.get('nc')
1180-        algo = self.fields.get('algorithm', 'MD5')
1181-        qop = self.fields.get('qop-options', 'auth')
1182-        opaque = self.fields.get('opaque')
1183+        response = self.fields.get( 'response' )
1184+        uri = self.fields.get( 'uri' )
1185+        nonce = self.fields.get( 'nonce' )
1186+        cnonce = self.fields.get( 'cnonce' )
1187+        nc = self.fields.get( 'nc' )
1188+        algo = self.fields.get( 'algorithm', 'MD5' )
1189+        qop = self.fields.get( 'qop-options', 'auth' )
1190+        opaque = self.fields.get( 'opaque' )
1191 
1192         if opaque not in self.challenges:
1193             return False
1194         del self.challenges[opaque]
1195-       
1196-        user, domain = self.username.split('@', 1)
1197+
1198+        user, domain = self.username.split( '@', 1 )
1199         if uri is None:
1200             uri = 'sip:' + domain
1201 
1202-        expected = DigestCalcResponse(
1203-            DigestCalcHA1(algo, user, domain, password, nonce, cnonce),
1204+        expected = DigestCalcResponse(
1205+            DigestCalcHA1( algo, user, domain, password, nonce, cnonce ),
1206             nonce, nc, cnonce, qop, method, uri, None,
1207         )
1208-       
1209+
1210         return expected == response
1211 
1212 class DigestAuthorizer:
1213     CHALLENGE_LIFETIME = 15
1214-   
1215-    implements(IAuthorizer)
1216-   
1217-    def __init__(self):
1218+
1219+    implements( IAuthorizer )
1220+
1221+    def __init__( self ):
1222         self.outstanding = {}
1223-   
1224-    def generateNonce(self):
1225-        c = tuple([random.randrange(sys.maxint) for _ in range(3)])
1226+
1227+    def generateNonce( self ):
1228+        c = tuple( [random.randrange( sys.maxint ) for _ in range( 3 )] )
1229         c = '%d%d%d' % c
1230         return c
1231 
1232-    def generateOpaque(self):
1233-        return str(random.randrange(sys.maxint))
1234+    def generateOpaque( self ):
1235+        return str( random.randrange( sys.maxint ) )
1236 
1237-    def getChallenge(self, peer):
1238+    def getChallenge( self, peer ):
1239         c = self.generateNonce()
1240         o = self.generateOpaque()
1241         self.outstanding[o] = c
1242-        return ','.join((
1243+        return ','.join( (
1244             'nonce="%s"' % c,
1245             'opaque="%s"' % o,
1246             'qop-options="auth"',
1247             'algorithm="MD5"',
1248-        ))
1249-       
1250-    def decode(self, response):
1251-        response = ' '.join(response.splitlines())
1252-        parts = response.split(',')
1253-        auth = dict([(k.strip(), unq(v.strip())) for (k, v) in [p.split('=', 1) for p in parts]])
1254+        ) )
1255+
1256+    def decode( self, response ):
1257+        response = ' '.join( response.splitlines() )
1258+        parts = response.split( ',' )
1259+        auth = dict( [( k.strip(), unq( v.strip() ) ) for ( k, v ) in [p.split( '=', 1 ) for p in parts]] )
1260         try:
1261             username = auth['username']
1262         except KeyError:
1263-            raise SIPError(401)
1264+            raise SIPError( UNAUTHORIZED )
1265         try:
1266-            return DigestedCredentials(username, auth, self.outstanding)
1267+            return DigestedCredentials( username, auth, self.outstanding )
1268         except:
1269-            raise SIPError(400)
1270+            raise SIPError( BAD_REQUEST )
1271 
1272 
1273-class RegisterProxy(Proxy):
1274+class RegisterProxy( Proxy ):
1275     """A proxy that allows registration for a specific domain.
1276 
1277     Unregistered users won't be handled.
1278@@ -1095,12 +1100,12 @@
1279     authorizers = {
1280         'digest': DigestAuthorizer(),
1281     }
1282-   
1283-    def __init__(self, *args, **kw):
1284-        Proxy.__init__(self, *args, **kw)
1285+
1286+    def __init__( self, *args, **kw ):
1287+        Proxy.__init__( self, *args, **kw )
1288         self.liveChallenges = {}
1289-       
1290-    def handle_ACK_request(self, message, (host, port)):
1291+
1292+    def handle_ACK_request( self, message, ( host, port ) ):
1293         # XXX
1294         # ACKs are a client's way of indicating they got the last message
1295         # Responding to them is not a good idea.
1296@@ -1108,175 +1113,175 @@
1297         # if no ACK is received.
1298         pass
1299 
1300-    def handle_REGISTER_request(self, message, (host, port)):
1301+    def handle_REGISTER_request( self, message, ( host, port ) ):
1302         """Handle a registration request.
1303 
1304         Currently registration is not proxied.
1305         """
1306         if self.portal is None:
1307             # There is no portal.  Let anyone in.
1308-            self.register(message, host, port)
1309+            self.register( message, host, port )
1310         else:
1311             # There is a portal.  Check for credentials.
1312-            if not message.headers.has_key("authorization"):
1313-                return self.unauthorized(message, host, port)
1314+            if not message.headers.has_key( "authorization" ):
1315+                return self.unauthorized( message, host, port )
1316             else:
1317-                return self.login(message, host, port)
1318+                return self.login( message, host, port )
1319 
1320-    def unauthorized(self, message, host, port):
1321-        m = self.responseFromRequest(401, message)
1322-        for (scheme, auth) in self.authorizers.iteritems():
1323-            chal = auth.getChallenge((host, port))
1324+    def unauthorized( self, message, host, port ):
1325+        m = self.responseFromRequest( UNAUTHORIZED, message )
1326+        for ( scheme, auth ) in self.authorizers.iteritems():
1327+            chal = auth.getChallenge( ( host, port ) )
1328             if chal is None:
1329-                value = '%s realm="%s"' % (scheme.title(), self.host)
1330+                value = '%s realm="%s"' % ( scheme.title(), self.host )
1331             else:
1332-                value = '%s %s,realm="%s"' % (scheme.title(), chal, self.host)
1333-            m.headers.setdefault('www-authenticate', []).append(value)
1334-        self.deliverResponse(m)
1335+                value = '%s %s,realm="%s"' % ( scheme.title(), chal, self.host )
1336+            m.headers.setdefault( 'www-authenticate', [] ).append( value )
1337+        self.deliverResponse( m )
1338 
1339-
1340-    def login(self, message, host, port):
1341-        parts = message.headers['authorization'][0].split(None, 1)
1342-        a = self.authorizers.get(parts[0].lower())
1343+
1344+    def login( self, message, host, port ):
1345+        parts = message.headers['authorization'][0].split( None, 1 )
1346+        a = self.authorizers.get( parts[0].lower() )
1347         if a:
1348             try:
1349-                c = a.decode(parts[1])
1350+                c = a.decode( parts[1] )
1351             except SIPError:
1352                 raise
1353             except:
1354                 log.err()
1355-                self.deliverResponse(self.responseFromRequest(500, message))
1356+                self.deliverResponse( self.responseFromRequest( INTERNAL_SERVER_ERROR, message ) )
1357             else:
1358                 c.username += '@' + self.host
1359-                self.portal.login(c, None, IContact
1360-                    ).addCallback(self._cbLogin, message, host, port
1361-                    ).addErrback(self._ebLogin, message, host, port
1362-                    ).addErrback(log.err
1363+                self.portal.login( c, None, IContact
1364+                    ).addCallback( self._cbLogin, message, host, port
1365+                    ).addErrback( self._ebLogin, message, host, port
1366+                    ).addErrback( log.err
1367                     )
1368         else:
1369-            self.deliverResponse(self.responseFromRequest(501, message))
1370+            self.deliverResponse( self.responseFromRequest( NOT_IMPLEMENTED, message ) )
1371 
1372-    def _cbLogin(self, (i, a, l), message, host, port):
1373+    def _cbLogin( self, ( i, a, l ), message, host, port ):
1374         # It's stateless, matey.  What a joke.
1375-        self.register(message, host, port)
1376-   
1377-    def _ebLogin(self, failure, message, host, port):
1378-        failure.trap(cred.error.UnauthorizedLogin)
1379-        self.unauthorized(message, host, port)
1380+        self.register( message, host, port )
1381 
1382-    def register(self, message, host, port):
1383+    def _ebLogin( self, failure, message, host, port ):
1384+        failure.trap( cred.error.UnauthorizedLogin )
1385+        self.unauthorized( message, host, port )
1386+
1387+    def register( self, message, host, port ):
1388         """Allow all users to register"""
1389-        name, toURL, params = parseAddress(message.headers["to"][0], clean=1)
1390+        name, toURL, params = parseAddress( message.headers["to"][0], clean=1 )
1391         contact = None
1392-        if message.headers.has_key("contact"):
1393+        if message.headers.has_key( "contact" ):
1394             contact = message.headers["contact"][0]
1395 
1396-        if message.headers.get("expires", [None])[0] == "0":
1397-            self.unregister(message, toURL, contact)
1398+        if message.headers.get( "expires", [None] )[0] == "0":
1399+            self.unregister( message, toURL, contact )
1400         else:
1401             # XXX Check expires on appropriate URL, and pass it to registry
1402             # instead of having registry hardcode it.
1403             if contact is not None:
1404-                name, contactURL, params = parseAddress(contact, host=host, port=port)
1405-                d = self.registry.registerAddress(message.uri, toURL, contactURL)
1406+                name, contactURL, params = parseAddress( contact, host=host, port=port )
1407+                d = self.registry.registerAddress( message.uri, toURL, contactURL )
1408             else:
1409-                d = self.registry.getRegistrationInfo(toURL)
1410-            d.addCallbacks(self._cbRegister, self._ebRegister,
1411-                callbackArgs=(message,),
1412-                errbackArgs=(message,)
1413+                d = self.registry.getRegistrationInfo( toURL )
1414+            d.addCallbacks( self._cbRegister, self._ebRegister,
1415+                callbackArgs=( message, ),
1416+                errbackArgs=( message, )
1417             )
1418 
1419-    def _cbRegister(self, registration, message):
1420-        response = self.responseFromRequest(200, message)
1421+    def _cbRegister( self, registration, message ):
1422+        response = self.responseFromRequest( OK, message )
1423         if registration.contactURL != None:
1424-            response.addHeader("contact", registration.contactURL.toString())
1425-            response.addHeader("expires", "%d" % registration.secondsToExpiry)
1426-        response.addHeader("content-length", "0")
1427-        self.deliverResponse(response)
1428+            response.addHeader( "contact", registration.contactURL.toString() )
1429+            response.addHeader( "expires", "%d" % registration.secondsToExpiry )
1430+        response.addHeader( "content-length", "0" )
1431+        self.deliverResponse( response )
1432 
1433-    def _ebRegister(self, error, message):
1434-        error.trap(RegistrationError, LookupError)
1435+    def _ebRegister( self, error, message ):
1436+        error.trap( RegistrationError, LookupError )
1437         # XXX return error message, and alter tests to deal with
1438         # this, currently tests assume no message sent on failure
1439 
1440-    def unregister(self, message, toURL, contact):
1441+    def unregister( self, message, toURL, contact ):
1442         try:
1443-            expires = int(message.headers["expires"][0])
1444+            expires = int( message.headers["expires"][0] )
1445         except ValueError:
1446-            self.deliverResponse(self.responseFromRequest(400, message))
1447+            self.deliverResponse( self.responseFromRequest( BAD_REQUEST, message ) )
1448         else:
1449             if expires == 0:
1450                 if contact == "*":
1451                     contactURL = "*"
1452                 else:
1453-                    name, contactURL, params = parseAddress(contact)
1454-                d = self.registry.unregisterAddress(message.uri, toURL, contactURL)
1455-                d.addCallback(self._cbUnregister, message
1456-                    ).addErrback(self._ebUnregister, message
1457+                    name, contactURL, params = parseAddress( contact )
1458+                d = self.registry.unregisterAddress( message.uri, toURL, contactURL )
1459+                d.addCallback( self._cbUnregister, message
1460+                    ).addErrback( self._ebUnregister, message
1461                     )
1462 
1463-    def _cbUnregister(self, registration, message):
1464-        msg = self.responseFromRequest(200, message)
1465-        msg.headers.setdefault('contact', []).append(registration.contactURL.toString())
1466-        msg.addHeader("expires", "0")
1467-        self.deliverResponse(msg)
1468+    def _cbUnregister( self, registration, message ):
1469+        msg = self.responseFromRequest( OK, message )
1470+        msg.headers.setdefault( 'contact', [] ).append( registration.contactURL.toString() )
1471+        msg.addHeader( "expires", "0" )
1472+        self.deliverResponse( msg )
1473 
1474-    def _ebUnregister(self, registration, message):
1475+    def _ebUnregister( self, registration, message ):
1476         pass
1477 
1478 
1479 class InMemoryRegistry:
1480     """A simplistic registry for a specific domain."""
1481 
1482-    implements(IRegistry, ILocator)
1483-   
1484-    def __init__(self, domain):
1485+    implements( IRegistry, ILocator )
1486+
1487+    def __init__( self, domain ):
1488         self.domain = domain # the domain we handle registration for
1489         self.users = {} # map username to (IDelayedCall for expiry, address URI)
1490 
1491-    def getAddress(self, userURI):
1492+    def getAddress( self, userURI ):
1493         if userURI.host != self.domain:
1494-            return defer.fail(LookupError("unknown domain"))
1495-        if self.users.has_key(userURI.username):
1496+            return defer.fail( LookupError( "unknown domain" ) )
1497+        if self.users.has_key( userURI.username ):
1498             dc, url = self.users[userURI.username]
1499-            return defer.succeed(url)
1500+            return defer.succeed( url )
1501         else:
1502-            return defer.fail(LookupError("no such user"))
1503-           
1504-    def getRegistrationInfo(self, userURI):
1505+            return defer.fail( LookupError( "no such user" ) )
1506+
1507+    def getRegistrationInfo( self, userURI ):
1508         if userURI.host != self.domain:
1509-            return defer.fail(LookupError("unknown domain"))
1510-        if self.users.has_key(userURI.username):
1511+            return defer.fail( LookupError( "unknown domain" ) )
1512+        if self.users.has_key( userURI.username ):
1513             dc, url = self.users[userURI.username]
1514-            return defer.succeed(Registration(int(dc.getTime() - time.time()), url))
1515+            return defer.succeed( Registration( int( dc.getTime() - time.time() ), url ) )
1516         else:
1517-            return defer.fail(LookupError("no such user"))
1518-       
1519-    def _expireRegistration(self, username):
1520+            return defer.fail( LookupError( "no such user" ) )
1521+
1522+    def _expireRegistration( self, username ):
1523         try:
1524             dc, url = self.users[username]
1525         except KeyError:
1526-            return defer.fail(LookupError("no such user"))
1527+            return defer.fail( LookupError( "no such user" ) )
1528         else:
1529             dc.cancel()
1530             del self.users[username]
1531-        return defer.succeed(Registration(0, url))
1532-   
1533-    def registerAddress(self, domainURL, logicalURL, physicalURL):
1534+        return defer.succeed( Registration( 0, url ) )
1535+
1536+    def registerAddress( self, domainURL, logicalURL, physicalURL ):
1537         if domainURL.host != self.domain:
1538-            log.msg("Registration for domain we don't handle.")
1539-            return defer.fail(RegistrationError(404))
1540+            log.msg( "Registration for domain we don't handle." )
1541+            return defer.fail( RegistrationError( 404 ) )
1542         if logicalURL.host != self.domain:
1543-            log.msg("Registration for domain we don't handle.")
1544-            return defer.fail(RegistrationError(404))
1545-        if self.users.has_key(logicalURL.username):
1546+            log.msg( "Registration for domain we don't handle." )
1547+            return defer.fail( RegistrationError( 404 ) )
1548+        if self.users.has_key( logicalURL.username ):
1549             dc, old = self.users[logicalURL.username]
1550-            dc.reset(3600)
1551+            dc.reset( 3600 )
1552         else:
1553-            dc = reactor.callLater(3600, self._expireRegistration, logicalURL.username)
1554-        log.msg("Registered %s at %s" % (logicalURL.toString(), physicalURL.toString()))
1555-        self.users[logicalURL.username] = (dc, physicalURL)
1556-        return defer.succeed(Registration(int(dc.getTime() - time.time()), physicalURL))
1557+            dc = reactor.callLater( 3600, self._expireRegistration, logicalURL.username )
1558+        log.msg( "Registered %s at %s" % ( logicalURL.toString(), physicalURL.toString() ) )
1559+        self.users[logicalURL.username] = ( dc, physicalURL )
1560+        return defer.succeed( Registration( int( dc.getTime() - time.time() ), physicalURL ) )
1561 
1562-    def unregisterAddress(self, domainURL, logicalURL, physicalURL):
1563-        return self._expireRegistration(logicalURL.username)
1564+    def unregisterAddress( self, domainURL, logicalURL, physicalURL ):
1565+        return self._expireRegistration( logicalURL.username )