MIME Headers

    An HTTP request or response consists of a header, body, and trailer. The HTTP header contains a request (or response) line and a MIME header. A MIME header is composed of zero or more MIME fields. A MIME field is composed of a field name, a colon, and zero or more field values (values in a field are separated by commas).

    In the example below: is the MIME field name, bar is the first MIME field value, and car is the second MIME field value.

    The following example is an augmented Backus-Naur Form (BNF) for the form of a MIME header - it specifies exactly what was described above. A header consists of zero or more fields that contain a name, separating colon, and zero or more values. A name or value is simply a string of tokens that is potentially zero length; a token is any character except certain control characters and separators (such as colons). For the purpose of retrieving a field, field names are not case-sensitive; therefore, the field names Foo, foo and fOO are all equivalent.

    1. MIME-header = *MIME-field
    2. MIME-field = field-name ":" #field-value
    3. field-name = *token
    4. field-value = *token

    The MIME header data structure is a parsed version of a standard Internet MIME header. The MIME header data structure is similar to the URL data structure (see URLs). The actual data is stored in a marshal buffer; the MIME header functions operate on a marshal buffer and a location (TSMLoc) within the buffer.

    After a call to TSMimeHdrFieldDestroy or TSMimeHdrFieldRemove is made, you must deallocate the TSMLoc handle with a call to TSHandleMLocRelease. You do not need to deallocate a NULL handles. For example: if you call TSMimeHdrFieldValueStringGet to get the value of the content type field and the field does not exist, then it returns TS_NULL_MLOC. In such a case, you wouldn’t need to deallocate the handle with a call to TSHandleMLocRelease.

    The location (TSMLoc) in the can be either an HTTP header location or a MIME header location. If an HTTP header location is passed to these functions, then the system locates the MIME header associated with that HTTP header and executes the corresponding MIME header operations specified by the functions (see the example in the description of TSMimeHdrCopy()).

    Note: MIME headers may contain more than one MIME field with the same name. Previous versions of Traffic Server joined multiple fields with the same name into one field with composite values, but this behavior came at a performance cost and caused compatibility issues with older clients and servers. Hence, the current version of Traffic Server does not coalesce duplicate fields. Correctly-behaving plugins should check for the presence of duplicate fields and iterate over the duplicate fields by using TSMimeHdrFieldNextDup.

    To facilitate fast comparisons and reduce storage size, Traffic Server defines several pre-allocated field names. These field names correspond to the field names in HTTP and NNTP headers.

    const char* TS_MIME_FIELD_ACCEPT

    "Accept"

    int TS_MIME_LEN_ACCEPT

    const char* TS_MIME_FIELD_ACCEPT_CHARSET

    "Accept-Charset"

    int TS_MIME_LEN_ACCEPT_CHARSET

    const char* TS_MIME_FIELD_ACCEPT_ENCODING

    "Accept-Encoding"

    int TS_MIME_LEN_ACCEPT_ENCODING

    const char* TS_MIME_FIELD_ACCEPT_LANGUAGE

    "Accept-Language"

    int TS_MIME_LEN_ACCEPT_LANGUAGE

    const char* TS_MIME_FIELD_ACCEPT_RANGES

    "Accept-Ranges"

    int TS_MIME_LEN_ACCEPT_RANGES

    const char* TS_MIME_FIELD_AGE

    "Age"

    int TS_MIME_LEN_AGE

    const char* TS_MIME_FIELDALLOW

    "Allow"

    int TS_MIME_LEN_ALLOW

    const char* TS_MIME_FIELDAPPROVED

    "Approved"

    int TS_MIME_LEN_APPROVED

    const char* TS_MIME_FIELDAUTHORIZATION

    "Authorization"

    int TS_MIME_LEN_AUTHORIZATION

    const char* TS_MIME_FIELDBYTES

    "Bytes"

    int TS_MIME_LEN_BYTES

    const char* TS_MIME_FIELDCACHE_CONTROL

    "Cache-Control"

    int TS_MIME_LEN_CACHE_CONTROL

    const char* TS_MIME_FIELDCLIENT_IP

    "Client-ip"

    int TS_MIME_LEN_CLIENT_IP

    const char* TS_MIME_FIELDCONNECTION

    "Connection"

    int TS_MIME_LEN_CONNECTION

    const char* TS_MIME_FIELDCONTENT_BASE

    "Content-Base"

    int TS_MIME_LEN_CONTENT_BASE

    const char* TS_MIME_FIELDCONTENT_ENCODING

    "Content-Encoding"

    int TS_MIME_LEN_CONTENT_ENCODING

    const char* TS_MIME_FIELDCONTENT_LANGUAGE

    "Content-Language"

    int TS_MIME_LEN_CONTENT_LANGUAGE

    const char* TS_MIME_FIELDCONTENT_LENGTH

    "Content-Length"

    int TS_MIME_LEN_CONTENT_LENGTH

    const char* TS_MIME_FIELDCONTENT_LOCATION

    "Content-Location"

    int TS_MIME_LEN_CONTENT_LOCATION

    const char* TS_MIME_FIELDCONTENT_MD5

    "Content-MD5"

    int TS_MIME_LEN_CONTENT_MD5

    const char* TS_MIME_FIELDCONTENT_RANGE

    "Content-Range"

    int TS_MIME_LEN_CONTENT_RANGE

    const char* TS_MIME_FIELDCONTENT_TYPE

    "Content-Type"

    int TS_MIME_LEN_CONTENT_TYPE

    const char* TS_MIME_FIELDCONTROL

    "Control"

    int TS_MIME_LEN_CONTROL

    const char* TS_MIME_FIELDCOOKIE

    "Cookie"

    int TS_MIME_LEN_COOKIE

    const char* TS_MIME_FIELDDATE

    int TS_MIME_LEN_DATE

    const char* TS_MIME_FIELDDISTRIBUTION

    "Distribution"

    int TS_MIME_LEN_DISTRIBUTION

    const char* TS_MIME_FIELDETAG

    "Etag"

    int TS_MIME_LEN_ETAG

    "Expect"

    int TS_MIME_LEN_EXPECT

    const char* TS_MIME_FIELDEXPIRES

    "Expires"

    int TS_MIME_LEN_EXPIRES

    const char* TS_MIME_FIELDFOLLOWUP_TO

    "Followup-To"

    int TS_MIME_LEN_FOLLOWUP_TO

    const char* TS_MIME_FIELDFROM

    "From"

    int TS_MIME_LEN_FROM

    const char* TS_MIME_FIELDHOST

    "Host"

    int TS_MIME_LEN_HOST

    const char* TS_MIME_FIELDIF_MATCH

    "If-Match"

    int TS_MIME_LEN_IF_MATCH

    const char* TS_MIME_FIELDIF_MODIFIED_SINCE

    "If-Modified-Since"

    int TS_MIME_LEN_IF_MODIFIED_SINCE

    const char* TS_MIME_FIELDIF_NONE_MATCH

    "If-None-Match"

    int TS_MIME_LEN_IF_NONE_MATCH

    const char* TS_MIME_FIELDIF_RANGE

    "If-Range"

    int TS_MIME_LEN_IF_RANGE

    const char* TS_MIME_FIELDIF_UNMODIFIED_SINCE

    "If-Unmodified-Since"

    int TS_MIME_LEN_IF_UNMODIFIED_SINCE

    const char* TS_MIME_FIELDKEEP_ALIVE

    "Keep-Alive"

    int TS_MIME_LEN_KEEP_ALIVE

    const char* TS_MIME_FIELDKEYWORDS

    "Keywords"

    int TS_MIME_LEN_KEYWORDS

    const char* TS_MIME_FIELDLAST_MODIFIED

    "Last-Modified"

    int TS_MIME_LEN_LAST_MODIFIED

    const char* TS_MIME_FIELDLINES

    "Lines"

    int TS_MIME_LEN_LINES

    const char* TS_MIME_FIELDLOCATION

    "Location"

    int TS_MIME_LEN_LOCATION

    const char* TS_MIME_FIELDMAX_FORWARDS

    "Max-Forwards"

    int TS_MIME_LEN_MAX_FORWARDS

    const char* TS_MIME_FIELDMESSAGE_ID

    "Message-ID"

    int TS_MIME_LEN_MESSAGE_ID

    const char* TS_MIME_FIELDNEWSGROUPS

    "Newsgroups"

    int TS_MIME_LEN_NEWSGROUPS

    const char* TS_MIME_FIELDORGANIZATION

    "Organization"

    int TS_MIME_LEN_ORGANIZATION

    const char* TS_MIME_FIELDPATH

    "Path"

    int TS_MIME_LEN_PATH

    const char* TS_MIME_FIELDPRAGMA

    "Pragma"

    int TS_MIME_LEN_PRAGMA

    const char* TS_MIME_FIELDPROXY_AUTHENTICATE

    "Proxy-Authenticate"

    int TS_MIME_LEN_PROXY_AUTHENTICATE

    const char* TS_MIME_FIELDPROXY_AUTHORIZATION

    "Proxy-Authorization"

    int TS_MIME_LEN_PROXY_AUTHORIZATION

    const char* TS_MIME_FIELDPROXY_CONNECTION

    "Proxy-Connection"

    int TS_MIME_LEN_PROXY_CONNECTION

    const char* TS_MIME_FIELDPUBLIC

    "Public"

    int TS_MIME_LEN_PUBLIC

    const char* TS_MIME_FIELDRANGE

    "Range"

    int TS_MIME_LEN_RANGE

    const char* TS_MIME_FIELDREFERENCES

    "References"

    int TS_MIME_LEN_REFERENCES

    const char* TS_MIME_FIELDREFERER

    "Referer"

    int TS_MIME_LEN_REFERER

    const char*

    "Reply-To"

    int TS_MIME_LEN_REPLY_TO

    "Retry-After"

    int TS_MIME_LEN_RETRY_AFTER

    const char* TS_MIME_FIELDSENDER

    "Sender"

    int TS_MIME_LEN_SENDER

    const char* TS_MIME_FIELDSERVER

    "Server"

    int TS_MIME_LEN_SERVER

    const char* TS_MIME_FIELDSET_COOKIE

    "Set-Cookie"

    int TS_MIME_LEN_SET_COOKIE

    const char* TS_MIME_FIELDSUBJECT

    "Subject"

    int TS_MIME_LEN_SUBJECTTS_MIME_LEN_SUBJECT

    const char* TS_MIME_FIELDSUMMARY

    "Summary"

    int TS_MIME_LEN_SUMMARY

    const char* TS_MIME_FIELDTE

    "TE"

    int TS_MIME_LEN_TE

    const char* TS_MIME_FIELDTRANSFER_ENCODING

    "Transfer-Encoding"

    int TS_MIME_LEN_TRANSFER_ENCODING

    const char* TS_MIME_FIELDUPGRADE

    "Upgrade"

    int TS_MIME_LEN_UPGRADE

    const char* TS_MIME_FIELDUSER_AGENT

    "User-Agent"

    int TS_MIME_LEN_USER_AGENT

    const char* TS_MIME_FIELDVARY

    "Vary"

    int TS_MIME_LEN_VARY

    const char* TS_MIME_FIELDVIA

    "Via"

    int TS_MIME_LEN_VIA

    const char* TS_MIME_FIELDWARNING

    "Warning"

    int TS_MIME_LEN_WARNING

    const char* TS_MIME_FIELDWWW_AUTHENTICATE

    "Www-Authenticate"

    int TS_MIME_LEN_WWW_AUTHENTICATE

    const char* TS_MIME_FIELDXREF

    "Xref"

    int TS_MIME_LEN_XREF

    The header field names above are defined in ts.h as const char* strings. When Traffic Server sets the name portion of a header field (or any portion for that matter), it quickly checks to see if the new value is one of the known values. If it is, then Traffic Server stores a pointer into a global table instead of storing the known value in the marshal buffer. The header field names listed above are also pointers into this table, which enables simple pointer comparison of the value returned from TSMimeHdrFieldNameGet with one of the values listed above. It is recommended that you use the above values when referring to one of the known header field names to avoid the possibility of a spelling error.

    Traffic Server adds one important feature to MIME fields that you may not know about: Traffic Server does not print a MIME field if the field name begins with the ‘@’ symbol. For example: a plugin can add the field “@My-Field” to a header. Even though Traffic Server never sends that field out in a request to an origin server or in a response to a client, it can be printed to Traffic Server logs by defining a custom log configuration file that explicitly logs such fields. This provides a useful mechanism for plugins to store information about an object in one of the MIME headers associated with the object.

    The MIME header functions are listed below: