A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-dev/attachments/20170215/9852f2bb/attachment-0001.html below:

<div dir="ltr">I posted this on StackOverflow [1], but I'm posting it here as well, as I believe this is a bug (or at least quirk) in cgi.FieldStorage where you can't access a file upload properly if "filename=" is not present in the MIME part's Content-Disposition header. There are a couple of related bugs open (and closed) on bugs.python.ord, but not quite this issue.<div><br></div><div>Is it legitimate for cgi.FieldStorage to use the presence of "filename=" to determine "this is a binary file" (in which case this is not a bug and my client is just buggy), or is this a bug? I lean towards the latter as the spec indicates that the filename is optional [2].<div><br></div><div>Copying from my StackOverflow question, including a test/repro case:<div><div><br></div><div><div>When I use `cgi.FieldStorage` to parse a `multipart/form-data` request (or any web framework like Pyramid which uses `cgi.FieldStorage`) I have trouble processing file uploads from certain clients which don't provide a `filename=file.ext` in the part's `Content-Disposition` header.</div><div><br></div><div>If the `filename=` option is missing, `FieldStorage()` tries to decode the contents of the file as UTF-8 and return a string. And obviously many files are binary and not UTF-8 and as such give bogus results.</div><div><br></div><div>For example:</div><div><br></div><div>  Â  >>> import cgi</div><div>  Â  >>> import io</div><div>  Â  >>> body = (b'--KQNTvuH-itP09uVKjjZiegh7\r\n' +</div><div>  Â  ... Â  Â  Â  Â  b'Content-Disposition: form-data; name=payload\r\n\r\n' +</div><div>  Â  ... Â  Â  Â  Â  b'\xff\xd8\xff\xe0\x00\x10JFIF')</div><div>  Â  >>> env = {</div><div>  Â  ... Â  Â  'REQUEST_METHOD': 'POST',</div><div>  Â  ... Â  Â  'CONTENT_TYPE': 'multipart/form-data; boundary=KQNTvuH-itP09uVKjjZiegh7',</div><div>  Â  ... Â  Â  'CONTENT_LENGTH': len(body),</div><div>  Â  ... }</div><div>  Â  >>> fs = cgi.FieldStorage(fp=io.BytesIO(body), environ=env)</div><div>  Â  >>> (fs['payload'].filename, fs['payload'].file.read())</div><div>  Â  (None, '����\x00\x10JFIF')</div><div><br></div><div>Browsers, and *most* HTTP libraries do include the `filename=` option for file uploads, but I'm currently dealing with a client that doesn't (and omitting the `filename` does seem to be valid according to the spec).</div><div><br></div><div>Currently I'm using a pretty hacky workaround by subclassing `FieldStorage` and replacing the relevant `Content-Disposition` header with one that does have the filename:</div><div><br></div><div>  Â  import cgi</div><div>  Â  import os</div><div><br></div><div>  Â  class FileFieldStorage(cgi.FieldStorage):</div><div>  Â  Â  Â  """To use, subclass FileFieldStorage and override _file_fields with a tuple</div><div>  Â  Â  Â  of the names of the file field(s). You can also override _file_name with</div><div>  Â  Â  Â  the filename to add.</div><div>  Â  Â  Â  """</div><div><br></div><div>  Â  Â  Â  _file_fields = ()</div><div>  Â  Â  Â  _file_name = 'file_name'</div><div><br></div><div>  Â  Â  Â  def __init__(self, fp=None, headers=None, outerboundary=b'',</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â environ=os.environ, keep_blank_values=0, strict_parsing=0,</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â limit=None, encoding='utf-8', errors='replace'):</div><div><br></div><div>  Â  Â  Â  Â  Â  if self._file_fields and headers and headers.get('content-disposition'):</div><div>  Â  Â  Â  Â  Â  Â  Â  content_disposition = headers['content-disposition']</div><div>  Â  Â  Â  Â  Â  Â  Â  key, pdict = cgi.parse_header(content_disposition)</div><div>  Â  Â  Â  Â  Â  Â  Â  if (key == 'form-data' and pdict.get('name') in self._file_fields and</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  'filename' not in pdict):</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  del headers['content-disposition']</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  quoted_file_name = self._file_name.replace('"', '\\"')</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  headers['content-disposition'] = '{}; filename="{}"'.format(</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  content_disposition, quoted_file_name)</div><div><br></div><div>  Â  Â  Â  Â  Â  super().__init__(fp=fp, headers=headers, outerboundary=outerboundary,</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â environ=environ, keep_blank_values=keep_blank_values,</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â strict_parsing=strict_parsing, limit=limit,</div><div>  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â encoding=encoding, errors=errors)</div><div><br></div><div>Using the `body` and `env` in my first test, this works now:</div><div><br></div><div>  Â  >>> class TestFieldStorage(FileFieldStorage):</div><div>  Â  ... Â  Â  _file_fields = ('payload',)</div><div>  Â  >>> fs = TestFieldStorage(fp=io.BytesIO(body), environ=env)</div><div>  Â  >>> (fs['payload'].filename, fs['payload'].file.read())</div><div>  Â  ('file_name', b'\xff\xd8\xff\xe0\x00\x10JFIF')</div><div><br></div><div>Is there some way to avoid this hack and tell `FieldStorage` not to decode as UTF-8? It would be nice if you could provide `encoding=None` or something, but it doesn't look like it supports that.</div></div><div><br></div><div>Thanks,</div><div>Ben.</div><div><br></div><div><br></div><div>[1] <a href="https://stackoverflow.com/questions/42213318/cgi-fieldstorage-with-multipart-form-data-tries-to-decode-binary-file-as-utf-8-e">https://stackoverflow.com/questions/42213318/cgi-fieldstorage-with-multipart-form-data-tries-to-decode-binary-file-as-utf-8-e</a></div><div>[2] <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1">https://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.5.1</a></div><div><br></div></div></div></div></div>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4