~ubuntu-branches/ubuntu/trusty/coherence/trusty

« back to all changes in this revision

Viewing changes to coherence/upnp/core/utils.py

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2008-06-12 16:49:04 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080612164904-894cpjy93grsdo76
Tags: 0.5.6-1
* New upstream release (Closes: #474729)
* debian/control:
  - allow Depends on python2.5 or python2.4 (Closes: #485592)
  - update Standards-Version to 3.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
# Copyright (C) 2006 Fluendo, S.A. (www.fluendo.com).
5
5
# Copyright 2006, Frank Scholz <coherence@beebits.net>
6
6
 
 
7
import urlparse
 
8
 
7
9
from coherence.extern.et import parse_xml as et_parse_xml
8
10
 
9
11
from twisted.web import server, http, static
12
14
from twisted.internet import reactor, protocol
13
15
from twisted.python import failure
14
16
 
15
 
import socket
16
 
import fcntl
17
 
import struct
18
 
import string
19
 
import os
20
 
import urlparse
 
17
try:
 
18
    import netifaces
 
19
    have_netifaces = True
 
20
except ImportError:
 
21
    have_netifaces = False
21
22
 
22
23
def parse_xml(data, encoding="utf-8"):
23
24
    return et_parse_xml(data,encoding)
50
51
 
51
52
    Updated to work on BSD. OpenBSD and OSX share the same value for
52
53
    SIOCGIFADDR, and its likely that other BSDs do too.
 
54
 
 
55
    Updated to work on Windows,
 
56
    using the optional Python module netifaces
 
57
    http://alastairs-place.net/netifaces/
 
58
 
 
59
    Thx Lawrence for that patch!
53
60
    """
54
61
 
55
 
    system_type = os.uname()[0]
 
62
    import sys
 
63
    if sys.platform == 'win32':
 
64
        if have_netifaces:
 
65
            if ifname in netifaces.interfaces():
 
66
                iface = netifaces.ifaddresses(ifname)
 
67
                ifaceadr = iface(netifaces.AF_INET)
 
68
                # we now have a list of address dictionaries, there may be multiple addresses bound
 
69
                return ifaceadr[0]['addr']
 
70
        return '127.0.0.1'
 
71
 
 
72
    from os import uname
 
73
    import socket
 
74
    import fcntl
 
75
    import struct
 
76
 
 
77
    system_type = uname()[0]
 
78
 
56
79
    if system_type == "Linux":
57
80
        SIOCGIFADDR = 0x8915
58
81
    else:
70
93
        the default route, as this is most likely
71
94
        the interface we should bind to (on a single homed host!)
72
95
    """
 
96
 
 
97
    import sys
 
98
    if sys.platform == 'win32':
 
99
        if have_netifaces:
 
100
            return get_ip_address(netifaces.interfaces()[0])    # on windows assume first interface is primary
 
101
 
73
102
    try:
74
103
        route_file = '/proc/net/route'
75
104
        route = open(route_file)
84
113
                        return get_ip_address(l[0])
85
114
    except IOerror:
86
115
        """ fallback to parsing the output of netstat """
87
 
        import os, posix
88
 
        (osname,_, _, _,_) = os.uname()
 
116
        from os import uname
 
117
        import posix
 
118
        (osname,_, _, _,_) = uname()
89
119
        osname = osname.lower()
90
120
        f = posix.popen('netstat -rn')
91
121
        lines = f.readlines()
98
128
                else:
99
129
                    return get_ip_address(parts[-1])
100
130
 
101
 
    """ return localhost if we havn't found anything """
 
131
    """ return localhost if we haven't found anything """
102
132
    return '127.0.0.1'
103
133
 
 
134
def de_chunk_payload(response):
 
135
 
 
136
    import StringIO
 
137
    """ This method takes a chunked HTTP data object and unchunks it."""
 
138
    newresponse = StringIO.StringIO()
 
139
    # chunked encoding consists of a bunch of lines with
 
140
    # a length in hex followed by a data chunk and a CRLF pair.
 
141
    response = StringIO.StringIO(response)
 
142
 
 
143
    def read_chunk_length():
 
144
        line = response.readline()
 
145
        try:
 
146
            len = int(line.strip(),16)
 
147
        except ValueError:
 
148
            len = 0
 
149
        return len
 
150
 
 
151
    len = read_chunk_length()
 
152
    while (len > 0):
 
153
        newresponse.write(response.read(len))
 
154
        line = response.readline() # after chunk and before next chunk length
 
155
        len = read_chunk_length()
 
156
 
 
157
    return newresponse.getvalue()
 
158
 
 
159
 
104
160
class Site(server.Site):
105
161
 
106
162
    noisy = False
280
336
            self.factory.noPage(failure.Failure(
281
337
                client.PartialDownloadError(self.status, self.message, response)))
282
338
        else:
283
 
            self.factory.page(response)
 
339
            if(self.headers.has_key('transfer-encoding') and
 
340
               self.headers['transfer-encoding'][0].lower() == 'chunked'):
 
341
                self.factory.page(de_chunk_payload(response))
 
342
            else:
 
343
                self.factory.page(response)
284
344
        # server might be stupid and not close connection. admittedly
285
345
        # the fact we do only one request per connection is also
286
346
        # stupid...
406
466
        tsize = size
407
467
        if range is not None:
408
468
            # This is a request for partial data...
409
 
            bytesrange = string.split(range, '=')
 
469
            bytesrange = range.split('=')
410
470
            assert bytesrange[0] == 'bytes',\
411
471
                   "Syntactically invalid http range header!"
412
 
            start, end = string.split(bytesrange[1],'-', 1)
 
472
            start, end = bytesrange[1].split('-', 1)
413
473
            if start:
414
474
                f.seek(int(start))
415
475
                if end: