def __call__(self, environ, start_response): cl_key = 'CONTENT_LENGTH' if environ['REQUEST_METHOD'] == 'POST': if cl_key not in environ: r = Response() r.status_code = 411 r.content = '<html><head></head><body>length required</body></html>' return r(environ, start_response) try: cl_int = int(environ[cl_key]) except ValueError: r = Response() r.status_code = 400 r.content = '<html><head></head><body>bad request</body></html>' return r(environ, start_response) if cl_int > self.max_size: from r2.lib.strings import string_dict error_msg = string_dict['css_validator_messages']['max_size'] % dict(max_size = self.max_size/1024) r = Response() r.status_code = 413 r.content = ("<html>" "<head>" "<script type='text/javascript'>" "parent.completedUploadImage('failed'," "''," "''," "[['BAD_CSS_NAME', ''], ['IMAGE_ERROR', '", error_msg,"']]," "'image-upload');" "</script></head><body>you shouldn\'t be here</body></html>") return r(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): cl_key = "CONTENT_LENGTH" if environ["REQUEST_METHOD"] == "POST": if (cl_key not in environ) or int(environ[cl_key]) > self.max_size: r = Response() r.status_code = 500 r.content = '<html><head></head><body><script type="text/javascript">parent.too_big();</script>request too big</body></html>' return r(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): g = config["pylons.g"] http_host = environ.get("HTTP_HOST", "localhost").lower() domain, s, port = http_host.partition(":") # remember the port try: environ["request_port"] = int(port) except ValueError: pass # localhost is exempt so paster run/shell will work # media_domain doesn't need special processing since it's just ads if domain == "localhost" or is_subdomain(domain, g.media_domain): return self.app(environ, start_response) # tell reddit_base to redirect to the appropriate subreddit for # a legacy CNAME if not is_subdomain(domain, g.domain): environ["legacy-cname"] = domain return self.app(environ, start_response) # figure out what subdomain we're on if any subdomains = domain[: -len(g.domain) - 1].split(".") extension_subdomains = dict(m="mobile", i="compact", api="api", rss="rss", xml="xml", json="json") sr_redirect = None for subdomain in subdomains[:]: if subdomain in g.reserved_subdomains: continue extension = extension_subdomains.get(subdomain) if extension: environ["reddit-domain-extension"] = extension elif self.lang_re.match(subdomain): environ["reddit-prefer-lang"] = subdomain environ["reddit-domain-prefix"] = subdomain else: sr_redirect = subdomain subdomains.remove(subdomain) # if there was a subreddit subdomain, redirect if sr_redirect and environ.get("FULLPATH"): r = Response() if not subdomains and g.domain_prefix: subdomains.append(g.domain_prefix) subdomains.append(g.domain) redir = "%s/r/%s/%s" % (".".join(subdomains), sr_redirect, environ["FULLPATH"]) redir = "http://" + redir.replace("//", "/") r.status_code = 301 r.headers["location"] = redir r.content = "" return r(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): cl_key = 'CONTENT_LENGTH' if environ['REQUEST_METHOD'] == 'POST': if ((cl_key not in environ) or int(environ[cl_key]) > self.max_size): r = Response() r.status_code = 500 r.content = '<html><head></head><body><script type="text/javascript">parent.too_big();</script>request too big</body></html>' return r(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): cl_key = 'CONTENT_LENGTH' if environ['REQUEST_METHOD'] == 'POST': if cl_key not in environ: r = Response() r.status_code = 411 r.content = '<html><head></head><body>length required</body></html>' return r(environ, start_response) try: cl_int = int(environ[cl_key]) except ValueError: r = Response() r.status_code = 400 r.content = '<html><head></head><body>bad request</body></html>' return r(environ, start_response) if cl_int > self.max_size: r = Response() r.status_code = 413 r.content = '<html><head></head><body><script type="text/javascript">parent.too_big();</script>request entity too large</body></html>' return r(environ, start_response) return self.app(environ, start_response)
def filter(self, execution_func, prof_arg = None): # put thie imports here so the app doesn't choke if profiling # is not present (this is a debug-only feature anyway) import cProfile as profile from pstats import Stats from r2.lib.contrib import gprof2dot # profiling needs an actual file to dump to. Everything else # can be mitigated with streams tmpfile = tempfile.NamedTemporaryFile() dotfile = StringIO() # simple cutoff validation try: cutoff = .01 if prof_arg is None else float(prof_arg)/100 except ValueError: cutoff = .01 try: # profile the code in the current context profile.runctx('execution_func()', globals(), locals(), tmpfile.name) # parse the data parser = gprof2dot.PstatsParser(tmpfile.name) prof = parser.parse() # remove nodes and edges with less than cutoff work prof.prune(cutoff, cutoff) # make the dotfile dot = gprof2dot.DotWriter(dotfile) dot.graph(prof, gprof2dot.TEMPERATURE_COLORMAP) # convert the dotfile to PNG in local stdout proc = subprocess.Popen("dot -Tpng", shell = True, stdin =subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, error = proc.communicate(input = dotfile.getvalue()) # generate the response r = Response() r.status_code = 200 r.headers['content-type'] = "image/png" r.content = out return r finally: tmpfile.close()
def filter(self, execution_func, prof_arg=None): # put thie imports here so the app doesn't choke if profiling # is not present (this is a debug-only feature anyway) import cProfile as profile from pstats import Stats from r2.lib.contrib import gprof2dot # profiling needs an actual file to dump to. Everything else # can be mitigated with streams tmpfile = tempfile.NamedTemporaryFile() dotfile = StringIO() # simple cutoff validation try: cutoff = .01 if prof_arg is None else float(prof_arg) / 100 except ValueError: cutoff = .01 try: # profile the code in the current context profile.runctx('execution_func()', globals(), locals(), tmpfile.name) # parse the data parser = gprof2dot.PstatsParser(tmpfile.name) prof = parser.parse() # remove nodes and edges with less than cutoff work prof.prune(cutoff, cutoff) # make the dotfile dot = gprof2dot.DotWriter(dotfile) dot.graph(prof, gprof2dot.TEMPERATURE_COLORMAP) # convert the dotfile to PNG in local stdout proc = subprocess.Popen("dot -Tpng", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, error = proc.communicate(input=dotfile.getvalue()) # generate the response r = Response() r.status_code = 200 r.headers['content-type'] = "image/png" r.content = out return r finally: tmpfile.close()
def __call__(self, environ, start_response): # get base domain as defined in INI file base_domain = config['global_conf']['domain'] sub_domains = environ['HTTP_HOST'] if not sub_domains.endswith(base_domain): #if the domain doesn't end with base_domain, don't do anything return self.app(environ, start_response) sub_domains = sub_domains[:-len(base_domain)].strip('.') sub_domains = sub_domains.split('.') sr_redirect = None for sd in list(sub_domains): # subdomains to disregard completely if sd in ('www', 'origin', 'beta'): continue # subdomains which change the extension elif sd == 'm': environ['reddit-domain-extension'] = 'mobile' elif sd in ('api', 'rss', 'xml', 'json'): environ['reddit-domain-extension'] = sd elif (len(sd) == 2 or (len(sd) == 5 and sd[2] == '-')) and self.lang_re.match(sd): environ['reddit-prefer-lang'] = sd else: sr_redirect = sd sub_domains.remove(sd) if sr_redirect and environ.get("FULLPATH"): r = Response() sub_domains.append(base_domain) redir = "%s/r/%s/%s" % ('.'.join(sub_domains), sr_redirect, environ['FULLPATH']) redir = "http://" + redir.replace('//', '/') r.status_code = 301 r.headers['location'] = redir r.content = "" return r(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): # get base domain as defined in INI file base_domain = config['global_conf']['domain'] try: sub_domains, request_port = environ['HTTP_HOST'].split(':') environ['request_port'] = int(request_port) except ValueError: sub_domains = environ['HTTP_HOST'].split(':')[0] except KeyError: sub_domains = "localhost" #If the domain doesn't end with base_domain, assume #this is a cname, and redirect to the frame controller. #Ignore localhost so paster shell still works. #If this is an error, don't redirect if (not sub_domains.endswith(base_domain) and (not sub_domains == 'localhost')): environ['sub_domain'] = sub_domains if not environ.get('extension'): if environ['PATH_INFO'].startswith('/frame'): return self.app(environ, start_response) elif self.is_auth_cname(sub_domains): environ['frameless_cname'] = True environ['authorized_cname'] = True elif ("redditSession=cname" in environ.get('HTTP_COOKIE', '') and environ['REQUEST_METHOD'] != 'POST' and not environ['PATH_INFO'].startswith('/error')): environ['original_path'] = environ['PATH_INFO'] environ['FULLPATH'] = environ['PATH_INFO'] = '/frame' else: environ['frameless_cname'] = True return self.app(environ, start_response) sub_domains = sub_domains[:-len(base_domain)].strip('.') sub_domains = sub_domains.split('.') sr_redirect = None for sd in list(sub_domains): # subdomains to disregard completely if sd in ('www', 'origin', 'beta', 'pay'): continue # subdomains which change the extension elif sd == 'm': environ['reddit-domain-extension'] = 'mobile' elif sd == 'I': environ['reddit-domain-extension'] = 'compact' elif sd == 'i': environ['reddit-domain-extension'] = 'compact' elif sd in ('api', 'rss', 'xml', 'json'): environ['reddit-domain-extension'] = sd elif (len(sd) == 2 or (len(sd) == 5 and sd[2] == '-')) and self.lang_re.match(sd): environ['reddit-prefer-lang'] = sd environ['reddit-domain-prefix'] = sd else: sr_redirect = sd sub_domains.remove(sd) if sr_redirect and environ.get("FULLPATH"): r = Response() sub_domains.append(base_domain) redir = "%s/r/%s/%s" % ('.'.join(sub_domains), sr_redirect, environ['FULLPATH']) redir = "http://" + redir.replace('//', '/') r.status_code = 301 r.headers['location'] = redir r.content = "" return r(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): # get base domain as defined in INI file base_domain = config['global_conf']['domain'] try: sub_domains, request_port = environ['HTTP_HOST'].split(':') environ['request_port'] = int(request_port) except ValueError: sub_domains = environ['HTTP_HOST'].split(':')[0] except KeyError: sub_domains = "localhost" #If the domain doesn't end with base_domain, assume #this is a cname, and redirect to the frame controller. #Ignore localhost so paster shell still works. #If this is an error, don't redirect if (not sub_domains.endswith(base_domain) and (not sub_domains == 'localhost')): environ['sub_domain'] = sub_domains if not environ.get('extension'): if environ['PATH_INFO'].startswith('/frame'): return self.app(environ, start_response) elif self.is_auth_cname(sub_domains): environ['frameless_cname'] = True environ['authorized_cname'] = True elif ("redditSession" in environ.get('HTTP_COOKIE', '') and environ['REQUEST_METHOD'] != 'POST' and not environ['PATH_INFO'].startswith('/error')): environ['original_path'] = environ['PATH_INFO'] environ['PATH_INFO'] = '/frame' else: environ['frameless_cname'] = True return self.app(environ, start_response) sub_domains = sub_domains[:-len(base_domain)].strip('.') sub_domains = sub_domains.split('.') sr_redirect = None for sd in list(sub_domains): # subdomains to disregard completely if sd in ('www', 'origin', 'beta'): continue # subdomains which change the extension elif sd == 'm': environ['reddit-domain-extension'] = 'mobile' elif sd in ('api', 'rss', 'xml', 'json'): environ['reddit-domain-extension'] = sd elif (len(sd) == 2 or (len(sd) == 5 and sd[2] == '-')) and self.lang_re.match(sd): environ['reddit-prefer-lang'] = sd else: sr_redirect = sd sub_domains.remove(sd) if sr_redirect and environ.get("FULLPATH"): r = Response() sub_domains.append(base_domain) redir = "%s/r/%s/%s" % ('.'.join(sub_domains), sr_redirect, environ['FULLPATH']) redir = "http://" + redir.replace('//', '/') r.status_code = 301 r.headers['location'] = redir r.content = "" return r(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): g = config['pylons.g'] http_host = environ.get('HTTP_HOST', 'localhost').lower() domain, s, port = http_host.partition(':') # remember the port try: environ['request_port'] = int(port) except ValueError: pass # localhost is exempt so paster run/shell will work # media_domain doesn't need special processing since it's just ads if domain == "localhost" or is_subdomain(domain, g.media_domain): return self.app(environ, start_response) # tell reddit_base to redirect to the appropriate subreddit for # a legacy CNAME if not is_subdomain(domain, g.domain): environ['legacy-cname'] = domain return self.app(environ, start_response) # figure out what subdomain we're on if any subdomains = domain[:-len(g.domain) - 1].split('.') extension_subdomains = dict(m="mobile", i="compact", api="api", rss="rss", xml="xml", json="json") sr_redirect = None for subdomain in subdomains[:]: if subdomain in g.reserved_subdomains: continue extension = extension_subdomains.get(subdomain) if extension: environ['reddit-domain-extension'] = extension elif self.lang_re.match(subdomain): environ['reddit-prefer-lang'] = subdomain environ['reddit-domain-prefix'] = subdomain else: sr_redirect = subdomain subdomains.remove(subdomain) # if there was a subreddit subdomain, redirect if sr_redirect and environ.get("FULLPATH"): r = Response() if not subdomains and g.domain_prefix: subdomains.append(g.domain_prefix) subdomains.append(g.domain) redir = "%s/r/%s/%s" % ('.'.join(subdomains), sr_redirect, environ['FULLPATH']) redir = "http://" + redir.replace('//', '/') r.status_code = 301 r.headers['location'] = redir r.content = "" return r(environ, start_response) return self.app(environ, start_response)
def __call__(self, environ, start_response): # get base domain as defined in INI file base_domain = config["global_conf"]["domain"] try: sub_domains, request_port = environ["HTTP_HOST"].split(":") environ["request_port"] = int(request_port) except ValueError: sub_domains = environ["HTTP_HOST"].split(":")[0] except KeyError: sub_domains = "localhost" # If the domain doesn't end with base_domain, assume # this is a cname, and redirect to the frame controller. # Ignore localhost so paster shell still works. # If this is an error, don't redirect if not sub_domains.endswith(base_domain) and (not sub_domains == "localhost"): environ["sub_domain"] = sub_domains if not environ.get("extension"): if environ["PATH_INFO"].startswith("/frame"): return self.app(environ, start_response) elif self.is_auth_cname(sub_domains): environ["frameless_cname"] = True environ["authorized_cname"] = True elif ( "redditSession=cname" in environ.get("HTTP_COOKIE", "") and environ["REQUEST_METHOD"] != "POST" and not environ["PATH_INFO"].startswith("/error") ): environ["original_path"] = environ["PATH_INFO"] environ["FULLPATH"] = environ["PATH_INFO"] = "/frame" else: environ["frameless_cname"] = True return self.app(environ, start_response) sub_domains = sub_domains[: -len(base_domain)].strip(".") sub_domains = sub_domains.split(".") sr_redirect = None for sd in list(sub_domains): # subdomains to disregard completely if sd in ("www", "origin", "beta", "lab", "pay", "buttons", "ssl"): continue # subdomains which change the extension elif sd == "m": environ["reddit-domain-extension"] = "mobile" elif sd == "I": environ["reddit-domain-extension"] = "compact" elif sd == "i": environ["reddit-domain-extension"] = "compact" elif sd in ("api", "rss", "xml", "json"): environ["reddit-domain-extension"] = sd elif (len(sd) == 2 or (len(sd) == 5 and sd[2] == "-")) and self.lang_re.match(sd): environ["reddit-prefer-lang"] = sd environ["reddit-domain-prefix"] = sd else: sr_redirect = sd sub_domains.remove(sd) if sr_redirect and environ.get("FULLPATH"): r = Response() sub_domains.append(base_domain) redir = "%s/r/%s/%s" % (".".join(sub_domains), sr_redirect, environ["FULLPATH"]) redir = "http://" + redir.replace("//", "/") r.status_code = 301 r.headers["location"] = redir r.content = "" return r(environ, start_response) return self.app(environ, start_response)