Approach to Validating Messages
One of the guiding principles of the Internet is to “be conservative in what you send and be liberal in what you accept.” By this principle, the MIME package should make a strong effort to enforce correctness whenever the package produces a message. In contrast, one of the guiding principles of a useful class library is not to enforce policy, but instead to allow the programmer as much control as possible over the output of the program. By this principle, the MIME package should never enforce the validity of a message.
The MIME package balances these approaches. The package considers validity whenever an object sends or accepts data – in the
asString(),
fromString(), and
setBody() member functions. The
fromString() functions accept any string that the function is able to parse, regardless of whether the string contains valid MIME. However, if the function cannot extract data from the string (for example, if the string is empty), the function throws
RWMimeParseError. Likewise, the
setBody() functions accept any body string that the function can parse.
The
asString() functions create data for a program to send, so these functions are more conservative. An
asString() function throws
RWMimeError if the function is unable to produce a structurally correct string. Of course, a structurally correct string can still be nonsensical – the message could claim to contain text, but actually contain a compressed image. The MIME package guarantees correct structure, but lets the programmer determine the content.
For example, the Content-ID header is required to contain both a label and a value. Given a string like "Content-ID: ", which has a label but no value, a fromString() function interprets the string as a Content-ID header with an empty value, even though such a header is invalid. However, an asString() function will not produce a Content-ID header with an empty value since there are no circumstances under which the header would be valid MIME.