Content-type should not be set to ""

Bug #1433767 reported by Timur Alperovich
18
This bug affects 3 people
Affects Status Importance Assigned to Milestone
python-swiftclient
Fix Released
Undecided
Timur Alperovich

Bug Description

When performing an upload, the content-type may be set to an empty string (in client.py):

 978 if content_type is not None:
 979 headers['Content-Type'] = content_type
 980 else: # python-requests sets application/x-www-form-urlencoded otherwise
 981 headers['Content-Type'] = ''

This breaks against certain servers (I've found this to be a problem in Jersey), which return 400 error for an invalid content type (""). Can swiftclient default to a valid type, e.g. binary/octet-stream? Happy to submit a small patch to change this.

Revision history for this message
Jason Cwik (13-jcsoz-k4) wrote :

Per RFC7231, the format of this field is

     media-type = type "/" subtype *( OWS ";" OWS parameter )
     type = token
     subtype = token

https://tools.ietf.org/html/rfc7231#section-3.1.1.1

Therefore, an empty string is *not* valid. As Timur says, this can cause issue with clients and servers that are proxying the requests.

An appropriate fix here is to default to "application/octet-stream".

Revision history for this message
Timur Alperovich (timur-alperovich) wrote :
Changed in python-swiftclient:
assignee: nobody → Timur Alperovich (timur-alperovich)
status: New → In Progress
Revision history for this message
Alistair Coles (alistair-coles) wrote :

the work around to set content-type='' in the client is not required with requests>=2.4.0:

```

swift@vm-15:~/swift$ python3 -c "import requests; print(requests.post('http://httpbin.org/post', data=b'hi', headers={}).text)"
{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "hi": ""
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "2",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.3.0 CPython/3.5.2 Linux/4.8.0-52-generic"
  },
  "json": null,
  "origin": "79.68.30.192",
  "url": "http://httpbin.org/post"
}

swift@vm-15:~/swift$ pip3 install requests==2.4.0Collecting requests==2.4.0

...

swift@vm-15:~/swift$ python3 -c "import requests; print(requests.post('http://httpbin.org/post', data=b'hi', headers={}).text)"
{
  "args": {},
  "data": "hi",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "2",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.4.0 CPython/3.5.2 Linux/4.8.0-52-generic"
  },
  "json": null,
  "origin": "79.68.30.192",
  "url": "http://httpbin.org/post"
}

swift@vm-15:~/swift$ pip3 install requests -U
Collecting requests

...

swift@vm-15:~/swift$ python3 -c "import requests; print(requests.post('http://httpbin.org/post', data=b'hi', headers={}).text)"
{
  "args": {},
  "data": "hi",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Content-Length": "2",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.17.3"
  },
  "json": null,
  "origin": "79.68.30.192",
  "url": "http://httpbin.org/post"
}

```

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to python-swiftclient (master)

Reviewed: https://review.openstack.org/472541
Committed: https://git.openstack.org/cgit/openstack/python-swiftclient/commit/?id=32f6b3c642fd5830a63e06ea4fac86956160312f
Submitter: Jenkins
Branch: master

commit 32f6b3c642fd5830a63e06ea4fac86956160312f
Author: Timur Alperovich <email address hidden>
Date: Thu Jun 8 23:47:14 2017 -0700

    Do not set Content-Type to '' with new requests.

    Previously, python-swiftclient worked around a requests issue where
    Content-Type could be set to application/x-www-form-urlencoded when
    using python3. This issue has been resolved and a fix released in
    requests 2.4 (fixed in subsequent releases as well). The patch makes
    the workaround conditional on the requests version, so that with
    sufficiently new requests libraries, the Content-Type is not set.

    For reference, requests 2.4 was released August 29th, 2014. The
    specific issue filed in the requests tracker is:
    https://github.com/requests/requests/issues/2071.

    Related-Change: I035f8b4b9c9ccdc79820b907770a48f86d0343b4
    Closes-Bug: #1433767

    Change-Id: Ieb2243d2ff5326920a27ce8c3c6f0f5c396701ed

Changed in python-swiftclient:
status: In Progress → Fix Released
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix included in openstack/python-swiftclient 3.4.0

This issue was fixed in the openstack/python-swiftclient 3.4.0 release.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.