Return preferred mime type for serving XHTML

"""
The preferred MIME type to serve XHTML is ``application/xhtml+xml``.  While it
is optional in XHTML 1.0 (``text/html`` may be used for compatibility reasons,
i.e. with Internet Explorer 6 [again!]), it is mandatory for XHTML 1.1.
"""

import os


def get_mimetype():
    """Return apt MIME type for the requesting client.

    Use ``text/html`` as fallback if the client sends no accept header for the
    preferred type ``application/xhtml+xml``.
    """
    xhtml = 'application/xhtml+xml'
    try:
        if os.environ['HTTP_ACCEPT'].find(xhtml) == -1:
            raise ValueError
        return xhtml
    except:
        return 'text/html'