Skip to content

Instantly share code, notes, and snippets.

@kellymclaughlin
Created June 7, 2011 17:33
Show Gist options
  • Save kellymclaughlin/1012706 to your computer and use it in GitHub Desktop.
Save kellymclaughlin/1012706 to your computer and use it in GitHub Desktop.
malformed_index_headers(RD, Ctx) ->
%% Get a list of index_headers...
L1 = extract_index_headers(RD),
%% Remove the prefix...
%% PrefixSize = length(?HEAD_INDEX_PREFIX),
%% F = fun(X) -> element(2, lists:split(PrefixSize, X)) end,
%% L2 = [{K,V} || {?HEAD_INDEX_PREFIX ++ K, V} <- L1],
%% Validate the fields. If validation passes, then the index
%% headers are correctly formed.
case riak_index:validate_fields(L2) of
true ->
{false, RD, Ctx#ctx { index_fields=L1 }};
{false, Reasons} ->
{true,
wrq:append_to_resp_body(
[riak_index:format_failure_reason(X) || X <- Reasons],
wrq:set_resp_header(?HEAD_CTYPE, "text/plain", RD)),
Ctx}
end.
%% @spec extract_index_headers(reqdata()) -> proplist()
%% @doc Extract headers prefixed by "index-" in the client's
%% PUT request, to be indexed at write time.
extract_index_headers(RD) ->
lists:foldl(fun({K,_V}, Acc) ->
LowerK = string:to_lower(any_to_list(K)),
case lists:prefix(?HEAD_INDEX_PREFIX, LowerK) of
true ->
?HEAD_INDEX_PREFIX ++ IndexField = LowerK,
[{IndexField, _V} | Acc];
false ->
Acc
end
end,
[],
mochiweb_headers:to_list(wrq:req_headers(RD))).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment