home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import mimetypes
- mimetypes.init()
- mimetypes.types_map['.dwg'] = 'image/x-dwg'
- mimetypes.types_map['.ico'] = 'image/x-icon'
- import os
- import re
- import stat
- import time
- import urllib
- import cherrypy
- from cherrypy.lib import cptools, http, file_generator_limited
-
- def serve_file(path, content_type = None, disposition = None, name = None):
- response = cherrypy.response
- if not os.path.isabs(path):
- raise ValueError("'%s' is not an absolute path." % path)
- os.path.isabs(path)
-
- try:
- st = os.stat(path)
- except OSError:
- raise cherrypy.NotFound()
-
- if stat.S_ISDIR(st.st_mode):
- raise cherrypy.NotFound()
- stat.S_ISDIR(st.st_mode)
- response.headers['Last-Modified'] = http.HTTPDate(st.st_mtime)
- cptools.validate_since()
- if content_type is None:
- ext = ''
- i = path.rfind('.')
- if i != -1:
- ext = path[i:].lower()
-
- content_type = mimetypes.types_map.get(ext, 'text/plain')
-
- response.headers['Content-Type'] = content_type
- if disposition is not None:
- if name is None:
- name = os.path.basename(path)
-
- cd = '%s; filename="%s"' % (disposition, name)
- response.headers['Content-Disposition'] = cd
-
- c_len = st.st_size
- bodyfile = open(path, 'rb')
- if cherrypy.request.protocol >= (1, 1):
- response.headers['Accept-Ranges'] = 'bytes'
- r = http.get_ranges(cherrypy.request.headers.get('Range'), c_len)
- if r == []:
- response.headers['Content-Range'] = 'bytes */%s' % c_len
- message = 'Invalid Range (first-byte-pos greater than Content-Length)'
- raise cherrypy.HTTPError(416, message)
- r == []
- if r:
- if len(r) == 1:
- (start, stop) = r[0]
- if stop > c_len:
- stop = c_len
-
- r_len = stop - start
- response.status = '206 Partial Content'
- response.headers['Content-Range'] = 'bytes %s-%s/%s' % (start, stop - 1, c_len)
- response.headers['Content-Length'] = r_len
- bodyfile.seek(start)
- response.body = file_generator_limited(bodyfile, r_len)
- else:
- response.status = '206 Partial Content'
- import mimetools
- boundary = mimetools.choose_boundary()
- ct = 'multipart/byteranges; boundary=%s' % boundary
- response.headers['Content-Type'] = ct
- if response.headers.has_key('Content-Length'):
- del response.headers['Content-Length']
-
-
- def file_ranges():
- yield '\r\n'
- for start, stop in r:
- yield '--' + boundary
- yield '\r\nContent-type: %s' % content_type
- yield '\r\nContent-range: bytes %s-%s/%s\r\n\r\n' % (start, stop - 1, c_len)
- bodyfile.seek(start)
- for chunk in file_generator_limited(bodyfile, stop - start):
- yield chunk
-
- yield '\r\n'
-
- yield '--' + boundary + '--'
- yield '\r\n'
-
- response.body = file_ranges()
- else:
- response.headers['Content-Length'] = c_len
- response.body = bodyfile
- else:
- response.headers['Content-Length'] = c_len
- response.body = bodyfile
- return response.body
-
-
- def serve_download(path, name = None):
- return serve_file(path, 'application/x-download', 'attachment', name)
-
-
- def _attempt(filename, content_types):
-
- try:
- content_type = None
- if content_types:
- (r, ext) = os.path.splitext(filename)
- content_type = content_types.get(ext[1:], None)
-
- serve_file(filename, content_type = content_type)
- return True
- except cherrypy.NotFound:
- return False
-
-
-
- def staticdir(section, dir, root = '', match = '', content_types = None, index = ''):
- if cherrypy.request.method not in ('GET', 'HEAD'):
- return False
- if match and not re.search(match, cherrypy.request.path_info):
- return False
- dir = os.path.expanduser(dir)
- if section == 'global':
- section = '/'
-
- section = section.rstrip('\\/')
- branch = cherrypy.request.path_info[len(section) + 1:]
- branch = urllib.unquote(branch.lstrip('\\/'))
- filename = os.path.join(dir, branch)
- if not os.path.normpath(filename).startswith(os.path.normpath(dir)):
- raise cherrypy.HTTPError(403)
- os.path.normpath(filename).startswith(os.path.normpath(dir))
- handled = _attempt(filename, content_types)
- if not handled:
- if index:
- handled = _attempt(os.path.join(filename, index), content_types)
- if handled:
- cherrypy.request.is_index = filename[-1] in '\\/'
-
-
-
- return handled
-
-
- def staticfile(filename, root = None, match = '', content_types = None):
- if cherrypy.request.method not in ('GET', 'HEAD'):
- return False
- if match and not re.search(match, cherrypy.request.path_info):
- return False
- return _attempt(filename, content_types)
-
-