Opened 14 years ago
Closed 14 years ago
#15637 closed New feature (fixed)
Add a require_safe decorator for views to accept GET or HEAD
Reported by: | Aymeric Augustin | Owned by: | nobody |
---|---|---|---|
Component: | HTTP handling | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Tom Christie, leidel@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
It is a good practice to reject POST requests on views that are not intended to process them. Django provides the require_GET decorator for this purpose.
Although browsers only implement GET and POST methods, other software such as link checkers uses HEAD requests. Views protected by require_GET will (obviously) reject such requests with "405 Method Not Allowed". I've encountered the problem with the "Check My Links" extension for Google Chrome.
However, RFC2616 says that "The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response." Django already enforces this behavior by stripping the body of the responses to HEAD requests. So I think that in most cases, it makes more sense to accept GET and HEAD than only GET.
That is why I'm proposing a "require_safe" decorator for this purpose.
Attachments (3)
Change History (13)
by , 14 years ago
Attachment: | add-require-safe-http-decorator.patch added |
---|
comment:1 by , 14 years ago
Has patch: | set |
---|
comment:2 by , 14 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 14 years ago
Patch needs improvement: | set |
---|
by , 14 years ago
Attachment: | add-require-safe-http-decorator.2.patch added |
---|
comment:4 by , 14 years ago
Patch needs improvement: | unset |
---|
Thanks for accepting the feature, julien. Here is a new patch with more details in the documentation and more tests.
comment:5 by , 14 years ago
Cc: | added |
---|
comment:6 by , 14 years ago
Type: | → New feature |
---|
comment:7 by , 14 years ago
Severity: | → Normal |
---|
comment:8 by , 14 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Great work, thank you. I've just made a few minor improvements to the doc.
comment:9 by , 14 years ago
Cc: | added |
---|---|
Easy pickings: | unset |
comment:10 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
In [16115]:
(The changeset message doesn't reference this ticket)
The patch looks good but needs a few improvements. The doc should explain why the decorator is called
require_safe
(i.e. GET and HEAD methods are commonly referred to as "safe" methods), and the tests should actually check that the decorator actually only allows GET and HEAD requests.