def stream( self, stream, chunk_size=DEFAULT_CHUNK_SIZE, request=None, attachment=False, filename=None ): """ If in a controller function:: return response.stream(file, 100) the file content will be streamed at 100 bytes at the time Args: stream: filename or read()able content chunk_size(int): Buffer size request: the request object attachment(bool): prepares the correct headers to download the file as an attachment. Usually creates a pop-up download window on browsers filename(str): the name for the attachment Note: for using the stream name (filename) with attachments the option must be explicitly set as function parameter (will default to the last request argument otherwise) """ headers = self.headers # for attachment settings and backward compatibility keys = [item.lower() for item in headers] if attachment: if filename is None: attname = "" else: attname = filename headers["Content-Disposition"] = \ "attachment;filename=%s" % attname if not request: request = current.request if isinstance(stream, (str, unicode)): stream_file_or_304_or_206(stream, chunk_size=chunk_size, request=request, headers=headers, status=self.status) # ## the following is for backward compatibility if hasattr(stream, 'name'): filename = stream.name if filename and not 'content-type' in keys: headers['Content-Type'] = contenttype(filename) if filename and not 'content-length' in keys: try: headers['Content-Length'] = \ os.path.getsize(filename) except OSError: pass env = request.env # Internet Explorer < 9.0 will not allow downloads over SSL unless caching is enabled if request.is_https and isinstance(env.http_user_agent, str) and \ not re.search(r'Opera', env.http_user_agent) and \ re.search(r'MSIE [5-8][^0-9]', env.http_user_agent): headers['Pragma'] = 'cache' headers['Cache-Control'] = 'private' if request and env.web2py_use_wsgi_file_wrapper: wrapped = env.wsgi_file_wrapper(stream, chunk_size) else: wrapped = streamer(stream, chunk_size=chunk_size) return wrapped
def stream(self, stream, chunk_size=DEFAULT_CHUNK_SIZE, request=None, attachment=False, filename=None ): """ If in a controller function:: return response.stream(file, 100) the file content will be streamed at 100 bytes at the time Args: stream: filename or read()able content chunk_size(int): Buffer size request: the request object attachment(bool): prepares the correct headers to download the file as an attachment. Usually creates a pop-up download window on browsers filename(str): the name for the attachment Note: for using the stream name (filename) with attachments the option must be explicitly set as function parameter (will default to the last request argument otherwise) """ headers = self.headers # for attachment settings and backward compatibility keys = [item.lower() for item in headers] if attachment: if filename is None: attname = "" else: attname = filename headers["Content-Disposition"] = \ 'attachment;filename="%s"' % attname if not request: request = current.request if isinstance(stream, (str, unicodeT)): stream_file_or_304_or_206(stream, chunk_size=chunk_size, request=request, headers=headers, status=self.status) # ## the following is for backward compatibility if hasattr(stream, 'name'): filename = stream.name if filename and 'content-type' not in keys: headers['Content-Type'] = contenttype(filename) if filename and 'content-length' not in keys: try: headers['Content-Length'] = \ os.path.getsize(filename) except OSError: pass env = request.env # Internet Explorer < 9.0 will not allow downloads over SSL unless caching is enabled if request.is_https and isinstance(env.http_user_agent, str) and \ not re.search(r'Opera', env.http_user_agent) and \ re.search(r'MSIE [5-8][^0-9]', env.http_user_agent): headers['Pragma'] = 'cache' headers['Cache-Control'] = 'private' if request and env.web2py_use_wsgi_file_wrapper: wrapped = env.wsgi_file_wrapper(stream, chunk_size) else: wrapped = streamer(stream, chunk_size=chunk_size) return wrapped
def main_wsgi_app(environ, start_response): import gluon common_context = {key:getattr(gluon,key) for key in dir(gluon)} have_databases = False try: try: current.request = request = Request(environ) response = session = None request_folder = request.folder # if client requested a static page if request.controller == 'static': current.response = None current.session = None static_folder = os_path_join(request_folder,'static') n = 3 if request.items[2].startswith('_') else 2 filename = os_path_join(static_folder,*request.items[n:]) if not filename.startswith(static_folder+'/'): raise HTTP(404) if not os_path_exists(filename): raise HTTP(404) stream_file_or_304_or_206(filename, environ=environ) # raise HTTP 200 # if instead client requested a dynamic page else: # build context and inject variables into context runner = CodeRunner(common_context.copy()) # inject request specific variables into context runner.context['request'] = request runner.context['response'] = current.response = response = Response() runner.context['session'] = current.session = session = Session() runner.context['T'] = current.T = translator( os_path_join(request_folder,'languages'), request.environ.get('HTTP_ACCEPT_LANGUAGE')) # check if there is a database folder and set the folder database_folder = os_path_join(request_folder,'databases') have_databases = os_path_exists(database_folder) if have_databases: BaseAdapter.set_folder(os_path_join(request_folder, 'databases')) # raise an error if the controller file is missing controllers_folder = os_path_join(request_folder,'controllers') controller_filename = os_path_join(controllers_folder,request.controller+'.py') if not controller_filename.startswith(controllers_folder+'/'): raise HTTP(404) if not os_path_exists(controller_filename): raise HTTP(404) # import models, ugly but faster than glob models_folder = os_path_join(request_folder,'models') if os_path_exists(models_folder): for filename in sorted(filter(lambda x: x[-3:]=='.py',os.listdir(models_folder))): runner.import_code(models_folder+os.sep+filename) # run controller action view_context = runner.context.copy() content = runner.import_code(controller_filename, request.function) # optionally run view func_ext = request.function+'.'+request.extension if isinstance(content, dict): view_context.update(content) template_folder = os_path_join(request_folder,'views') # maybe a response.view is specified if response.view: template_filename = os_path_join(template_folder,response.view) # or maybe not else: template_filename = os_path_join(template_folder,request.controller,func_ext) # if the view exists use it if os_path_exists(template_filename): content = render(filename=template_filename, path = template_folder, context = view_context) # else but represent the context as a dict (generic views?) else: content = repr(view_context) # set the content type response.headers["Content-type"] = contenttype(func_ext) raise HTTP(response.status, content, headers=response.headers) # if a HTTP is raised, everything is ok, return except HTTP, http: if response: # commit databases, if any have_databases = have_databases and response.auto_commit if have_databases: session._try_store_in_db(request, response) BaseAdapter.close_all_instances('commit') have_databases = False # save session, if changed session._try_store_in_cookie_or_file(request, response) # deal with cookies if hasattr(response,'_cookies'): http.cookies2headers(response.cookies) return http.to(start_response, env=environ) # there was an error except Exception, err: # maybe log the ticket if isinstance(err, RestrictedError): ticket = err.log(request) # or maybe not else: print traceback.format_exc() #request.logger.error(traceback.format_exc()) ticket = 'unknown' # return HTTP 500 return HTTP(500, ticket).to(start_response, env=environ)
def stream(self, filename): stream_file_or_304_or_206(filename, environ=current.request.environ)
def stream(self, stream, chunk_size=DEFAULT_CHUNK_SIZE, request=None, attachment=False, filename=None): """ if a controller function:: return response.stream(file, 100) the file content will be streamed at 100 bytes at the time Optional kwargs: (for custom stream calls) attachment=True # Send as attachment. Usually creates a # pop-up download window on browsers filename=None # The name for the attachment Note: for using the stream name (filename) with attachments the option must be explicitly set as function parameter(will default to the last request argument otherwise) """ headers = self.headers # for attachment settings and backward compatibility keys = [item.lower() for item in headers] if attachment: if filename is None: attname = "" else: attname = filename headers["Content-Disposition"] = "attachment;filename=%s" % attname if not request: request = current.request if isinstance(stream, (str, unicode)): stream_file_or_304_or_206( stream, chunk_size=chunk_size, request=request, headers=headers, status=self.status ) # ## the following is for backward compatibility if hasattr(stream, "name"): filename = stream.name if filename and not "content-type" in keys: headers["Content-Type"] = contenttype(filename) if filename and not "content-length" in keys: try: headers["Content-Length"] = os.path.getsize(filename) except OSError: pass env = request.env # Internet Explorer < 9.0 will not allow downloads over SSL unless caching is enabled if ( request.is_https and isinstance(env.http_user_agent, str) and not re.search(r"Opera", env.http_user_agent) and re.search(r"MSIE [5-8][^0-9]", env.http_user_agent) ): headers["Pragma"] = "cache" headers["Cache-Control"] = "private" if request and env.web2py_use_wsgi_file_wrapper: wrapped = env.wsgi_file_wrapper(stream, chunk_size) else: wrapped = streamer(stream, chunk_size=chunk_size) return wrapped