Repository: couchdb-chttpd Updated Branches: refs/heads/master 06880c35d -> 5b4520b9b
Backport content negotiation from couch_httpd_* COUCHDB-2409 Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/5b4520b9 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/5b4520b9 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/5b4520b9 Branch: refs/heads/master Commit: 5b4520b9baada836485a5a20c2291d22e0de124f Parents: 06880c3 Author: Alexander Shorin <[email protected]> Authored: Thu Oct 22 22:25:21 2015 +0300 Committer: Alexander Shorin <[email protected]> Committed: Thu Oct 29 17:24:13 2015 +0300 ---------------------------------------------------------------------- src/chttpd.erl | 46 ++++++++++++++++++++++------------------------ src/chttpd_db.erl | 16 ++++------------ 2 files changed, 26 insertions(+), 36 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/5b4520b9/src/chttpd.erl ---------------------------------------------------------------------- diff --git a/src/chttpd.erl b/src/chttpd.erl index 1b22c9a..d3bc4e7 100644 --- a/src/chttpd.erl +++ b/src/chttpd.erl @@ -878,31 +878,29 @@ error_headers(#httpd{mochi_req=MochiReq}=Req, 401=Code, ErrorStr, ReasonStr) -> % send the browser popup header no matter what if we are require_valid_user {Code, [{"WWW-Authenticate", "Basic realm=\"server\""}]}; _False -> - % if the accept header matches html, then do the redirect. else proceed as usual. - Accepts = case MochiReq:get_header_value("Accept") of - undefined -> - % According to the HTTP 1.1 spec, if the Accept - % header is missing, it means the client accepts - % all media types. - "html"; - Else -> - Else - end, - case re:run(Accepts, "\\bhtml\\b", - [{capture, none}, caseless]) of - nomatch -> + case MochiReq:accepts_content_type("application/json") of + true -> {Code, []}; - match -> - AuthRedirectBin = ?l2b(AuthRedirect), - % Redirect to the path the user requested, not - % the one that is used internally. - UrlReturnRaw = case MochiReq:get_header_value("x-couchdb-vhost-path") of - undefined -> MochiReq:get(path); - VHostPath -> VHostPath - end, - UrlReturn = ?l2b(couch_util:url_encode(UrlReturnRaw)), - UrlReason = ?l2b(couch_util:url_encode(ReasonStr)), - {302, [{"Location", couch_httpd:absolute_uri(Req, <<AuthRedirectBin/binary,"?return=",UrlReturn/binary,"&reason=",UrlReason/binary>>)}]} + false -> + case MochiReq:accepts_content_type("text/html") of + true -> + % Redirect to the path the user requested, not + % the one that is used internally. + UrlReturnRaw = case MochiReq:get_header_value("x-couchdb-vhost-path") of + undefined -> + MochiReq:get(path); + VHostPath -> + VHostPath + end, + RedirectLocation = lists:flatten([ + AuthRedirect, + "?return=", couch_util:url_encode(UrlReturnRaw), + "&reason=", couch_util:url_encode(ReasonStr) + ]), + {302, [{"Location", absolute_uri(Req, RedirectLocation)}]}; + false -> + {Code, []} + end end end end; http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/5b4520b9/src/chttpd_db.erl ---------------------------------------------------------------------- diff --git a/src/chttpd_db.erl b/src/chttpd_db.erl index 65d21ea..bce04e1 100644 --- a/src/chttpd_db.erl +++ b/src/chttpd_db.erl @@ -639,7 +639,7 @@ db_doc_req(#httpd{method='DELETE'}=Req, Db, DocId) -> end, send_updated_doc(Req, Db, DocId, couch_doc_from_req(Req, DocId, Body)); -db_doc_req(#httpd{method='GET'}=Req, Db, DocId) -> +db_doc_req(#httpd{method='GET', mochi_req=MochiReq}=Req, Db, DocId) -> #doc_query_args{ rev = Rev, open_revs = Revs, @@ -658,15 +658,11 @@ db_doc_req(#httpd{method='GET'}=Req, Db, DocId) -> send_doc(Req, Doc, Options2); _ -> {ok, Results} = fabric:open_revs(Db, DocId, Revs, Options), - AcceptedTypes = case couch_httpd:header_value(Req, "Accept") of - undefined -> []; - AcceptHeader -> string:tokens(AcceptHeader, ", ") - end, case Results of [] when Revs == all -> chttpd:send_error(Req, {not_found, missing}); _Else -> - case lists:member("multipart/mixed", AcceptedTypes) of + case MochiReq:accepts_content_type("multipart/mixed") of false -> {ok, Resp} = start_json_response(Req, 200), send_chunk(Resp, "["), @@ -840,16 +836,12 @@ send_doc(Req, Doc, Options) -> send_doc_efficiently(Req, #doc{atts=[]}=Doc, Headers, Options) -> send_json(Req, 200, Headers, couch_doc:to_json_obj(Doc, Options)); -send_doc_efficiently(Req, #doc{atts=Atts}=Doc, Headers, Options) -> +send_doc_efficiently(#httpd{mochi_req=MochiReq}=Req, #doc{atts=Atts}=Doc, Headers, Options) -> case lists:member(attachments, Options) of true -> Refs = monitor_attachments(Atts), try - AcceptedTypes = case couch_httpd:header_value(Req, "Accept") of - undefined -> []; - AcceptHeader -> string:tokens(AcceptHeader, ", ") - end, - case lists:member("multipart/related", AcceptedTypes) of + case MochiReq:accepts_content_type("multipart/related") of false -> send_json(Req, 200, Headers, couch_doc:to_json_obj(Doc, Options)); true ->
