<?xml version="1.0" encoding="ISO-8859-1"?>

<document>

  <properties>
    <title>HttpClient Cookie Guide</title>
    <author email="adrian@intencha.com">Adrian Sutton</author>
    <author email="oleg@ural.ru">Oleg Kalnichevski</author>
    <revision>$Id$</revision>
  </properties>

  <body>

    <section name="Introduction">
      <p>HttpClient supports automatic management of cookies, including
      allowing the server to set cookies and automatically return them to the
      server when required.  It is also possible to manually set cookies to be
      sent to the server.</p>

      <p>Unfortunately, there are several at times conflicting  standards for 
      handling Cookies: the Netscape Cookie draft, RFC2109, RFC2965 and a large
      number of vendor specific implementations that are compliant with neither 
      specification. To deal with this, HttpClient provides policy driven cookie 
      management. This guide will explain how to use the different cookie 
      specifications and identify some of the common problems people have when 
      using Cookies and HttpClient.</p>
    </section>

    <section name="Supported Specifications">
      <p>The following cookie specifications are supported by HttpClient 3.1.
      </p>

      <subsection name="RFC2109">
        <p>RFC2109 is the first official cookie specification released by the W3C.
        Theoretically, all servers that handle version 1 cookies should use this
        specification and as such this specification is used by default within
        HttpClient.</p>

        <p>Unfortunately, many servers either incorrectly implement this
        standard or are still using the Netscape draft so occasionally this
        specification is too strict.  If this is the case, you should switch to
        the compatibility specification as described below.</p>

        <p>RFC2109 is available at
        <a href="http://www.w3.org/Protocols/rfc2109/rfc2109.txt">http://www.w3.org/Protocols/rfc2109/rfc2109.txt</a>
        </p>

        <p>RFC2109 is the default cookie policy used by HttpClient.</p>
      </subsection>

      <subsection name="RFC2965">
        <p>RFC2965 defines cookie version 2 and attempts to address the
           shortcomings of the RFC2109 regarding cookie version 1.
           RFC2965 is intended to eventually supersede RFC2109.</p>

        <p>Servers that send RFC2965 cookies will use the Set-Cookie2 header
           in addition to the Set-Cookie header. RFC2965 cookies are port
           sensitive.
        </p>
        <p>RFC2965 is available at
        <a href="http://www.w3.org/Protocols/rfc2965/rfc2965.txt">http://www.w3.org/Protocols/rfc2965/rfc2965.txt</a>
        </p>
      </subsection>

      <subsection name="Netscape Draft">
        <p>The Netscape draft is the original cookie specification which formed
        the basis for RFC2109.  Despite this it has some significant
        differences with RFC2109 and thus may be required for compatibility
        with some servers.</p>

        <p>The Netscape cookie draft is available at <a
        href="http://wp.netscape.com/newsref/std/cookie_spec.html">http://wp.netscape.com/newsref/std/cookie_spec.html</a>
        </p>
      </subsection>

      <subsection name="Browser Compatibility">
        <p>The compatibility specification is designed to be compatible with as
        many different servers as possible even if they are not completely
        standards compliant. If you are encountering problems with parsing
        cookies, you should probably try using this specification.</p>
        <p>There are many web sites with badly written CGI scripts that only work 
        when all cookies are put into one request header. It is advisable to 
        set <a href="preference-api.html">http.protocol.single-cookie-header</a>
        parameter to <code>true</code> for maximum compatibility.
        </p>
      </subsection>

      <subsection name="Ignore Cookies">
        <p>This cookie specification ignores all cookies.  It should be used to
        prevent HttpClient from accepting and sending cookies.</p>
      </subsection>

    </section>

    <section name="Specifying the Specification">
      <p>There are two ways to specify which cookie specification should be
      used, either for each <code>HttpMethod</code> instance using the 
      <code>HttpMethodParams</code>, or by setting the default value on
      <code>CookiePolicy</code>.</p>

      <subsection name="Per HttpMethod">
        <p>In most cases, the best way to specify the cookie spec to use is the
        <code>setCookiePolicy(String policy)</code> method on
        <code>HttpMethodParams</code>.  The value of <code>policy</code>
        must be one of the values registered with <code>CookiePolicy.registerCookieSpec()</code>.</p>

        <source>
        HttpMethod method = new GetMethod();
        method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
        </source>
      </subsection>

      <!-- re-enable when issue #37712 is fixed
      <subsection name="Default">
        <p>The default cookie specification can be set by defining the default cookie
        policy on <code>CookiePolicy</code>.</p>

        <source><![CDATA[
        CookiePolicy.registerCookieSpec(CookiePolicy.DEFAULT, <Some CookieSpec>);
        ]]></source>
      </subsection>
      -->
    </section>

    <section name="Manual handling of cookies">
      <p>The cookie management API of HttpClient can co-exist with the manual 
      cookie handling. One can manually set request <code>Cookie</code> 
      headers or process response <code>Set-Cookie</code> headers in addition 
      or instead of the automatic cookie management</p>

        <source><![CDATA[
        HttpMethod method = new GetMethod();
        method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
        method.setRequestHeader("Cookie", "special-cookie=value");
        ]]></source>

    </section>

    <section name="Common Problems">
      <p>The most common problems encountered with parsing cookies is due to
      non-compliant servers.  In these cases, switching to the compatibility
      cookie specification usually solves the problem.</p>
    </section>
    
    <section name="Encoding Issues">
      <p>Since cookies are transfered as HTTP Headers they are confined to
      the <tt>US-ASCII</tt> character set. Other characters will be lost or
      mangeled. Cookies are typically set and read by the same server, so
      a custom scheme for escaping non-ASCII characters can be used, for
      instance the well-established URL encoding scheme. If cookies are
      used to transfer data between server and client both parties must
      agree on the escaping scheme used in a custom way. The HttpClient
      cookie implementation provides no special means to handle non-ASCII
      characters nor does it issue warnings.</p>
    </section>
  </body>
</document>
