def __call__(self, environ, start_response): if 'REQUEST_URI' not in environ: environ['REQUEST_URI'] = ( environ.get('SCRIPT_NAME', '') + environ.get('PATH_INFO', '')) if self.include_os_environ: cgi_environ = os.environ.copy() else: cgi_environ = {} for name in environ: # Should unicode values be encoded? if (name.upper() == name and isinstance(environ[name], str)): cgi_environ[name] = environ[name] if self.query_string is not None: old = cgi_environ.get('QUERY_STRING', '') if old: old += '&' cgi_environ['QUERY_STRING'] = old + self.query_string # When running the unit tests without nose, the script path is not # absolute and tests fail. self.script = os.path.abspath(self.script) cgi_environ['SCRIPT_FILENAME'] = self.script proc = subprocess.Popen( # Begin Paste modification. # The -u option is used prevent Python replacing \n to \r\n when # writing to sys.stdout on Windows. [sys.executable, '-u', self.script], # End Paste modification. stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=cgi_environ, cwd=os.path.dirname(self.script), ) writer = CGIWriter(environ, start_response) if select and sys.platform != 'win32': proc_communicate( proc, stdin=StdinReader.from_environ(environ), stdout=writer, stderr=environ['wsgi.errors']) else: stdout, stderr = proc.communicate(StdinReader.from_environ(environ).read()) if stderr: environ['wsgi.errors'].write(stderr) writer.write(stdout) if not writer.headers_finished: start_response(writer.status, writer.headers) return []
def __call__(self, request, environ, logger=None): if not logger: self.logger = LoggerWriter( logging.getLogger(__name__), logging.ERROR) else: self.logger = logger if 'REQUEST_URI' not in environ: environ['REQUEST_URI'] = ( urllib.quote(environ.get('SCRIPT_NAME', '')) + urllib.quote(environ.get('PATH_INFO', ''))) if self.include_os_environ: cgi_environ = os.environ.copy() else: cgi_environ = {} for name in environ: # Should unicode values be encoded? if (name.upper() == name and isinstance(environ[name], str)): cgi_environ[name] = environ[name] if self.query_string is not None: old = cgi_environ.get('QUERY_STRING', '') if old: old += '&' cgi_environ['QUERY_STRING'] = old + self.query_string cgi_environ['SCRIPT_FILENAME'] = self.script proc = subprocess.Popen( [self.script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=cgi_environ, cwd=os.path.dirname(self.script), ) writer = CGIWriter() if select and sys.platform != 'win32': proc_communicate( proc, stdin=request, stdout=writer, stderr=self.logger) else: stdout, stderr = proc.communicate(request.read()) if stderr: self.logger.write(stderr) writer.write(stdout) if not writer.headers_finished: return HttpResponse(status=400) return writer.response
def __call__(self, request, environ, logger=None): if not logger: self.logger = LoggerWriter(logging.getLogger(__name__), logging.ERROR) else: self.logger = logger if 'REQUEST_URI' not in environ: environ['REQUEST_URI'] = ( urllib.quote(environ.get('SCRIPT_NAME', '')) + urllib.quote(environ.get('PATH_INFO', ''))) if self.include_os_environ: cgi_environ = os.environ.copy() else: cgi_environ = {} for name in environ: # Should unicode values be encoded? if (name.upper() == name and isinstance(environ[name], str)): cgi_environ[name] = environ[name] if self.query_string is not None: old = cgi_environ.get('QUERY_STRING', '') if old: old += '&' cgi_environ['QUERY_STRING'] = old + self.query_string cgi_environ['SCRIPT_FILENAME'] = self.script proc = subprocess.Popen( [self.script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=cgi_environ, cwd=os.path.dirname(self.script), ) writer = CGIWriter() if select and sys.platform != 'win32': proc_communicate(proc, stdin=request, stdout=writer, stderr=self.logger) else: stdout, stderr = proc.communicate(request.read()) if stderr: self.logger.write(stderr) writer.write(stdout) if not writer.headers_finished: return HttpResponse(status=400) return writer.response