Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Request and Response classes lack some useful methods #581

Open
1 of 11 tasks
Changaco opened this issue Sep 17, 2017 · 0 comments · May be fixed by #605
Open
1 of 11 tasks

The Request and Response classes lack some useful methods #581

Changaco opened this issue Sep 17, 2017 · 0 comments · May be fixed by #605
Labels

Comments

@Changaco
Copy link
Member

Changaco commented Sep 17, 2017

I have a growing list of Pando monkey patches in Liberapay that should be moved upstream if possible.

  • Request.source: an IP address that can be used in rate limiting, requires a new website.trusted_proxies attribute as well as the ipaddress package for python < 3.3
  • Response.encode_url(url): static method used by Response.redirect(), doesn't have to be part of a class once moved upstream
  • Response.error(code, msg=''): modifies the response and raises it
  • Response.success(code=200, msg=''): same as above except that code defaults to 200
  • Response.json(obj, code=200): same as above except that obj is serialized to JSON and the Content-Type header is set to application/json
  • Response.sanitize_untrusted_url(url): prevents XSS attacks
  • Response.redirect(url, code=302, trusted_url=True): raises the response after modifying its code, setting the Location header, and sanitizing the URL if trusted_url is set to False
  • Response.render(path, state, **extra): renders the resource path (e.g. templates/confirm.spt) with state plus extra as context, sort of an internal redirect
  • Response.set_cookie(key, value, expires=None, httponly=True, path='/'): self-explanatory
  • Response.erase_cookie(key, **kw): shortcut for set_cookie() with an empty value and an expiration date in the past
  • Response.text: the decoded body of the response, useful in tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant