Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/network.py
blob: 0e25d731ad27a634b2112efe238824e4520cbef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright (C) 2006-2007 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

"""
STABLE.
"""

import os
import threading
import urllib
import fcntl
import tempfile

import gobject
import SimpleHTTPServer
import SocketServer

__authinfos = {}

def _add_authinfo(authinfo):
    __authinfos[threading.currentThread()] = authinfo

def get_authinfo():
    return __authinfos.get(threading.currentThread())

def _del_authinfo():
    del __authinfos[threading.currentThread()]


class GlibTCPServer(SocketServer.TCPServer):
    """GlibTCPServer

    Integrate socket accept into glib mainloop.
    """

    allow_reuse_address = True
    request_queue_size = 20

    def __init__(self, server_address, RequestHandlerClass):
        SocketServer.TCPServer.__init__(self, server_address,
                                        RequestHandlerClass)
        self.socket.setblocking(0)  # Set nonblocking

        # Watch the listener socket for data
        gobject.io_add_watch(self.socket, gobject.IO_IN, self._handle_accept)

    def _handle_accept(self, source, condition):
        """Process incoming data on the server's socket by doing an accept()
        via handle_request()."""
        if not (condition & gobject.IO_IN):
            return True
        self.handle_request()
        return True

    def close_request(self, request):
        """Called to clean up an individual request."""
        # let the request be closed by the request handler when its done
        pass


class ChunkedGlibHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    """RequestHandler class that integrates with Glib mainloop.  It writes
       the specified file to the client in chunks, returning control to the
       mainloop between chunks.
    """

    CHUNK_SIZE = 4096

    def __init__(self, request, client_address, server):
        self._file = None
        self._srcid = 0
        SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(
                                        self, request, client_address, server)

    def log_request(self, code='-', size='-'):
        pass

    def do_GET(self):
        """Serve a GET request."""
        self._file = self.send_head()
        if self._file:
            self._srcid = gobject.io_add_watch(self.wfile, gobject.IO_OUT |
                                               gobject.IO_ERR,
                                               self._send_next_chunk)
        else:
            self._cleanup()

    def _send_next_chunk(self, source, condition):
        if condition & gobject.IO_ERR:
            self._cleanup()
            return False
        if not (condition & gobject.IO_OUT):
            self._cleanup()
            return False
        data = self._file.read(self.CHUNK_SIZE)
        count = os.write(self.wfile.fileno(), data)
        if count != len(data) or len(data) != self.CHUNK_SIZE:
            self._cleanup()
            return False
        return True

    def _cleanup(self):
        if self._file:
            self._file.close()
            self._file = None
        if self._srcid > 0:
            gobject.source_remove(self._srcid)
            self._srcid = 0
        if not self.wfile.closed:
            self.wfile.flush()
        self.wfile.close()
        self.rfile.close()
        
    def finish(self):
        """Close the sockets when we're done, not before"""
        pass

    def send_head(self):
        """Common code for GET and HEAD commands.

        This sends the response code and MIME headers.

        Return value is either a file object (which has to be copied
        to the outputfile by the caller unless the command was HEAD,
        and must be closed by the caller under all circumstances), or
        None, in which case the caller has nothing further to do.

        ** [dcbw] modified to send Content-disposition filename too
        """
        path = self.translate_path(self.path)
        if not path or not os.path.exists(path):
            self.send_error(404, "File not found")
            return None

        f = None
        if os.path.isdir(path):
            for index in "index.html", "index.htm":
                index = os.path.join(path, index)
                if os.path.exists(index):
                    path = index
                    break
            else:
                return self.list_directory(path)
        ctype = self.guess_type(path)
        try:
            # Always read in binary mode. Opening files in text mode may cause
            # newline translations, making the actual size of the content
            # transmitted *less* than the content-length!
            f = open(path, 'rb')
        except IOError:
            self.send_error(404, "File not found")
            return None
        self.send_response(200)
        self.send_header("Content-type", ctype)
        self.send_header("Content-Length", str(os.fstat(f.fileno())[6]))
        self.send_header("Content-Disposition", 'attachment; filename="%s"' %
                         os.path.basename(path))
        self.end_headers()
        return f

class GlibURLDownloader(gobject.GObject):
    """Grabs a URL in chunks, returning to the mainloop after each chunk"""

    __gsignals__ = {
        'finished': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                         ([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
        'error':    (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                         ([gobject.TYPE_PYOBJECT])),
        'progress': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                         ([gobject.TYPE_PYOBJECT]))
    }

    CHUNK_SIZE = 4096

    def __init__(self, url, destdir=None):
        self._url = url
        if not destdir:
            destdir = tempfile.gettempdir()
        self._destdir = destdir
        self._srcid = 0
        self._fname = None
        self._outf = None
        self._suggested_fname = None
        self._info = None
        self._written = 0
        gobject.GObject.__init__(self)

    def start(self, destfile=None, destfd=None):
        self._info = urllib.urlopen(self._url)
        self._outf = None
        self._fname = None
        if destfd and not destfile:
            raise ValueError("Must provide destination file too when" \
                             "specifying file descriptor")
        if destfile:
            self._suggested_fname = os.path.basename(destfile)
            self._fname = os.path.abspath(os.path.expanduser(destfile))
            if destfd:
                # Use the user-supplied destination file descriptor
                self._outf = destfd
            else:
                self._outf = os.open(self._fname, os.O_RDWR |
                                     os.O_TRUNC | os.O_CREAT, 0644)
        else:
            fname = self._get_filename_from_headers(self._info.headers)
            self._suggested_fname = fname
            garbage_, path = urllib.splittype(self._url)
            garbage_, path = urllib.splithost(path or "")
            path, garbage_ = urllib.splitquery(path or "")
            path, garbage_ = urllib.splitattr(path or "")
            suffix = os.path.splitext(path)[1]
            (self._outf, self._fname) = tempfile.mkstemp(suffix=suffix,
                                                         dir=self._destdir)

        fcntl.fcntl(self._info.fp.fileno(), fcntl.F_SETFD, os.O_NDELAY)
        self._srcid = gobject.io_add_watch(self._info.fp.fileno(),
                                           gobject.IO_IN | gobject.IO_ERR,
                                           self._read_next_chunk)

    def cancel(self):
        if self._srcid == 0:
            raise RuntimeError("Download already canceled or stopped")
        self.cleanup(remove=True)

    def _get_filename_from_headers(self, headers):
        if not headers.has_key("Content-Disposition"):
            return None

        ftag = "filename="
        data = headers["Content-Disposition"]
        fidx = data.find(ftag)
        if fidx < 0:
            return None
        fname = data[fidx+len(ftag):]
        if fname[0] == '"' or fname[0] == "'":
            fname = fname[1:]
        if fname[len(fname)-1] == '"' or fname[len(fname)-1] == "'":
            fname = fname[:len(fname)-1]
        return fname

    def _read_next_chunk(self, source, condition):
        if condition & gobject.IO_ERR:
            self.cleanup(remove=True)
            self.emit("error", "Error downloading file.")
            return False
        elif not (condition & gobject.IO_IN):
            # shouldn't get here, but...
            return True

        try:
            data = self._info.fp.read(self.CHUNK_SIZE)
            count = os.write(self._outf, data)
            self._written += len(data)

            # error writing data to file?
            if count < len(data):
                self.cleanup(remove=True)
                self.emit("error", "Error writing to download file.")
                return False

            self.emit("progress", self._written)

            # done?
            if len(data) < self.CHUNK_SIZE:
                self.cleanup()
                self.emit("finished", self._fname, self._suggested_fname)
                return False
        except Exception, err:
            self.cleanup(remove=True)
            self.emit("error", "Error downloading file: %s" % err)
            return False
        return True

    def cleanup(self, remove=False):
        if self._srcid > 0:
            gobject.source_remove(self._srcid)
            self._srcid = 0
        del self._info
        self._info = None
        os.close(self._outf)
        if remove:
            os.remove(self._fname)
        self._outf = None