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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow dashes, periods, and other RFC6838-compliant characters in header vendor #1170

Merged
merged 1 commit into from Oct 5, 2015

Conversation

suan
Copy link
Contributor

@suan suan commented Oct 2, 2015

The current accept header parsing logic treats everything after the first - as the version, whereas it's not uncommon to have dashes in more complex vendor/resource media type definitions (an IANA-registered example is application/vnd.ms-office.activeX+xml)

This change correctly handles dashes in the vendor part, while still assuming what's after the last dash is the version.

The current logic also doesn't allow underscores, ampersands, etc, which are allowed media type characters per RFC 6838. This change makes Grape itself handle these characters correctly, however for them to actually work is contingent on mjackson/rack-accept#17 which unfortunately has been open for more than 6 months (I'll ping maintainer again, but any weight you can throw around there would also be appreciated 馃槃)

P/S: It should be noted that the current header versioning logic is very limited and doesn't support formats such as application/vnd.github.v3+json (as what github uses now) I'll try to add a caveat in the README while I add the CHANGELOG entry for this

@dblock
Copy link
Member

dblock commented Oct 3, 2015

This is good. It needs a CHANGELOG entry and possibly a README update, please.

I would be down with a monkey-patch of rack-accept and a corresponding test. We have some precedent for patches in #1167 where we monkey-patched virtus.

# @param [String] media_type a content type
# @return [Boolean] whether the content type sets an API version
def version?(media_type)
_, subtype = Rack::Accept::Header.parse_media_type(media_type)
subtype[/\Avnd\.[a-z0-9*.]+-[a-z0-9*\-.]+/]
subtype[/\Avnd\.([a-z0-9.\-_!#\$&\^]+?)(?:-([a-z0-9*.]+))+/]
Copy link

Choose a reason for hiding this comment

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

Should we extract this to a constant to avoid recreating the regex every time?

@suan suan force-pushed the better_vendor_resource_handling branch 2 times, most recently from 8a43c4f to 8dac028 Compare October 5, 2015 15:37
@suan suan force-pushed the better_vendor_resource_handling branch from 8dac028 to 460e443 Compare October 5, 2015 15:38
@suan
Copy link
Contributor Author

suan commented Oct 5, 2015

@dblock I've updated the CHANGELOG, added a README section about Grape's limitations with the structure of versioned mediatypes, and converted those regexes to constants like recommended by @aselder

I'm thinking I can make a followup PR where the appropriate rack-accept call is monkey-patched to allow all the RFC-compliant characters, once this PR is merged.

@dblock
Copy link
Member

dblock commented Oct 5, 2015

Merging.

dblock added a commit that referenced this pull request Oct 5, 2015
allow dashes, periods, and other RFC6838-compliant characters in header vendor
@dblock dblock merged commit a7e4b2c into ruby-grape:master Oct 5, 2015
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.

None yet

3 participants