1 | try:
|
---|
2 | from cStringIO import StringIO
|
---|
3 | except ImportError:
|
---|
4 | from StringIO import StringIO
|
---|
5 | import mimetools
|
---|
6 | import os
|
---|
7 | import socket
|
---|
8 | import urllib
|
---|
9 | from urllib2 import BaseHandler, URLError
|
---|
10 |
|
---|
11 |
|
---|
12 | class FileHandler(BaseHandler):
|
---|
13 | # Use local file or FTP depending on form of URL
|
---|
14 | def file_open(self, req):
|
---|
15 | url = req.get_selector()
|
---|
16 | if url[:2] == '//' and url[2:3] != '/':
|
---|
17 | req.type = 'ftp'
|
---|
18 | return self.parent.open(req)
|
---|
19 | else:
|
---|
20 | return self.open_local_file(req)
|
---|
21 |
|
---|
22 | # names for the localhost
|
---|
23 | names = None
|
---|
24 | def get_names(self):
|
---|
25 | if FileHandler.names is None:
|
---|
26 | try:
|
---|
27 | FileHandler.names = (socket.gethostbyname('localhost'),
|
---|
28 | socket.gethostbyname(socket.gethostname()))
|
---|
29 | except socket.gaierror:
|
---|
30 | FileHandler.names = (socket.gethostbyname('localhost'),)
|
---|
31 | return FileHandler.names
|
---|
32 |
|
---|
33 | # not entirely sure what the rules are here
|
---|
34 | def open_local_file(self, req):
|
---|
35 | try:
|
---|
36 | import email.utils as emailutils
|
---|
37 | except ImportError:
|
---|
38 | import email.Utils as emailutils
|
---|
39 | import mimetypes
|
---|
40 | host = req.get_host()
|
---|
41 | file = req.get_selector()
|
---|
42 | localfile = urllib.url2pathname(file)
|
---|
43 | try:
|
---|
44 | stats = os.stat(localfile)
|
---|
45 | size = stats.st_size
|
---|
46 | modified = emailutils.formatdate(stats.st_mtime, usegmt=True)
|
---|
47 | mtype = mimetypes.guess_type(file)[0]
|
---|
48 | headers = mimetools.Message(StringIO(
|
---|
49 | 'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
|
---|
50 | (mtype or 'text/plain', size, modified)))
|
---|
51 | if host:
|
---|
52 | host, port = urllib.splitport(host)
|
---|
53 | if not host or \
|
---|
54 | (not port and socket.gethostbyname(host) in self.get_names()):
|
---|
55 | return urllib.addinfourl(open(localfile, 'rb'),
|
---|
56 | headers, 'file:' + file)
|
---|
57 | except OSError, msg:
|
---|
58 | # urllib2 users shouldn't expect OSErrors coming from urlopen()
|
---|
59 | raise URLError(msg)
|
---|
60 | raise URLError('file not on local host')
|
---|