Package wsgitools :: Package scgi
[hide private]
[frames] | no frames]

Source Code for Package wsgitools.scgi

 1  __all__ = [] 
 2   
 3  try: 
 4      import sendfile 
 5  except ImportError: 
 6      have_sendfile = False 
 7  else: 
 8      have_sendfile = True 
 9   
10 -class FileWrapper(object):
11 """ 12 @ivar offset: Initially 0. Becomes -1 when reading using next and 13 becomes positive when reading using next. In the latter case it 14 counts the number of bytes sent. It also ensures that next and 15 transfer are never mixed. 16 """
17 - def __init__(self, filelike, blksize=8192):
18 self.filelike = filelike 19 self.blksize = blksize 20 self.offset = 0 21 if hasattr(filelike, "close"): 22 self.close = filelike.close
23
24 - def can_transfer(self):
25 return have_sendfile and hasattr(self.filelike, "fileno") and \ 26 self.offset >= 0
27
28 - def transfer(self, sock, blksize=None):
29 assert self.offset >= 0 30 if blksize is None: 31 blksize = self.blksize 32 else: 33 blksize = min(self.blksize, blksize) 34 try: 35 sent = sendfile.sendfile(sock.fileno(), self.filelike.fileno(), 36 self.offset, blksize) 37 except OSError: 38 return -1 39 # There are two different sendfile libraries. Yeah! 40 if isinstance(sent, tuple): 41 sent = sent[1] 42 self.offset += sent 43 return sent
44
45 - def __iter__(self):
46 return self
47
48 - def __next__(self):
49 assert self.offset <= 0 50 self.offset = -1 51 data = self.filelike.read(self.blksize) 52 if data: 53 return data 54 raise StopIteration
55 - def next(self):
56 return self.__next__()
57
58 -def _convert_environ(environ, multithread=False, multiprocess=False, 59 run_once=False):
60 environ.update({ 61 "wsgi.version": (1, 0), 62 "wsgi.url_scheme": "http", 63 "wsgi.multithread": multithread, 64 "wsgi.multiprocess": multiprocess, 65 "wsgi.run_once": run_once}) 66 if environ.get("HTTPS", "no").lower() in ('yes', 'y', 'on', '1'): 67 environ["wsgi.url_scheme"] = "https" 68 try: 69 environ["CONTENT_TYPE"] = environ.pop("HTTP_CONTENT_TYPE") 70 except KeyError: 71 pass 72 environ.pop("HTTP_CONTENT_LENGTH", None) # TODO: better way? 73 if have_sendfile: 74 environ["wsgi.file_wrapper"] = FileWrapper
75