def sendfile(self, path, offset=0, the_len=-1): try: self.send_http_header() file_to_send = open(path) file_to_send.seek(offset) file_wrapper = FileWrapper(file_to_send) count = 0 if the_len < 0: for chunk in file_wrapper: count += len(chunk) self.__bytes_sent += len(chunk) self.__write(chunk) else: for chunk in file_wrapper: if the_len >= len(chunk): the_len -= len(chunk) count += len(chunk) self.__bytes_sent += len(chunk) self.__write(chunk) else: count += the_len self.__bytes_sent += the_len self.__write(chunk[:the_len]) break except IOError, err: if "failed to write data" in str(err): pass elif "client connection closed" in str(err): registerException() else: raise
def sendfile(self, path, offset=0, the_len=-1): try: self.send_http_header() file_to_send = open(path) file_to_send.seek(offset) file_wrapper = FileWrapper(file_to_send) count = 0 if the_len < 0: for chunk in file_wrapper: count += len(chunk) self.__bytes_sent += len(chunk) self.__write(chunk) else: for chunk in file_wrapper: if the_len >= len(chunk): the_len -= len(chunk) count += len(chunk) self.__bytes_sent += len(chunk) self.__write(chunk) else: count += the_len self.__bytes_sent += the_len self.__write(chunk[:the_len]) break except IOError, err: if "failed to write data" in str(err): pass elif "client connection closed" in str(err): registerException() else: raise
def mp_legacy_publisher(req, possible_module, possible_handler): """ mod_python legacy publisher minimum implementation. """ the_module = open(possible_module).read() module_globals = {} try: exec(the_module, module_globals) except: # Log which file caused the exec error (traceback won't do it for # some reason) and relaunch the exception registerException('Error executing the module %s' % possible_module) raise if possible_handler in module_globals and \ callable(module_globals[possible_handler]): from indico.web.wsgi.indico_wsgi_handler_utils import _check_result ## the req.form must be casted to dict because of Python 2.4 and earlier ## otherwise any object exposing the mapping interface can be ## used with the magic ** form = dict(req.form) _convert_to_string(form) try: return _check_result(req, module_globals[possible_handler](req, **form)) except TypeError, err: if ("%s() got an unexpected keyword argument" % possible_handler) in \ str(err) or ('%s() takes at least' % possible_handler) in str(err): import inspect inspected_args = inspect.getargspec( module_globals[possible_handler]) expected_args = list(inspected_args[0]) expected_defaults = list(inspected_args[3]) expected_args.reverse() expected_defaults.reverse() # Write the exception to Apache error log file registerException("Wrong GET parameter set in calling a legacy " "publisher handler for %s: expected_args=%s, " "found_args=%s" % \ (possible_handler, repr(expected_args), repr(req.form.keys()))) cleaned_form = {} for index, arg in enumerate(expected_args): if arg == 'req': continue if index < len(expected_defaults): cleaned_form[arg] = form.get(arg, expected_defaults[index]) else: cleaned_form[arg] = form.get(arg, None) return _check_result( req, module_globals[possible_handler](req, **cleaned_form)) else: raise
def mp_legacy_publisher(req, possible_module, possible_handler): """ mod_python legacy publisher minimum implementation. """ the_module = open(possible_module).read() module_globals = {} try: exec(the_module, module_globals) except: # Log which file caused the exec error (traceback won't do it for # some reason) and relaunch the exception registerException("Error executing the module %s" % possible_module) raise if possible_handler in module_globals and callable(module_globals[possible_handler]): from indico.web.wsgi.indico_wsgi_handler_utils import _check_result ## the req.form must be casted to dict because of Python 2.4 and earlier ## otherwise any object exposing the mapping interface can be ## used with the magic ** form = dict(req.form) _convert_to_string(form) try: return _check_result(req, module_globals[possible_handler](req, **form)) except TypeError, err: if ("%s() got an unexpected keyword argument" % possible_handler) in str(err) or ( "%s() takes at least" % possible_handler ) in str(err): import inspect inspected_args = inspect.getargspec(module_globals[possible_handler]) expected_args = list(inspected_args[0]) expected_defaults = list(inspected_args[3]) expected_args.reverse() expected_defaults.reverse() # Write the exception to Apache error log file registerException( "Wrong GET parameter set in calling a legacy " "publisher handler for %s: expected_args=%s, " "found_args=%s" % (possible_handler, repr(expected_args), repr(req.form.keys())) ) cleaned_form = {} for index, arg in enumerate(expected_args): if arg == "req": continue if index < len(expected_defaults): cleaned_form[arg] = form.get(arg, expected_defaults[index]) else: cleaned_form[arg] = form.get(arg, None) return _check_result(req, module_globals[possible_handler](req, **cleaned_form)) else: raise
def flush(self): self.send_http_header() if self.__buffer: self.__bytes_sent += len(self.__buffer) try: if not self.__write_error: self.__write(self.__buffer) except IOError, err: if "failed to write data" in str(err): self.__write_error = True elif "client connection closed" in str(err): registerException() # do not over-report error self.__write_error = True else: raise self.__buffer = ""
def flush(self): self.send_http_header() if self.__buffer: self.__bytes_sent += len(self.__buffer) try: if not self.__write_error: self.__write(self.__buffer) except IOError, err: if "failed to write data" in str(err): self.__write_error = True elif "client connection closed" in str(err): registerException() # do not over-report error self.__write_error = True else: raise self.__buffer = ''
def application(environ, start_response): """ Entry point for wsgi. """ ## Needed for mod_wsgi ## see: <http://code.google.com/p/modwsgi/wiki/ApplicationIssues> req = SimulatedModPythonRequest(environ, start_response) if req.method == "OPTIONS": start_response("501 Not Implemented", [], None) return "" possible_module, possible_handler = is_mp_legacy_publisher_path(req) # The POST form processing has to be done after checking the path req.get_post_form() try: try: # if condition: # wsgi_publisher(possible_module, possible_handler) if possible_module is not None: mp_legacy_publisher(req, possible_module, possible_handler) else: from indico.web.wsgi.indico_wsgi_file_handler import stream_file url = req.URLFields["PATH_INFO"] # Check if it is a static file possible_static_path = is_static_path(environ["PATH_INFO"]) if not possible_static_path: # maybe the url is owned by some plugin? pluginRHMap = RHMapMemory()._map for urlRE, rh in pluginRHMap.iteritems(): m = urlRE.match(url) if m: if type(rh) == ClassType or RHHtdocs not in rh.mro(): raise SERVER_RETURN, plugin_publisher(req, url, rh, m.groupdict()) else: # calculate the path to the resource possible_static_path = rh.calculatePath(**m.groupdict()) if possible_static_path is not None: stream_file(req, possible_static_path) else: raise SERVER_RETURN, HTTP_NOT_FOUND req.flush() # Exception treatment except SERVER_RETURN, status: status = int(str(status)) if status not in (OK, DONE): req.status = status req.headers_out["content-type"] = "text/html" start_response(req.get_wsgi_status(), req.get_low_level_headers(), sys.exc_info()) return ("%s" % indicoErrorWebpage(status),) else: req.flush() # If we reach this block, there was probably # an error importing a legacy python module except Exception: req.status = HTTP_INTERNAL_SERVER_ERROR req.headers_out["content-type"] = "text/html" start_response(req.get_wsgi_status(), req.get_low_level_headers(), sys.exc_info()) registerException() return ("%s" % indicoErrorWebpage(500),)
def application(environ, start_response): """ Entry point for wsgi. """ ## Needed for mod_wsgi ## see: <http://code.google.com/p/modwsgi/wiki/ApplicationIssues> req = SimulatedModPythonRequest(environ, start_response) if req.method == 'OPTIONS': start_response("501 Not Implemented", [], None) return '' possible_module, possible_handler = is_mp_legacy_publisher_path(req) # The POST form processing has to be done after checking the path req.get_post_form() try: try: #if condition: # wsgi_publisher(possible_module, possible_handler) if possible_module is not None: mp_legacy_publisher(req, possible_module, possible_handler) else: from indico.web.wsgi.indico_wsgi_file_handler import stream_file url = req.URLFields['PATH_INFO'] # Check if it is a static file possible_static_path = is_static_path(environ['PATH_INFO']) if not possible_static_path: # maybe the url is owned by some plugin? pluginRHMap = RHMapMemory()._map for urlRE, rh in pluginRHMap.iteritems(): m = urlRE.match(url) if m: if type(rh) == ClassType or RHHtdocs not in rh.mro( ): raise SERVER_RETURN, plugin_publisher( req, url, rh, m.groupdict()) else: # calculate the path to the resource possible_static_path = rh.calculatePath( **m.groupdict()) if possible_static_path is not None: stream_file(req, possible_static_path) else: raise SERVER_RETURN, HTTP_NOT_FOUND req.flush() #Exception treatment except SERVER_RETURN, status: status = int(str(status)) if status != HTTP_OK: req.status = status req.headers_out['content-type'] = 'text/html' start_response(req.get_wsgi_status(), req.get_low_level_headers(), sys.exc_info()) return ('%s' % indicoErrorWebpage(status), ) else: req.flush() # If we reach this block, there was probably # an error importing a legacy python module except Exception: req.status = HTTP_INTERNAL_SERVER_ERROR req.headers_out['content-type'] = 'text/html' start_response(req.get_wsgi_status(), req.get_low_level_headers(), sys.exc_info()) registerException() return ('%s' % indicoErrorWebpage(500), )