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

implement "415 Unsupported Media Type" #91

Merged
merged 1 commit into from
May 17, 2013
Merged

Conversation

amotl
Copy link
Contributor

@amotl amotl commented Nov 30, 2012

This implements validation of the Content-Type header sent in requests against a list of allowed content types and responds with "415 Unsupported Media Type", if there's a mismatch. Could resolve #57. ;]

# check for proper value of 'Content-Type' header
content_type_received = request.headers['Content-Type']
matches = []
for content_type_valid in content_types:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name this valid_content_type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, with pleasure. ;] Also made it return a boolean value, so somebody else could ask if valid_content_type(...).

@almet
Copy link
Contributor

almet commented Dec 3, 2012

Thanks for working on that, I made some comments about it.

@ghost ghost assigned almet Dec 8, 2012
@almet
Copy link
Contributor

almet commented Feb 12, 2013

Any update on this issue?

@almet
Copy link
Contributor

almet commented Mar 3, 2013

Hi Andreas,

Do you think you can find some time to review my comments and make the necessary changes to this pull request in order to make it upstream?

@amotl
Copy link
Contributor Author

amotl commented Mar 18, 2013

Hi Alex,

sorry for not following up in time, right now going back to this. I think the implementation is feasible, but clutters the details around more places than the current one (see implementation for "Accept"). Especially the unrolling logic in "_fallback_view" and the duplicate/redundant match in there gives headaches from a design perspective, at least from my point of view.

I'm halfway through, so you'll receive an updated changeset soon.

Nonetheless i really question if this is too much overkill. Maybe we should just skip all that jazz wiring it into the innards of Cornice, but provide an additional/standard validator which can be optionally used if desired. At least from our point of view this is what it should be in Cornice: A validator which checks for valid Content-Type and otherwise returns a nice formatted response in line with the style of request.errors.

On the other hand we see the value if this would be implemented using Pyramid predicates regarding interoperability with Pyramid applications which would rely on this. We just can't estimate the cost-benefit ratio.

FYI - our current "validator"-style code in production is:

def valid_content_type(request):

    # check for existence of 'Content-Type' header
    try:
        request.headers['Content-Type']
    except KeyError:
        request.errors.add('headers', 'Content-Type', 'Content-Type header must be given')
        return

    # check for proper value of 'Content-Type' header
    try:
        content_type = request.headers['Content-Type']
        assert content_type.startswith('application/json')
    except AssertionError:
        request.errors.add('headers', 'Content-Type', "Content-Type header should start with 'application/json', not '{0}'".format(content_type))
        return

We could put this into cornice.validators and would be fine. ;-)

Cheers,
Andreas.

P.S.: Stay tuned...

I'm halfway through, so you'll receive an updated changeset soon.

I'll finish the predicate-style implementation if nothing gets in the way, then maybe we can decide whether to provide both implementation flavors.

@amotl
Copy link
Contributor Author

amotl commented Mar 18, 2013

Hi Alexis,

just updated the code to follow the predicate path, you can have a look. Didn't pull from master yet, let's get things conceptually sorted first, ok?

Best wishes,
Andreas.

@@ -0,0 +1,39 @@
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you probably don't want to put that in ext. The "ext" repository is meant for "extensions".

Unless I'm missing something, this thing will be an addition to the "normal" cornice behaviors. I would rather put that in pyramid_hooks for instance, or in utils.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright!

@amotl
Copy link
Contributor Author

amotl commented Mar 26, 2013

Hi Alexis,

i've put all your suggestions into code. You can have a look as soon as you can catch an opportunity.

Cheers,
Andreas.

Content Types
=============
Content Types (ingress)
=======================
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this explanation. That's making things far better to understand (even if the ingress and egress terms are not that much used)

@almet
Copy link
Contributor

almet commented May 6, 2013

I was off for some time, so I'm just catching things up now. That sounds great. Do you think you can rebase your work on top of the latest master so I can merge it?

@amotl
Copy link
Contributor Author

amotl commented May 6, 2013

Rebased on top of master. Travis fails on Python 3.3, but it wasn't me. ;-) Could you have a look?

@almet
Copy link
Contributor

almet commented May 7, 2013

yeah, we have intermitent failures with travis and I don't know why. I'm having a look right now.

@amotl
Copy link
Contributor Author

amotl commented May 7, 2013

Thanks, had the opportunity to install a Py3k environment yesterday: repoze/repoze.debug#3 (comment)

Cornice tests on branch "master" succeed like a charm locally, but unfortunately fail on branch "content_types" with Python 3.3. I'm totally AFK today, but may be able to have a look tomorrow.

Cheers,
Andreas.

@almet
Copy link
Contributor

almet commented May 8, 2013

ok, awesome.

@amotl
Copy link
Contributor Author

amotl commented May 10, 2013

Should now be working on recent Python 2.x and Python 3.x, at least it looks good to Travis.

@almet
Copy link
Contributor

almet commented May 13, 2013

Awesome. We're close to there.

Can you squash the commits into something that makes more sense while reading it? That would be much appreciated and could let the history cleaner. This feature is interesting, and the history of it as well, but I don't think that (the history, not the code) is valuable enough to appear in the changelog.

You can do so by using the git rebase -i command. I can give you some help if that's needed.

…st header) on top of Pyramid's predicate system
@amotl
Copy link
Contributor Author

amotl commented May 14, 2013

Rebased and squashed on top of current master. Unfortunately Travis fails on downloading docutils-0.8.1.tar.gz ;[

@almet
Copy link
Contributor

almet commented May 17, 2013

Yeah, Travis does what he wants when he wants. I'm thinking more and more about removing it from this project since it doesn't serve any purpose at the moment.

almet added a commit that referenced this pull request May 17, 2013
implement "415 Unsupported Media Type"
@almet almet merged commit 2894158 into Cornices:master May 17, 2013
@almet
Copy link
Contributor

almet commented May 17, 2013

Thanks for your work on this, and sorry for the delay it took to integrate!

@amotl
Copy link
Contributor Author

amotl commented May 17, 2013

Hey Alex, thanks for merging, we finally made it. :-) And thanks to @tseaver for the chance to lose my Py3k virginity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implement 415-Unsupported-Media-Type
2 participants