1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2015-2015. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20%% 21-module(httpd_custom). 22 23-export([response_header/1, request_header/1, response_default_headers/0]). 24-export([customize_headers/3, response_default_headers/1]). 25 26-include("../inets_app/inets_internal.hrl"). 27 28-behaviour(httpd_custom_api). 29 30%%-------------------------------------------------------------------- 31%% Behavior API ----------------------------------- 32%%-------------------------------------------------------------------- 33 34response_header(Header) -> 35 {true, httpify(Header)}. 36request_header(Header) -> 37 {true, Header}. 38response_default_headers() -> 39 []. 40 41%%-------------------------------------------------------------------- 42%% Internal API ----------------------------------- 43%%-------------------------------------------------------------------- 44customize_headers(?MODULE, Function, Arg) -> 45 ?MODULE:Function(Arg); 46customize_headers(Module, Function, Arg) -> 47 try Module:Function(Arg) of 48 {true, Value} -> 49 ?MODULE:Function(Value); 50 false -> 51 false 52 catch 53 _:_ -> 54 ?MODULE:Function(Arg) 55 end. 56 57response_default_headers(?MODULE) -> 58 response_default_headers(); 59response_default_headers(Module) -> 60 try Module:response_default_headers() of 61 Defaults -> 62 [{http_util:to_lower(Key), Value} || {Key, Value} <- Defaults, 63 is_list(Key), is_list(Value)] 64 catch 65 _:_ -> 66 ?MODULE:response_default_headers() 67 end. 68%%-------------------------------------------------------------------- 69%% Internal functions ----------------------------------- 70%%-------------------------------------------------------------------- 71httpify({Key0, Value}) -> 72 %% make sure first letter is capital (defacto standard) 73 Words1 = string:tokens(Key0, "-"), 74 Words2 = upify(Words1, []), 75 Key = new_key(Words2), 76 Key ++ ": " ++ Value ++ ?CRLF . 77 78new_key([]) -> 79 ""; 80new_key([W]) -> 81 W; 82new_key([W1,W2]) -> 83 W1 ++ "-" ++ W2; 84new_key([W|R]) -> 85 W ++ "-" ++ new_key(R). 86 87upify([], Acc) -> 88 lists:reverse(Acc); 89upify([Key|Rest], Acc) -> 90 upify(Rest, [upify2(Key)|Acc]). 91 92upify2([C|Rest]) when (C >= $a) andalso (C =< $z) -> 93 [C-($a-$A)|Rest]; 94upify2(Str) -> 95 Str. 96