def send404(self): c.response.status_code = 404 if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] return pages.RedditError( _("page not found"), _("the page you requested does not exist")).render()
def search_fail(self, exception): from r2.lib.contrib.pysolr import SolrError from r2.lib.indextank import IndextankException if isinstance(exception, SolrError): errmsg = "SolrError: %r" % exception if (str(exception) == 'None'): # Production error logs only get non-None errors g.log.debug(errmsg) else: g.log.error(errmsg) elif isinstance(exception, (IndextankException, socket.error)): g.log.error("IndexTank Error: %s" % repr(exception)) sf = pages.SearchFail() us = filters.unsafe(sf.render()) errpage = pages.RedditError(_('search failed'), us) c.response = Response() c.response.status_code = 503 request.environ['usable_error_content'] = errpage.render() request.environ['retry_after'] = 60 abort(503)
def send400(self): if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] else: res = pages.RedditError( title=_("bad request (%(domain)s)") % dict(domain=g.domain), message=_("you sent an invalid request"), explanation=request.GET.get('explanation')) return res.render()
def send403(self): c.site = DefaultSR() if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] else: res = pages.RedditError( title=_("forbidden (%(domain)s)") % dict(domain=g.domain), message=_("you are not allowed to do that"), explanation=request.GET.get('explanation')) return res.render()
def search_fail(self, exception): from r2.lib.search import SearchException if isinstance(exception, SearchException + (socket.error, )): g.log.error("Search Error: %s" % repr(exception)) errpage = pages.RedditError(_("search failed"), strings.search_failed) request.environ['usable_error_content'] = errpage.render() request.environ['retry_after'] = 60 abort(503)
def send403(self): c.response.status_code = 403 c.site = DefaultSR() if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] else: res = pages.RedditError( title=_("forbidden (%(domain)s)") % dict(domain=g.domain), message=_("you are not allowed to do that")) return res.render()
def send404(self): c.response.status_code = 404 if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] if c.site._spam and not c.user_is_admin: subject = ("the subreddit /r/%s has been incorrectly banned" % c.site.name) message = ( strings.banned_subreddit % dict(link='/message/compose?to=%s&subject=%s' % (url_escape(g.admin_message_acct), url_escape(subject)))) res = pages.RedditError(_('this reddit has been banned'), unsafe(safemarkdown(message))) return res.render() else: return pages.Reddit404().render()
def send404(self): c.response.status_code = 404 if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] if c.site.spammy() and not c.user_is_admin: subject = ("the subreddit /r/%s has been incorrectly banned" % c.site.name) lnk = ("/r/redditrequest/submit?url=%s&title=%s" % (url_escape("http://%s/r/%s" % (g.domain, c.site.name)), ("the subreddit /r/%s has been incorrectly banned" % c.site.name))) message = strings.banned_subreddit % dict(link=lnk) res = pages.RedditError(_('this reddit has been banned'), unsafe(safemarkdown(message))) return res.render() else: return pages.Reddit404().render()
def search_fail(self, exception): from r2.lib.contrib.pysolr import SolrError from r2.lib.search import SearchException if isinstance(exception, SolrError): errmsg = "SolrError: %r" % exception if (str(exception) == 'None'): # Production error logs only get non-None errors g.log.debug(errmsg) else: g.log.error(errmsg) elif isinstance(exception, SearchException + (socket.error, )): g.log.error("Search Error: %s" % repr(exception)) errpage = pages.RedditError(_("search failed"), strings.search_failed) request.environ['usable_error_content'] = errpage.render() request.environ['retry_after'] = 60 abort(503)
def send404(self): if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] return pages.RedditError(_("page not found"), ( "The page you requested does not exist. This might have occurred because BlockPath does not use all of the features the Reddit codebase has... We are working on closing these open-ends!" )).render()
def pre(self): c.response_wrappers = [] MinimalController.pre(self) set_cnameframe() # populate c.cookies unless we're on the unsafe media_domain if request.host != g.media_domain or g.media_domain == g.domain: try: for k, v in request.cookies.iteritems(): # minimalcontroller can still set cookies if k not in c.cookies: # we can unquote even if it's not quoted c.cookies[k] = Cookie(value=unquote(v), dirty=False) except CookieError: #pylons or one of the associated retarded libraries #can't handle broken cookies request.environ['HTTP_COOKIE'] = '' c.firsttime = firsttime() # the user could have been logged in via one of the feeds maybe_admin = False # no logins for RSS feed unless valid_feed has already been called if not c.user: if c.extension != "rss": session_cookie = c.cookies.get(g.login_cookie) if session_cookie: c.user = valid_cookie(session_cookie.value) if c.user: c.user_is_loggedin = True admin_cookie = c.cookies.get(g.admin_cookie) if c.user_is_loggedin and admin_cookie: maybe_admin, first_login = valid_admin_cookie( admin_cookie.value) if maybe_admin: self.enable_admin_mode(c.user, first_login=first_login) else: self.disable_admin_mode(c.user) if not c.user: c.user = UnloggedUser(get_browser_langs()) # patch for fixing mangled language preferences if (not isinstance(c.user.pref_lang, basestring) or not all( isinstance(x, basestring) for x in c.user.pref_content_langs)): c.user.pref_lang = g.lang c.user.pref_content_langs = [g.lang] c.user._commit() if c.user_is_loggedin: if not c.user._loaded: c.user._load() c.modhash = c.user.modhash() if request.method.upper() == 'GET': read_mod_cookie() if hasattr(c.user, 'msgtime') and c.user.msgtime: c.have_messages = c.user.msgtime c.show_mod_mail = Subreddit.reverse_moderator_ids(c.user) c.have_mod_messages = getattr(c.user, "modmsgtime", False) c.user_is_admin = maybe_admin and c.user.name in g.admins c.user_special_distinguish = c.user.special_distinguish() c.user_is_sponsor = c.user_is_admin or c.user.name in g.sponsors if request.path != '/validuser' and not g.disallow_db_writes: c.user.update_last_visit(c.start_time) c.over18 = over18() #set_browser_langs() set_host_lang() set_iface_lang() set_content_lang() set_recent_clicks() # used for HTML-lite templates set_colors() # set some environmental variables in case we hit an abort if not isinstance(c.site, FakeSubreddit): request.environ['REDDIT_NAME'] = c.site.name # random reddit trickery -- have to do this after the content lang is set if c.site == Random: c.site = Subreddit.random_reddit() redirect_to("/" + c.site.path.strip('/') + request.path) elif c.site == RandomNSFW: c.site = Subreddit.random_reddit(over18=True) redirect_to("/" + c.site.path.strip('/') + request.path) if not request.path.startswith("/api/login/"): # is the subreddit banned? if c.site.spammy() and not c.user_is_admin and not c.error_page: ban_info = getattr(c.site, "ban_info", {}) if "message" in ban_info: message = ban_info['message'] else: sitelink = url_escape(add_sr("/")) subject = ("/r/%s has been incorrectly banned" % c.site.name) link = ("/r/redditrequest/submit?url=%s&title=%s" % (sitelink, subject)) message = strings.banned_subreddit_message % dict( link=link) errpage = pages.RedditError(strings.banned_subreddit_title, message, image="subreddit-banned.png") request.environ['usable_error_content'] = errpage.render() self.abort404() # check if the user has access to this subreddit if not c.site.can_view(c.user) and not c.error_page: errpage = pages.RedditError(strings.private_subreddit_title, strings.private_subreddit_message, image="subreddit-private.png") request.environ['usable_error_content'] = errpage.render() self.abort403() #check over 18 if (c.site.over_18 and not c.over18 and request.path not in ("/frame", "/over18") and c.render_style == 'html'): return self.intermediate_redirect("/over18") #check whether to allow custom styles c.allow_styles = True c.can_apply_styles = self.allow_stylesheets if g.css_killswitch: c.can_apply_styles = False #if the preference is set and we're not at a cname elif not c.user.pref_show_stylesheets and not c.cname: c.can_apply_styles = False #if the site has a cname, but we're not using it elif c.site.domain and c.site.css_on_cname and not c.cname: c.can_apply_styles = False
def pre(self): record_timings = g.admin_cookie in request.cookies or g.debug admin_bar_eligible = response.content_type == 'text/html' if admin_bar_eligible and record_timings: g.stats.start_logging_timings() # set up stuff needed in base templates at error time here. c.js_preload = JSPreload() MinimalController.pre(self) set_cnameframe() # populate c.cookies unless we're on the unsafe media_domain if request.host != g.media_domain or g.media_domain == g.domain: cookie_counts = collections.Counter() try: for k, v in request.cookies.iteritems(): # minimalcontroller can still set cookies if k not in c.cookies: # we can unquote even if it's not quoted c.cookies[k] = Cookie(value=unquote(v), dirty=False) cookie_counts[Cookie.classify(k)] += 1 except CookieError: #pylons or one of the associated retarded libraries #can't handle broken cookies request.environ['HTTP_COOKIE'] = '' for cookietype, count in cookie_counts.iteritems(): g.stats.simple_event("cookie.%s" % cookietype, count) delete_obsolete_cookies() # the user could have been logged in via one of the feeds maybe_admin = False is_otpcookie_valid = False # no logins for RSS feed unless valid_feed has already been called if not c.user: if c.extension != "rss": authenticate_user() admin_cookie = c.cookies.get(g.admin_cookie) if c.user_is_loggedin and admin_cookie: maybe_admin, first_login = valid_admin_cookie( admin_cookie.value) if maybe_admin: self.enable_admin_mode(c.user, first_login=first_login) else: self.disable_admin_mode(c.user) otp_cookie = read_user_cookie(g.otp_cookie) if c.user_is_loggedin and otp_cookie: is_otpcookie_valid = valid_otp_cookie(otp_cookie) if not c.user: c.user = UnloggedUser(get_browser_langs()) # patch for fixing mangled language preferences if (not isinstance(c.user.pref_lang, basestring) or not all( isinstance(x, basestring) for x in c.user.pref_content_langs)): c.user.pref_lang = g.lang c.user.pref_content_langs = [g.lang] c.user._commit() if c.user_is_loggedin: if not c.user._loaded: c.user._load() c.modhash = c.user.modhash() if hasattr(c.user, 'msgtime') and c.user.msgtime: c.have_messages = c.user.msgtime c.show_mod_mail = Subreddit.reverse_moderator_ids(c.user) c.have_mod_messages = getattr(c.user, "modmsgtime", False) c.user_is_admin = maybe_admin and c.user.name in g.admins c.user_special_distinguish = c.user.special_distinguish() c.user_is_sponsor = c.user_is_admin or c.user.name in g.sponsors c.otp_cached = is_otpcookie_valid if not isinstance(c.site, FakeSubreddit) and not g.disallow_db_writes: c.user.update_sr_activity(c.site) c.over18 = over18() set_obey_over18() # looking up the multireddit requires c.user. set_multireddit() #set_browser_langs() set_host_lang() set_iface_lang() set_content_lang() set_recent_clicks() # used for HTML-lite templates set_colors() # set some environmental variables in case we hit an abort if not isinstance(c.site, FakeSubreddit): request.environ['REDDIT_NAME'] = c.site.name # random reddit trickery -- have to do this after the content lang is set if c.site == Random: c.site = Subreddit.random_reddit(user=c.user) redirect_to("/" + c.site.path.strip('/') + request.path_qs) elif c.site == RandomSubscription: if c.user.gold: c.site = Subreddit.random_subscription(c.user) redirect_to('/' + c.site.path.strip('/') + request.path_qs) else: redirect_to('/gold/about') elif c.site == RandomNSFW: c.site = Subreddit.random_reddit(over18=True, user=c.user) redirect_to("/" + c.site.path.strip('/') + request.path_qs) if not request.path.startswith("/api/login/"): # is the subreddit banned? if c.site.spammy() and not c.user_is_admin and not c.error_page: ban_info = getattr(c.site, "ban_info", {}) if "message" in ban_info: message = ban_info['message'] else: sitelink = url_escape(add_sr("/")) subject = ("/r/%s has been incorrectly banned" % c.site.name) link = ("/r/redditrequest/submit?url=%s&title=%s" % (sitelink, subject)) message = strings.banned_subreddit_message % dict( link=link) errpage = pages.RedditError(strings.banned_subreddit_title, message, image="subreddit-banned.png") request.environ['usable_error_content'] = errpage.render() self.abort404() # check if the user has access to this subreddit if not c.site.can_view(c.user) and not c.error_page: if isinstance(c.site, LabeledMulti): # do not leak the existence of multis via 403. self.abort404() else: public_description = c.site.public_description errpage = pages.RedditError( strings.private_subreddit_title, strings.private_subreddit_message, image="subreddit-private.png", sr_description=public_description, ) request.environ['usable_error_content'] = errpage.render() self.abort403() #check over 18 if (c.site.over_18 and not c.over18 and request.path not in ("/frame", "/over18") and c.render_style == 'html'): return self.intermediate_redirect("/over18") #check whether to allow custom styles c.allow_styles = True c.can_apply_styles = self.allow_stylesheets if g.css_killswitch: c.can_apply_styles = False #if the preference is set and we're not at a cname elif not c.user.pref_show_stylesheets and not c.cname: c.can_apply_styles = False #if the site has a cname, but we're not using it elif c.site.domain and c.site.css_on_cname and not c.cname: c.can_apply_styles = False c.bare_content = request.GET.pop('bare', False) c.show_admin_bar = admin_bar_eligible and (c.user_is_admin or g.debug) if not c.show_admin_bar: g.stats.end_logging_timings() hooks.get_hook("reddit.request.begin").call() c.request_timer.intermediate("base-pre")
def send403(self): c.response.status_code = 403 c.site = DefaultSR() res = pages.RedditError( _("forbidden (%(domain)s)") % dict(domain=g.domain)) return res.render()
def send404(self): if 'usable_error_content' in request.environ: return request.environ['usable_error_content'] return pages.RedditError(_("page not found"), ( "The page you requested does not exist. We are working on closing these open-ends!" )).render()