Exemplo n.º 1
0
 def open_local_file(self, req):
     host = req.get_host()
     file = req.get_selector()
     localfile = urllib.request.url2pathname(file)
     stats = os.stat(localfile)
     size = stats[stat.ST_SIZE]
     modified = email.utils.formatdate(stats[stat.ST_MTIME])
     mtype = mimetypes.guess_type(file)[0]
     if host:
         host, port = urllib.parse.splitport(host)
         if port or socket.gethostbyname(host) not in self.get_names():
             raise urllib.error.URLError('file not on local host')
     fo = open(localfile,'rb')
     brange = req.headers.get('Range',None)
     brange = range_header_to_tuple(brange)
     assert brange != ()
     if brange:
         (fb,lb) = brange
         if lb == '':
             lb = size
         if fb < 0 or fb > size or lb > size:
             raise RangeError(9, 'Requested Range Not Satisfiable')
         size = (lb - fb)
         fo = RangeableFileObject(fo, (fb,lb))
     headers = email.message.Message(BytesIO(
         b'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
         (mtype or 'text/plain', size, modified)))
     return urllib.addinfourl(fo, headers, 'file:'+file)
Exemplo n.º 2
0
 def open_local_file(self, req):
     host = req.get_host()
     file = req.get_selector()
     localfile = urllib.request.url2pathname(file)
     stats = os.stat(localfile)
     size = stats[stat.ST_SIZE]
     modified = email.utils.formatdate(stats[stat.ST_MTIME])
     mtype = mimetypes.guess_type(file)[0]
     if host:
         host, port = urllib.parse.splitport(host)
         if port or socket.gethostbyname(host) not in self.get_names():
             raise urllib.error.URLError('file not on local host')
     fo = open(localfile, 'rb')
     brange = req.headers.get('Range', None)
     brange = range_header_to_tuple(brange)
     assert brange != ()
     if brange:
         (fb, lb) = brange
         if lb == '':
             lb = size
         if fb < 0 or fb > size or lb > size:
             raise RangeError(9, 'Requested Range Not Satisfiable')
         size = (lb - fb)
         fo = RangeableFileObject(fo, (fb, lb))
     headers = email.message.Message(
         BytesIO(
             b'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
             (mtype or 'text/plain', size, modified)))
     return urllib.addinfourl(fo, headers, 'file:' + file)
Exemplo n.º 3
0
 def http_error_206(self, req, fp, code, msg, hdrs):
     # 206 Partial Content Response
     r = urllib.addinfourl(fp, hdrs, req.get_full_url())
     r.code = code
     r.msg = msg
     return r
Exemplo n.º 4
0
 def urlopen(url, *a, **kw):
     if url == given_url:
         return urllib.addinfourl(StringIO(response_string), [], url)
     else:
         return _urlopen(url, *a, **kw)
Exemplo n.º 5
0
 def http_error_206(self, req, fp, code, msg, hdrs):
     # 206 Partial Content Response
     r = urllib.addinfourl(fp, hdrs, req.get_full_url())
     r.code = code
     r.msg = msg
     return r