kennethreitz/requests — pull request #1153 by cbare (2013-01-30 23:50:32+00:00)
Title: use only base filename in multipart file upload

I have a tiny patch, which enforces the correct behavior in multipart/form-data file uploads. The function guess_filename in requests/utils.py extracts the filename from the file-like-object from which we stream the file's contents. In the case of a file, this gives the full path on the local filesystem to the given file, which is then propagated to the request headers like this (in urllib3/filepost.py):

'Content-Disposition: form-data; name="%s"; filename="%s"\r\n' % (fieldname, filename)

RFC 2388 says this:

> The original local file name may be supplied as well, either as a
> "filename" parameter either of the "content-disposition: form-data"
> header or, in the case of multiple files, in a "content-disposition:
> file" header of the subpart.

It's my understanding that the correct behavior, implemented in web browsers, is to include the file's *name* and not its full *path* on the local filesystem. I added a call to *os.path.basename* to shorted any paths to the base filename.