1

I was recently studying Internet Message Format originally RFC2822 and RFC5322. I was looking at definition of folding whitespace.

Each header field is logically a single line of characters comprising the field name, the colon, and the field body. For convenience however, and to deal with the 998/78 character limitations per line, the field body portion of a header field can be split into a multiple-line representation; this is called "folding". The general rule is that wherever this specification allows for folding white space (not simply WSP characters), a CRLF may be inserted before any WSP.

For example, the header field:

Subject: This is a test

can be represented as:

Subject: This
  is a test

The rule definition is in ABNF notation and as follows:

FWS = ([WSP CRLF] 1WSP) / obs-FWS ; Folding white space

WSP is a whitespace, CRLF is carriage return and line feed In ABNF []-enclosed can or cannot be present which is not what the text says.
The rule definition means:

FWS is 1 or more whitespace optionally preceded by (zero or more whitespace and carriage return and line feed)
Which means that just one or more whitespace is a folding whitespace. Is that correct?

2 Answers 2

2

Both the specification and your understanding is correct, but the specification is slightly confusing. The FWS rule does not only represent folding whitespace, but any whitespace that may also include folding. Similarly, the CFWS rule doesn't just denote a comment with a folding whitespace, but any location where whitespace and/or comments and/or foldings are allowed.

This practice of combining multiple rules is common in grammars where the rules explicitly encode precedence. E.g. an arithmetic language may allow multiplication and addition. This might then be specified as

Addition = Addition "+" Multiplication / Multiplication
Multiplication = Multiplication "*" Number / Number

In that example, Addition doesn't just match an addition expression, but also any literal Number. In the RFC you cite, CFWS similarly allows a comment or FWS, and FWS allows a folding whitespace or really any whitespace – although the grammar uses an optional group [...] that doesn't quite make this alternative clear.

1
  • Ok, now I got it.
    – Unicorn
    Commented Apr 30, 2018 at 16:14
1

When you show us:

FWS = ([*WSP CRLF] 1*WSP) / obs-FWS 

what you're missing is obs-FWS1.

obs-FWS = 1*WSP *(CRLF 1*WSP)

It's the obsolete alternative allowed by the "/" operator2 in ABNF

Elements separated by a forward slash ("/") are alternatives.
Therefore,

 foo / bar

will accept <foo> or <bar>.

If you understand the "*" operator3:

The operator "*" preceding an element indicates repetition. The full form is:

 <a>*<b>element

where <a> and <b> are optional decimal values, indicating at least <a> and at most <b> occurrences of the element.

Default values are 0 and infinity so that *<element> allows any number, including zero; 1*<element> requires at least one; 3*3<element> allows exactly 3; and 1*2<element> allows one or two.

and the Optional Sequence ("[" and "]") operators4

Square brackets enclose an optional element sequence:

 [foo bar]

is equivalent to

 *1(foo bar).

You should be able to see that either alternative will accept as FWS a single WSP5 or multiple WSP. So yes, "just one or more whitespace is a folding whitespace".

The interesting bit is how it deals with CRLF. Neither alternative will accept repeated CRLF without WSP separating them.

1
  • yeah there is a line which says However, where CFWS occurs in this specification, it MUST NOT be inserted in such a way that any line of a folded header field is made up entirely of WSP characters and nothing else. So no lines made out of blank and still doing folding thingy
    – Unicorn
    Commented Apr 30, 2018 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.