Skip to content
This repository
Browse code

BZ1020: Add conditional headers for PUT/POST with returnbody=true.

  • Loading branch information...
commit 58cb87f8c148ebb22f04eaf66a00e9990dfe42df 1 parent 7d19a9f
Sean Cribbs seancribbs authored

Showing 1 changed file with 17 additions and 3 deletions. Show diff stats Hide diff stats

  1. +17 3 src/riak_kv_wm_object.erl
20 src/riak_kv_wm_object.erl
@@ -605,7 +605,8 @@ accept_doc_body(RD, Ctx=#ctx{bucket=B, key=K, client=C, links=L, index_fields=IF
605 605 %% Handle the no-sibling case. Just send the object.
606 606 send_returnbody(RD, DocCtx, _HasSiblings = false) ->
607 607 {Body, DocRD, DocCtx2} = produce_doc_body(RD, DocCtx),
608   - {true, wrq:append_to_response_body(Body, DocRD), DocCtx2};
  608 + {DocRD2, DocCtx3} = add_conditional_headers(DocRD, DocCtx2),
  609 + {true, wrq:append_to_response_body(Body, DocRD2), DocCtx3};
609 610
610 611 %% Handle the sibling case. Send either the sibling message body, or a
611 612 %% multipart body, depending on what the client accepts.
@@ -614,12 +615,25 @@ send_returnbody(RD, DocCtx, _HasSiblings = true) ->
614 615 case webmachine_util:choose_media_type(["multipart/mixed", "text/plain"], AcceptHdr) of
615 616 "multipart/mixed" ->
616 617 {Body, DocRD, DocCtx2} = produce_multipart_body(RD, DocCtx),
617   - {true, wrq:append_to_response_body(Body, DocRD), DocCtx2};
  618 + {DocRD2, DocCtx3} = add_conditional_headers(DocRD, DocCtx2),
  619 + {true, wrq:append_to_response_body(Body, DocRD2), DocCtx3};
618 620 _ ->
619 621 {Body, DocRD, DocCtx2} = produce_sibling_message_body(RD, DocCtx),
620   - {true, wrq:append_to_response_body(Body, DocRD), DocCtx2}
  622 + {DocRD2, DocCtx3} = add_conditional_headers(DocRD, DocCtx2),
  623 + {true, wrq:append_to_response_body(Body, DocRD2), DocCtx3}
621 624 end.
622 625
  626 +%% Add ETag and Last-Modified headers to responses that might not
  627 +%% necessarily include them, specifically when the client requests
  628 +%% returnbody on a PUT or POST.
  629 +add_conditional_headers(RD, Ctx) ->
  630 + {ETag, RD2, Ctx2} = generate_etag(RD, Ctx),
  631 + {LM, RD3, Ctx3} = last_modified(RD2, Ctx2),
  632 + RD4 = wrq:set_resp_header("ETag", webmachine_util:quoted_string(ETag), RD3),
  633 + RD5 = wrq:set_resp_header("Last-Modified",
  634 + httpd_util:rfc1123_date(calendar:universal_time_to_local_time(LM)), RD4),
  635 + {RD5,Ctx3}.
  636 +
623 637 %% @spec extract_content_type(reqdata()) ->
624 638 %% {ContentType::string(), Charset::string()|undefined}
625 639 %% @doc Interpret the Content-Type header in the client's PUT request.

0 comments on commit 58cb87f

Please sign in to comment.
Something went wrong with that request. Please try again.