RFC 标准是否要求将 MIME 附件的文件名值封装在引号中?

Do RFC standards require the filename value for MIME attachment to be encapsulated in quotes?

我有两段相互冲突的代码。一个产生:

Content-Type: text/html; name=foo_foo2.blah
Content-Disposition: attachment; filename=foo_foo2.blah

另一个产生:

Content-Type: text/html; name="foo_foo2.blah"
Content-Disposition: attachment; filename="foo_foo2.blah"

不带引号的会导致接收应用程序出现意外行为。需要报价吗?

RFC 2183 中我没有看到明确的要求:

In the extended BNF notation of [RFC 822], the Content-Disposition
header field is defined as follows:

 disposition := "Content-Disposition" ":"
                disposition-type
                *(";" disposition-parm)

 disposition-type := "inline"
                   / "attachment"
                   / extension-token
                   ; values are not case-sensitive

 disposition-parm := filename-parm
                   / creation-date-parm
                   / modification-date-parm
                   / read-date-parm
                   / size-parm
                   / parameter

 filename-parm := "filename" "=" value

 creation-date-parm := "creation-date" "=" quoted-date-time

 modification-date-parm := "modification-date" "=" quoted-date-time

 read-date-parm := "read-date" "=" quoted-date-time

 size-parm := "size" "=" 1*DIGIT

 quoted-date-time := quoted-string
                  ; contents MUST be an RFC 822 `date-time'
                  ; numeric timezones (+HHMM or -HHMM) MUST be used

也许我是瞎子。有人可以确认一下吗?

不,不需要。

rfc2183 指出:

`Extension-token', `parameter', `tspecials' and `value' are defined
according to [RFC 2045] (which references [RFC 822] in the definition
of some of these tokens).  `quoted-string' and `DIGIT' are defined in
[RFC 822].

和 rfc2045 定义 value 如下:

value := token / quoted-string

token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
            or tspecials>

tspecials :=  "(" / ")" / "<" / ">" / "@" /
              "," / ";" / ":" / "\" / <">
              "/" / "[" / "]" / "?" / "="
              ; Must be in quoted-string,
              ; to use within parameter values

这意味着 filename 参数不需要引用,只要它符合 token.

的定义即可

也就是说,在文件名参数周围添加引号可能是提高与现有软件兼容性的好方法。正如您所发现的,并非所有软件都能正确实现这些规范,通​​常会对格式做出(错误的)假设。

BNF 正下方是这段话:

`Extension-token', `parameter', `tspecials' and `value' are defined according to [RFC 2045] (which references [RFC 822] in the definition of some of these tokens). `quoted-string' and `DIGIT' are defined in [RFC 822].

2045 在第 5.1 节中有此定义(但是描述 Content-type:):

  value := token / quoted-string

 token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
             or tspecials>

 tspecials :=  "(" / ")" / "<" / ">" / "@" /
               "," / ";" / ":" / "\" / <">
               "/" / "[" / "]" / "?" / "="
               ; Must be in quoted-string,
               ; to use within parameter values

所以一个 token 的文件名不需要被引用;但如果它包含任何 tspecials (或控制字符或空格),你毕竟需要引用它。

只是为了专门解决下划线的情况,根据 RFC,它不是需要引用 的字符(它不是控件、空格或枚举为 tspecials), 但事情是在野外的方式,你可能最好引用一切以防万一。 (我们可以称其为反 Postel 吗?过分 对您传输的内容保守,并且在您认为可以推断出明显无效输入的内容上不要过于自由。)

顺便说一句,电子邮件中的 MIME 文件名实际上完全是荒野西部;许多流行的应用程序只是简单地忽略 RFC2231 并在此处使用 RFC2047 编码,或者不使用编码,或者完全是它们自己的临时 "I thought this might work and nobody has complained" 混合物。

顺便说一句

来自 RFC2183

filename-parm := "filename" "=" value


token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
             or tspecials>

来自 RFC2045

value := token / quoted-string

tspecials :=  "(" / ")" / "<" / ">" / "@" /
                "," / ";" / ":" / "\" / <">
                "/" / "[" / "]" / "?" / "="
                ; Must be in quoted-string,
                ; to use within parameter values

来自 RFC822

SPACE       =  <ASCII SP, space>            ; (     40,      32.)
CTL         =  <any ASCII control           ; (  0- 37,  0.- 31.)
                character and DEL>          ; (    177,     127.)

是不是意味着 header 比如

Content-Disposition: attachment; filename=ja    r.jar

在 jar.jar 中间有一个 HTAB(水平制表符)字符 smack 是一个有效的 header,不需要我用双引号引用 ja r.jar