Search code examples
user-agent

What is the standard format for a browser's User-Agent string?


Is there an RFC, official standard, or template for creating a User Agent string? The iphone's user-agent string seems strange...

Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7D11 Safari/528.16


Solution

  • Note: In June 2022 the IETF (Internet Engineering Task Force) published RFC9110, which obsoleted RFC7231, therefore I'm updating this answer with the new RFC information.

    The User-Agent header is part of the RFC9110, which describes the HTTP Semantics, where is states:

    The "User-Agent" header field contains information about the user agent originating the request, which is often used by servers to help identify the scope of reported interoperability problems, to work around or tailor responses to avoid particular user agent limitations, and for analytics regarding browser or operating system use. A user agent SHOULD send a User-Agent header field in each request unless specifically configured not to do so.

    EBNF Specification

    User-Agent = product *( RWS ( product | comment ) )
    

    Where product is defined as:

    product         = token ["/" product-version]
    product-version = token
    token           = 1*tchar
    tchar           = "!" / "#" / "$" / "%" / "&" / "'" / "*"
                    / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
                    / DIGIT / ALPHA
                    ; any VCHAR, except delimiters
    

    And comment is defined as:

    comment     = "(" *( ctext | quoted-pair | comment ) ")"
    ctext       = HTAB | SP | %x21-27 | %x2A-5B | %x5D-7E | obs-text
    quoted-pair = "\" ( HTAB | SP | VCHAR | obs-text )
    obs-text    = %x80-ff
    

    Other rules for reference:

    HTAB  = <ASCII horizontal tab %x09, aka '\t'>
    SP    = <ASCII space, i.e. " ">
    VCHAR = <any visible US-ASCII character>
    DIGIT = <digits from 0 to 9>
    ALPHA = <letters>
    RWS   = 1*( SP | HTAB )
    1*    = <One or more>
    

    Note that this means that product cannot contain spaces, but comments can.


    Examples:

    Here are some valid examples of product strings (with and without product-version strings):

    # Single `product` without product-version:
    Foobar
    Foobar-baz
    
    # Single `product` with product-version:
    Foobar/abc
    Foobar/1.0.0
    Foobar/2021.44.30.15-b917dc
    

    Here are some valid examples of comment strings; note how all strings are enclosed in matched parentheses ( ):

    # This was the default `comment` used by Internet Explorer 11:
    (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0)
    
    # You can put almost any text inside a comment:
    (Why are you looking at HTTP headers? Go outside, find love, do some good in the world)
    
    # Note that `comment` strings can also be nested, provided their delimiting parentheses are matched, for example:
    (Outer comment (Inner comment))
    

    As a User-Agent header's value is comprised of arbitrary product and comment strings, these are all valid User-Agent headers:

    User-Agent: Foobar
    User-Agent: Foobar/2021.44.30.15-b917dc
    User-Agent: MyProduct Foobar/2021.44.30.15-b917dc
    User-Agent: Tsom/OfraHaza (Life is short and love is always over in the morning) AnotherProduct