2007-06-24 14:43:48 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2007-05-24 19:57:11 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2008-10-28 14:19:01 +01:00
|
|
|
"""
|
|
|
|
STABLE.
|
|
|
|
"""
|
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
import os
|
|
|
|
import threading
|
|
|
|
import urllib
|
|
|
|
import fcntl
|
2007-07-23 07:17:21 +02:00
|
|
|
import tempfile
|
2007-05-24 19:57:11 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
from gi.repository import GObject
|
2007-05-24 19:57:11 +02:00
|
|
|
import SimpleHTTPServer
|
|
|
|
import SocketServer
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
__authinfos = {}
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
def _add_authinfo(authinfo):
|
|
|
|
__authinfos[threading.currentThread()] = authinfo
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
def get_authinfo():
|
|
|
|
return __authinfos.get(threading.currentThread())
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
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):
|
2008-04-19 13:05:48 +02:00
|
|
|
SocketServer.TCPServer.__init__(self, server_address,
|
|
|
|
RequestHandlerClass)
|
2007-05-24 19:57:11 +02:00
|
|
|
self.socket.setblocking(0) # Set nonblocking
|
|
|
|
|
|
|
|
# Watch the listener socket for data
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.io_add_watch(self.socket, GObject.IO_IN, self._handle_accept)
|
2007-05-24 19:57:11 +02:00
|
|
|
|
|
|
|
def _handle_accept(self, source, condition):
|
|
|
|
"""Process incoming data on the server's socket by doing an accept()
|
|
|
|
via handle_request()."""
|
2011-11-15 19:29:07 +01:00
|
|
|
if not (condition & GObject.IO_IN):
|
2007-05-24 19:57:11 +02:00
|
|
|
return True
|
|
|
|
self.handle_request()
|
|
|
|
return True
|
|
|
|
|
2007-07-18 04:30:23 +02:00
|
|
|
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
|
|
|
|
|
2011-06-29 00:01:12 +02:00
|
|
|
def shutdown_request(self, request):
|
|
|
|
"""Called to shutdown and close an individual request."""
|
|
|
|
# like close_request, let the request be closed by the request handler
|
|
|
|
# when done
|
|
|
|
pass
|
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
|
|
|
|
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
|
2008-04-19 13:05:48 +02:00
|
|
|
SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(
|
2013-05-18 04:33:36 +02:00
|
|
|
self, request, client_address, server)
|
2007-05-24 19:57:11 +02:00
|
|
|
|
|
|
|
def log_request(self, code='-', size='-'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def do_GET(self):
|
|
|
|
"""Serve a GET request."""
|
|
|
|
self._file = self.send_head()
|
|
|
|
if self._file:
|
2011-11-15 19:29:07 +01:00
|
|
|
self._srcid = GObject.io_add_watch(self.wfile, GObject.IO_OUT |
|
|
|
|
GObject.IO_ERR,
|
2008-04-19 13:05:48 +02:00
|
|
|
self._send_next_chunk)
|
2007-05-24 19:57:11 +02:00
|
|
|
else:
|
|
|
|
self._cleanup()
|
|
|
|
|
|
|
|
def _send_next_chunk(self, source, condition):
|
2011-11-15 19:29:07 +01:00
|
|
|
if condition & GObject.IO_ERR:
|
2007-05-24 19:57:11 +02:00
|
|
|
self._cleanup()
|
|
|
|
return False
|
2011-11-15 19:29:07 +01:00
|
|
|
if not (condition & GObject.IO_OUT):
|
2007-05-24 19:57:11 +02:00
|
|
|
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()
|
2007-07-18 04:30:23 +02:00
|
|
|
self._file = None
|
2007-05-24 19:57:11 +02:00
|
|
|
if self._srcid > 0:
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.source_remove(self._srcid)
|
2007-05-24 19:57:11 +02:00
|
|
|
self._srcid = 0
|
|
|
|
if not self.wfile.closed:
|
|
|
|
self.wfile.flush()
|
|
|
|
self.wfile.close()
|
|
|
|
self.rfile.close()
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
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)
|
2007-08-31 20:32:33 +02:00
|
|
|
if not path or not os.path.exists(path):
|
2010-10-15 21:14:59 +02:00
|
|
|
self.send_error(404, 'File not found')
|
2007-08-31 20:32:33 +02:00
|
|
|
return None
|
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
f = None
|
|
|
|
if os.path.isdir(path):
|
2010-10-15 21:14:59 +02:00
|
|
|
for index in 'index.html', 'index.htm':
|
2007-05-24 19:57:11 +02:00
|
|
|
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:
|
2010-10-15 21:14:59 +02:00
|
|
|
self.send_error(404, 'File not found')
|
2007-05-24 19:57:11 +02:00
|
|
|
return None
|
|
|
|
self.send_response(200)
|
2010-10-15 21:14:59 +02:00
|
|
|
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"' %
|
2008-04-19 13:05:48 +02:00
|
|
|
os.path.basename(path))
|
2007-05-24 19:57:11 +02:00
|
|
|
self.end_headers()
|
|
|
|
return f
|
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2011-11-15 19:29:07 +01:00
|
|
|
class GlibURLDownloader(GObject.GObject):
|
2007-05-24 19:57:11 +02:00
|
|
|
"""Grabs a URL in chunks, returning to the mainloop after each chunk"""
|
|
|
|
|
|
|
|
__gsignals__ = {
|
2011-11-15 19:29:07 +01:00
|
|
|
'finished': (GObject.SignalFlags.RUN_FIRST, None,
|
2013-05-18 04:33:36 +02:00
|
|
|
([GObject.TYPE_PYOBJECT, GObject.TYPE_PYOBJECT])),
|
2011-11-15 19:29:07 +01:00
|
|
|
'error': (GObject.SignalFlags.RUN_FIRST, None,
|
2013-05-18 04:33:36 +02:00
|
|
|
([GObject.TYPE_PYOBJECT])),
|
2011-11-15 19:29:07 +01:00
|
|
|
'progress': (GObject.SignalFlags.RUN_FIRST, None,
|
2013-05-18 04:33:36 +02:00
|
|
|
([GObject.TYPE_PYOBJECT])),
|
2007-05-24 19:57:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CHUNK_SIZE = 4096
|
|
|
|
|
|
|
|
def __init__(self, url, destdir=None):
|
|
|
|
self._url = url
|
|
|
|
if not destdir:
|
2007-07-23 07:17:21 +02:00
|
|
|
destdir = tempfile.gettempdir()
|
2007-05-24 19:57:11 +02:00
|
|
|
self._destdir = destdir
|
|
|
|
self._srcid = 0
|
|
|
|
self._fname = None
|
|
|
|
self._outf = None
|
2008-04-19 13:05:48 +02:00
|
|
|
self._suggested_fname = None
|
|
|
|
self._info = None
|
2007-08-20 22:48:28 +02:00
|
|
|
self._written = 0
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.GObject.__init__(self)
|
2007-05-24 19:57:11 +02:00
|
|
|
|
2007-08-20 22:48:28 +02:00
|
|
|
def start(self, destfile=None, destfd=None):
|
2007-05-24 19:57:11 +02:00
|
|
|
self._info = urllib.urlopen(self._url)
|
2007-07-23 07:17:21 +02:00
|
|
|
self._outf = None
|
|
|
|
self._fname = None
|
2007-08-20 22:48:28 +02:00
|
|
|
if destfd and not destfile:
|
2010-10-15 21:14:59 +02:00
|
|
|
raise ValueError('Must provide destination file too when'
|
|
|
|
' specifying file descriptor')
|
2007-07-23 07:17:21 +02:00
|
|
|
if destfile:
|
|
|
|
self._suggested_fname = os.path.basename(destfile)
|
|
|
|
self._fname = os.path.abspath(os.path.expanduser(destfile))
|
2007-08-20 22:48:28 +02:00
|
|
|
if destfd:
|
|
|
|
# Use the user-supplied destination file descriptor
|
|
|
|
self._outf = destfd
|
|
|
|
else:
|
2008-04-19 13:05:48 +02:00
|
|
|
self._outf = os.open(self._fname, os.O_RDWR |
|
|
|
|
os.O_TRUNC | os.O_CREAT, 0644)
|
2007-07-23 07:17:21 +02:00
|
|
|
else:
|
2008-04-19 13:05:48 +02:00
|
|
|
fname = self._get_filename_from_headers(self._info.headers)
|
|
|
|
self._suggested_fname = fname
|
2008-04-24 16:55:19 +02:00
|
|
|
garbage_, path = urllib.splittype(self._url)
|
|
|
|
garbage_, path = urllib.splithost(path or "")
|
|
|
|
path, garbage_ = urllib.splitquery(path or "")
|
|
|
|
path, garbage_ = urllib.splitattr(path or "")
|
2007-07-23 07:17:21 +02:00
|
|
|
suffix = os.path.splitext(path)[1]
|
2008-04-19 13:05:48 +02:00
|
|
|
(self._outf, self._fname) = tempfile.mkstemp(suffix=suffix,
|
|
|
|
dir=self._destdir)
|
2007-05-24 19:57:11 +02:00
|
|
|
|
|
|
|
fcntl.fcntl(self._info.fp.fileno(), fcntl.F_SETFD, os.O_NDELAY)
|
2011-11-15 19:29:07 +01:00
|
|
|
self._srcid = GObject.io_add_watch(self._info.fp.fileno(),
|
|
|
|
GObject.IO_IN | GObject.IO_ERR,
|
2007-05-24 19:57:11 +02:00
|
|
|
self._read_next_chunk)
|
|
|
|
|
2007-08-20 22:48:28 +02:00
|
|
|
def cancel(self):
|
|
|
|
if self._srcid == 0:
|
2010-10-15 21:14:59 +02:00
|
|
|
raise RuntimeError('Download already canceled or stopped')
|
2007-08-20 22:48:28 +02:00
|
|
|
self.cleanup(remove=True)
|
|
|
|
|
2007-05-24 19:57:11 +02:00
|
|
|
def _get_filename_from_headers(self, headers):
|
2010-10-15 20:18:15 +02:00
|
|
|
if 'Content-Disposition' not in headers:
|
2007-05-24 19:57:11 +02:00
|
|
|
return None
|
|
|
|
|
2010-10-15 21:14:59 +02:00
|
|
|
ftag = 'filename='
|
|
|
|
data = headers['Content-Disposition']
|
2007-05-24 19:57:11 +02:00
|
|
|
fidx = data.find(ftag)
|
|
|
|
if fidx < 0:
|
|
|
|
return None
|
2010-10-15 20:23:34 +02:00
|
|
|
fname = data[fidx + len(ftag):]
|
2007-05-24 19:57:11 +02:00
|
|
|
if fname[0] == '"' or fname[0] == "'":
|
|
|
|
fname = fname[1:]
|
2010-10-15 20:23:34 +02:00
|
|
|
if fname[len(fname) - 1] == '"' or fname[len(fname) - 1] == "'":
|
|
|
|
fname = fname[:len(fname) - 1]
|
2007-05-24 19:57:11 +02:00
|
|
|
return fname
|
|
|
|
|
|
|
|
def _read_next_chunk(self, source, condition):
|
2011-11-15 19:29:07 +01:00
|
|
|
if condition & GObject.IO_ERR:
|
2007-08-20 22:48:28 +02:00
|
|
|
self.cleanup(remove=True)
|
2010-10-15 21:14:59 +02:00
|
|
|
self.emit('error', 'Error downloading file.')
|
2007-05-24 19:57:11 +02:00
|
|
|
return False
|
2011-11-15 19:29:07 +01:00
|
|
|
elif not (condition & GObject.IO_IN):
|
2007-05-24 19:57:11 +02:00
|
|
|
# shouldn't get here, but...
|
|
|
|
return True
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = self._info.fp.read(self.CHUNK_SIZE)
|
|
|
|
count = os.write(self._outf, data)
|
2007-08-20 22:48:28 +02:00
|
|
|
self._written += len(data)
|
|
|
|
|
|
|
|
# error writing data to file?
|
|
|
|
if count < len(data):
|
|
|
|
self.cleanup(remove=True)
|
2010-10-15 21:14:59 +02:00
|
|
|
self.emit('error', 'Error writing to download file.')
|
2007-08-20 22:48:28 +02:00
|
|
|
return False
|
|
|
|
|
2010-10-15 21:14:59 +02:00
|
|
|
self.emit('progress', self._written)
|
2007-08-20 22:48:28 +02:00
|
|
|
|
|
|
|
# done?
|
2007-05-24 19:57:11 +02:00
|
|
|
if len(data) < self.CHUNK_SIZE:
|
|
|
|
self.cleanup()
|
2010-10-15 21:14:59 +02:00
|
|
|
self.emit('finished', self._fname, self._suggested_fname)
|
2007-05-24 19:57:11 +02:00
|
|
|
return False
|
|
|
|
except Exception, err:
|
2007-08-20 22:48:28 +02:00
|
|
|
self.cleanup(remove=True)
|
2010-10-15 21:14:59 +02:00
|
|
|
self.emit('error', 'Error downloading file: %r' % err)
|
2007-05-24 19:57:11 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2007-08-20 22:48:28 +02:00
|
|
|
def cleanup(self, remove=False):
|
2007-05-24 19:57:11 +02:00
|
|
|
if self._srcid > 0:
|
2011-11-15 19:29:07 +01:00
|
|
|
GObject.source_remove(self._srcid)
|
2007-05-24 19:57:11 +02:00
|
|
|
self._srcid = 0
|
|
|
|
del self._info
|
|
|
|
self._info = None
|
|
|
|
os.close(self._outf)
|
2007-08-20 22:48:28 +02:00
|
|
|
if remove:
|
|
|
|
os.remove(self._fname)
|
2007-05-24 19:57:11 +02:00
|
|
|
self._outf = None
|