执行 POST 时是否需要对表单参数名称进行编码?

发布于 2024-10-05 22:25:09 字数 1311 浏览 0 评论 0原文

快速版:使用标准multipart/form-data编码发送的“表单”的参数名称是否需要编码?

较长版本: 1fichier.com 上的上传表单(一项上传大型图片的服务) files) 使用以下内容指定要上传的文件参数:

<input type="file" name="file[]" size="50" title="Select the files to upload" />

参数名称为 file[] (注意括号)。

使用 LiveHTTPHeaders 我看到在 Firefox 中提交表单时参数是这样发送的(即带有括号)。但是,对于我用 Python 编写的 程序,我使用 poster 模块能够使用标准 multipart/form-data 编码上传文件。如果我用括号输入参数名称,它会像这样发送:

file%5B%5D

在内部,海报使用此函数对参数名称进行编码:

def encode_and_quote(data):
    """If ``data`` is unicode, return urllib.quote_plus(data.encode("utf-8"))
    otherwise return urllib.quote_plus(data)"""
    if data is None:
        return None

    if isinstance(data, unicode):
        data = data.encode("utf-8")
    return urllib.quote_plus(data)

urllib.quote_plus 文档说这只是“在构建进入 URL 的查询字符串时引用 HTML 表单值所必需的”。但这里我们正在执行 POST,因此表单值不会出现在 url 中。

那么,它们是否仍然需要编码,或者这样做是海报的错误吗?

Quick version: Do the names of parameters of "forms" being sent using the standard multipart/form-data encoding need to be encoded?

Longer version: The upload form on 1fichier.com (a service to upload large files) uses the following to specify the file parameter to upload:

<input type="file" name="file[]" size="50" title="Select the files to upload" />

The name of the parameter is file[] (notice the brackets).

Using LiveHTTPHeaders I see that the parameter is sent like this (i.e. with brackets) when submitting the form in Firefox. However, for a program I'm writing in Python, I am using the poster module to be able to upload files using the standard multipart/form-data encoding. If I enter the parameter name with the brackets, it gets sent like this:

file%5B%5D

Internally, poster encodes the names of the parameters using this function:

def encode_and_quote(data):
    """If ``data`` is unicode, return urllib.quote_plus(data.encode("utf-8"))
    otherwise return urllib.quote_plus(data)"""
    if data is None:
        return None

    if isinstance(data, unicode):
        data = data.encode("utf-8")
    return urllib.quote_plus(data)

The urllib.quote_plus documentation says that this is only "required for quoting HTML form values when building up a query string to go into a URL". But here we're doing a POST, so the form values don't go in the url.

So, do they still need to be encoded, or is it an error of poster to be doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷迭香的记忆 2024-10-12 22:25:09

RFC 2388 涵盖多部分/表单数据提交。第 3 节指定参数名称应为 ASCII 或按照 RFC 2047 进行编码。

因此,如果您的 POST 请求被编码为 multipart/form-data (海报正在做的事情),那么不,参数名称不需要以这种方式编码。我建议向作者提交一个错误(咳咳...),他可能愿意在未来的版本中修复它;)

解决方法是直接设置 MultipartParam 的 name 属性,例如

   p.name = 'file[]'

RFC 2388 covers multipart/form-data submissions. Section 3 specifies that parameter names should be either ASCII or encoded as per RFC 2047.

So if your POST request is encoded as multipart/form-data (which poster is doing), then no, parameter names don't need to be encoded this way. I suggest filing a bug with the author (ahem...), he might be willing to fix it in a future release ;)

A workaround is to set your MultipartParam's name attribute directly, e.g.

   p.name = 'file[]'
浊酒尽余欢 2024-10-12 22:25:09

尽管本质上这个问题已经得到解答,但我还提供了一些有关如何挖掘这些 RFC 的更多详细信息。

RFC 2388 第 3 节 规定需要 Content-Disposition 标头。非 ASCII 数据应使用 RFC 2047 进行编码,即使 看起来像是冲突<​​/a>。 RFC 2183 第 2 部分 描述了此 Content-disposition 标头的格式。 name 符合该语法的一般 parameter 规则,但引用了 RFC 2045 为此。 在第 5.1 节中,您会发现参数的右侧< /code> 是一个 token 或一个 quoted-string。两种产品均未提及表单名称的任何 URL 编码格式。但[]位于tspecials中,因此它们不能成为令牌的一部分。因此,我们

Content-Disposition: form-data; name="file[]"        (correct)
Content-Disposition: form-data; name=file[]          (invalid)
Content-Disposition: form-data; name="file%5B%5D" (wrong name)
Content-Disposition: form-data; name=file%5B%5D   (wrong name)

对非 ASCII 文件名还有一个注释: 当前的 HTML 5 规范草案 要求不以 7 位安全方式对它们进行编码,而是以整个请求中使用的编码方式传输它们。 关于非 ASCII 字段名称的问题促使我今天看到您的这个问题。

Although in essence this question has been answered, I'm including some more details on how to dig through those RFCs.

RFC 2388 section 3 states that a Content-Disposition header is reqired. Non-ASCII data should be encoded using RFC 2047 even though that looks like a conflict. RFC 2183 section 2 describes the format of this Content-disposition header. The name fits in the general parameter rule of that grammar, but references RFC 2045 for that. There in section 5.1 you find that the right hand side of a parameter is either a token or a quoted-string. Neither production mentions any URL-encoded format for form names. But [ and ] are in tspecials, so they cannot be part of a token. So we get

Content-Disposition: form-data; name="file[]"        (correct)
Content-Disposition: form-data; name=file[]          (invalid)
Content-Disposition: form-data; name="file%5B%5D" (wrong name)
Content-Disposition: form-data; name=file%5B%5D   (wrong name)

One more note for non-ASCII file names: the current HTML 5 specification draft requires not encoding them in a 7-bit safe manner, but instead transferring them in the encoding used throughout the request. A question about non-ascii field names is what brought me to look at this question of yours today.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文