<?xml version="1.0"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
  <!ENTITY rfc2616 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.2616.xml'>
  <!ENTITY rfc3284 PUBLIC '' 'http://xml.resource.org/public/rfc/bibxml/reference.RFC.3284.xml'>
]>
<?rfc toc="yes"?>
<?rfc strict="yes"?>
<rfc ipr="full3978" docName="draft-dusseault-http-patch-07">
  <front>
    <title abbrev="HTTP PATCH">
      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>
    
    <author initials="J.M." surname="Snell" fullname="James M Snell">
      <organization></organization>
      <address>
        <postal>
          <street></street>
          <city></city> <region></region> <code></code>
          <country></country>
        </postal>
        <phone></phone>
        <email>jasnell@gmail.com</email>
        <uri>http://www.snellspace.com</uri>
      </address>
    </author>
    
    <date month="June" year="2007"  day="22"/>

    <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>
    <keyword>delta</keyword>
    <keyword>instance</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>This specification defines a new HTTP 1.1 <xref target="RFC2616" /> method 
      PATCH that is used to apply partial modifications to a HTTP resource.  
      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) as defined in RFC2616.   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 delta encodings) 
      was desired, as well as a method that would not confuse caching proxies.</t>

    </section>
        
    <section title="Mechanisms" anchor="mech" >
      <section title="PATCH Method">

        <t>The PATCH method requests that a set of changes described in 
        the request entity be applied to the resource identified by the Request-URI.
        The set of changes is represented in a format called a "delta encoding"
        identified by a media type and MUST include sufficient information to 
        allow the server to recreate the changes necessary to convert 
        the original version of the resource into the desired version.
        The server MUST NOT create a new resource with the contents of the 
        request body, although it MAY (depending on the delta encoding) apply the 
        request body to an empty resource. The recipient of the entity MUST NOT 
        ignore any Content-* (e.g. Content-Range) headers that it does not understand 
        or implement and MUST return a 501 (Not Implemented) response in such cases.</t>
        
        <t>The server SHOULD always 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 delta encoding 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>The actual method for determining how to apply the delta encoding to
        the resource is defined entirely by the origin server.</t>
      
        <t>If the request passes through a cache and the Request-URI identifies
        one or more currently cached entities, those entries SHOULD be
        treated as stale. Responses to this method are not cacheable.</t>
        
        <t>Collisions from multiple requests are more dangerous than PUT collisions, 
        because a delta encoding that is not operating from a known base point may 
        corrupt the resource.  Therefore, the client MUST verify that it is applying 
        the delta encoding to a known entity by first acquiring the strong ETag of 
        the resource to be modified, and using that Etag in the If-Match header on 
        the PATCH request to make sure the resource is still unchanged.  If a
        strong ETag is not available for a given resource, the client MUST use
        If-Unmodified-Since as a less-reliable safeguard.</t>
        
        <t>Servers SHOULD provide strong ETags for all resources for which the 
        PATCH method is supported.</t>
        
        <t>Servers advertise the types of delta encoding documents supported
        for PATCH, and clients specify which one they're using by including its 
        media type in the request using the Content-Type request header.</t>
                
        <figure anchor="PATCH_example">
          <preamble>Simple PATCH example</preamble>

          <artwork>

    PATCH /file.txt HTTP/1.1
    Host: www.example.com
    Content-type: application/delta
    If-Match: "e0023aa4e"
    Content-Length: 100

    [description of changes]

          </artwork>
        </figure>
        <t>This example illustrates use of a hypothetical delta encoding on an existing text file.</t>
    
      </section>
      <section title="PATCH Response">
        <section title="Success Response">

          <t>A response with a 2xx status code indicates that the
          PATCH request was a success. The server MAY include a representation 
          of the modified resource in the response and MAY include appropriate Content-* 
          headers to allow 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 200 OK
    ETag: "e0023aa4f"
    Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
    Content-Type: text/plain
    
    [modified resource]

            </artwork>
          </figure>
        </section>

        <section title="Error handling">
        
          <t>There are several known conditions under which a PATCH request can fail.</t>
          
          <t><list style="hanging">
            <t></t>
            <t hangText="Malformed Delta Encoding:">  Specified using a 400 Bad Request
            when the server finds that the delta encoding provided by the client
            was badly formatted or non-compliant.  The definition of badly formatted 
            or non-compliant depends on the delta encoding 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 hangText="Unsupported Delta Encoding:">  Specified using a 415 Unsupported
            Media Type when the client sends a delta encoding that the server
            doesn't support for the resource identified by the Request-URI. 
            Such a response SHOULD include an Accept-Patch response header as 
            described in Section 2.3 to notify the client what delta encoding formats 
            are supported.</t>
            
            <t hangText="Patch Conflict:">  Specified with a 409 Conflict when 
            the server understands the delta encoding and the delta encoding 
            looks valid, but it cannot be applied to the resource.  There are 
            a number of ways the resource could conflict with the delta encoding, 
            for example:
            
            <list style="symbols">
              <t>The client attempted to apply a delta encoding to an empty 
              file, but the delta encoding chosen cannot be applied to an 
              empty file. </t>
              <t>The client attempted to apply a structural delta algorithm and 
              the structures assumed to exist didn't exist (e.g. an XML delta 
              which specifies changing element 'foo' to element 'bar' but 
              element 'foo' doesn't exist).</t>
            </list>
            </t>
            
            <t hangText="Concurrent modification:"> Specified with a 
            412 Precondition Failed when a client attempts to apply a delta 
            encoding to a resource whose state has changed since the delta 
            encoding was created.</t>

            <t hangText="Invalid Result:">  Specified with  a 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.</t>
          </list></t>
 
          <t>Other status codes MAY also be used under the appropriate circumstances. 
          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>
          
          <t>The entity body of error responses SHOULD contain enough information
          to communicate the nature of the error to the client. The content-type 
          of the response entity can vary across implementations. XML error 
          responses as defined by [RFC2518bis] MAY be used.</t>
          
        <section title="Example error response with body detail">
          
        <t>
        <figure>
          <artwork>
    HTTP/1.1 409 Conflict
    Content-Type: text/plain; charset="utf-8"
    Content-Length: xxx
            
    Invalid result
          </artwork>
        </figure>
        </t>            
        </section>
          
      
      </section>
        
      </section>

      <section anchor="adv" title="Advertising Support in OPTIONS">
        
        <t>The server advertises its support for the PATCH method 
        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 specific delta encoding
        formats, so this document introduces a new response header "Accept-Patch"
        used to specify the delta encoding formats accepted by the server.
        "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 delta encodings for all types of resources.</t>

        <t>Accept-Patch = "Accept-Patch" ":" #( media-range )</t>

        <t>The Accept-Patch header specifies a listing of media ranges as defined
        by RFC2616 Section 14.1. Note that, unlike the HTTP Accept request header, 
        the Accept-Patch header does not use quality factors.</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: application/diff, application/diff+xml

          </artwork>
        </figure>
        </t>

        <t>The examples show a server that supports PATCH generally using two 
        hypothetical delta encodings.</t>
        
      </section>
            
    </section>

      <section title="Delta Encodings">
       
      <t>There is no guarantee that a resource can be modified with PATCH. 
      Further, it is expected that different delta encodings will be appropriate for 
      different types of resources and that no single delta encoding will be 
      appropriate for all types of resources. Therefore, there is no single default 
      delta encoding that implementations are required to support. Servers 
      MUST ensure that a received delta encoding is appropriate for the type
      of resource identified by the Request-URI.</t>
      
      <t>Byte-based or binary delta encodings are useful for many types of 
      resources as long as the server stores resources identically to 
      the way they're presented on the wire (or can behave as if it does).</t>
      
      <t>Character-based delta encodings operate on a variable number of bytes 
      depending on the length of each character, thus correct use of these algorithms 
      depends on the encoding of the resource. Such delta encodings MUST either use
      the same character set encoding as the resource being modified or MUST 
      produce an otherwise valid result.  The validity of the result is dependent 
      on the type of resource being modified.</t>
      
      <t>Structure-based delta encodings allow changes to be applied independent
      of exact formats or canonicalizations. For example, a delta encoding format 
      targeted at the modification of XML-based resources may allow for the insertion 
      or deletion of elements and attributes without concern for the exact 
      serialization of those in the modified resource.</t>
      
      </section>

    <section title="IANA Considerations">

      <t>This document does not specify any actions for IANA.</t>

      <!-- section title="The 'application/vcdiff' media type">
      
      <t>The "application/vcdiff" MIME media type is used to identify the "vcdiff"
      delta encoding defined by <xref target="RFC3284" />.</t>
      
      <figure>
      <artwork>
  MIME media type name: application
  MIME subtype name: vcdiff
  Mandatory parameters: None
  Optional parameters: None
  Encoding considerations:  The vcdiff format is encoded in a 
     portable binary format described by RFC3284.
  Security considerations:  As defined in RFC3284.
  Interoperability considerations:  There are no known 
     interoperability issues.
  Published specification:  RFC3284.
  Applications that use this media type:  No known applications
    currently use this media type.

  Additional information:

  File extension:  .vcdiff
  Fragment identifiers:  None
  Person and email address to contact for further information:  
      James Snell &lt;jasnell@gmail.com&gt;
  Intended usage:  COMMON
  Author/Change controller:  IESG      
      </artwork>
      </figure>
      </section-->

    </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 can be addressed through the use of mechanisms such as conditional
      requests using ETags and the If-Match request header.</t>
      
      <t>Sometimes an HTTP intermediary might try to detect viruses being sent via HTTP by 
      checking the body of the PUT/POST request or GET response.  The PATCH method 
      complicates such watch-keeping because neither the source document nor the patch
      document might be a virus, yet the result could be. This security consideration is
      not materially different from those already introduced by byte-range downloads, 
      downloading patch documents, uploading zipped (compressed) files and so on.</t>
      
      <t>Individual delta encodings will have their own specific security considerations
      that will likely vary depending on the types of resources being patched.
      The considerations for patched binary resources, for instance, will be different 
      than those for patched XML documents.</t>
    </section>
    
  </middle>
  
  <back>

    <references title="Normative References">
      &rfc2616;
    </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, Joe Hildebrand, Mark Nottingham and 
        Michael Balloni
      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 delta encodings: 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 delta encodings.</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 usage
        of required diff format.</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 RFC3229 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 title="Changes from -05">
        <t>Due to various concerns it didn't seem likely the application/gdiff
          registration could go through so switching to vcdiff as required 
        diff format, and to RFC3229's approach to specifying diff formats, including
        use of the IM header.</t>
        <t>Clarified what header server MUST use to return MD5 hash.</t>

        <t>Reverted to  using 501 Unsupported for unsupported delta encodings.</t>
      </section>
      
      <section title="Changes from -06">
        <t>The reliance on RFC 3229 defined delta encodings has been factored
        out in favor of delta encodings identified by MIME media type.</t>
        <t>The required use of DeltaV-based error reporting has been removed
        in favor of using basic HTTP status codes to report error conditions.</t>
        <t>The Accept-Patch response header has been redefined as a listing of
        media-ranges with quality factors, similar to the Accept request header.</t>
        <t>Added James Snell as a co-author.</t>
      </section>
            
    </section>
      
    <section title="Notes to RFC Editor">
      <t>The RFC Editor should remove this section and the Changes section.</t>
    </section>
  </back>

</rfc>

