Skip to content

Commit

Permalink
gateway: Fix leo-project#673
Browse files Browse the repository at this point in the history
  • Loading branch information
mocchira committed Apr 19, 2018
1 parent 73a8053 commit 5f0b9f4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apps/leo_gateway/include/leo_http.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
-define(HTTP_ST_CONFLICT, 409).
-define(HTTP_ST_BAD_RANGE, 416).
-define(HTTP_ST_INTERNAL_ERROR, 500).
-define(HTTP_ST_NOT_IMPLEMENTED, 501).
-define(HTTP_ST_SERVICE_UNAVAILABLE, 503).
-define(HTTP_ST_GATEWAY_TIMEOUT, 504).

Expand Down Expand Up @@ -221,6 +222,9 @@
%% - code:500
-define(reply_internal_error_without_body(_H,_R),
cowboy_req:reply(?HTTP_ST_INTERNAL_ERROR, _H,_R)).
%% - code:501
-define(reply_not_implemented_without_body(_H,_R),
cowboy_req:reply(?HTTP_ST_NOT_IMPLEMENTED, _H,_R)).
%% - code:503
-define(reply_timeout_without_body(_H,_R),
cowboy_req:reply(?HTTP_ST_SERVICE_UNAVAILABLE,_H,_R)).
Expand Down Expand Up @@ -469,6 +473,10 @@
-define(XML_BUCKET_VERSIONING,
<<"<VersioningConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"/>">>).

%% ETS Identifier
-define(ETS_HTTP_OPTION_TBL, 'leo_gateway_http_opts_tbl').
-define(ETS_HTTP_OPTION_KEY, 'leo_gateway_http_opts_key').

%% Records
-type aws_chunk_state() :: wait_size | wait_head | read_chunk | error | done.

Expand Down
4 changes: 4 additions & 0 deletions apps/leo_gateway/src/leo_gateway_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ after_process_1(Pid, Managers) ->
%% Retrieve http-options
{ok, HttpOptions} = get_options(),

%% Store http-options for the graceful update to the http header config file
_ = ets:new(?ETS_HTTP_OPTION_TBL, [set, public, named_table]),
true = ets:insert(?ETS_HTTP_OPTION_TBL, {?ETS_HTTP_OPTION_KEY, HttpOptions}),

%% Launch bucket-sync, s3-related-procs
%% [S3, NFS]
Handler = HttpOptions#http_options.handler,
Expand Down
37 changes: 37 additions & 0 deletions apps/leo_gateway/src/leo_gateway_http_commons.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
delete_object/3, head_object/3,
range_object/3,
do_health_check/0]).
-export([reload_http_header_conf/0, validate_http_header_conf/0]).

-record(req_large_obj, {
handler :: pid(),
Expand Down Expand Up @@ -140,6 +141,42 @@ start(Sup, Options) ->
%% launch http-handler(s)
start(Options).

%% @doc Reload HTTP header conf
%%
-spec(reload_http_header_conf() -> ok | {error, any()}).
reload_http_header_conf() ->
Ret = validate_http_header_conf(),
reload_http_header_conf(Ret).

reload_http_header_conf({error, _Reason} = Err) ->
Err;
reload_http_header_conf({ok, HttpOpts, CustomHeaderSettings}) ->
InternalCache = (HttpOpts#http_options.cache_method == 'inner'),
Dispatch = cowboy_router:compile(
[{'_', [{'_', HttpOpts#http_options.handler,
[?env_layer_of_dirs(), InternalCache,
CustomHeaderSettings, HttpOpts]}]}]),
ok = cowboy:set_env(HttpOpts#http_options.handler, dispatch, Dispatch).

%% @doc Validate HTTP header conf
%%
-spec(validate_http_header_conf() -> {ok, #http_options{}, any()} | {error, any()}).
validate_http_header_conf() ->

[{_Key, HttpOpts}] = ets:lookup(?ETS_HTTP_OPTION_TBL, ?ETS_HTTP_OPTION_KEY),
case leo_nginx_conf_parser:parse(HttpOpts#http_options.headers_config_file) of
{ok, CustomHeaderSettings} ->
{ok, HttpOpts, CustomHeaderSettings};
not_found ->
{error, not_found};
{error, enoent} ->
{error, enoent};
{error, Reason} ->
?error("validate_http_header_conf/0",
[{simple_cause, "reading http custom header file failed"},
{cause, Reason}]),
{error, Reason}
end.

%% @doc Handle request
%%
Expand Down
16 changes: 16 additions & 0 deletions rel/common/launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,22 @@ case "$1" in
exec $ERTS_PATH/to_erl $PIPE_DIR
;;

reload_http_conf)
$NODETOOL reload_http_conf
ES=$?
if [ "$ES" -ne 0 ]; then
exit $ES
fi
;;

test_http_conf)
$NODETOOL test_http_conf
ES=$?
if [ "$ES" -ne 0 ]; then
exit $ES
fi
;;

ready)
## See if application is ready
$NODETOOL ready
Expand Down
18 changes: 18 additions & 0 deletions rel/common/nodetool
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ main(Args) ->
Other ->
io:format("~p\n", [Other])
end;
["reload_http_conf"] ->
case rpc:call(TargetNode, leo_gateway_http_commons, reload_http_header_conf,
[], 60000) of
{badrpc, Reason} ->
io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
halt(1);
Other ->
io:format("~p\n", [Other])
end;
["test_http_conf"] ->
case rpc:call(TargetNode, leo_gateway_http_commons, validate_http_header_conf,
[], 60000) of
{badrpc, Reason} ->
io:format("RPC to ~p failed: ~p\n", [TargetNode, Reason]),
halt(1);
Other ->
io:format("~p\n", [Other])
end;
["ready"] ->
case rpc:call(TargetNode, application, which_applications, [], 60000) of
{badrpc, Reason} ->
Expand Down

0 comments on commit 5f0b9f4

Please sign in to comment.