def handle_static(environ, start_response, path): """ Creates a response for a static file. There might be a longer path then just /static/... - if so strip the path leading up to static. :param environ: wsgi enviroment :param start_response: wsgi start response :param path: the static file and path to the file. :return: wsgi response for the static file. """ try: data = open(path, "rb").read() if path.endswith(".ico"): resp = Response(data, headers=[("Content-Type", "image/x-icon")]) elif path.endswith(".html"): resp = Response(data, headers=[("Content-Type", "text/html")]) elif path.endswith(".txt"): resp = Response(data, headers=[("Content-Type", "text/plain")]) elif path.endswith(".css"): resp = Response(data, headers=[("Content-Type", "text/css")]) elif path.endswith(".js"): resp = Response(data, headers=[("Content-Type", "text/javascript")]) elif path.endswith(".png"): resp = Response(data, headers=[("Content-Type", "image/png")]) else: resp = Response(data) except IOError: resp = NotFound() return resp(environ, start_response)
def run_server(self, environ, start_response, debug=False): path = environ.get('PATH_INFO', '').lstrip('/') if ".." in path: resp = Unauthorized() return resp(environ, start_response) context = Context() context.path = path # copy wsgi.input stream to allow it to be re-read later by satosa plugins # see: http://stackoverflow.com/questions/1783383/how-do-i-copy-wsgi-input-if-i-want-to-process-post-data-more-than-once content_length = int(environ.get('CONTENT_LENGTH', '0') or '0') body = io.BytesIO(environ['wsgi.input'].read(content_length)) environ['wsgi.input'] = body context.request = unpack_either(environ) environ['wsgi.input'].seek(0) context.wsgi_environ = environ context.cookie = environ.get("HTTP_COOKIE", "") try: resp = self.run(context) if isinstance(resp, Exception): raise resp return resp(environ, start_response) except SATOSANoBoundEndpointError: resp = NotFound("Couldn't find the side you asked for!") return resp(environ, start_response) except Exception as err: logger.exception("%s" % err) if debug: raise resp = ServiceError("%s" % err) return resp(environ, start_response)
def handleStatic(environ, start_response, path): """ Creates a response for a static file. :param environ: wsgi enviroment :param start_response: wsgi start response :param path: the static file and path to the file. :return: wsgi response for the static file. """ try: text = open(path).read() if path.endswith(".ico"): resp = Response(text, headers=[('Content-Type', "image/x-icon")]) elif path.endswith(".html"): resp = Response(text, headers=[('Content-Type', 'text/html')]) elif path.endswith(".txt"): resp = Response(text, headers=[('Content-Type', 'text/plain')]) elif path.endswith(".css"): resp = Response(text, headers=[('Content-Type', 'text/css')]) elif path.endswith(".js"): resp = Response(text, headers=[('Content-Type', 'text/javascript')]) elif path.endswith(".png"): resp = Response(text, headers=[('Content-Type', 'image/png')]) else: resp = Response(text) except IOError: resp = NotFound() return resp(environ, start_response)
def css(environ, start_response): try: info = open(environ["PATH_INFO"]).read() resp = Response(info) except (OSError, IOError): resp = NotFound(environ["PATH_INFO"]) return resp(environ, start_response)
def do(self, aid, binding, relay_state="", encrypt_cert=None): logger.info("--- Assertion ID Service ---") try: assertion = IDP.create_assertion_id_request_response(aid) except Unknown: resp = NotFound(aid) return resp(self.environ, self.start_response) hinfo = IDP.apply_binding(BINDING_URI, "%s" % assertion, response=True) logger.debug("HINFO: %s", hinfo) resp = Response(hinfo["data"], headers=hinfo["headers"]) return resp(self.environ, self.start_response)
def static(environ, start_response, path): try: text = open(path).read() if path.endswith(".ico"): resp = Response(text, headers=[('Content-Type', "image/x-icon")]) elif path.endswith(".html"): resp = Response(text, headers=[('Content-Type', 'text/html')]) elif path.endswith(".txt"): resp = Response(text, headers=[('Content-Type', 'text/plain')]) else: resp = Response(text, headers=[('Content-Type', 'text/xml')]) except IOError: resp = NotFound() return resp(environ, start_response)
def static(environ, start_response, path): LOGGER.info("[static]sending: %s" % (path, )) try: text = open(path).read() if path.endswith(".ico"): start_response('200 OK', [('Content-Type', "image/x-icon")]) elif path.endswith(".html"): start_response('200 OK', [('Content-Type', 'text/html')]) elif path.endswith(".json"): start_response('200 OK', [('Content-Type', 'application/json')]) elif path.endswith(".txt"): start_response('200 OK', [('Content-Type', 'text/plain')]) elif path.endswith(".css"): start_response('200 OK', [('Content-Type', 'text/css')]) else: start_response('200 OK', [('Content-Type', "text/xml")]) return [text] except IOError: resp = NotFound() return resp(environ, start_response)
def run_server(self, environ, start_response): """ The main WSGI application. If nothing matches return NotFound. :param environ: The HTTP application environment :param start_response: The application to run when the handling of the request is done :return: The response as a list of lines """ path = environ.get('PATH_INFO', '').lstrip('/') if ".." in path: resp = Unauthorized() return resp(environ, start_response) for regex, spec in self.urls: match = re.search(regex, path) if match is not None: try: environ['oic.url_args'] = match.groups()[0] except IndexError: environ['oic.url_args'] = path try: return self.run_entity(spec, environ, start_response) except Exception as err: if not self.debug: print("%s" % err, file=sys.stderr) traceback.print_exc() logger.exception("%s" % err) resp = ServiceError("%s" % err) return resp(environ, start_response) else: raise logger.debug("unknown side: %s" % path) resp = NotFound("Couldn't find the side you asked for!") return resp(environ, start_response)
def not_found(environ, start_response): """Called if no URL matches.""" resp = NotFound() return resp(environ, start_response)
destination, query["RelayState"], response=True) except Exception, exc: resp = BadRequest('%s' % exc) return resp(environ, start_response) delco = delete_cookie(environ, "pysaml2idp") if delco: http_args["headers"].append(delco) if binding == BINDING_HTTP_POST: resp = Response(http_args["data"], headers=http_args["headers"]) else: resp = NotFound(http_args["data"], headers=http_args["headers"]) return resp(environ, start_response) def delete_cookie(environ, name): kaka = environ.get("HTTP_COOKIE", '') if kaka: cookie_obj = SimpleCookie(kaka) morsel = cookie_obj.get(name, None) cookie = SimpleCookie() cookie[name] = morsel cookie[name]["expires"] = \ _expiration("now", "%a, %d-%b-%Y %H:%M:%S CET") return tuple(cookie.output().split(": ", 1)) return None
def not_found(environ, start_response): resp = NotFound() return resp(environ, start_response)
environ['oic.url_args'] = match.groups()[0] except IndexError: environ['oic.url_args'] = path try: return run(spec, environ, start_response) except Exception, err: print >> sys.stderr, "%s" % err message = traceback.format_exception(*sys.exc_info()) print >> sys.stderr, message LOGGER.exception("%s" % err) resp = ServiceError("%s" % err) return resp(environ, start_response) LOGGER.debug("unknown side: %s" % path) resp = NotFound("Couldn't find the side you asked for!") return resp(environ, start_response) # ---------------------------------------------------------------------------- if __name__ == '__main__': import argparse import importlib from cherrypy import wsgiserver from cherrypy.wsgiserver import ssl_pyopenssl parser = argparse.ArgumentParser() parser.add_argument('-d', dest='debug', action='store_true') parser.add_argument('-e', dest="entityid")