<?xml version="1.0"?>
<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
  <!ENTITY rfc2616 PUBLIC '' 'bibxml/reference.RFC.2616.xml'>
  <!ENTITY rfc4918 PUBLIC '' 'bibxml/reference.RFC.4918.xml'>
  <!ENTITY rfc2119 PUBLIC '' 'bibxml/reference.RFC.2119.xml'>
  <!ENTITY rfc3864 PUBLIC '' 'bibxml/reference.RFC.3864.xml'>
]>
<?rfc toc="yes"?>
<?rfc strict="yes"?>
<?rfc symrefs="yes" ?>
<?rfc sortrefs="yes"?>
<?rfc compact="yes"?>
<rfc ipr="full3978" docName="draft-dusseault-http-patch-09">
  <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>https://umn0mtkzgjqkdk5mvr8xqd8.julianrbryant.com</uri>
      </address>
    </author>
    
    <date month="August" year="2007"  day="19"/>

    <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 the new HTTP 1.1 <xref target="RFC2616" /> method 
      PATCH that is used to apply partial modifications to a HTTP resource.</t>
        
      <t>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 can 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>In this document, the key words "MUST", "MUST NOT", "REQUIRED", "SHALL", 
      "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" 
      are to be  interpreted as described in <xref target="RFC2119" />.</t>

    </section>
        
    <section title="The PATCH Method" anchor="patch" >

      <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 "patch document"
         identified by a media type. PATCH is neither safe or idempotent as defined 
         by <xref target="RFC2616" /> Section 9.1.</t>
        
      <t>The difference between the PUT and PATCH requests is reflected in the
         way the server processes the enclosed entity to modify the resource 
         identified by the Request-URI.  In a PUT request, the enclosed entity
         is considered to be a modified version of the resource stored on the 
         origin server and the client is requesting that stored version be replaced.
         With PATCH, however, the enclosed entity contains a set of instructions 
         describing how a resource currently residing on the origin server should
         be modified to produce a new version.  The changes described by the 
         entity MAY result in the creation of one or more new resources on the 
         server, however it is not intended that the body of the PATCH request 
         be used as the content of such resources.</t>
        
      <t>The server MUST always apply the entire set of changes atomically and never
         provide (e.g. in response to a GET during this operation) a partially-modified 
         representation.  If the entire patch document cannot be successfully applied 
         then the server MUST fail the entire request, applying none of the changes. 
         The determination of what constitutes a successful PATCH can vary depending 
         on the patch document and the type of resource being modified. The actual 
         method for determining how to apply the patch document to the resource is 
         defined entirely by the origin server. See Error Handling in section 2.2 
         for details on status codes and possible error conditions.</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, unless
         the response includes appropriate Cache-Control or Expires header fields
         or the response uses the 209 Content Returned status code as defined in 
         Section 3. The 303 (See Other) response can be used to direct the user 
         agent to retrieve a cacheable resource.</t>
         
        <t>Collisions from multiple requests are more dangerous than PUT collisions, 
        because a patch document that is not operating from a known base point may 
        corrupt the resource.  Clients wishing to apply a patch document to a known 
        entity can first acquire the strong ETag of the resource to be modified, 
        and use that Etag in the If-Match header on the PATCH request to verify 
        that the resource is still unchanged.  If a strong ETag is not available 
        for a given resource, the client can use If-Unmodified-Since as a 
        less-reliable safeguard.</t>
        
        <t>It is RECOMMENDED that Servers provide strong ETags for all resources 
        for which PATCH is supported.</t>
        
        <t>If a PATCH request contains any entity-headers the server does not 
        understand, the server MUST return a 501 (Not Implemented) response.  
        A server that understands a particular entity-header can choose to 
        ignore it; however, doing so can produce results that are unexpected or 
        unintended by the client. All entity-headers contained in the request 
        apply only to the contained patch document and MUST NOT be applied to
        the resource being modified.</t>
                
        <t>If the Request-URI identifies a resource with multiple alternate 
        representations, the server can choose to respond in a variety of ways.
        For instance, the server can decide which representation to alter 
        and might even be able to change them all consistently depending on 
        the patch format.  A particular patch document might be able to 
        identify specific representations to modify or might be capable of describing
        changes to multiple representations.  If the server cannot choose a 
        representation, it can reject the request with an error or the server 
        can choose to redirect the request (e.g. using 301 Moved Permanently or 
        302 Found), in which case the user agent makes its own decision regarding
        whether or not to proceed with the request.</t>
        
        <t>Clients are advised to take caution when sending multiple PATCH requests,
        or sequences of requests that include PATCH, over a pipelined connection 
        as there are no guarantees that pipelined requests will be processed by 
        the server in the same order in which the client sends them. Such sequences 
        of requests can be made safer by using conditional request mechanisms such 
        as If-Match. See <xref target="RFC2616" /> Section 8.1.2.2 for additional 
        details regarding pipelining and non-idempotent requests.</t>
                
        <t>There is no guarantee that a resource can be modified with PATCH. 
        Further, it is expected that different patch document formats will be 
        appropriate for different types of resources and that no single format 
        will be appropriate for all types of resources. Therefore, there is no single 
        default patch document format that implementations are required to support. 
        Servers MUST ensure that a received patch document is appropriate for the type
        of resource identified by the Request-URI.</t>
        
        <section title="A simple PATCH example">
        
        <figure><artwork>
    PATCH /file.txt HTTP/1.1
    Host: www.example.com
    Content-type: application/example
    If-Match: "e0023aa4e"
    Content-Length: 100

    [description of changes]

          </artwork>
        </figure>
        <t>This example illustrates use of a hypothetical patch document on an existing text file.</t>
        
        <figure>
          <preamble>Successful PATCH response to existing text file</preamble>
            <artwork>
    HTTP/1.1 204 No Content
    ETag: "e0023aa4f"
    Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
            </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 hangText="Malformed patch document:">  Can be specified using a 
          400 Bad Request when the server finds that the patch document provided 
          by the client was improperly formatted.  The definition of badly formatted 
          depends on the patch document chosen, but generally if the server finds it 
          cannot handle the patch due to the serialization of the patch document, 
          this response ought to be appropriate.</t> 
            
          <t hangText="Unsupported patch document:">  Specified using a 415 Unsupported
          Media Type when the client sends a patch document 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 patch document formats 
          are supported.</t>
            
          <t hangText="Unprocessable request:"> Can be specified with a 422 Unprocessable
          Entity <xref target="RFC4918" /> when the server understands the patch 
          document and the syntax of the patch document appears valid, but the server 
          is incapable of processing the request. There are a number of situations that 
          could lead to such a result, for example:
            
          <list style="symbols">
            <t>The client attempted to apply a patch document to an empty 
            resource, but the patch document chosen cannot be applied to an 
            empty resource. </t>
            <t>The client attempted to apply a structural modification and 
            the structures assumed to exist did not exist (e.g. a patch  
            which specifies changing element 'foo' to element 'bar' but 
            element 'foo' doesn't exist).</t>
            <t>The client attempted to modify a resource in a way that would
            cause the resource to become invalid. For instance, a modification 
            to a well-formed XML document that would cause it to no longer 
            be well-formed.</t>
            <t>The client attempted to modify a resource that has multiple 
            representations but the server was unable to choose which 
            representation to modify.</t>
          </list>
          </t>
          
          <t hangText="Conflicting modification:"> Specified with a 
          412 Precondition Failed when a client uses either the If-Match or
          If-Unmodified-Since request headers and attempts to apply a patch 
          document to a resource whose state has changed since the patch 
          was created. If the server detects a possible conflicting 
          modification and neither the If-Match or If-Unmodified-Since request
          headers are used, the server can return a 409 Conflict response.</t>
          
          <t hangText="Concurrent modification:"> When a server receives multiple
          concurrent requests to modify a resource, those requests SHOULD be
          queued and processed in the order in which they are received. If
          a server is incapable of queuing concurrent requests, all subsequent 
          requests SHOULD be rejected using a 409 Conflict response until the 
          first modification request is complete.</t>
         </list></t>
         
         <t>Other HTTP status codes can also be used under the appropriate circumstances. </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.</t>
                
      </section>

      <section anchor="adv" title="Advertising Support in OPTIONS">
        
        <t>A server can advertise its support for the PATCH method by adding
        it to the listing of allowed methods in the "Allow" OPTIONS response 
        header defined in HTTP/1.1.</t>
        
        <t>Clients also need to know whether the server supports specific patch document
        formats, so this specification introduces a new response header "Accept-Patch"
        used to specify the patch document formats accepted by the server.
        "Accept-Patch" MUST appear in the OPTIONS response for any resource 
        that supports the use of the PATCH method.  The presence of the "Accept-Patch"
        header in response to any method is an implicit indication that PATCH is allowed 
        on the resource identified by the Request-URI.</t>
        
        <t>Accept-Patch = "Accept-Patch" ":" #( media-range )</t>

        <t>The Accept-Patch header specifies a listing of media ranges as defined
        by <xref target="RFC2616"/>, Section 14.1. Note that, unlike the HTTP Accept request header, 
        the Accept-Patch header does not use quality factors.</t>
        
        <section title="An example OPTIONS request and response">
        
        <figure>
          <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, DELETE, PATCH
    Accept-Patch: application/example, text/example

          </artwork>
        </figure>

        <t>The examples show a server that supports PATCH generally using two 
        hypothetical patch documents.</t>
        
        </section>
      </section>
            
    </section>
    
    <section title="209 Content Returned">
    
      <t>The 209 "Content Returned" status code can be used to indicate
      that a response is equivalent to what would have been returned
      with a 200 status code response to a GET sent to the URI immediately 
      following the successful completion of the request.</t>
      
    </section>
    
    <section title="The Prefer Request Header">
    
      <t>The Prefer request-header is used to indicate that particular 
      server behaviors are preferred, but not required, by the client.  Prefer
      is similar in nature to the Expect header defined by <xref target="RFC2616" /> 
      with the exception that servers are allowed to ignore a clients stated 
      preference.</t>
      
      <figure><artwork>
  Prefer       =  "Prefer" ":" 1#preference

  preference  =  "no-content" | "content-returned" | 
                 preference-extension
  preference-extension =  token [ "=" ( token | quoted-string )
                          *prefer-params ]
  prefer-params =  ";" token [ "=" ( token | quoted-string ) ]
      </artwork></figure>
  
      <t>This header is defined with an extensible syntax to allow for future
      extensions. A server that does not understand or is unable to comply with 
      any of the preference values in the Prefer field of a request MUST ignore 
      those values and MUST NOT stop processing or signal an error.</t>
      
      <t>Comparison of preference values is case-insensitive for unquoted 
      tokens and is case-sensitive for quoted-string preference-extensions.</t>
      
      <t>The Prefer mechanism is hop-by-hop: that is, an HTTP proxy MAY 
      choose to honor a preference even if the origin server does not. However,
      the Prefer request-header itself is end-to-end; it MUST be forwarded if 
      the request is forwarded.</t>
      
      <section title="The &quot;no-content&quot; Preference">
      
      <t>The "no-content" preference token indicates that the client prefers
      that the server respond to a successful request using the 204 No Content
      status code as defined in Section 10.2.5 of <xref target="RFC2616" />.</t>
      
      </section>
      
      <section title="The &quot;content-returned&quot; Preference">
      
      <t>The "content-returned" preference token indicates that the client
      prefers that the server respond to a successful request using the 209 
      Content Returned status code as defined in Section 3.</t>
      
      </section>
      
      <section title="The 601 Preference Ignored Warn Code">
      
      <t>When a server chooses to ignore a preference value specified in the 
      Prefer header, it MAY include a Warning header field in the response 
      message using the "601 Preference Ignored" warn code.  The warning 
      text SHOULD specify the specific preference tokens that were ignored.</t>
      
      <t>For instance, the following example notifies the client that the 
      "content-returned" preference token was intentionally ignored by the 
      server.</t>
      
      <figure><artwork>
  HTTP/1.1 204 No Content
  Warning: 601 origin "content-returned" 
      </artwork></figure>
      
      </section>
      
      <section title="The 602 Preference Honored Warn Code">
      
      <t>When an HTTP proxy chooses to honor a preference even when the origin
      server does not, it SHOULD include a Warning header field in the response
      message using the "602 Preference Honored" warn code.  The warning text 
      SHOULD specify the specific preference tokens that were honored.</t>
      
      <t>For instance, the following example notifies the client that the 
      "no-content" preference token was intentionally ignored by the 
      origin server, however a proxy chose to honor the preference and return
      the prefered status code.</t>
      
      <figure><artwork>
  HTTP/1.1 204 No Content
  Warning: 601 origin "no-content" 
  Warning: 602 proxy "no-content"  
      </artwork></figure>
      
      <t>Use of the 602 warn-code is restricted to HTTP proxies and caches. 
      HTTP origin servers MUST NOT use the 602 warn-code.</t>
      
      </section>
    
    </section>

    <section title="IANA Considerations">

      <section title="The 'Accept-Patch' Response Header">
      
        <t>The 'Accept-Patch' response header should be added to the permanent 
        registry (see <xref target="RFC3864" />).</t>
        
    <t><figure><artwork>
    Header field name: Accept-Patch
    
    Applicable Protocol: HTTP
    
    Status: standard
    
    Author/Change controller: IETF
    
    Specification document: this specification
    </artwork></figure></t>
      
      </section>
      
      <section title="HTTP Status codes">
      
      <t>This specification defines the 209 Content Returned status code (Section 3) 
      to be updated in the registry at &lt;https://umn0mtkzgjpuem6gt32g.julianrbryant.com/assignments/http-status-codes&gt;.</t>
      
      </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 patch documents 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">
      &rfc2119;
      &rfc2616;
      &rfc3864;
    </references>
    
    <references title="Informative References">
      &rfc4918;
    </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 patch documents 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, similar to the Accept request header.</t>
        <t>Added James Snell as a co-author.</t>
      </section>
      
      <section title="Changes from -07">
        <t>Terminology change from "delta encoding" to "patch document"</t>
        <t>Added clarification on the safety and idempotency of PATCH</t>
        <t>Updated the caching rules of PATCH responses</t>
        <t>200 responses MUST include a representation of the modified resource. 
        204 responses are used to indicate successful response without returning a representation.</t>
        <t>Suggest using 422 Unprocessable Entity to indicate that a properly formatted patch document cannot be processed</t>
        <t>Clarify the use of 412 and 409 to indicate concurrent and conflicting resource modifications.</t>
        <t>Added registration for the Accept-Patch header.</t>
        <t>Relaxed the requirements for the use of If-Match and If-Unmodified-Since.</t>
        <t>Add language that clarifies the difference between PUT and PATCH.</t>
        <t>Add language that clarifies the issues with PATCH and Content Negotiation.</t>
        <t>Use of Accept-Patch on any response implies that PATCH is supported.</t>
        <t>Add language advising caution when pipelining PATCH requests.</t>
      </section>
      
      <section title="Changes from -08">
        <t>Addition of the 209 Content Returned status code</t>
        <t>Addition of the Prefer header field mechanism</t>
        <t>Removed the paragraph discussing the use of 200+Content-Location. This 
        is replaced by the 209 Content Returned status code.</t>
      </section>
            
    </section>
      
    <section title="Notes to RFC Editor">
      <t>The RFC Editor should remove this section and the Changes section.</t>
    </section>
    
    <section title="Editorial Notes">
      <t>There are several outstanding issues with the Prefer section:</t>
      <t><list style="symbols">
      <t>Should the Prefer section be separated out into a separate I-D?</t>
      <t>We need to determine how new preference codes are created/registered</t>
      <t>Are warn-codes ok or do we need a new response header?</t>
      </list></t>
    </section>
    
  </back>

</rfc>

