<?xml version="1.0"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
  <!ENTITY rfc2046 PUBLIC '' 'bibxml/reference.RFC.2046.xml'>
  <!ENTITY rfc2518 PUBLIC '' 'bibxml/reference.RFC.2518.xml'>
  <!ENTITY rfc2616 PUBLIC '' 'bibxml/reference.RFC.2616.xml'>
  <!ENTITY rfc3229 PUBLIC '' 'bibxml/reference.RFC.3229.xml'>
  <!ENTITY rfc3230 PUBLIC '' 'bibxml/reference.RFC.3230.xml'>
  <!ENTITY rfc3253 PUBLIC '' 'bibxml/reference.RFC.3253.xml'>
  <!ENTITY rfc3744 PUBLIC '' 'bibxml/reference.RFC.3744.xml'>
]>
<?rfc toc="yes"?>
<?rfc strict="yes"?>
<?rfc symrefs="no"?>
<rfc ipr="full3667" docName="draft-dusseault-http-patch-05">
  <front>
    <title abbrev="HTTP PATCH">
      Partial Document Changes (PATCH Method) for HTTP
    </title>

    <author initials="L.M." surname="Dusseault" fullname="Lisa Dusseault">
      <organization abbrev="OSAF">Open Source Application Foundation</organization>
      <address>
        <postal>
          <street>2064 Edgewood Dr.</street>
          <city>Palo Alto</city> <region>CA</region>
          <code>94303</code>
          <country>US</country>
        </postal>
        <email>lisa@osafoundation.org</email>
      </address>
    </author>

    <date month="August" year="2004"  day="30"/>
    <area>Applications</area>
    <workgroup>Individual Submission</workgroup>
    <keyword>I-D</keyword>
    <keyword>webdav</keyword>
    <keyword>http</keyword>
    <keyword>simple</keyword>
    <keyword>patch</keyword>
    <keyword>deltav</keyword>

    <abstract>
      <t>Several applications extending HTTP require a feature to do 
	partial resource modification.  Existing
	HTTP functionality only allows a complete replacement of a document.
	This proposal adds a new HTTP method, PATCH, to modify an 
	existing HTTP resource.</t>
    </abstract>

  </front>
  

  <middle>
    <section anchor="intro" title="Introduction">
      <t>Three use cases initially motivated this proposal</t>

      <t><list style="numbers">
        <t>WebDAV <xref target="RFC2518"/> is used by authoring applications 
        to store and share files on
	the internet.  For example, Adobe Photoshop has a Workgroup feature 
	allowing the user to browse a repository and save the file.  Currently,
	Photoshop only publishes the file to the repository rarely, because 
	Photoshop files are typically large and upload is slow.  Worse, large
	uploads are more likely to be interrupted.  Although <xref
        target="RFC2616">HTTP</xref> provides byte
	range downloads, it does not provide a mechanism for partial uploads.</t>
	
   <t><xref target="RFC3253">DeltaV</xref> extends WebDAV to do versioning.  In versioning environments, 
	a large number of files may be updated with very small changes.  For 
	example, a programmer may change the name of a function used in a 
	hundred source files.  Versioning applications typically send deltas or 
	patches to the server to modify these files, however DetaV does not yet
	have this functionality.</t>
	<t>The SIMPLE WG is devising a way to store and modify configuration 
	information. The biggest feature missing from HTTP is the ability to 
	modify information in a very lightweight manner, so that the client that
	decides to change its presence state from "free" to "busy" doesn't have
	to upload a large document. This can be accomplished through changes
	to a HTTP resource as well.</t>
      </list></t>

      <t>Other working groups (like netconf) are also considering manipulating 
      large files using HTTP GET and PUT. Sometimes the files aren't that large
      but the device is small or bandwidth is limited, as when phones need to
      add a new contact to an address book file. This feature would allow much more 
      efficient changes to files.</t>


        <t>This specification defines a new HTTP 1.1 method for patches.  A new
        method is necessary to improve interoperability and prevent errors.  
        The PUT method is already defined to overwrite a resource
	with a complete new body, and MUST NOT be reused to do partial changes.
	Otherwise, proxies and caches and even clients and servers may get 
	confused as to the result of the operation. </t>

      <t>Note that byte ranges are already used in HTTP to do partial downloads 
      (GET method).   However, they
	are not defined for uploads, and there are some missing pieces for uploads.  
	For example, the HTTP specification does not define a particularly informative
        error to send if the byte range in a PUT is invalid.
	Byte ranges (or some other kind of range) could be made to work 
        in this specification but a more flexible mechanism (one that 
        could also encompass XML patch formats) was desired, as well as a 
        method that would not confuse caching proxies.  Reliable and
	tested patch algorithms already exist, and this specification takes 
	advantage of that existing work.</t>

      <t>Other patch formats ("delta encodings") are defined for HTTP in <xref target="RFC3229">
        RFC 3229</xref>.
        That specification defines delta encodings for cache updates, not for user
	write operations.  It does mean that servers can reuse delta encoding
	algorithms to support both that specification and this proposal.</t>

   <t>This specification defines the new method PATCH to alter a single existing
   resource, in place, by applying a patch.  The operation 
   is atomic. Note that WebDAV MOVE and COPY requests, if supported by 
   the HTTP server, can be useful
   to independently rename or copy a whole resource before applying PATCH to 
   either the source or destination URL to modify the contents.</t>


    </section>

      <section title="Patch Formats">
        <t>A set of changes for a resource is itself a document, called a patch
	document.  The patch format is uniquely identified through a 
	MIME type.  Servers advertise supported patch formats by advertising
	these MIME types, and clients specify which one they're using by 
	including the MIME type in the request.  MIME types were specifically chosen 
	so that there would be a well-defined way for other PATCH extensions to define
	their own patch formats and how to use them.</t>
	
        <t>This specification only defines usage of the platform-portable 
       <xref target="refs.W3C-GDIFF">gdiff</xref> format identified as 
       'application/gdiff'.  Servers SHOULD support gdiff for all authorable resources, that is all
        resources that support PUT.    Some requirements apply only to specific patch 
        formats, and in this specification those requirements are spelled out
        only for gdiff.</t>
      </section>
      
    <section title="Mechanisms" anchor="mech" >
      <section title="PATCH Method">
	
	<t>The PATCH method requests that the request body (a patch document)
	be applied to the resource identified by the Request-URI.  The server MUST NOT
	create a new resource with the contents of the request body, although it
	MAY (depending on the patch document format) apply the request body to an 
	empty entity to result in the content for the new resource. The 
	target resource's content type MUST be one to which the patch format 
	applies.  The server MUST apply the entire patch atomically and never
    provide (e.g. in response to a GET during this operation) a partially-patched body.  If 
        the entire patch file cannot be successfully applied 
        then the server MUST fail the entire request, applying none of the
        changes. See error handling section for details on status codes and
	possible error conditions. </t> 

	    
	<t>PATCH request bodies MUST NOT be cached.  A cache MAY mark the resource identified
	in the Request-URI as stale if it sees a successful response to the PATCH
	request. </t>
	
	<t>The PATCH request MUST have a body.  It MUST include the Content-Type 
	header with a <xref target="RFC2046">
        MIME</xref> type value identifying the patch format used in the request body.  
    The request body MUST be in some
	format which has the semantics of defining a change to an existing document. </t>
	    
   <t>If the gdiff format is used:</t>
       
       <t><list style="symbols">
           <t>The client MUST verify that it is applying the patch document to a known entity.  
               There are two reliable ways to do this.
   The first way is to find out the resource ETag at the time the body is 
   downloaded, and use that Etag in the If-Match header on the PATCH 
             request to make sure the resource
   is still unchanged.  The second way to use WebDAV LOCK/UNLOCK to reserve the 
   file (first LOCK, then GET, then PATCH, then UNLOCK).  Gdiff collisions from
   multiple users are more dangerous than PUT collisions, because a gdiff that
   is not operating from a known base point may corrupt the resource.  Therefore,
   if neither strong ETags nor LOCKS are available from the server, the client
   MUST use If-Unmodified-Since as a less-reliable safeguard.</t>
       <t>If the Request-URI does not identify an existing resource,
         the server SHOULD (subject
       of course to access control and other restrictions) create a 
      resource with an empty body and apply the gdiff changes to that empty entity.
      A client SHOULD verify that the URL is unmapped, as expected, with use
      of the "If-None-Match: *" header.
       </t>
   
       </list>
       </t>
       
       

        <figure anchor="PATCH_example">
          <preamble>Simple PATCH example</preamble>
          <artwork>

    PATCH /file.txt HTTP/1.1
    Host: www.example.com
    Content-type: application/gdiff
    If-Match: "e0023aa4e"
    Content-Length: 100

    [gdiff-binary-body]

          </artwork>
        </figure>
        <t>This example illustrates use of the gdiff algorithm on an existing text file.</t>
	  
      </section>
      <section title="PATCH Response">
        <section title="Success Response">
	  <t>A successful response with the 204 No Content status code implies that
	    no new resource was created.  A successful response with the  
	      201 Created status code informs the client that a new resource 
	      was created.</t>

	  <t>The server SHOULD provide a MD5 hash of the resource entity after the
	  patch was applied.  This allows the client to verify the success of
	  the operation.  
	  </t>
          <t>
	    As with PUT, the PATCH method MUST change the resource's ETag if
	  the resulting entity is not identical to the original.  
	  If the server supports strong ETags, the server MUST return
	  a strong ETag for use in future client operations.  The server MUST
	  return the Last-Modified header if it does not support strong ETags.</t>
          <figure>
            <preamble>Successful PATCH response to existing text file</preamble>
            <artwork>
    HTTP/1.1 204 No Content
    Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
    ETag: "e0023aa4e"

            </artwork>
          </figure>
        </section>

        <section title="Error handling">
          <t>This proposal uses the same mechanism as DeltaV (defined in section
            1.6 of RFC3253) to add
	    machine-parsable info to provide more detail than HTTP status codes can.  Existing HTTP 
	    status codes are not infinitely extensible but XML elements and
	    namespaces are more so, and it's simple to treat the HTTP error code
	    as a rough category and put detailed error codes in the body.
          Clients that do not use the extra information 
          ignore the bodies of error responses.  These error
	    codes are not meant to be displayed directly to end-users, so there is no language code
          or other display information.  Clients MUST ignore any unrecognized elements within
          the XML response body because extensions allow implementors to add custom debug information
          to the response.</t>

          <t>The PATCH method can return the following errors. All these errors
            are represented as XML elements in an XML document, where the specific error element
            appears inside a root element called "error" in the "DAV:" namespace.  The new elements
            defined in this specification are all in the "urn:ietf:params:xml:ns:patch" 
            namespace.</t>

          <t><list style="hanging">
            <t></t>
            <t hangText="delta-format-unsupported:">
       Used with 403 Forbidden 
	    status code.  Returned by the server when it doesn't support the
	    patch format chosen by the client.  </t>
            <t></t>

            <t hangText="delta-format-forbidden-on-resource:">  Used with 403 Forbidden
	    when the patch format chosen by the client is supported by the server
	    but not allowed on this kind of resource.</t>
            <t></t>

            <t hangText="delta-format-badly-formatted:">  Used with 400 Bad Request
	    when the server finds that the patch document provided by the client
	    was badly formatted or non-compliant.  The definition of badly formatted 
	    or non-compliant depends on the patch format chosen, but generally if the
	    server finds it can't handle the current patch even though it supports the format
	    used, this error ought to be appropriate.</t> 
            <t></t>

            <t hangText="patch-empty-resource:">  Used with 409 Conflict when the
	    resource addressed in the Request-URI exists but is empty, and 
	    the patch format cannot be applied to an empty document.  Note that
	    some patch formats may be applied to an empty document, in which
	    case this error wouldn't be used.</t>
            <t></t>

            <t hangText="patch-result-invalid:">  Used with 409 Conflict when the
	    resource could be patched but the result of the patch would be a resource
	    which is invalid.  This could mean, for example, that a XML resource
	    would become an invalid XML file if the patch specified that a close
	    element text line should be deleted.</t>
            <t></t>
          </list></t>
 
            
            <t>"404 Not Found" can be used (with no body/error element) when
	    the URL in by the Request-URI does not map to a resource and the server
	    cannot apply the patch document to a new empty resource (thus this 
            error wouldn't be used with gdiff patch documents). </t>

          <t>Other status codes defined in RFC2616 may also be used under
            the appropriate circumstances, with no response body. 
            For example, an unauthenticated user
            may be prompted to authenticate, in order to use PATCH, with 
            "401 Unauthorized".  An authenticated user who does not have
            sufficient privilege to use PATCH may receive a "403 Forbidden" response.</t>
          
        <section title="Example error response with body detail">
          
        <t>
        <figure>
          <artwork>
    HTTP/1.1 409 Conflict
    Content-Type: text/xml; charset="utf-8"
    Content-Length: xxx
            
    &lt;?xml version="1.0" encoding="utf-8" ?&gt;  
    &lt;D:error xmlns:D="DAV:"&gt;
      &lt;P:patch-result-invalid 
            xmlns:P="urn:ietf:params:xml:ns:patch"/&gt;
    &lt;/D:error&gt;

          </artwork>
        </figure>
        </t>            
        </section>
          
	    
      </section>
        
      </section>

      <section anchor="adv" title="Advertising Support in OPTIONS">
        <t>The server advertises its support for the features described here
	with OPTIONS response headers.  The "Allow" OPTIONS header is already
        defined in HTTP 1.1  to contain all the allowed methods on the 
        addressed resource, so the server MUST add PATCH if it is allowed.</t>

        <t>Clients also need to know whether the server supports special patch 
        formats, so this 
        document introduces a new OPTIONS response header "Accept-Patch".
        "Accept-Patch" MUST appear in the OPTIONS response for any resource 
        where the PATCH method is shown as an allowed method.  </t>
        
        <t>OPTIONS * is not used to advertise support for PATCH because the patch
            formats supported are likely to change from one resource to another.
            A server MAY include the Accept-Patch header in response to OPTIONS *,
            and its value MAY be the union of known supported patch formats.
        </t>

        <t>Accept-Patch = "Accept-Patch" ":" #media-type</t>
            
    <t>

        <figure>
          <preamble>Example: OPTIONS request and response for specific resource</preamble>
          <artwork>

    [request]

    OPTIONS /example/buddies.xml HTTP/1.1
    Host: www.example.com

    [response]

    HTTP/1.1 200 OK
    Allow: GET, PUT, POST, OPTIONS, HEAD, TRACE, DELETE, PATCH
    Accept-Patch: example/xcap+xml, application/gdiff

          </artwork>
        </figure>
        </t>
        <t>
        The examples show a server that supports PATCH generally, with two formats
        supported (one of them is fictional).  On some resources, for example on
        XML files, different kinds of patch formats more appropriate to the
        resource may be supported.
        </t>
        
      </section>
    </section>
    <section title="Interdependencies with other Standards">
      <section title="PATCH and Access Control (RFC3744)">
      
  <t>If the server supports <xref target="RFC3744">
   WebDAV Access Control</xref>, then the PATCH request SHOULD be subject to the same
  access control permissions as the PUT request.</t>
      </section>
      <section title="PATCH and Instance Manipulations (RFC3230)">

	<t>A patch document is modelled as an instance being sent to the server, 
	  following the model of <xref target="RFC3230">RFC3230</xref>. Thus, if the server supports
	    instance manipulations, the client MAY apply a supported manipulation to 
	    the patch document after it is generated (for 
	    example, a compression algorithm could be applied to the patch document). 
	  On the receiving end, the server 
	MUST undo the instance manipulation then apply the resulting document as
	a patch.  </t>      
      </section>
      
    </section>
    
    <section title="IANA Considerations">
   <t>This document uses URNs to describe XML namespaces and XML schemas
   conforming to a registry mechanism described in [RFC3688].</t>

   <t>Registration request for the patch namespace:</t>

   <t>URI: urn:ietf:params:xml:ns:patch</t>

   <t>Registrant Contact: See the "Author's Address" section of this
   document.</t>

   <t>XML: None.  Namespace URIs do not represent an XML specification.</t>


    </section>
    
  
    <section title="Security Considerations">
      <t>
      The security considerations for PATCH are nearly identical to the security considerations for 
      PUT.  In addition, one might be concerned that a document that is patched might
      be more likely to be corrupted, but that concern is addressed through use of MD5
      digests.
        </t>
    </section>
    
  
  </middle>
  
  <back>

    <references title="Normative References">
      &rfc2046;
      &rfc2616;
      <reference anchor="refs.W3C-GDIFF" target="http://www.w3.org/TR/NOTE-gdiff-19970901">
        <front>
          <title>Generic Diff Format Specification</title>
            <author initials="A." surname="van Hoff" fullname="Arthur van Hoff">
              <organization abbrev="Marimba">Marimba</organization>
            </author>
            <author initials="J." surname="Payne" fullname="Jonathan Payne">
              <organization abbrev="Marimba">Marimba</organization>
            </author>
            <date month="August" year="1997" />
        </front>
            <seriesInfo name="W3C" value="NOTE-gdiff-19970901" />
        
      </reference>
    </references>
    <references title="Non-Normative References">
      &rfc2518; 
      &rfc3229;
      &rfc3230;
      &rfc3253;
      &rfc3744;

    </references>
    <section title="Acknowledgements">
      <t>PATCH is not a new concept, it first appeared in HTTP in drafts of 
      version 1.1 written by Roy Fielding and Henrik Frystyk.</t>
      <t>Thanks to Adam Roach, Chris Sharp, Julian Reschke, Geoff Clemm,
      Scott Lawrence, Jeffrey Mogul, Roy Fielding, Greg Stein, Jim Luther, 
      Alex Rousskov, Jamie Lokier and Joe Hildebrand for review and advice on this document.</t>
    </section>
    <section title="Changes">
      <section title="Changes from -00">
        <t>OPTIONS support: removed "Patch" header definition and used
        Allow and new "Accept-Patch" headers instead.  </t>
        <t>Supported patch formats: removed vcdiff and diffe as these
        do not have defined MIME types and did not seem to be strongly 
        desired.</t>
        <t>PATCH method definition: Clarified cache behavior.</t>
      </section>
      <section title="Changes from -01">
        <t>Removed references to XCAP - not yet a RFC.</t>
        <t>Fixed use of MIME types (this "fix" now obsolete) </t>
        <t>Explained how to use MOVE or COPY in conjunction with PATCH,
        to create a new resource based on an existing resource
        in a different location. </t>
      </section>
      <section title="Changes from -02">
        <t>Clarified that MOVE and COPY are really independent of PATCH.</t>
        <t>Clarified when an ETag must change, and when Last-Modified must be used.</t>
        <t>Clarified what server should do if both Content-Type and IM headers appear 
        in PATCH request.</t>
        <t>Filled in missing reference to DeltaV and ACL RFCs.</t>
        <t>Stopped using 501 Unsupported for unsupported patch formats.</t>
        <t>Clarified what a static resource is.</t>
        <t>Refixed use of MIME types for patch formats.</t>
        <t>Limited the scope of some restrictions to apply only to 'gdiff' usage.</t>
      </section>
      <section title="Changes from -03">
        <t>Various typographical, terminology consistency, and other minor
        clarifications or fixes.</t>
      </section>
      <section title="Changes from -04">
        <t>Moved paragraphs on ACL and RFC3230 interoperability to new section.</t>
        <t>Added security considerations.</t>
        <t>Added IANA considerations, registration of new namespace, and discontinued use
        of "DAV:" namespace for new elements. </t>
        <t>Added example of error response.</t>
      </section>
      
    </section>
  </back>

</rfc>

