Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Check for wrong media types (notice 1282)
Browse files Browse the repository at this point in the history
plain/text actually happened to us at work,
and I know of text/json from
http://stackoverflow.com/posts/30741120/revisions
  • Loading branch information
vfaronov committed Jan 14, 2017
1 parent 367834c commit fa3fcd3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ Added
- Python 3.6 compatibility.
- Decompression of `brotli`_ compressed payloads (``Content-Encoding: br``).
- Checks for JSON charsets (notices `1280`_ and `1281`_).
- Checks for some wrong media types,
currently ``plain/text`` and ``text/json`` (notice `1282`_).

.. _brotli: https://tools.ietf.org/html/rfc7932
.. _1280: http://pythonhosted.org/HTTPolice/notices.html#1280
.. _1281: http://pythonhosted.org/HTTPolice/notices.html#1281
.. _1282: http://pythonhosted.org/HTTPolice/notices.html#1282

Removed
-------
Expand Down
5 changes: 5 additions & 0 deletions httpolice/notices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2059,4 +2059,9 @@ One non-obvious thing is how references work
<ref to="msg.displayable_body"/>
</error>

<error id="1282">
<title>Wrong media type ‘<var ref="bad"/>’ in <var ref="place"/></title>
<explain>There is no such media type ‘<var ref="bad"/>’. Probably ‘<var ref="good"/>’ was intended.</explain>
</error>

</notices>
16 changes: 14 additions & 2 deletions httpolice/syntax/rfc7231.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
u'Saturady', u'Sunday']


_BAD_MEDIA_TYPES = {
MediaType(u'plain/text'): MediaType(u'text/plain'),
MediaType(u'text/json'): MediaType(u'application/json'),
}

@can_complain
def _check_media_type(complain, mtype):
if mtype in _BAD_MEDIA_TYPES:
complain(1282, bad=mtype, good=_BAD_MEDIA_TYPES[mtype])
return mtype


def parameter(exclude=None):
return (
(CaseInsensitive << token__excluding(exclude or [])) *
Expand All @@ -33,7 +45,7 @@ def parameter(exclude=None):
type_ = token > pivot
subtype = token > pivot
media_type = Parametrized << (
(MediaType << type_ + '/' + subtype) *
(_check_media_type << (MediaType << type_ + '/' + subtype)) *
(MultiDict << many(skip(OWS * ';' * OWS) * parameter()))) > pivot

content_coding = ContentCoding << token > pivot
Expand Down Expand Up @@ -175,7 +187,7 @@ def media_range(no_q=False):
(
literal('*/*') |
type_ + '/' + '*' |
MediaType << type_ + '/' + subtype
_check_media_type << (MediaType << type_ + '/' + subtype)
) *
(
MultiDict << many(
Expand Down
14 changes: 14 additions & 0 deletions test/combined_data/1282_1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1282

======== BEGIN INBOUND STREAM ========
GET / HTTP/1.1
Host: example.com
User-Agent: demo

======== BEGIN OUTBOUND STREAM ========
HTTP/1.1 200 OK
Date: Thu, 31 Dec 2015 18:26:56 GMT
Content-Type: plain/text
Content-Length: 14

Hello world!
15 changes: 15 additions & 0 deletions test/combined_data/1282_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1282

======== BEGIN INBOUND STREAM ========
GET / HTTP/1.1
Host: example.com
User-Agent: demo
Accept: application/xml, plain/text;q=0.5

======== BEGIN OUTBOUND STREAM ========
HTTP/1.1 200 OK
Date: Thu, 31 Dec 2015 18:26:56 GMT
Content-Type: text/plain
Content-Length: 14

Hello world!
14 changes: 14 additions & 0 deletions test/combined_data/1282_3
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1282

======== BEGIN INBOUND STREAM ========
OPTIONS /articles/123/ HTTP/1.1
Host: example.com
User-Agent: demo

======== BEGIN OUTBOUND STREAM ========
HTTP/1.1 200 OK
Date: Thu, 31 Dec 2015 18:26:56 GMT
Content-Length: 0
Allow: GET, HEAD, PUT, PATCH, DELETE, OPTIONS
Accept-Patch: text/json

0 comments on commit fa3fcd3

Please sign in to comment.