<?xml version="1.0" encoding="US-ASCII"?>
<?xml-stylesheet type='text/xsl' href='rfc2629.xslt' ?>
<?rfc toc="yes"?>
<?rfc strict="no"?>
<?rfc symrefs="yes" ?>
<?rfc sortrefs="yes"?>
<?rfc rfcedstyle="yes"?>
<?rfc comments="yes"?>
<?rfc inline="yes"?>
<?rfc-ext html-pretty-print="prettyprint https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"?>

<rfc number="5789" ipr="trust200902" category="std" xmlns:x='http://purl.org/net/xml2rfc/ext'>
<front>
  <title abbrev="HTTP PATCH">
    PATCH Method for HTTP
  </title>

  <author initials="L.M." surname="Dusseault" fullname="Lisa Dusseault">
    <organization abbrev="Linden Lab">Linden Lab</organization>
    <address>
      <postal>
        <street>945 Battery Street</street>
        <city>San Francisco</city> <region>CA</region>

        <code>94111</code>
        <country>USA</country>
      </postal>
      <email>lisa.dusseault@gmail.com</email>
    </address>
  </author>

  <author initials="J.M." surname="Snell" fullname="James M. Snell">
    <address>
      <email>jasnell@gmail.com</email>
      <uri>http://www.snellspace.com</uri>
    </address>
  </author>

  <date month="March" year="2010"/>

  <area>Applications</area>
  <keyword>http</keyword>
  <keyword>patch</keyword>

  <abstract>
    <t>Several applications extending the Hypertext Transfer Protocol (HTTP) 
	require a feature to do 
    partial resource modification.  The existing HTTP PUT method 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, which is used to apply partial modifications to a 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 cannot 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. POST is already used but without broad interoperability (for
    one, there is no standard way to discover patch format support).  
    PATCH was mentioned in earlier HTTP specifications, but 
	not completely defined.</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>

    <t>Furthermore, this document uses the ABNF syntax defined in
    <xref target="RFC2616" x:fmt="of" x:sec="2.1"/>.

    </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. If the Request-URI does not 
       point to an existing resource, the server MAY create a 
		new resource, depending on the patch document type (whether it can 
		logically modify a null resource) and permissions, etc.  </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 the 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 PATCH method affects  
	the resource identified by the Request-URI, and it also MAY have side effects on 
	other resources; i.e., new resources may be created, or existing ones modified, 
	by the application of a PATCH.    </t>

   <t>	PATCH is neither safe nor idempotent as defined 
       by <xref target="RFC2616" x:fmt="," x:sec="9.1"/>.</t>

   <t>A PATCH request can be issued in such a way as to be idempotent, which also 
	helps prevent bad outcomes from collisions between two PATCH requests on the 
	same resource in a similar time frame.  Collisions from multiple PATCH requests 
	may be more dangerous than PUT collisions 
       because some patch formats need to operate from a known base-point or else 
       they will corrupt the resource. 

 Clients using this kind of patch application SHOULD use a conditional
  request such that the request will fail if the resource has been
  updated since the client last accessed the resource.  For example,
  the client can use a strong ETag <xref target="RFC2616"/> in an If-Match header
  on the PATCH request.   </t>

    <t>There are also cases where patch formats do not need to operate from a known
	   base-point (e.g., appending text lines to log files, or non-colliding rows to database 
	   tables), in which case the same care in client requests is not needed.</t>

    <t>The server MUST 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 NOT apply any of the changes. 
       The determination of what constitutes a successful PATCH can vary depending 
       on the patch document and the type of resource(s) being modified. For example, 
	   the common 'diff' utility can generate a patch document that applies to multiple
	files in a directory hierarchy.  The atomicity requirement holds for all directly
	affected files.  See "Error Handling", <xref target="error.handling"/>, 
       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. A response to this method is only cacheable if it contains
 	explicit freshness information (such as an Expires header or "Cache-Control: max-age" directive)
	as well as the Content-Location header matching the Request-URI, indicating that the PATCH
	response body is a resource representation.
	A cached PATCH response can only be used to respond to subsequent GET and HEAD requests; it MUST
	NOT be used to respond to other methods (in particular, PATCH).  </t>

    <t>Note that entity-headers contained in the request apply only to the contained 
       patch document and MUST NOT be applied to the resource being modified.  Thus, a 
		Content-Language header could be present on the request, but it would only mean
		(for whatever that's worth) that the patch document had a language. Servers 
		SHOULD NOT store such headers except as trace information, and SHOULD NOT use
		such header values the same way they might be used on PUT requests.  Therefore,
		this document does not specify a way to modify a document's Content-Type or 
		Content-Language value through headers, though a mechanism could well be 
		designed to achieve this goal through a patch document. </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>

    <t>
	  Clients need to choose when to use
	  PATCH rather than PUT. For example, if the patch document size is
	  larger than the size of the new resource data that would be used in a
	  PUT, then it might make sense to use PUT instead of PATCH.  A comparison
	to POST is even more difficult, because POST is used in widely varying ways
	and can encompass PUT and PATCH-like operations if the server chooses.  
	If the operation does not modify the resource identified by the Request-URI in a 
	predictable way, POST should be considered instead of PATCH or PUT.</t>
    <section title="A Simple PATCH Example">

      <figure><artwork type='message/http; msgtype="request"'>
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 resource.
	</t>

      <figure>
	
        <preamble>Successful PATCH response to existing text file:</preamble>
          <artwork type='message/http; msgtype="response"'>
HTTP/1.1 204 No Content
Content-Location: /file.txt
ETag: "e0023aa4f"
</artwork>
        </figure>

<t>The 204 response code is used because the response does not
 carry a message body (which a response with the 200 code would have).
 Note that other success codes could be used as well.</t>

<t>Furthermore, the ETag response header field contains the ETag for
 the entity created by applying the PATCH, available at
 http://www.example.com/file.txt, as indicated by the Content-Location
 response header field.</t>

      </section>

      <section title="Error Handling" anchor="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:"> When the server determines that the 
	patch document provided by the client is not properly formatted, it SHOULD return
	a 400 (Bad Request) response.  The definition of badly formatted depends on the 
	patch document chosen.</t> 

        <t hangText="Unsupported patch document:">  Can be specified using a 415 (Unsupported
        Media Type) response when the client sends a patch document format that the server
        does not support for the resource identified by the Request-URI. 
        Such a response SHOULD include an Accept-Patch response header as 
        described in <xref target="accept-patch"/> to notify the client what patch document media types 
        are supported.</t>

        <t hangText="Unprocessable request:"> Can be specified with a 422 (Unprocessable
        Entity) response (<xref target="RFC4918" x:fmt="," x:sec="11.2"/>) when the server understands the patch 
        document and the syntax of the patch document appears to be valid, but the server 
        is incapable of processing the request. This might include attempts 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.  There may also be more specific errors like "Conflicting State"
		that could be signaled with this status code, but the more specific error would 
		generally be more helpful. </t>

		<t hangText="Resource not found:">  Can be specified with a 404 (Not Found)
			status code when the client attempted to apply a patch document to a 
			non-existent resource, but the patch document chosen cannot be 
          applied to a non-existent resource. </t>

		<t hangText = "Conflicting state:">  Can be specified with a 409 (Conflict) status code  when 
			the request cannot be applied given the state of the resource.  For example,
			if the client attempted to apply a structural modification and 
          the structures assumed to exist did not exist (with XML, a patch  
          might specify changing element 'foo' to element 'bar' but 
          element 'foo' might not exist). </t>

        <t hangText="Conflicting modification:"> When a client uses either the 
			If-Match or If-Unmodified-Since header to define a precondition, and that
			precondition failed, then the 
			412 (Precondition Failed) error is most helpful to the client.  However,
			that response makes no sense if there was no precondition on the request.
			In cases when the server detects a possible conflicting 
        modification and no precondition was defined in the request, 
		the server can return a 409 (Conflict) response.</t>

        <t hangText="Concurrent modification:"> Some applications of PATCH might require 
	    the server to process requests in the order in which they are received.
	    If a server is operating under those restrictions, and it receives concurrent 
	    requests to modify the same resource, but is unable to queue those requests, the
	    server can usefully indicate this error by using a 409 (Conflict) response.  </t>
	</list></t>
	
	   <t>Note that the 409 Conflict response gives reasonably consistent information
		 to clients.  Depending on the application and the nature of the patch format, 
		the client might be able to reissue the request as is (e.g., an instruction to 
		append a line to a log file), have to retrieve the resource content
		to recalculate a patch, or have to fail the operation.	</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>

  <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.  The PATCH method MAY appear in the "Allow" 
		header even if the Accept-Patch header is absent, in which case the list
		of allowed patch documents is not advertised.</t>

      <section anchor="accept-patch" title="The Accept-Patch Header">

      <t>This specification introduces a new response header Accept-Patch
      used to specify the patch document formats accepted by the server.
      Accept-Patch SHOULD 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.  The presence of a specific
	patch document format in this header indicates that that specific format is 
	allowed on the resource identified by the Request-URI.</t>

      <figure><artwork type="abnf2616">
Accept-Patch = "Accept-Patch" ":" 1#media-type </artwork></figure>

      <t>The Accept-Patch header specifies a comma-separated listing of media-types (with optional parameters)
      as defined by <xref target="RFC2616" x:fmt="," x:sec="3.7"/>.</t>

<t>Example:</t>

<t>Accept-Patch: text/example;charset=utf-8</t>

      </section>

      <section title="Example OPTIONS Request and Response">

      <figure>
      <preamble>[request]</preamble>
      <artwork type='message/http; msgtype="request"'>
OPTIONS /example/buddies.xml HTTP/1.1
Host: www.example.com
</artwork>
</figure>
      <figure>
      <preamble>[response]</preamble>
      <artwork type='message/http; msgtype="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 document formats.</t>

    </section>
  </section>

  <section title="IANA Considerations">

    <section title="The Accept-Patch Response Header">

      <t>The Accept-Patch response header has been added to the permanent 
      registry (see <xref target="RFC3864" />).</t>

  <t><list style="hanging">
    <t hangText="Header field name:">
      Accept-Patch
    </t>
    <t hangText="Applicable Protocol:">
      HTTP
    </t>
    <t hangText="Author/Change controller:">
      IETF
    </t>
    <t hangText="Specification document:">
      this specification
    </t>
  </list></t>

    </section>


  </section>


  <section title="Security Considerations">
    <t>The security considerations for PATCH are nearly identical to the 
    security considerations for PUT (<xref target="RFC2616" x:fmt="," x:sec="9.6"/>).  These 
    include authorizing requests (possibly through access control and/or authentication)
    and ensuring that data is not corrupted through transport errors or through
    accidental overwrites.  Whatever mechanisms are used for PUT can be used for PATCH
    as well.  The following considerations apply especially to PATCH.</t>

    <t>A document that is patched might be more likely to be corrupted than a document
	that is overridden in entirety, but that 
    concern can be addressed through the use of mechanisms such as conditional
    requests using ETags and the If-Match request header as described in <xref 
    target="patch"/>.    If a PATCH request fails, the client can issue a GET request
    to the resource to see what state it is in.  In some cases, the client might be able
    to check the contents of the resource to see if the PATCH request can be resent, 
    but in other cases, the attempt will just fail and/or a user will have to verify intent.
    In the case of a failure of the underlying transport channel, where a PATCH 
    response is not received before the channel fails or some other timeout happens, 
    the client might have to issue a GET request to see whether the request was applied.
    The client might want to ensure that the GET request bypasses caches using mechanisms
    described in HTTP specifications (see, for example, <xref target="RFC2616" x:fmt="of" x:sec="13.1.6"/>).
    </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.  Servers MUST take adequate precautions to ensure that malicious
	  clients cannot consume excessive server resources (e.g., CPU, disk I/O)
	  through the client's use of PATCH.</t>
  </section>

</middle>

<back>

  <references title="Normative References">

<reference anchor='RFC2119'>

<front>
<title abbrev='RFC Key Words'>Key words for use in RFCs to Indicate Requirement Levels</title>
<author initials='S.' surname='Bradner' fullname='Scott Bradner'>
<organization>Harvard University</organization>
<address>
<postal>
<street>1350 Mass. Ave.</street>
<street>Cambridge</street>
<street>MA 02138</street></postal>
<phone>- +1 617 495 3864</phone>
<email>sob@harvard.edu</email></address></author>
<date year='1997' month='March' />
</front>
<seriesInfo name='BCP' value='14' />
<seriesInfo name='RFC' value='2119' />
</reference>

<reference anchor='RFC2616'>
<front>
<title abbrev='HTTP/1.1'>Hypertext Transfer Protocol -- HTTP/1.1</title>
<author initials='R.' surname='Fielding' fullname='Roy T. Fielding'>
<organization abbrev='UC Irvine'>Department of Information and Computer Science</organization>
<address>
<postal>
<street>University of California, Irvine</street>
<city>Irvine</city>
<region>CA</region>
<code>92697-3425</code></postal>
<facsimile>+1(949)824-1715</facsimile>
<email>fielding@ics.uci.edu</email></address></author>
<author initials='J.' surname='Gettys' fullname='James Gettys'>
<organization abbrev='Compaq/W3C'>World Wide Web Consortium</organization>
<address>
<postal>
<street>MIT Laboratory for Computer Science, NE43-356</street>
<street>545 Technology Square</street>
<city>Cambridge</city>
<region>MA</region>
<code>02139</code></postal>
<facsimile>+1(617)258-8682</facsimile>
<email>jg@w3.org</email></address></author>
<author initials='J.' surname='Mogul' fullname='Jeffrey C. Mogul'>
<organization abbrev='Compaq'>Compaq Computer Corporation</organization>
<address>
<postal>
<street>Western Research Laboratory</street>
<street>250 University Avenue</street>
<city>Palo Alto</city>
<region>CA</region>
<code>94305</code></postal>
<email>mogul@wrl.dec.com</email></address></author>
<author initials='H.' surname='Frystyk' fullname='Henrik Frystyk Nielsen'>
<organization abbrev='MIT/LCS'>World Wide Web Consortium</organization>
<address>
<postal>
<street>MIT Laboratory for Computer Science, NE43-356</street>
<street>545 Technology Square</street>
<city>Cambridge</city>
<region>MA</region>
<code>02139</code></postal>
<facsimile>+1(617)258-8682</facsimile>
<email>frystyk@w3.org</email></address></author>
<author initials='L.' surname='Masinter' fullname='Larry Masinter'>
<organization abbrev='Xerox'>Xerox Corporation</organization>
<address>
<postal>
<street>MIT Laboratory for Computer Science, NE43-356</street>
<street>3333 Coyote Hill Road</street>
<city>Palo Alto</city>
<region>CA</region>
<code>94034</code></postal>
<email>masinter@parc.xerox.com</email></address></author>
<author initials='P.' surname='Leach' fullname='Paul J. Leach'>
<organization abbrev='Microsoft'>Microsoft Corporation</organization>
<address>
<postal>
<street>1 Microsoft Way</street>
<city>Redmond</city>
<region>WA</region>
<code>98052</code></postal>
<email>paulle@microsoft.com</email></address></author>
<author initials='T.' surname='Berners-Lee' fullname='Tim Berners-Lee'>
<organization abbrev='MIT/LCS'>World Wide Web Consortium</organization>
<address>
<postal>
<street>MIT Laboratory for Computer Science, NE43-356</street>
<street>545 Technology Square</street>
<city>Cambridge</city>
<region>MA</region>
<code>02139</code></postal>
<facsimile>+1(617)258-8682</facsimile>
<email>timbl@w3.org</email></address></author>
<date year='1999' month='June' />
</front>
<seriesInfo name='RFC' value='2616' />
</reference>
<reference anchor='RFC3864'>
<front>
<title>Registration Procedures for Message Header Fields</title>
<author initials='G.' surname='Klyne' fullname='G. Klyne'>
<organization /></author>
<author initials='M.' surname='Nottingham' fullname='M. Nottingham'>
<organization /></author>
<author initials='J.' surname='Mogul' fullname='J. Mogul'>
<organization /></author>
<date year='2004' month='September' />
</front>
<seriesInfo name='BCP' value='90' />
<seriesInfo name='RFC' value='3864' />
</reference>
</references>

  <references title="Informative References">
<reference anchor='RFC4918'>
<front>
<title>HTTP Extensions for Web Distributed Authoring and Versioning (WebDAV)</title>
<author initials='L.' surname='Dusseault' fullname='L. Dusseault'>
<organization /></author>
<date year='2007' month='June' />
</front>
<seriesInfo name='RFC' value='4918' />
</reference>
</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 and also 
    appears in Section 19.6.1.1 of RFC 2068.</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, Michael
   Balloni, Cyrus Daboo, Brian Carpenter, John Klensin, Eliot Lear, SM,
   and Bernie Hoeneisen for review and advice on this document.  In 
   particular, Julian Reschke did repeated reviews, made many useful suggestions,
   and was critical to the publication of this document. </t>

  </section>
 
</back>

</rfc>
