diff options
Diffstat (limited to 'lib/inets/src')
30 files changed, 3125 insertions, 1699 deletions
diff --git a/lib/inets/src/ftp/Makefile b/lib/inets/src/ftp/Makefile index 70d51115e6..0c15277a18 100644 --- a/lib/inets/src/ftp/Makefile +++ b/lib/inets/src/ftp/Makefile @@ -1,19 +1,19 @@ # # %CopyrightBegin% -# -# Copyright Ericsson AB 2005-2009. All Rights Reserved. -# +# +# Copyright Ericsson AB 2005-2010. All Rights Reserved. +# # The contents of this file are subject to the Erlang Public License, # Version 1.1, (the "License"); you may not use this file except in # compliance with the License. You should have received a copy of the # Erlang Public License along with this software. If not, it can be # retrieved online at http://www.erlang.org/. -# +# # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. -# +# # %CopyrightEnd% # # @@ -32,7 +32,8 @@ VSN = $(INETS_VSN) # ---------------------------------------------------- # Release directory specification # ---------------------------------------------------- -RELSYSDIR = $(RELEASE_PATH)/lib/inets-$(VSN) +RELSYSDIR = $(RELEASE_PATH)/lib/$(APPLICATION)-$(VSN) + # ---------------------------------------------------- # Target Specs @@ -49,15 +50,17 @@ ERL_FILES = $(MODULES:%=%.erl) TARGET_FILES= $(MODULES:%=$(EBIN)/%.$(EMULATOR)) + # ---------------------------------------------------- # INETS FLAGS # ---------------------------------------------------- -INETS_FLAGS = -D'SERVER_SOFTWARE="inets/$(VSN)"' +INETS_FLAGS = -D'SERVER_SOFTWARE="$(APPLICATION)/$(VSN)"' ifeq ($(FTP_DEBUG),true) INETS_FLAGS += -Dftp_debug endif + # ---------------------------------------------------- # FLAGS # ---------------------------------------------------- @@ -94,6 +97,7 @@ release_spec: opt release_docs_spec: info: + @echo "APPLICATION = $(APPLICATION)" @echo "INETS_DEBUG = $(INETS_DEBUG)" @echo "INETS_FLAGS = $(INETS_FLAGS)" @echo "ERL_COMPILE_FLAGS = $(ERL_COMPILE_FLAGS)" diff --git a/lib/inets/src/http_client/Makefile b/lib/inets/src/http_client/Makefile index 23170f439f..628c91421f 100644 --- a/lib/inets/src/http_client/Makefile +++ b/lib/inets/src/http_client/Makefile @@ -1,19 +1,19 @@ # # %CopyrightBegin% -# -# Copyright Ericsson AB 2005-2009. All Rights Reserved. -# +# +# Copyright Ericsson AB 2005-2010. All Rights Reserved. +# # The contents of this file are subject to the Erlang Public License, # Version 1.1, (the "License"); you may not use this file except in # compliance with the License. You should have received a copy of the # Erlang Public License along with this software. If not, it can be # retrieved online at http://www.erlang.org/. -# +# # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. -# +# # %CopyrightEnd% # # @@ -22,6 +22,7 @@ include $(ERL_TOP)/make/target.mk EBIN = ../../ebin include $(ERL_TOP)/make/$(TARGET)/otp.mk + # ---------------------------------------------------- # Application version # ---------------------------------------------------- @@ -29,17 +30,20 @@ include ../../vsn.mk VSN = $(INETS_VSN) + # ---------------------------------------------------- # Release directory specification # ---------------------------------------------------- -RELSYSDIR = $(RELEASE_PATH)/lib/inets-$(VSN) +RELSYSDIR = $(RELEASE_PATH)/lib/$(APPLICATION)-$(VSN) + # ---------------------------------------------------- # Target Specs # ---------------------------------------------------- MODULES = \ http \ - http_cookie \ + httpc \ + httpc_cookie \ httpc_handler \ httpc_manager \ httpc_sup \ @@ -55,20 +59,24 @@ ERL_FILES = $(MODULES:%=%.erl) TARGET_FILES= $(MODULES:%=$(EBIN)/%.$(EMULATOR)) + # ---------------------------------------------------- # INETS FLAGS # ---------------------------------------------------- -INETS_FLAGS = -D'SERVER_SOFTWARE="inets/$(VSN)"' \ +INETS_FLAGS = -D'SERVER_SOFTWARE="$(APPLICATION)/$(VSN)"' + # ---------------------------------------------------- # FLAGS # ---------------------------------------------------- INETS_ERL_FLAGS += -I ../http_lib -I ../inets_app -pa ../../ebin -ERL_COMPILE_FLAGS += $(INETS_ERL_FLAGS)\ +ERL_COMPILE_FLAGS += $(INETS_ERL_FLAGS) \ $(INETS_FLAGS) \ +'{parse_transform,sys_pre_attributes}' \ +'{attribute,insert,app_vsn,$(APP_VSN)}' + + # ---------------------------------------------------- # Targets # ---------------------------------------------------- @@ -94,6 +102,7 @@ release_spec: opt release_docs_spec: info: + @echo "APPLICATION = $(APPLICATION)" @echo "INETS_DEBUG = $(INETS_DEBUG)" @echo "INETS_FLAGS = $(INETS_FLAGS)" @echo "ERL_COMPILE_FLAGS = $(ERL_COMPILE_FLAGS)" diff --git a/lib/inets/src/http_client/http.erl b/lib/inets/src/http_client/http.erl index ce5d7723f0..7e1e90b50e 100644 --- a/lib/inets/src/http_client/http.erl +++ b/lib/inets/src/http_client/http.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2002-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2002-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -24,7 +24,6 @@ %%% - RFC 2818 HTTP Over TLS -module(http). --behaviour(inets_service). %% API -export([request/1, request/2, request/4, request/5, @@ -35,15 +34,6 @@ cookie_header/2, stream_next/1, default_profile/0]). -%% Behavior callbacks --export([start_standalone/1, start_service/1, - stop_service/1, services/0, service_info/1]). - --include("http_internal.hrl"). --include("httpc_internal.hrl"). - --define(DEFAULT_PROFILE, default). - %%%========================================================================= %%% API @@ -51,751 +41,75 @@ %%-------------------------------------------------------------------------- %% request(Url [, Profile]) -> -%% {ok, {StatusLine, Headers, Body}} | {error,Reason} -%% -%% Url - string() -%% Description: Calls request/4 with default values. +%% request(Method, Request, HTTPOptions, Options [, Profile]) %%-------------------------------------------------------------------------- -request(Url) -> - request(Url, default_profile()). - -request(Url, Profile) -> - request(get, {Url, []}, [], [], Profile). - - -%%-------------------------------------------------------------------------- -%% request(Method, Request, HTTPOptions, Options [, Profile]) -> -%% {ok, {StatusLine, Headers, Body}} | {ok, {Status, Body}} | -%% {ok, RequestId} | {error,Reason} | {ok, {saved_as, FilePath} -%% -%% Method - atom() = head | get | put | post | trace | options| delete -%% Request - {Url, Headers} | {Url, Headers, ContentType, Body} -%% Url - string() -%% HTTPOptions - [HttpOption] -%% HTTPOption - {timeout, Time} | {connect_timeout, Time} | -%% {ssl, SSLOptions} | {proxy_auth, {User, Password}} -%% Ssloptions = [SSLOption] -%% SSLOption = {verify, code()} | {depth, depth()} | {certfile, path()} | -%% {keyfile, path()} | {password, string()} | {cacertfile, path()} | -%% {ciphers, string()} -%% Options - [Option] -%% Option - {sync, Boolean} | {body_format, BodyFormat} | -%% {full_result, Boolean} | {stream, To} | -%% {headers_as_is, Boolean} -%% StatusLine = {HTTPVersion, StatusCode, ReasonPhrase}</v> -%% HTTPVersion = string() -%% StatusCode = integer() -%% ReasonPhrase = string() -%% Headers = [Header] -%% Header = {Field, Value} -%% Field = string() -%% Value = string() -%% Body = string() | binary() - HTLM-code -%% -%% Description: Sends a HTTP-request. The function can be both -%% syncronus and asynchronous in the later case the function will -%% return {ok, RequestId} and later on a message will be sent to the -%% calling process on the format {http, {RequestId, {StatusLine, -%% Headers, Body}}} or {http, {RequestId, {error, Reason}}} -%%-------------------------------------------------------------------------- +request(Url) -> httpc:request(Url). +request(Url, Profile) -> httpc:request(Url, Profile). request(Method, Request, HttpOptions, Options) -> - request(Method, Request, HttpOptions, Options, default_profile()). + httpc:request(Method, Request, HttpOptions, Options). +request(Method, Request, HttpOptions, Options, Profile) -> + httpc:request(Method, Request, HttpOptions, Options, Profile). -request(Method, {Url, Headers}, HTTPOptions, Options, Profile) - when (Method =:= options) orelse - (Method =:= get) orelse - (Method =:= head) orelse - (Method =:= delete) orelse - (Method =:= trace) -> - case http_uri:parse(Url) of - {error, Reason} -> - {error, Reason}; - ParsedUrl -> - handle_request(Method, Url, ParsedUrl, Headers, [], [], - HTTPOptions, Options, Profile) - end; - -request(Method, {Url,Headers,ContentType,Body}, HTTPOptions, Options, Profile) - when (Method =:= post) orelse (Method =:= put) -> - case http_uri:parse(Url) of - {error, Reason} -> - {error, Reason}; - ParsedUrl -> - handle_request(Method, Url, - ParsedUrl, Headers, ContentType, Body, - HTTPOptions, Options, Profile) - end. %%-------------------------------------------------------------------------- -%% request(RequestId) -> ok -%% RequestId - As returned by request/4 -%% -%% Description: Cancels a HTTP-request. +%% cancel_request(RequestId [, Profile]) %%------------------------------------------------------------------------- -cancel_request(RequestId) -> - cancel_request(RequestId, default_profile()). +cancel_request(RequestId) -> + httpc:cancel_request(RequestId). cancel_request(RequestId, Profile) -> - ok = httpc_manager:cancel_request(RequestId, profile_name(Profile)), - receive - %% If the request was allready fullfilled throw away the - %% answer as the request has been canceled. - {http, {RequestId, _}} -> - ok - after 0 -> - ok - end. - - -set_option(Key, Value) -> - set_option(Key, Value, default_profile()). - -set_option(Key, Value, Profile) -> - set_options([{Key, Value}], Profile). + httpc:cancel_request(RequestId, Profile). %%-------------------------------------------------------------------------- -%% set_options(Options [, Profile]) -> ok | {error, Reason} -%% Options - [Option] -%% Profile - atom() -%% Option - {proxy, {Proxy, NoProxy}} | {max_sessions, MaxSessions} | -%% {max_pipeline_length, MaxPipeline} | -%% {pipeline_timeout, PipelineTimeout} | {cookies, CookieMode} | -%% {ipfamily, IpFamily} -%% Proxy - {Host, Port} -%% NoProxy - [Domain | HostName | IPAddress] -%% MaxSessions, MaxPipeline, PipelineTimeout = integer() -%% CookieMode - enabled | disabled | verify -%% IpFamily - inet | inet6 | inet6fb4 -%% Description: Informs the httpc_manager of the new settings. +%% set_options(Options [, Profile]) +%% set_option(Key, Value [, Profile]) %%------------------------------------------------------------------------- + set_options(Options) -> - set_options(Options, default_profile()). + httpc:set_options(Options). set_options(Options, Profile) -> - case validate_options(Options) of - {ok, Opts} -> - try httpc_manager:set_options(Opts, profile_name(Profile)) of - Result -> - Result - catch - exit:{noproc, _} -> - {error, inets_not_started} - end; - {error, Reason} -> - {error, Reason} - end. + httpc:set_options(Options, Profile). + +set_option(Key, Value) -> + httpc:set_option(Key, Value). +set_option(Key, Value, Profile) -> + httpc:set_option(Key, Value, Profile). %%-------------------------------------------------------------------------- -%% verify_cookies(SetCookieHeaders, Url [, Profile]) -> ok | {error, reason} -%% -%% -%% Description: Store the cookies from <SetCookieHeaders> -%% in the cookie database -%% for the profile <Profile>. This function shall be used when the option -%% cookie is set to verify. +%% verify_cookies(SetCookieHeaders, Url [, Profile]) %%------------------------------------------------------------------------- -verify_cookies(SetCookieHeaders, Url) -> - verify_cookies(SetCookieHeaders, Url, default_profile()). +verify_cookies(SetCookieHeaders, Url) -> + httpc:store_cookies(SetCookieHeaders, Url). verify_cookies(SetCookieHeaders, Url, Profile) -> - {_, _, Host, Port, Path, _} = http_uri:parse(Url), - ProfileName = profile_name(Profile), - Cookies = http_cookie:cookies(SetCookieHeaders, Path, Host), - try httpc_manager:store_cookies(Cookies, {Host, Port}, ProfileName) of - _ -> - ok - catch - exit:{noproc, _} -> - {error, {not_started, Profile}} - end. + httpc:store_cookies(SetCookieHeaders, Url, Profile). + %%-------------------------------------------------------------------------- -%% cookie_header(Url [, Profile]) -> Header | {error, Reason} -%% -%% Description: Returns the cookie header that would be sent when making -%% a request to <Url>. +%% cookie_header(Url [, Profile]) %%------------------------------------------------------------------------- -cookie_header(Url) -> - cookie_header(Url, default_profile()). +cookie_header(Url) -> + httpc:cookie_header(Url). cookie_header(Url, Profile) -> - try httpc_manager:cookies(Url, profile_name(Profile)) of - Header -> - Header - catch - exit:{noproc, _} -> - {error, {not_started, Profile}} - end. - - -stream_next(Pid) -> - httpc_handler:stream_next(Pid). - -%%%======================================================================== -%%% Behavior callbacks -%%%======================================================================== -start_standalone(PropList) -> - case proplists:get_value(profile, PropList) of - undefined -> - {error, no_profile}; - Profile -> - Dir = - proplists:get_value(data_dir, PropList, only_session_cookies), - httpc_manager:start_link({Profile, Dir}, stand_alone) - end. - -start_service(Config) -> - httpc_profile_sup:start_child(Config). - -stop_service(Profile) when is_atom(Profile) -> - httpc_profile_sup:stop_child(Profile); -stop_service(Pid) when is_pid(Pid) -> - case service_info(Pid) of - {ok, [{profile, Profile}]} -> - stop_service(Profile); - Error -> - Error - end. - -services() -> - [{httpc, Pid} || {_, Pid, _, _} <- - supervisor:which_children(httpc_profile_sup)]. -service_info(Pid) -> - try [{ChildName, ChildPid} || - {ChildName, ChildPid, _, _} <- - supervisor:which_children(httpc_profile_sup)] of - Children -> - child_name2info(child_name(Pid, Children)) - catch - exit:{noproc, _} -> - {error, service_not_available} - end. - - -%%%======================================================================== -%%% Internal functions -%%%======================================================================== -handle_request(Method, Url, - {Scheme, UserInfo, Host, Port, Path, Query}, - Headers, ContentType, Body, - HTTPOptions0, Options, Profile) -> - - HTTPOptions = http_options(HTTPOptions0), - Sync = proplists:get_value(sync, Options, true), - NewHeaders = lists:map(fun({Key, Val}) -> - {http_util:to_lower(Key), Val} end, - Headers), - Stream = proplists:get_value(stream, Options, none), - case {Sync, Stream} of - {true, self} -> - {error, streaming_error}; - _ -> - RecordHeaders = header_record(NewHeaders, - #http_request_h{}, - Host, - HTTPOptions#http_options.version), - Request = #request{from = self(), - scheme = Scheme, - address = {Host,Port}, - path = Path, - pquery = Query, - method = Method, - headers = RecordHeaders, - content = {ContentType,Body}, - settings = HTTPOptions, - abs_uri = Url, - userinfo = UserInfo, - stream = Stream, - headers_as_is = headers_as_is(Headers, Options)}, - try httpc_manager:request(Request, profile_name(Profile)) of - {ok, RequestId} -> - handle_answer(RequestId, Sync, Options); - {error, Reason} -> - {error, Reason} - catch - error:{noproc, _} -> - {error, {not_started, Profile}} - end - end. - - -handle_answer(RequestId, false, _) -> - {ok, RequestId}; -handle_answer(RequestId, true, Options) -> - receive - {http, {RequestId, saved_to_file}} -> - {ok, saved_to_file}; - {http, {RequestId, Result = {_,_,_}}} -> - return_answer(Options, Result); - {http, {RequestId, {error, Reason}}} -> - {error, Reason} - end. - -return_answer(Options, {{"HTTP/0.9",_,_}, _, BinBody}) -> - Body = format_body(BinBody, Options), - {ok, Body}; - -return_answer(Options, {StatusLine, Headers, BinBody}) -> - - Body = format_body(BinBody, Options), - - case proplists:get_value(full_result, Options, true) of - true -> - {ok, {StatusLine, Headers, Body}}; - false -> - {_, Status, _} = StatusLine, - {ok, {Status, Body}} - end. - -format_body(BinBody, Options) -> - case proplists:get_value(body_format, Options, string) of - string -> - binary_to_list(BinBody); - _ -> - BinBody - end. + httpc:cookie_header(Url, Profile). -%% This options is a workaround for http servers that do not follow the -%% http standard and have case sensative header parsing. Should only be -%% used if there is no other way to communicate with the server or for -%% testing purpose. -headers_as_is(Headers, Options) -> - case proplists:get_value(headers_as_is, Options, false) of - false -> - []; - true -> - Headers - end. +%%-------------------------------------------------------------------------- +%% stream_next(Pid) +%%------------------------------------------------------------------------- -http_options(HttpOptions) -> - HttpOptionsDefault = http_options_default(), - http_options(HttpOptionsDefault, HttpOptions, #http_options{}). - -http_options([], [], Acc) -> - Acc; -http_options([], HttpOptions, Acc) -> - Fun = fun(BadOption) -> - Report = io_lib:format("Invalid option ~p ignored ~n", - [BadOption]), - error_logger:info_report(Report) - end, - lists:foreach(Fun, HttpOptions), - Acc; -http_options([{Tag, Default, Idx, Post} | Defaults], HttpOptions, Acc) -> - case lists:keysearch(Tag, 1, HttpOptions) of - {value, {Tag, Val0}} -> - case Post(Val0) of - {ok, Val} -> - Acc2 = setelement(Idx, Acc, Val), - HttpOptions2 = lists:keydelete(Tag, 1, HttpOptions), - http_options(Defaults, HttpOptions2, Acc2); - error -> - Report = io_lib:format("Invalid option ~p:~p ignored ~n", - [Tag, Val0]), - error_logger:info_report(Report), - HttpOptions2 = lists:keydelete(Tag, 1, HttpOptions), - http_options(Defaults, HttpOptions2, Acc) - end; - false -> - DefaultVal = - case Default of - {value, Val} -> - Val; - {field, DefaultIdx} -> - element(DefaultIdx, Acc) - end, - Acc2 = setelement(Idx, Acc, DefaultVal), - http_options(Defaults, HttpOptions, Acc2) - end. - -http_options_default() -> - VersionPost = - fun(Value) when is_atom(Value) -> - {ok, http_util:to_upper(atom_to_list(Value))}; - (Value) when is_list(Value) -> - {ok, http_util:to_upper(Value)}; - (_) -> - error - end, - TimeoutPost = fun(Value) when is_integer(Value) andalso (Value >= 0) -> - {ok, Value}; - (infinity = Value) -> - {ok, Value}; - (_) -> - error - end, - AutoRedirectPost = fun(Value) when (Value =:= true) orelse - (Value =:= false) -> - {ok, Value}; - (_) -> - error - end, - SslPost = fun(Value) when is_list(Value) -> - {ok, Value}; - (_) -> - error - end, - ProxyAuthPost = fun({User, Passwd} = Value) when is_list(User) andalso - is_list(Passwd) -> - {ok, Value}; - (_) -> - error - end, - RelaxedPost = fun(Value) when (Value =:= true) orelse - (Value =:= false) -> - {ok, Value}; - (_) -> - error - end, - ConnTimeoutPost = - fun(Value) when is_integer(Value) andalso (Value >= 0) -> - {ok, Value}; - (infinity = Value) -> - {ok, Value}; - (_) -> - error - end, - [ - {version, {value, "HTTP/1.1"}, #http_options.version, VersionPost}, - {timeout, {value, ?HTTP_REQUEST_TIMEOUT}, #http_options.timeout, TimeoutPost}, - {autoredirect, {value, true}, #http_options.autoredirect, AutoRedirectPost}, - {ssl, {value, []}, #http_options.ssl, SslPost}, - {proxy_auth, {value, undefined}, #http_options.proxy_auth, ProxyAuthPost}, - {relaxed, {value, false}, #http_options.relaxed, RelaxedPost}, - %% this field has to be *after* the timeout field (as that field is used for the default value) - {connect_timeout, {field, #http_options.timeout}, #http_options.connect_timeout, ConnTimeoutPost} - ]. - -validate_options(Options) -> - (catch validate_options(Options, [])). - -validate_options([], ValidateOptions) -> - {ok, lists:reverse(ValidateOptions)}; - -validate_options([{proxy, Proxy} = Opt| Tail], Acc) -> - validate_proxy(Proxy), - validate_options(Tail, [Opt | Acc]); - -validate_options([{max_sessions, Value} = Opt| Tail], Acc) -> - validate_max_sessions(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{keep_alive_timeout, Value} = Opt| Tail], Acc) -> - validate_keep_alive_timeout(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{max_keep_alive_length, Value} = Opt| Tail], Acc) -> - validate_max_keep_alive_length(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{pipeline_timeout, Value} = Opt| Tail], Acc) -> - validate_pipeline_timeout(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{max_pipeline_length, Value} = Opt| Tail], Acc) -> - validate_max_pipeline_length(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{cookies, Value} = Opt| Tail], Acc) -> - validate_cookies(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{ipfamily, Value} = Opt| Tail], Acc) -> - validate_ipfamily(Value), - validate_options(Tail, [Opt | Acc]); - -%% For backward compatibillity -validate_options([{ipv6, Value}| Tail], Acc) -> - NewValue = validate_ipv6(Value), - Opt = {ipfamily, NewValue}, - validate_options(Tail, [Opt | Acc]); - -validate_options([{ip, Value} = Opt| Tail], Acc) -> - validate_ip(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{port, Value} = Opt| Tail], Acc) -> - validate_port(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{verbose, Value} = Opt| Tail], Acc) -> - validate_verbose(Value), - validate_options(Tail, [Opt | Acc]); - -validate_options([{_, _} = Opt| _], _Acc) -> - {error, {not_an_option, Opt}}. - - -validate_proxy({{ProxyHost, ProxyPort}, NoProxy} = Proxy) - when is_list(ProxyHost) andalso - is_integer(ProxyPort) andalso - is_list(NoProxy) -> - Proxy; -validate_proxy(BadProxy) -> - bad_option(proxy, BadProxy). - -validate_max_sessions(Value) when is_integer(Value) andalso (Value >= 0) -> - Value; -validate_max_sessions(BadValue) -> - bad_option(max_sessions, BadValue). - -validate_keep_alive_timeout(Value) when is_integer(Value) andalso (Value >= 0) -> - Value; -validate_keep_alive_timeout(infinity = Value) -> - Value; -validate_keep_alive_timeout(BadValue) -> - bad_option(keep_alive_timeout, BadValue). - -validate_max_keep_alive_length(Value) when is_integer(Value) andalso (Value >= 0) -> - Value; -validate_max_keep_alive_length(BadValue) -> - bad_option(max_keep_alive_length, BadValue). - -validate_pipeline_timeout(Value) when is_integer(Value) -> - Value; -validate_pipeline_timeout(infinity = Value) -> - Value; -validate_pipeline_timeout(BadValue) -> - bad_option(pipeline_timeout, BadValue). - -validate_max_pipeline_length(Value) when is_integer(Value) -> - Value; -validate_max_pipeline_length(BadValue) -> - bad_option(max_pipeline_length, BadValue). - -validate_cookies(Value) - when ((Value =:= enabled) orelse - (Value =:= disabled) orelse - (Value =:= verify)) -> - Value; -validate_cookies(BadValue) -> - bad_option(cookies, BadValue). - -validate_ipv6(Value) when (Value =:= enabled) orelse (Value =:= disabled) -> - case Value of - enabled -> - inet6fb4; - disabled -> - inet - end; -validate_ipv6(BadValue) -> - bad_option(ipv6, BadValue). - -validate_ipfamily(Value) - when (Value =:= inet) orelse (Value =:= inet6) orelse (Value =:= inet6fb4) -> - Value; -validate_ipfamily(BadValue) -> - bad_option(ipfamily, BadValue). - -validate_ip(Value) - when is_tuple(Value) andalso ((size(Value) =:= 4) orelse (size(Value) =:= 8)) -> - Value; -validate_ip(BadValue) -> - bad_option(ip, BadValue). - -validate_port(Value) when is_integer(Value) -> - Value; -validate_port(BadValue) -> - bad_option(port, BadValue). - -validate_verbose(Value) - when ((Value =:= false) orelse - (Value =:= verbose) orelse - (Value =:= debug) orelse - (Value =:= trace)) -> - ok; -validate_verbose(BadValue) -> - bad_option(verbose, BadValue). - -bad_option(Option, BadValue) -> - throw({error, {bad_option, Option, BadValue}}). - - - -header_record([], RequestHeaders, Host, Version) -> - validate_headers(RequestHeaders, Host, Version); -header_record([{"cache-control", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'cache-control' = Val}, - Host, Version); -header_record([{"connection", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{connection = Val}, Host, - Version); -header_record([{"date", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{date = Val}, Host, - Version); -header_record([{"pragma", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{pragma = Val}, Host, - Version); -header_record([{"trailer", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{trailer = Val}, Host, - Version); -header_record([{"transfer-encoding", Val} | Rest], RequestHeaders, Host, - Version) -> - header_record(Rest, - RequestHeaders#http_request_h{'transfer-encoding' = Val}, - Host, Version); -header_record([{"upgrade", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{upgrade = Val}, Host, - Version); -header_record([{"via", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{via = Val}, Host, - Version); -header_record([{"warning", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{warning = Val}, Host, - Version); -header_record([{"accept", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{accept = Val}, Host, - Version); -header_record([{"accept-charset", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'accept-charset' = Val}, - Host, Version); -header_record([{"accept-encoding", Val} | Rest], RequestHeaders, Host, - Version) -> - header_record(Rest, RequestHeaders#http_request_h{'accept-encoding' = Val}, - Host, Version); -header_record([{"accept-language", Val} | Rest], RequestHeaders, Host, - Version) -> - header_record(Rest, RequestHeaders#http_request_h{'accept-language' = Val}, - Host, Version); -header_record([{"authorization", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{authorization = Val}, - Host, Version); -header_record([{"expect", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{expect = Val}, Host, - Version); -header_record([{"from", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{from = Val}, Host, - Version); -header_record([{"host", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{host = Val}, Host, - Version); -header_record([{"if-match", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'if-match' = Val}, - Host, Version); -header_record([{"if-modified-since", Val} | Rest], RequestHeaders, Host, - Version) -> - header_record(Rest, - RequestHeaders#http_request_h{'if-modified-since' = Val}, - Host, Version); -header_record([{"if-none-match", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'if-none-match' = Val}, - Host, Version); -header_record([{"if-range", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'if-range' = Val}, - Host, Version); - -header_record([{"if-unmodified-since", Val} | Rest], RequestHeaders, Host, - Version) -> - header_record(Rest, RequestHeaders#http_request_h{'if-unmodified-since' - = Val}, Host, Version); -header_record([{"max-forwards", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'max-forwards' = Val}, - Host, Version); -header_record([{"proxy-authorization", Val} | Rest], RequestHeaders, Host, - Version) -> - header_record(Rest, RequestHeaders#http_request_h{'proxy-authorization' - = Val}, Host, Version); -header_record([{"range", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{range = Val}, Host, - Version); -header_record([{"referer", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{referer = Val}, Host, - Version); -header_record([{"te", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{te = Val}, Host, - Version); -header_record([{"user-agent", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'user-agent' = Val}, - Host, Version); -header_record([{"allow", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{allow = Val}, Host, - Version); -header_record([{"content-encoding", Val} | Rest], RequestHeaders, Host, - Version) -> - header_record(Rest, - RequestHeaders#http_request_h{'content-encoding' = Val}, - Host, Version); -header_record([{"content-language", Val} | Rest], RequestHeaders, - Host, Version) -> - header_record(Rest, - RequestHeaders#http_request_h{'content-language' = Val}, - Host, Version); -header_record([{"content-length", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'content-length' = Val}, - Host, Version); -header_record([{"content-location", Val} | Rest], RequestHeaders, - Host, Version) -> - header_record(Rest, - RequestHeaders#http_request_h{'content-location' = Val}, - Host, Version); -header_record([{"content-md5", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'content-md5' = Val}, - Host, Version); -header_record([{"content-range", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'content-range' = Val}, - Host, Version); -header_record([{"content-type", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'content-type' = Val}, - Host, Version); -header_record([{"expires", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{expires = Val}, Host, - Version); -header_record([{"last-modified", Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{'last-modified' = Val}, - Host, Version); -header_record([{Key, Val} | Rest], RequestHeaders, Host, Version) -> - header_record(Rest, RequestHeaders#http_request_h{ - other = [{Key, Val} | - RequestHeaders#http_request_h.other]}, - Host, Version). +stream_next(Pid) -> + httpc:stream_next(Pid). -validate_headers(RequestHeaders = #http_request_h{te = undefined}, Host, - "HTTP/1.1" = Version) -> - validate_headers(RequestHeaders#http_request_h{te = ""}, Host, - "HTTP/1.1" = Version); -validate_headers(RequestHeaders = #http_request_h{host = undefined}, - Host, "HTTP/1.1" = Version) -> - validate_headers(RequestHeaders#http_request_h{host = Host}, Host, Version); -validate_headers(RequestHeaders, _, _) -> - RequestHeaders. +%%-------------------------------------------------------------------------- +%% default_profile() +%%------------------------------------------------------------------------- default_profile() -> - ?DEFAULT_PROFILE. - -profile_name(?DEFAULT_PROFILE) -> - httpc_manager; -profile_name(Pid) when is_pid(Pid) -> - Pid; -profile_name(Profile) -> - list_to_atom("httpc_manager_" ++ atom_to_list(Profile)). - -child_name2info(undefined) -> - {error, no_such_service}; -child_name2info(httpc_manager) -> - {ok, [{profile, default}]}; -child_name2info({http, Profile}) -> - {ok, [{profile, Profile}]}. - -child_name(_, []) -> - undefined; -child_name(Pid, [{Name, Pid} | _]) -> - Name; -child_name(Pid, [_ | Children]) -> - child_name(Pid, Children). - -%% d(F) -> -%% d(F, []). - -%% d(F, A) -> -%% d(get(dbg), F, A). - -%% d(true, F, A) -> -%% io:format(user, "~w:~w:" ++ F ++ "~n", [self(), ?MODULE | A]); -%% d(_, _, _) -> -%% ok. - + httpc:default_profile(). diff --git a/lib/inets/src/http_client/httpc.erl b/lib/inets/src/http_client/httpc.erl new file mode 100644 index 0000000000..5205605e0a --- /dev/null +++ b/lib/inets/src/http_client/httpc.erl @@ -0,0 +1,1079 @@ +%% +%% %CopyrightBegin% +%% +%% Copyright Ericsson AB 2009-2010. All Rights Reserved. +%% +%% The contents of this file are subject to the Erlang Public License, +%% Version 1.1, (the "License"); you may not use this file except in +%% compliance with the License. You should have received a copy of the +%% Erlang Public License along with this software. If not, it can be +%% retrieved online at http://www.erlang.org/. +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See +%% the License for the specific language governing rights and limitations +%% under the License. +%% +%% %CopyrightEnd% +%% +%% + +%% Description: +%%% This version of the HTTP/1.1 client supports: +%%% - RFC 2616 HTTP 1.1 client part +%%% - RFC 2818 HTTP Over TLS + +-module(httpc). + +-behaviour(inets_service). + +%% API +-export([ + request/1, request/2, request/4, request/5, + cancel_request/1, cancel_request/2, + set_option/2, set_option/3, + set_options/1, set_options/2, + store_cookies/2, store_cookies/3, + cookie_header/1, cookie_header/2, + which_cookies/0, which_cookies/1, + reset_cookies/0, reset_cookies/1, + stream_next/1, + default_profile/0, + profile_name/1, profile_name/2, + info/0, info/1 + ]). + +%% Behavior callbacks +-export([start_standalone/1, start_service/1, + stop_service/1, + services/0, service_info/1]). + +-include("http_internal.hrl"). +-include("httpc_internal.hrl"). + +-define(DEFAULT_PROFILE, default). + + +%%%========================================================================= +%%% API +%%%========================================================================= + +default_profile() -> + ?DEFAULT_PROFILE. + + +profile_name(?DEFAULT_PROFILE) -> + httpc_manager; +profile_name(Profile) -> + profile_name("httpc_manager_", Profile). + +profile_name(Prefix, Profile) when is_atom(Profile) -> + list_to_atom(Prefix ++ atom_to_list(Profile)); +profile_name(Prefix, Profile) when is_pid(Profile) -> + ProfileStr0 = + string:strip(string:strip(erlang:pid_to_list(Profile), left, $<), right, $>), + F = fun($.) -> $_; (X) -> X end, + ProfileStr = [F(C) || C <- ProfileStr0], + list_to_atom(Prefix ++ "pid_" ++ ProfileStr). + + +%%-------------------------------------------------------------------------- +%% request(Url) -> {ok, {StatusLine, Headers, Body}} | {error,Reason} +%% request(Url Profile) -> +%% {ok, {StatusLine, Headers, Body}} | {error,Reason} +%% +%% Url - string() +%% Description: Calls request/4 with default values. +%%-------------------------------------------------------------------------- + +request(Url) -> + request(Url, default_profile()). + +request(Url, Profile) -> + request(get, {Url, []}, [], [], Profile). + + +%%-------------------------------------------------------------------------- +%% request(Method, Request, HTTPOptions, Options [, Profile]) -> +%% {ok, {StatusLine, Headers, Body}} | {ok, {Status, Body}} | +%% {ok, RequestId} | {error,Reason} | {ok, {saved_as, FilePath} +%% +%% Method - atom() = head | get | put | post | trace | options| delete +%% Request - {Url, Headers} | {Url, Headers, ContentType, Body} +%% Url - string() +%% HTTPOptions - [HttpOption] +%% HTTPOption - {timeout, Time} | {connect_timeout, Time} | +%% {ssl, SSLOptions} | {proxy_auth, {User, Password}} +%% Ssloptions = [SSLOption] +%% SSLOption = {verify, code()} | {depth, depth()} | {certfile, path()} | +%% {keyfile, path()} | {password, string()} | {cacertfile, path()} | +%% {ciphers, string()} +%% Options - [Option] +%% Option - {sync, Boolean} | {body_format, BodyFormat} | +%% {full_result, Boolean} | {stream, To} | +%% {headers_as_is, Boolean} +%% StatusLine = {HTTPVersion, StatusCode, ReasonPhrase}</v> +%% HTTPVersion = string() +%% StatusCode = integer() +%% ReasonPhrase = string() +%% Headers = [Header] +%% Header = {Field, Value} +%% Field = string() +%% Value = string() +%% Body = string() | binary() - HTLM-code +%% +%% Description: Sends a HTTP-request. The function can be both +%% syncronus and asynchronous in the later case the function will +%% return {ok, RequestId} and later on a message will be sent to the +%% calling process on the format {http, {RequestId, {StatusLine, +%% Headers, Body}}} or {http, {RequestId, {error, Reason}}} +%%-------------------------------------------------------------------------- + +request(Method, Request, HttpOptions, Options) -> + request(Method, Request, HttpOptions, Options, default_profile()). + +request(Method, {Url, Headers}, HTTPOptions, Options, Profile) + when (Method =:= options) orelse + (Method =:= get) orelse + (Method =:= head) orelse + (Method =:= delete) orelse + (Method =:= trace) andalso + (is_atom(Profile) orelse is_pid(Profile)) -> + ?hcrt("request", [{method, Method}, + {url, Url}, + {headers, Headers}, + {http_options, HTTPOptions}, + {options, Options}, + {profile, Profile}]), + case http_uri:parse(Url) of + {error, Reason} -> + {error, Reason}; + ParsedUrl -> + handle_request(Method, Url, ParsedUrl, Headers, [], [], + HTTPOptions, Options, Profile) + end; + +request(Method, {Url,Headers,ContentType,Body}, HTTPOptions, Options, Profile) + when ((Method =:= post) orelse (Method =:= put)) andalso + (is_atom(Profile) orelse is_pid(Profile)) -> + ?hcrt("request", [{method, Method}, + {url, Url}, + {headers, Headers}, + {content_type, ContentType}, + {body, Body}, + {http_options, HTTPOptions}, + {options, Options}, + {profile, Profile}]), + case http_uri:parse(Url) of + {error, Reason} -> + {error, Reason}; + ParsedUrl -> + handle_request(Method, Url, + ParsedUrl, Headers, ContentType, Body, + HTTPOptions, Options, Profile) + end. + + +%%-------------------------------------------------------------------------- +%% cancel_request(RequestId) -> ok +%% cancel_request(RequestId, Profile) -> ok +%% RequestId - As returned by request/4 +%% +%% Description: Cancels a HTTP-request. +%%------------------------------------------------------------------------- +cancel_request(RequestId) -> + cancel_request(RequestId, default_profile()). + +cancel_request(RequestId, Profile) + when is_atom(Profile) orelse is_pid(Profile) -> + ?hcrt("cancel request", [{request_id, RequestId}, {profile, Profile}]), + ok = httpc_manager:cancel_request(RequestId, profile_name(Profile)), + receive + %% If the request was already fulfilled throw away the + %% answer as the request has been canceled. + {http, {RequestId, _}} -> + ok + after 0 -> + ok + end. + + +%%-------------------------------------------------------------------------- +%% set_options(Options) -> ok | {error, Reason} +%% set_options(Options, Profile) -> ok | {error, Reason} +%% Options - [Option] +%% Profile - atom() +%% Option - {proxy, {Proxy, NoProxy}} | {max_sessions, MaxSessions} | +%% {max_pipeline_length, MaxPipeline} | +%% {pipeline_timeout, PipelineTimeout} | {cookies, CookieMode} | +%% {ipfamily, IpFamily} +%% Proxy - {Host, Port} +%% NoProxy - [Domain | HostName | IPAddress] +%% MaxSessions, MaxPipeline, PipelineTimeout = integer() +%% CookieMode - enabled | disabled | verify +%% IpFamily - inet | inet6 | inet6fb4 +%% Description: Informs the httpc_manager of the new settings. +%%------------------------------------------------------------------------- +set_options(Options) -> + set_options(Options, default_profile()). +set_options(Options, Profile) when is_atom(Profile) orelse is_pid(Profile) -> + ?hcrt("set cookies", [{options, Options}, {profile, Profile}]), + case validate_options(Options) of + {ok, Opts} -> + try + begin + httpc_manager:set_options(Opts, profile_name(Profile)) + end + catch + exit:{noproc, _} -> + {error, inets_not_started} + end; + {error, Reason} -> + {error, Reason} + end. + +set_option(Key, Value) -> + set_option(Key, Value, default_profile()). + +set_option(Key, Value, Profile) -> + set_options([{Key, Value}], Profile). + + +%%-------------------------------------------------------------------------- +%% store_cookies(SetCookieHeaders, Url [, Profile]) -> ok | {error, reason} +%% +%% +%% Description: Store the cookies from <SetCookieHeaders> +%% in the cookie database +%% for the profile <Profile>. This function shall be used when the option +%% cookie is set to verify. +%%------------------------------------------------------------------------- +store_cookies(SetCookieHeaders, Url) -> + store_cookies(SetCookieHeaders, Url, default_profile()). + +store_cookies(SetCookieHeaders, Url, Profile) + when is_atom(Profile) orelse is_pid(Profile) -> + ?hcrt("store cookies", [{set_cookie_headers, SetCookieHeaders}, + {url, Url}, + {profile, Profile}]), + try + begin + {_, _, Host, Port, Path, _} = http_uri:parse(Url), + Address = {Host, Port}, + ProfileName = profile_name(Profile), + Cookies = httpc_cookie:cookies(SetCookieHeaders, Path, Host), + httpc_manager:store_cookies(Cookies, Address, ProfileName), + ok + end + catch + exit:{noproc, _} -> + {error, {not_started, Profile}}; + error:{badmatch, Bad} -> + {error, {parse_failed, Bad}} + end. + + +%%-------------------------------------------------------------------------- +%% cookie_header(Url [, Profile]) -> Header | {error, Reason} +%% +%% Description: Returns the cookie header that would be sent when making +%% a request to <Url>. +%%------------------------------------------------------------------------- +cookie_header(Url) -> + cookie_header(Url, default_profile()). + +cookie_header(Url, Profile) -> + ?hcrt("cookie header", [{url, Url}, + {profile, Profile}]), + try + begin + httpc_manager:which_cookies(Url, profile_name(Profile)) + end + catch + exit:{noproc, _} -> + {error, {not_started, Profile}} + end. + + +%%-------------------------------------------------------------------------- +%% which_cookies() -> [cookie()] +%% which_cookies(Profile) -> [cookie()] +%% +%% Description: Debug function, dumping the cookie database +%%------------------------------------------------------------------------- +which_cookies() -> + which_cookies(default_profile()). + +which_cookies(Profile) -> + ?hcrt("which cookies", [{profile, Profile}]), + try + begin + httpc_manager:which_cookies(profile_name(Profile)) + end + catch + exit:{noproc, _} -> + {error, {not_started, Profile}} + end. + + +%%-------------------------------------------------------------------------- +%% info() -> list() +%% info(Profile) -> list() +%% +%% Description: Debug function, retreive info about the profile +%%------------------------------------------------------------------------- +info() -> + info(default_profile()). + +info(Profile) -> + ?hcrt("info", [{profile, Profile}]), + try + begin + httpc_manager:info(profile_name(Profile)) + end + catch + exit:{noproc, _} -> + {error, {not_started, Profile}} + end. + + +%%-------------------------------------------------------------------------- +%% reset_cookies() -> void() +%% reset_cookies(Profile) -> void() +%% +%% Description: Debug function, reset the cookie database +%%------------------------------------------------------------------------- +reset_cookies() -> + reset_cookies(default_profile()). + +reset_cookies(Profile) -> + ?hcrt("reset cookies", [{profile, Profile}]), + try + begin + httpc_manager:reset_cookies(profile_name(Profile)) + end + catch + exit:{noproc, _} -> + {error, {not_started, Profile}} + end. + + +%%-------------------------------------------------------------------------- +%% stream_next(Pid) -> Header | {error, Reason} +%% +%% Description: Triggers the next message to be streamed, e.i. +%% same behavior as active once for sockets. +%%------------------------------------------------------------------------- +stream_next(Pid) -> + ?hcrt("stream next", [{handler, Pid}]), + httpc_handler:stream_next(Pid). + + +%%%======================================================================== +%%% Behaviour callbacks +%%%======================================================================== +start_standalone(PropList) -> + ?hcrt("start standalone", [{proplist, PropList}]), + case proplists:get_value(profile, PropList) of + undefined -> + {error, no_profile}; + Profile -> + Dir = + proplists:get_value(data_dir, PropList, only_session_cookies), + httpc_manager:start_link(Profile, Dir, stand_alone) + end. + +start_service(Config) -> + ?hcrt("start service", [{config, Config}]), + httpc_profile_sup:start_child(Config). + +stop_service(Profile) when is_atom(Profile) -> + ?hcrt("stop service", [{profile, Profile}]), + httpc_profile_sup:stop_child(Profile); +stop_service(Pid) when is_pid(Pid) -> + ?hcrt("stop service", [{pid, Pid}]), + case service_info(Pid) of + {ok, [{profile, Profile}]} -> + stop_service(Profile); + Error -> + Error + end. + +services() -> + [{httpc, Pid} || {_, Pid, _, _} <- + supervisor:which_children(httpc_profile_sup)]. +service_info(Pid) -> + try [{ChildName, ChildPid} || + {ChildName, ChildPid, _, _} <- + supervisor:which_children(httpc_profile_sup)] of + Children -> + child_name2info(child_name(Pid, Children)) + catch + exit:{noproc, _} -> + {error, service_not_available} + end. + + +%%%======================================================================== +%%% Internal functions +%%%======================================================================== + +handle_request(Method, Url, + {Scheme, UserInfo, Host, Port, Path, Query}, + Headers, ContentType, Body, + HTTPOptions0, Options0, Profile) -> + + Started = http_util:timestamp(), + NewHeaders = [{http_util:to_lower(Key), Val} || {Key, Val} <- Headers], + + try + begin + HTTPOptions = http_options(HTTPOptions0), + Options = request_options(Options0), + Sync = proplists:get_value(sync, Options), + Stream = proplists:get_value(stream, Options), + Host2 = header_host(Host, Port), + HeadersRecord = header_record(NewHeaders, Host2, HTTPOptions), + Receiver = proplists:get_value(receiver, Options), + SocketOpts = proplists:get_value(socket_opts, Options), + Request = #request{from = Receiver, + scheme = Scheme, + address = {Host, Port}, + path = Path, + pquery = Query, + method = Method, + headers = HeadersRecord, + content = {ContentType, Body}, + settings = HTTPOptions, + abs_uri = Url, + userinfo = UserInfo, + stream = Stream, + headers_as_is = headers_as_is(Headers, Options), + socket_opts = SocketOpts, + started = Started}, + case httpc_manager:request(Request, profile_name(Profile)) of + {ok, RequestId} -> + handle_answer(RequestId, Sync, Options); + {error, Reason} -> + {error, Reason} + end + end + catch + error:{noproc, _} -> + {error, {not_started, Profile}}; + throw:Error -> + Error + end. + + +handle_answer(RequestId, false, _) -> + {ok, RequestId}; +handle_answer(RequestId, true, Options) -> + receive + {http, {RequestId, saved_to_file}} -> + {ok, saved_to_file}; + {http, {RequestId, {_,_,_} = Result}} -> + return_answer(Options, Result); + {http, {RequestId, {error, Reason}}} -> + {error, Reason} + end. + +return_answer(Options, {{"HTTP/0.9",_,_}, _, BinBody}) -> + Body = maybe_format_body(BinBody, Options), + {ok, Body}; + +return_answer(Options, {StatusLine, Headers, BinBody}) -> + + Body = maybe_format_body(BinBody, Options), + + case proplists:get_value(full_result, Options, true) of + true -> + {ok, {StatusLine, Headers, Body}}; + false -> + {_, Status, _} = StatusLine, + {ok, {Status, Body}} + end. + +maybe_format_body(BinBody, Options) -> + case proplists:get_value(body_format, Options, string) of + string -> + binary_to_list(BinBody); + _ -> + BinBody + end. + +%% This options is a workaround for http servers that do not follow the +%% http standard and have case sensative header parsing. Should only be +%% used if there is no other way to communicate with the server or for +%% testing purpose. +headers_as_is(Headers, Options) -> + case proplists:get_value(headers_as_is, Options, false) of + false -> + []; + true -> + Headers + end. + + +http_options(HttpOptions) -> + HttpOptionsDefault = http_options_default(), + http_options(HttpOptionsDefault, HttpOptions, #http_options{}). + +http_options([], [], Acc) -> + Acc; +http_options([], HttpOptions, Acc) -> + Fun = fun(BadOption) -> + Report = io_lib:format("Invalid option ~p ignored ~n", + [BadOption]), + error_logger:info_report(Report) + end, + lists:foreach(Fun, HttpOptions), + Acc; +http_options([{Tag, Default, Idx, Post} | Defaults], HttpOptions, Acc) -> + case lists:keysearch(Tag, 1, HttpOptions) of + {value, {Tag, Val0}} -> + case Post(Val0) of + {ok, Val} -> + Acc2 = setelement(Idx, Acc, Val), + HttpOptions2 = lists:keydelete(Tag, 1, HttpOptions), + http_options(Defaults, HttpOptions2, Acc2); + error -> + Report = io_lib:format("Invalid option ~p:~p ignored ~n", + [Tag, Val0]), + error_logger:info_report(Report), + HttpOptions2 = lists:keydelete(Tag, 1, HttpOptions), + http_options(Defaults, HttpOptions2, Acc) + end; + false -> + DefaultVal = + case Default of + {value, Val} -> + Val; + {field, DefaultIdx} -> + element(DefaultIdx, Acc) + end, + Acc2 = setelement(Idx, Acc, DefaultVal), + http_options(Defaults, HttpOptions, Acc2) + end. + +http_options_default() -> + VersionPost = + fun(Value) when is_atom(Value) -> + {ok, http_util:to_upper(atom_to_list(Value))}; + (Value) when is_list(Value) -> + {ok, http_util:to_upper(Value)}; + (_) -> + error + end, + TimeoutPost = fun(Value) when is_integer(Value) andalso (Value >= 0) -> + {ok, Value}; + (infinity = Value) -> + {ok, Value}; + (_) -> + error + end, + AutoRedirectPost = fun(Value) when (Value =:= true) orelse + (Value =:= false) -> + {ok, Value}; + (_) -> + error + end, + SslPost = fun(Value) when is_list(Value) -> + {ok, Value}; + (_) -> + error + end, + ProxyAuthPost = fun({User, Passwd} = Value) when is_list(User) andalso + is_list(Passwd) -> + {ok, Value}; + (_) -> + error + end, + RelaxedPost = fun(Value) when (Value =:= true) orelse + (Value =:= false) -> + {ok, Value}; + (_) -> + error + end, + ConnTimeoutPost = + fun(Value) when is_integer(Value) andalso (Value >= 0) -> + {ok, Value}; + (infinity = Value) -> + {ok, Value}; + (_) -> + error + end, + [ + {version, {value, "HTTP/1.1"}, #http_options.version, VersionPost}, + {timeout, {value, ?HTTP_REQUEST_TIMEOUT}, #http_options.timeout, TimeoutPost}, + {autoredirect, {value, true}, #http_options.autoredirect, AutoRedirectPost}, + {ssl, {value, []}, #http_options.ssl, SslPost}, + {proxy_auth, {value, undefined}, #http_options.proxy_auth, ProxyAuthPost}, + {relaxed, {value, false}, #http_options.relaxed, RelaxedPost}, + %% this field has to be *after* the timeout field (as that field is used for the default value) + {connect_timeout, {field, #http_options.timeout}, #http_options.connect_timeout, ConnTimeoutPost} + ]. + + +request_options_defaults() -> + VerifyBoolean = + fun(Value) when ((Value =:= true) orelse (Value =:= false)) -> + ok; + (_) -> + error + end, + + VerifySync = VerifyBoolean, + + VerifyStream = + fun(none = _Value) -> + ok; + (self = _Value) -> + ok; + ({self, once} = _Value) -> + ok; + (Value) when is_list(Value) -> + ok; + (_) -> + error + end, + + VerifyBodyFormat = + fun(string = _Value) -> + ok; + (binary = _Value) -> + ok; + (_) -> + error + end, + + VerifyFullResult = VerifyBoolean, + + VerifyHeaderAsIs = VerifyBoolean, + + VerifyReceiver = + fun(Value) when is_pid(Value) -> + ok; + ({M, F, A}) when (is_atom(M) andalso + is_atom(F) andalso + is_list(A)) -> + ok; + (Value) when is_function(Value, 1) -> + ok; + (_) -> + error + end, + + VerifySocketOpts = + fun([]) -> + {ok, undefined}; + (Value) when is_list(Value) -> + ok; + (_) -> + error + end, + + [ + {sync, true, VerifySync}, + {stream, none, VerifyStream}, + {body_format, string, VerifyBodyFormat}, + {full_result, true, VerifyFullResult}, + {headers_as_is, false, VerifyHeaderAsIs}, + {receiver, self(), VerifyReceiver}, + {socket_opts, undefined, VerifySocketOpts} + ]. + +request_options(Options) -> + Defaults = request_options_defaults(), + request_options(Defaults, Options, []). + +request_options([], [], Acc) -> + request_options_sanity_check(Acc), + lists:reverse(Acc); +request_options([], Options, Acc) -> + Fun = fun(BadOption) -> + Report = io_lib:format("Invalid option ~p ignored ~n", + [BadOption]), + error_logger:info_report(Report) + end, + lists:foreach(Fun, Options), + Acc; +request_options([{Key, DefaultVal, Verify} | Defaults], Options, Acc) -> + case lists:keysearch(Key, 1, Options) of + {value, {Key, Value}} -> + case Verify(Value) of + ok -> + Options2 = lists:keydelete(Key, 1, Options), + request_options(Defaults, Options2, [{Key, Value} | Acc]); + {ok, Value2} -> + Options2 = lists:keydelete(Key, 1, Options), + request_options(Defaults, Options2, [{Key, Value2} | Acc]); + error -> + Report = io_lib:format("Invalid option ~p:~p ignored ~n", + [Key, Value]), + error_logger:info_report(Report), + Options2 = lists:keydelete(Key, 1, Options), + request_options(Defaults, Options2, Acc) + end; + false -> + request_options(Defaults, Options, [{Key, DefaultVal} | Acc]) + end. + +request_options_sanity_check(Opts) -> + case proplists:get_value(sync, Opts) of + Sync when (Sync =:= true) -> + case proplists:get_value(receiver, Opts) of + Pid when is_pid(Pid) andalso (Pid =:= self()) -> + ok; + BadReceiver -> + throw({error, {bad_options_combo, + [{sync, true}, {receiver, BadReceiver}]}}) + end, + case proplists:get_value(stream, Opts) of + Stream when (Stream =:= self) orelse + (Stream =:= {self, once}) -> + throw({error, streaming_error}); + _ -> + ok + end; + _ -> + ok + end, + ok. + +validate_options(Options) -> + (catch validate_options(Options, [])). + +validate_options([], ValidateOptions) -> + {ok, lists:reverse(ValidateOptions)}; + +validate_options([{proxy, Proxy} = Opt| Tail], Acc) -> + validate_proxy(Proxy), + validate_options(Tail, [Opt | Acc]); + +validate_options([{max_sessions, Value} = Opt| Tail], Acc) -> + validate_max_sessions(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{keep_alive_timeout, Value} = Opt| Tail], Acc) -> + validate_keep_alive_timeout(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{max_keep_alive_length, Value} = Opt| Tail], Acc) -> + validate_max_keep_alive_length(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{pipeline_timeout, Value} = Opt| Tail], Acc) -> + validate_pipeline_timeout(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{max_pipeline_length, Value} = Opt| Tail], Acc) -> + validate_max_pipeline_length(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{cookies, Value} = Opt| Tail], Acc) -> + validate_cookies(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{ipfamily, Value} = Opt| Tail], Acc) -> + validate_ipfamily(Value), + validate_options(Tail, [Opt | Acc]); + +%% For backward compatibillity +validate_options([{ipv6, Value}| Tail], Acc) -> + NewValue = validate_ipv6(Value), + Opt = {ipfamily, NewValue}, + validate_options(Tail, [Opt | Acc]); + +validate_options([{ip, Value} = Opt| Tail], Acc) -> + validate_ip(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{port, Value} = Opt| Tail], Acc) -> + validate_port(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{socket_opts, Value} = Opt| Tail], Acc) -> + validate_socket_opts(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{verbose, Value} = Opt| Tail], Acc) -> + validate_verbose(Value), + validate_options(Tail, [Opt | Acc]); + +validate_options([{_, _} = Opt| _], _Acc) -> + {error, {not_an_option, Opt}}. + + +validate_proxy({{ProxyHost, ProxyPort}, NoProxy} = Proxy) + when is_list(ProxyHost) andalso + is_integer(ProxyPort) andalso + is_list(NoProxy) -> + Proxy; +validate_proxy(BadProxy) -> + bad_option(proxy, BadProxy). + +validate_max_sessions(Value) when is_integer(Value) andalso (Value >= 0) -> + Value; +validate_max_sessions(BadValue) -> + bad_option(max_sessions, BadValue). + +validate_keep_alive_timeout(Value) when is_integer(Value) andalso (Value >= 0) -> + Value; +validate_keep_alive_timeout(infinity = Value) -> + Value; +validate_keep_alive_timeout(BadValue) -> + bad_option(keep_alive_timeout, BadValue). + +validate_max_keep_alive_length(Value) when is_integer(Value) andalso (Value >= 0) -> + Value; +validate_max_keep_alive_length(BadValue) -> + bad_option(max_keep_alive_length, BadValue). + +validate_pipeline_timeout(Value) when is_integer(Value) -> + Value; +validate_pipeline_timeout(infinity = Value) -> + Value; +validate_pipeline_timeout(BadValue) -> + bad_option(pipeline_timeout, BadValue). + +validate_max_pipeline_length(Value) when is_integer(Value) -> + Value; +validate_max_pipeline_length(BadValue) -> + bad_option(max_pipeline_length, BadValue). + +validate_cookies(Value) + when ((Value =:= enabled) orelse + (Value =:= disabled) orelse + (Value =:= verify)) -> + Value; +validate_cookies(BadValue) -> + bad_option(cookies, BadValue). + +validate_ipv6(Value) when (Value =:= enabled) orelse (Value =:= disabled) -> + case Value of + enabled -> + inet6fb4; + disabled -> + inet + end; +validate_ipv6(BadValue) -> + bad_option(ipv6, BadValue). + +validate_ipfamily(Value) + when (Value =:= inet) orelse (Value =:= inet6) orelse (Value =:= inet6fb4) -> + Value; +validate_ipfamily(BadValue) -> + bad_option(ipfamily, BadValue). + +validate_ip(Value) + when is_tuple(Value) andalso ((size(Value) =:= 4) orelse (size(Value) =:= 8)) -> + Value; +validate_ip(BadValue) -> + bad_option(ip, BadValue). + +validate_port(Value) when is_integer(Value) -> + Value; +validate_port(BadValue) -> + bad_option(port, BadValue). + +validate_socket_opts(Value) when is_list(Value) -> + Value; +validate_socket_opts(BadValue) -> + bad_option(socket_opts, BadValue). + +validate_verbose(Value) + when ((Value =:= false) orelse + (Value =:= verbose) orelse + (Value =:= debug) orelse + (Value =:= trace)) -> + ok; +validate_verbose(BadValue) -> + bad_option(verbose, BadValue). + +bad_option(Option, BadValue) -> + throw({error, {bad_option, Option, BadValue}}). + + +header_host(Host, 80 = _Port) -> + Host; +header_host(Host, Port) -> + Host ++ ":" ++ integer_to_list(Port). + + +header_record(NewHeaders, Host, #http_options{version = Version}) -> + header_record(NewHeaders, #http_request_h{}, Host, Version). + +header_record([], RequestHeaders, Host, Version) -> + validate_headers(RequestHeaders, Host, Version); +header_record([{"cache-control", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'cache-control' = Val}, + Host, Version); +header_record([{"connection", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{connection = Val}, Host, + Version); +header_record([{"date", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{date = Val}, Host, + Version); +header_record([{"pragma", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{pragma = Val}, Host, + Version); +header_record([{"trailer", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{trailer = Val}, Host, + Version); +header_record([{"transfer-encoding", Val} | Rest], RequestHeaders, Host, + Version) -> + header_record(Rest, + RequestHeaders#http_request_h{'transfer-encoding' = Val}, + Host, Version); +header_record([{"upgrade", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{upgrade = Val}, Host, + Version); +header_record([{"via", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{via = Val}, Host, + Version); +header_record([{"warning", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{warning = Val}, Host, + Version); +header_record([{"accept", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{accept = Val}, Host, + Version); +header_record([{"accept-charset", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'accept-charset' = Val}, + Host, Version); +header_record([{"accept-encoding", Val} | Rest], RequestHeaders, Host, + Version) -> + header_record(Rest, RequestHeaders#http_request_h{'accept-encoding' = Val}, + Host, Version); +header_record([{"accept-language", Val} | Rest], RequestHeaders, Host, + Version) -> + header_record(Rest, RequestHeaders#http_request_h{'accept-language' = Val}, + Host, Version); +header_record([{"authorization", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{authorization = Val}, + Host, Version); +header_record([{"expect", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{expect = Val}, Host, + Version); +header_record([{"from", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{from = Val}, Host, + Version); +header_record([{"host", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{host = Val}, Host, + Version); +header_record([{"if-match", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'if-match' = Val}, + Host, Version); +header_record([{"if-modified-since", Val} | Rest], RequestHeaders, Host, + Version) -> + header_record(Rest, + RequestHeaders#http_request_h{'if-modified-since' = Val}, + Host, Version); +header_record([{"if-none-match", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'if-none-match' = Val}, + Host, Version); +header_record([{"if-range", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'if-range' = Val}, + Host, Version); + +header_record([{"if-unmodified-since", Val} | Rest], RequestHeaders, Host, + Version) -> + header_record(Rest, RequestHeaders#http_request_h{'if-unmodified-since' + = Val}, Host, Version); +header_record([{"max-forwards", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'max-forwards' = Val}, + Host, Version); +header_record([{"proxy-authorization", Val} | Rest], RequestHeaders, Host, + Version) -> + header_record(Rest, RequestHeaders#http_request_h{'proxy-authorization' + = Val}, Host, Version); +header_record([{"range", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{range = Val}, Host, + Version); +header_record([{"referer", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{referer = Val}, Host, + Version); +header_record([{"te", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{te = Val}, Host, + Version); +header_record([{"user-agent", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'user-agent' = Val}, + Host, Version); +header_record([{"allow", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{allow = Val}, Host, + Version); +header_record([{"content-encoding", Val} | Rest], RequestHeaders, Host, + Version) -> + header_record(Rest, + RequestHeaders#http_request_h{'content-encoding' = Val}, + Host, Version); +header_record([{"content-language", Val} | Rest], RequestHeaders, + Host, Version) -> + header_record(Rest, + RequestHeaders#http_request_h{'content-language' = Val}, + Host, Version); +header_record([{"content-length", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'content-length' = Val}, + Host, Version); +header_record([{"content-location", Val} | Rest], RequestHeaders, + Host, Version) -> + header_record(Rest, + RequestHeaders#http_request_h{'content-location' = Val}, + Host, Version); +header_record([{"content-md5", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'content-md5' = Val}, + Host, Version); +header_record([{"content-range", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'content-range' = Val}, + Host, Version); +header_record([{"content-type", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'content-type' = Val}, + Host, Version); +header_record([{"expires", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{expires = Val}, Host, + Version); +header_record([{"last-modified", Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{'last-modified' = Val}, + Host, Version); +header_record([{Key, Val} | Rest], RequestHeaders, Host, Version) -> + header_record(Rest, RequestHeaders#http_request_h{ + other = [{Key, Val} | + RequestHeaders#http_request_h.other]}, + Host, Version). + +validate_headers(RequestHeaders = #http_request_h{te = undefined}, Host, + "HTTP/1.1" = Version) -> + validate_headers(RequestHeaders#http_request_h{te = ""}, Host, + "HTTP/1.1" = Version); +validate_headers(RequestHeaders = #http_request_h{host = undefined}, + Host, "HTTP/1.1" = Version) -> + validate_headers(RequestHeaders#http_request_h{host = Host}, Host, Version); +validate_headers(RequestHeaders, _, _) -> + RequestHeaders. + + +child_name2info(undefined) -> + {error, no_such_service}; +child_name2info(httpc_manager) -> + {ok, [{profile, default}]}; +child_name2info({httpc, Profile}) -> + {ok, [{profile, Profile}]}. + +child_name(_, []) -> + undefined; +child_name(Pid, [{Name, Pid} | _]) -> + Name; +child_name(Pid, [_ | Children]) -> + child_name(Pid, Children). + +%% d(F) -> +%% d(F, []). + +%% d(F, A) -> +%% d(get(dbg), F, A). + +%% d(true, F, A) -> +%% io:format(user, "~w:~w:" ++ F ++ "~n", [self(), ?MODULE | A]); +%% d(_, _, _) -> +%% ok. + diff --git a/lib/inets/src/http_client/http_cookie.erl b/lib/inets/src/http_client/httpc_cookie.erl index e091070f72..586701b4a1 100644 --- a/lib/inets/src/http_client/http_cookie.erl +++ b/lib/inets/src/http_client/httpc_cookie.erl @@ -1,164 +1,260 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2004-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2004-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% Description: Cookie handling according to RFC 2109 --module(http_cookie). +-module(httpc_cookie). -include("httpc_internal.hrl"). --export([header/4, cookies/3, open_cookie_db/1, close_cookie_db/1, insert/2]). +-export([open_db/3, close_db/1, insert/2, header/4, cookies/3]). +-export([reset_db/1, which_cookies/1]). + +-record(cookie_db, {db, session_db}). + %%%========================================================================= %%% API %%%========================================================================= -header(Scheme, {Host, _}, Path, CookieDb) -> - case lookup_cookies(Host, Path, CookieDb) of - [] -> - {"cookie", ""}; - Cookies -> - {"cookie", cookies_to_string(Scheme, Cookies)} + +%%-------------------------------------------------------------------- +%% Func: open_db(DbName, DbDir, SessionDbName) -> #cookie_db{} +%% Purpose: Create the cookie db +%%-------------------------------------------------------------------- + +open_db(_, only_session_cookies, SessionDbName) -> + ?hcrt("open (session cookies only) db", + [{session_db_name, SessionDbName}]), + SessionDb = ets:new(SessionDbName, + [protected, bag, {keypos, #http_cookie.domain}]), + #cookie_db{session_db = SessionDb}; + +open_db(Name, Dir, SessionDbName) -> + ?hcrt("open db", + [{name, Name}, {dir, Dir}, {session_db_name, SessionDbName}]), + File = filename:join(Dir, atom_to_list(Name)), + case dets:open_file(Name, [{keypos, #http_cookie.domain}, + {type, bag}, + {file, File}, + {ram_file, true}]) of + {ok, Db} -> + SessionDb = ets:new(SessionDbName, + [protected, bag, + {keypos, #http_cookie.domain}]), + #cookie_db{db = Db, session_db = SessionDb}; + {error, Reason} -> + throw({error, {failed_open_file, Name, File, Reason}}) end. -cookies(Headers, RequestPath, RequestHost) -> - Cookies = parse_set_cookies(Headers, {RequestPath, RequestHost}), - accept_cookies(Cookies, RequestPath, RequestHost). - -open_cookie_db({{_, only_session_cookies}, SessionDbName}) -> - EtsDb = ets:new(SessionDbName, [protected, bag, - {keypos, #http_cookie.domain}]), - {undefined, EtsDb}; - -open_cookie_db({{DbName, Dbdir}, SessionDbName}) -> - File = filename:join(Dbdir, atom_to_list(DbName)), - {ok, DetsDb} = dets:open_file(DbName, [{keypos, #http_cookie.domain}, - {type, bag}, - {file, File}, - {ram_file, true}]), - EtsDb = ets:new(SessionDbName, [protected, bag, - {keypos, #http_cookie.domain}]), - {DetsDb, EtsDb}. - -close_cookie_db({undefined, EtsDb}) -> - ets:delete(EtsDb); - -close_cookie_db({DetsDb, EtsDb}) -> - dets:close(DetsDb), - ets:delete(EtsDb). + +%%-------------------------------------------------------------------- +%% Func: reset_db(CookieDb) -> void() +%% Purpose: Reset (empty) the cookie database +%% +%%-------------------------------------------------------------------- + +reset_db(#cookie_db{db = undefined, session_db = SessionDb}) -> + ets:delete_all_objects(SessionDb), + ok; +reset_db(#cookie_db{db = Db, session_db = SessionDb}) -> + dets:delete_all_objects(Db), + ets:delete_all_objects(SessionDb), + ok. + + +%%-------------------------------------------------------------------- +%% Func: close_db(CookieDb) -> ok +%% Purpose: Close the cookie db +%%-------------------------------------------------------------------- + +close_db(#cookie_db{db = Db, session_db = SessionDb}) -> + ?hcrt("close db", []), + maybe_dets_close(Db), + ets:delete(SessionDb), + ok. + +maybe_dets_close(undefined) -> + ok; +maybe_dets_close(Db) -> + dets:close(Db). + + +%%-------------------------------------------------------------------- +%% Func: insert(CookieDb) -> ok +%% Purpose: Close the cookie db +%%-------------------------------------------------------------------- %% If no persistent cookie database is defined we %% treat all cookies as if they where session cookies. -insert(Cookie = #http_cookie{max_age = Int}, - Dbs = {undefined, _}) when is_integer(Int) -> - insert(Cookie#http_cookie{max_age = session}, Dbs); - -insert(Cookie = #http_cookie{domain = Key, name = Name, - path = Path, max_age = session}, - Db = {_, CookieDb}) -> - case ets:match_object(CookieDb, #http_cookie{domain = Key, - name = Name, - path = Path, - _ = '_'}) of +insert(#cookie_db{db = undefined} = CookieDb, + #http_cookie{max_age = Int} = Cookie) when is_integer(Int) -> + insert(CookieDb, Cookie#http_cookie{max_age = session}); + +insert(#cookie_db{session_db = SessionDb} = CookieDb, + #http_cookie{domain = Key, + name = Name, + path = Path, + max_age = session} = Cookie) -> + ?hcrt("insert session cookie", [{cookie, Cookie}]), + Pattern = #http_cookie{domain = Key, name = Name, path = Path, _ = '_'}, + case ets:match_object(SessionDb, Pattern) of [] -> - ets:insert(CookieDb, Cookie); + ets:insert(SessionDb, Cookie); [NewCookie] -> - delete(NewCookie, Db), - ets:insert(CookieDb, Cookie) + delete(CookieDb, NewCookie), + ets:insert(SessionDb, Cookie) end, ok; -insert(#http_cookie{domain = Key, name = Name, - path = Path, max_age = 0}, - Db = {CookieDb, _}) -> - case dets:match_object(CookieDb, #http_cookie{domain = Key, - name = Name, - path = Path, - _ = '_'}) of +insert(#cookie_db{db = Db} = CookieDb, + #http_cookie{domain = Key, + name = Name, + path = Path, + max_age = 0}) -> + ?hcrt("insert", [{domain, Key}, {name, Name}, {path, Path}]), + Pattern = #http_cookie{domain = Key, name = Name, path = Path, _ = '_'}, + case dets:match_object(Db, Pattern) of [] -> ok; [NewCookie] -> - delete(NewCookie, Db) + delete(CookieDb, NewCookie) end, ok; -insert(Cookie = #http_cookie{domain = Key, name = Name, path = Path}, - Db = {CookieDb, _}) -> - case dets:match_object(CookieDb, #http_cookie{domain = Key, - name = Name, - path = Path, - _ = '_'}) of +insert(#cookie_db{db = Db} = CookieDb, + #http_cookie{domain = Key, name = Name, path = Path} = Cookie) -> + ?hcrt("insert", [{cookie, Cookie}]), + Pattern = #http_cookie{domain = Key, + name = Name, + path = Path, + _ = '_'}, + case dets:match_object(Db, Pattern) of [] -> - dets:insert(CookieDb, Cookie); - [NewCookie] -> - delete(NewCookie, Db), - dets:insert(CookieDb, Cookie) + dets:insert(Db, Cookie); + [OldCookie] -> + delete(CookieDb, OldCookie), + dets:insert(Db, Cookie) end, ok. + + +%%-------------------------------------------------------------------- +%% Func: header(CookieDb) -> ok +%% Purpose: Cookies +%%-------------------------------------------------------------------- + +header(CookieDb, Scheme, {Host, _}, Path) -> + ?hcrd("header", [{scheme, Scheme}, {host, Host}, {path, Path}]), + case lookup_cookies(CookieDb, Host, Path) of + [] -> + {"cookie", ""}; + Cookies -> + {"cookie", cookies_to_string(Scheme, Cookies)} + end. + + +%%-------------------------------------------------------------------- +%% Func: cookies(Headers, RequestPath, RequestHost) -> [cookie()] +%% Purpose: Which cookies are stored +%%-------------------------------------------------------------------- + +cookies(Headers, RequestPath, RequestHost) -> + ?hcrt("cookies", [{headers, Headers}, + {request_path, RequestPath}, + {request_host, RequestHost}]), + Cookies = parse_set_cookies(Headers, {RequestPath, RequestHost}), + accept_cookies(Cookies, RequestPath, RequestHost). + + +%%-------------------------------------------------------------------- +%% Func: which_cookies(CookieDb) -> [cookie()] +%% Purpose: For test and debug purpose, +%% dump the entire cookie database +%%-------------------------------------------------------------------- + +which_cookies(#cookie_db{db = undefined, session_db = SessionDb}) -> + SessionCookies = ets:tab2list(SessionDb), + [{session_cookies, SessionCookies}]; +which_cookies(#cookie_db{db = Db, session_db = SessionDb}) -> + Cookies = dets:match_object(Db, '_'), + SessionCookies = ets:tab2list(SessionDb), + [{cookies, Cookies}, {session_cookies, SessionCookies}]. + + %%%======================================================================== %%% Internal functions %%%======================================================================== -lookup_cookies(Key, {undefined, Ets}) -> - ets:match_object(Ets, #http_cookie{domain = Key, - _ = '_'}); -lookup_cookies(Key, {Dets,Ets}) -> - SessionCookies = ets:match_object(Ets, #http_cookie{domain = Key, - _ = '_'}), - Cookies = dets:match_object(Dets, #http_cookie{domain = Key, - _ = '_'}), + +delete(#cookie_db{session_db = SessionDb}, + #http_cookie{max_age = session} = Cookie) -> + ets:delete_object(SessionDb, Cookie); +delete(#cookie_db{db = Db}, Cookie) -> + dets:delete_object(Db, Cookie). + + +lookup_cookies(#cookie_db{db = undefined, session_db = SessionDb}, Key) -> + Pattern = #http_cookie{domain = Key, _ = '_'}, + Cookies = ets:match_object(SessionDb, Pattern), + ?hcrt("lookup cookies", [{cookies, Cookies}]), + Cookies; + +lookup_cookies(#cookie_db{db = Db, session_db = SessionDb}, Key) -> + Pattern = #http_cookie{domain = Key, _ = '_'}, + SessionCookies = ets:match_object(SessionDb, Pattern), + ?hcrt("lookup cookies", [{session_cookies, SessionCookies}]), + Cookies = dets:match_object(Db, Pattern), + ?hcrt("lookup cookies", [{cookies, Cookies}]), Cookies ++ SessionCookies. -delete(Cookie = #http_cookie{max_age = session}, {_, CookieDb}) -> - ets:delete_object(CookieDb, Cookie); -delete(Cookie, {CookieDb, _}) -> - dets:delete_object(CookieDb, Cookie). -lookup_cookies(Host, Path, Db) -> +lookup_cookies(CookieDb, Host, Path) -> Cookies = case http_util:is_hostname(Host) of true -> - HostCookies = lookup_cookies(Host, Db), + HostCookies = lookup_cookies(CookieDb, Host), [_| DomainParts] = string:tokens(Host, "."), - lookup_domain_cookies(DomainParts, Db, HostCookies); + lookup_domain_cookies(CookieDb, DomainParts, HostCookies); false -> % IP-adress - lookup_cookies(Host, Db) + lookup_cookies(CookieDb, Host) end, - ValidCookies = valid_cookies(Cookies, [], Db), + ValidCookies = valid_cookies(CookieDb, Cookies), lists:filter(fun(Cookie) -> lists:prefix(Cookie#http_cookie.path, Path) end, ValidCookies). %% For instance if Host=localhost -lookup_domain_cookies([], _, AccCookies) -> +lookup_domain_cookies(_CookieDb, [], AccCookies) -> lists:flatten(AccCookies); + %% Top domains can not have cookies -lookup_domain_cookies([_], _, AccCookies) -> +lookup_domain_cookies(_CookieDb, [_], AccCookies) -> lists:flatten(AccCookies); -lookup_domain_cookies([Next | DomainParts], CookieDb, AccCookies) -> + +lookup_domain_cookies(CookieDb, [Next | DomainParts], AccCookies) -> Domain = merge_domain_parts(DomainParts, [Next ++ "."]), - lookup_domain_cookies(DomainParts, CookieDb, - [lookup_cookies(Domain, CookieDb) - | AccCookies]). + lookup_domain_cookies(CookieDb, DomainParts, + [lookup_cookies(CookieDb, Domain) | AccCookies]). merge_domain_parts([Part], Merged) -> lists:flatten(["." | lists:reverse([Part | Merged])]); merge_domain_parts([Part| Rest], Merged) -> merge_domain_parts(Rest, [".", Part | Merged]). -cookies_to_string(Scheme, Cookies = [Cookie | _]) -> +cookies_to_string(Scheme, [Cookie | _] = Cookies) -> Version = "$Version=" ++ Cookie#http_cookie.version ++ "; ", cookies_to_string(Scheme, path_sort(Cookies), [Version]). @@ -170,7 +266,7 @@ cookies_to_string(_, [], CookieStrs) -> lists:flatten(lists:reverse(CookieStrs)) end; -cookies_to_string(https, [Cookie = #http_cookie{secure = true}| Cookies], +cookies_to_string(https, [#http_cookie{secure = true} = Cookie| Cookies], CookieStrs) -> Str = case Cookies of [] -> @@ -193,7 +289,7 @@ cookies_to_string(Scheme, [Cookie | Cookies], CookieStrs) -> end, cookies_to_string(Scheme, Cookies, [Str | CookieStrs]). -cookie_to_string(Cookie = #http_cookie{name = Name, value = Value}) -> +cookie_to_string(#http_cookie{name = Name, value = Value} = Cookie) -> Str = Name ++ "=" ++ Value, add_domain(add_path(Str, Cookie), Cookie). @@ -208,19 +304,19 @@ add_domain(Str, #http_cookie{domain = Domain}) -> Str ++ "; $Domain=" ++ Domain. parse_set_cookies(OtherHeaders, DefaultPathDomain) -> - SetCookieHeaders = lists:foldl(fun({"set-cookie", Value}, Acc) -> - [string:tokens(Value, ",")| Acc]; - (_, Acc) -> - Acc - end, [], OtherHeaders), + SetCookieHeaders = + lists:foldl(fun({"set-cookie", Value}, Acc) -> + [string:tokens(Value, ",")| Acc]; + (_, Acc) -> + Acc + end, [], OtherHeaders), - lists:flatten(lists:map(fun(CookieHeader) -> - NewHeader = - fix_netscape_cookie(CookieHeader, - []), - parse_set_cookie(NewHeader, [], - DefaultPathDomain) end, - SetCookieHeaders)). + lists:flatten( + lists:map(fun(CookieHeader) -> + NewHeader = fix_netscape_cookie(CookieHeader, []), + parse_set_cookie(NewHeader, [], DefaultPathDomain) + end, + SetCookieHeaders)). parse_set_cookie([], AccCookies, _) -> AccCookies; @@ -282,16 +378,16 @@ cookie_attributes([{"expires", Value}| Attributes], Cookie) -> Time = http_util:convert_netscapecookie_date(Value), ExpireTime = calendar:datetime_to_gregorian_seconds(Time), cookie_attributes(Attributes, - Cookie#http_cookie{max_age = ExpireTime}); + Cookie#http_cookie{max_age = ExpireTime}); cookie_attributes([{"path", Value}| Attributes], Cookie) -> cookie_attributes(Attributes, - Cookie#http_cookie{path = Value}); + Cookie#http_cookie{path = Value}); cookie_attributes([{"secure", _}| Attributes], Cookie) -> cookie_attributes(Attributes, - Cookie#http_cookie{secure = true}); + Cookie#http_cookie{secure = true}); cookie_attributes([{"version", Value}| Attributes], Cookie) -> cookie_attributes(Attributes, - Cookie#http_cookie{version = Value}); + Cookie#http_cookie{version = Value}); %% Disregard unknown attributes. cookie_attributes([_| Attributes], Cookie) -> cookie_attributes(Attributes, Cookie). @@ -302,8 +398,7 @@ domain_default(Cookie = #http_cookie{domain = undefined}, domain_default(Cookie, _) -> Cookie. -path_default(Cookie = #http_cookie{path = undefined}, - DefaultPath) -> +path_default(#http_cookie{path = undefined} = Cookie, DefaultPath) -> Cookie#http_cookie{path = skip_right_most_slash(DefaultPath), path_default = true}; path_default(Cookie, _) -> @@ -321,7 +416,10 @@ accept_cookies(Cookies, RequestPath, RequestHost) -> end, Cookies). accept_cookie(Cookie, RequestPath, RequestHost) -> - accept_path(Cookie, RequestPath) and accept_domain(Cookie, RequestHost). + Accepted = + accept_path(Cookie, RequestPath) andalso + accept_domain(Cookie, RequestHost), + Accepted. accept_path(#http_cookie{path = Path}, RequestPath) -> lists:prefix(Path, RequestPath). @@ -330,18 +428,20 @@ accept_domain(#http_cookie{domain = RequestHost}, RequestHost) -> true; accept_domain(#http_cookie{domain = Domain}, RequestHost) -> - HostCheck = case http_util:is_hostname(RequestHost) of - true -> - (lists:suffix(Domain, RequestHost) andalso - (not - lists:member($., - string:substr(RequestHost, 1, - (length(RequestHost) - - length(Domain)))))); - false -> - false - end, - HostCheck andalso (hd(Domain) == $.) + HostCheck = + case http_util:is_hostname(RequestHost) of + true -> + (lists:suffix(Domain, RequestHost) andalso + (not + lists:member($., + string:substr(RequestHost, 1, + (length(RequestHost) - + length(Domain)))))); + false -> + false + end, + HostCheck + andalso (hd(Domain) =:= $.) andalso (length(string:tokens(Domain, ".")) > 1). cookie_expires(0) -> @@ -356,16 +456,20 @@ is_cookie_expired(#http_cookie{max_age = ExpireTime}) -> NowSec = calendar:datetime_to_gregorian_seconds({date(), time()}), ExpireTime - NowSec =< 0. -valid_cookies([], Valid, _) -> + +valid_cookies(Db, Cookies) -> + valid_cookies(Db, Cookies, []). + +valid_cookies(_Db, [], Valid) -> Valid; -valid_cookies([Cookie | Cookies], Valid, Db) -> +valid_cookies(Db, [Cookie | Cookies], Valid) -> case is_cookie_expired(Cookie) of true -> - delete(Cookie, Db), - valid_cookies(Cookies, Valid, Db); + delete(Db, Cookie), + valid_cookies(Db, Cookies, Valid); false -> - valid_cookies(Cookies, [Cookie | Valid], Db) + valid_cookies(Db, Cookies, [Cookie | Valid]) end. path_sort(Cookies)-> diff --git a/lib/inets/src/http_client/httpc_handler.erl b/lib/inets/src/http_client/httpc_handler.erl index 7b737c2f86..fec74932a2 100644 --- a/lib/inets/src/http_client/httpc_handler.erl +++ b/lib/inets/src/http_client/httpc_handler.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2002-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2002-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -28,7 +28,15 @@ %%-------------------------------------------------------------------- %% Internal Application API --export([start_link/3, send/2, cancel/2, stream/3, stream_next/1]). +-export([ + start_link/2, + connect_and_send/2, + send/2, + cancel/2, + stream/3, + stream_next/1, + info/1 + ]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, @@ -50,7 +58,7 @@ mfa, % {Moduel, Function, Args} pipeline = queue:new(), % queue() keep_alive = queue:new(), % queue() - status = new, % new | pipeline | keep_alive | close | ssl_tunnel + status, % undefined | new | pipeline | keep_alive | close | ssl_tunnel canceled = [], % [RequestId] max_header_size = nolimit, % nolimit | integer() max_body_size = nolimit, % nolimit | integer() @@ -85,9 +93,13 @@ %% the reply or part of it has arrived.) %%-------------------------------------------------------------------- %%-------------------------------------------------------------------- -start_link(Request, Options, ProfileName) -> - {ok, proc_lib:spawn_link(?MODULE, init, [[Request, Options, - ProfileName]])}. + +start_link(Options, ProfileName) -> + Args = [Options, ProfileName], + gen_server:start_link(?MODULE, Args, []). + +connect_and_send(Request, HandlerPid) -> + call({connect_and_send, Request}, HandlerPid). %%-------------------------------------------------------------------- @@ -126,6 +138,18 @@ stream_next(Pid) -> %%-------------------------------------------------------------------- +%% Function: info(Pid) -> [{Key, Val}] +%% Pid = pid() - the pid of the http-request handler process. +%% +%% Description: +%% Returns various information related to this handler +%% Used for debugging and testing +%%-------------------------------------------------------------------- +info(Pid) -> + call(info, Pid). + + +%%-------------------------------------------------------------------- %% Function: stream(BodyPart, Request, Code) -> _ %% BodyPart = binary() %% Request = #request{} @@ -138,21 +162,21 @@ stream_next(Pid) -> %%-------------------------------------------------------------------- %% Request should not be streamed stream(BodyPart, Request = #request{stream = none}, _) -> - ?hcrt("stream - none", [{body_part, BodyPart}]), + ?hcrt("stream - none", []), {BodyPart, Request}; %% Stream to caller stream(BodyPart, Request = #request{stream = Self}, Code) when ((Code =:= 200) orelse (Code =:= 206)) andalso ((Self =:= self) orelse (Self =:= {self, once})) -> - ?hcrt("stream - self", [{stream, Self}, {code, Code}, {body_part, BodyPart}]), + ?hcrt("stream - self", [{stream, Self}, {code, Code}]), httpc_response:send(Request#request.from, {Request#request.id, stream, BodyPart}), {<<>>, Request}; stream(BodyPart, Request = #request{stream = Self}, 404) when (Self =:= self) orelse (Self =:= {self, once}) -> - ?hcrt("stream - self with 404", [{stream, Self}, {body_part, BodyPart}]), + ?hcrt("stream - self with 404", [{stream, Self}]), httpc_response:send(Request#request.from, {Request#request.id, stream, BodyPart}), {<<>>, Request}; @@ -162,7 +186,7 @@ stream(BodyPart, Request = #request{stream = Self}, 404) %% We keep this for backward compatibillity... stream(BodyPart, Request = #request{stream = Filename}, Code) when ((Code =:= 200) orelse (Code =:= 206)) andalso is_list(Filename) -> - ?hcrt("stream - filename", [{stream, Filename}, {code, Code}, {body_part, BodyPart}]), + ?hcrt("stream - filename", [{stream, Filename}, {code, Code}]), case file:open(Filename, [write, raw, append, delayed_write]) of {ok, Fd} -> ?hcrt("stream - file open ok", [{fd, Fd}]), @@ -174,7 +198,7 @@ stream(BodyPart, Request = #request{stream = Filename}, Code) %% Stream to file stream(BodyPart, Request = #request{stream = Fd}, Code) when ((Code =:= 200) orelse (Code =:= 206)) -> - ?hcrt("stream to file", [{stream, Fd}, {code, Code}, {body_part, BodyPart}]), + ?hcrt("stream to file", [{stream, Fd}, {code, Code}]), case file:write(Fd, BodyPart) of ok -> {<<>>, Request}; @@ -183,7 +207,7 @@ stream(BodyPart, Request = #request{stream = Fd}, Code) end; stream(BodyPart, Request,_) -> % only 200 and 206 responses can be streamed - ?hcrt("stream - ignore", [{request, Request}, {body_part, BodyPart}]), + ?hcrt("stream - ignore", [{request, Request}]), {BodyPart, Request}. @@ -192,10 +216,9 @@ stream(BodyPart, Request,_) -> % only 200 and 206 responses can be streamed %%==================================================================== %%-------------------------------------------------------------------- -%% Function: init([Request, Options, ProfileName]) -> {ok, State} | -%% {ok, State, Timeout} | ignore |{stop, Reason} +%% Function: init([Options, ProfileName]) -> {ok, State} | +%% {ok, State, Timeout} | ignore | {stop, Reason} %% -%% Request = #request{} %% Options = #options{} %% ProfileName = atom() - id of httpc manager process %% @@ -206,30 +229,16 @@ stream(BodyPart, Request,_) -> % only 200 and 206 responses can be streamed %% but we do not want that so errors will be handled by the process %% sending an init_error message to itself. %%-------------------------------------------------------------------- -init([Request, Options, ProfileName]) -> +init([Options, ProfileName]) -> + ?hcrv("init - starting", [{options, Options}, {profile, ProfileName}]), process_flag(trap_exit, true), - handle_verbose(Options#options.verbose), - Address = handle_proxy(Request#request.address, Options#options.proxy), - {ok, State} = - case {Address /= Request#request.address, Request#request.scheme} of - {true, https} -> - Error = https_through_proxy_is_not_currently_supported, - self() ! {init_error, - Error, httpc_response:error(Request, Error)}, - {ok, #state{request = Request, options = Options, - status = ssl_tunnel}}; - %% This is what we should do if and when ssl supports - %% "socket upgrading" - %%send_ssl_tunnel_request(Address, Request, - %% #state{options = Options, - %% status = ssl_tunnel}); - {_, _} -> - send_first_request(Address, Request, - #state{options = Options, - profile_name = ProfileName}) - end, - gen_server:enter_loop(?MODULE, [], State). + State = #state{status = undefined, + options = Options, + profile_name = ProfileName}, + ?hcrd("init - started", []), + {ok, State}. + %%-------------------------------------------------------------------- %% Function: handle_call(Request, From, State) -> {reply, Reply, State} | @@ -240,39 +249,85 @@ init([Request, Options, ProfileName]) -> %% {stop, Reason, State} (terminate/2 is called) %% Description: Handling call messages %%-------------------------------------------------------------------- -handle_call(Request, _, State = #state{session = Session = - #tcp_session{socket = Socket, - type = pipeline}, - timers = Timers, - options = Options, - profile_name = ProfileName}) -> - Address = handle_proxy(Request#request.address, Options#options.proxy), + + +%% This is the first request, the reason the proc was started +handle_call({connect_and_send, #request{address = Address0, + scheme = Scheme} = Request}, + _From, + #state{options = #options{proxy = Proxy}, + status = undefined, + session = undefined} = State) -> + ?hcrv("connect and send", [{address0, Address0}, {proxy, Proxy}]), + Address = handle_proxy(Address0, Proxy), + if + ((Address =/= Address0) andalso (Scheme =:= https)) -> + %% This is what we should do if and when ssl supports + %% "socket upgrading" + %%send_ssl_tunnel_request(Address, Request, + %% #state{options = Options, + %% status = ssl_tunnel}); + Reason = https_through_proxy_is_not_currently_supported, + Error = {error, Reason}, + {stop, Error, Error, State}; + true -> + case connect_and_send_first_request(Address, Request, State) of + {ok, NewState} -> + {reply, ok, NewState}; + {stop, Error, NewState} -> + {stop, Error, Error, NewState} + end + end; + +handle_call(#request{address = Addr} = Request, _, + #state{status = Status, + session = #tcp_session{socket = Socket, + type = pipeline} = Session, + timers = Timers, + options = #options{proxy = Proxy} = _Options, + profile_name = ProfileName} = State) + when Status =/= undefined -> + + ?hcrv("new request on a pipeline session", + [{request, Request}, + {profile, ProfileName}, + {status, Status}, + {timers, Timers}]), + + Address = handle_proxy(Addr, Proxy), case httpc_request:send(Address, Request, Socket) of ok -> + + ?hcrd("request sent", []), + %% Activate the request time out for the new request - NewState = activate_request_timeout(State#state{request = - Request}), + NewState = + activate_request_timeout(State#state{request = Request}), + + ClientClose = + httpc_request:is_client_closing(Request#request.headers), - ClientClose = httpc_request:is_client_closing( - Request#request.headers), case State#state.request of - #request{} -> %% Old request no yet finished + #request{} -> %% Old request not yet finished + ?hcrd("old request still not finished", []), %% Make sure to use the new value of timers in state - NewTimers = NewState#state.timers, + NewTimers = NewState#state.timers, NewPipeline = queue:in(Request, State#state.pipeline), - NewSession = + NewSession = Session#tcp_session{queue_length = %% Queue + current queue:len(NewPipeline) + 1, client_close = ClientClose}, httpc_manager:insert_session(NewSession, ProfileName), + ?hcrd("session updated", []), {reply, ok, State#state{pipeline = NewPipeline, - session = NewSession, - timers = NewTimers}}; + session = NewSession, + timers = NewTimers}}; undefined -> - %% Note: tcp-message reciving has already been + %% Note: tcp-message receiving has already been %% activated by handle_pipeline/2. + ?hcrd("no current request", []), cancel_timer(Timers#timers.queue_timer, timeout_queue), NewSession = @@ -281,54 +336,67 @@ handle_call(Request, _, State = #state{session = Session = httpc_manager:insert_session(NewSession, ProfileName), Relaxed = (Request#request.settings)#http_options.relaxed, - {reply, ok, - NewState#state{request = Request, - session = NewSession, - mfa = {httpc_response, parse, - [State#state.max_header_size, - Relaxed]}, - timers = - Timers#timers{queue_timer = - undefined}}} + MFA = {httpc_response, parse, + [State#state.max_header_size, Relaxed]}, + NewTimers = Timers#timers{queue_timer = undefined}, + ?hcrd("session created", []), + {reply, ok, NewState#state{request = Request, + session = NewSession, + mfa = MFA, + timers = NewTimers}} end; {error, Reason} -> + ?hcri("failed sending request", [{reason, Reason}]), {reply, {pipeline_failed, Reason}, State} end; -handle_call(Request, _, #state{session = Session = - #tcp_session{type = keep_alive, - socket = Socket}, - timers = Timers, - options = Options, - profile_name = ProfileName} = State) -> - - ClientClose = httpc_request:is_client_closing(Request#request.headers), +handle_call(#request{address = Addr} = Request, _, + #state{status = Status, + session = #tcp_session{socket = Socket, + type = keep_alive} = Session, + timers = Timers, + options = #options{proxy = Proxy} = _Options, + profile_name = ProfileName} = State) + when Status =/= undefined -> - Address = handle_proxy(Request#request.address, - Options#options.proxy), + ?hcrv("new request on a keep-alive session", + [{request, Request}, + {profile, ProfileName}, + {status, Status}]), + + Address = handle_proxy(Addr, Proxy), case httpc_request:send(Address, Request, Socket) of ok -> + + ?hcrd("request sent", []), + + %% Activate the request time out for the new request NewState = - activate_request_timeout(State#state{request = - Request}), + activate_request_timeout(State#state{request = Request}), + + ClientClose = + httpc_request:is_client_closing(Request#request.headers), case State#state.request of #request{} -> %% Old request not yet finished %% Make sure to use the new value of timers in state - NewTimers = NewState#state.timers, + ?hcrd("old request still not finished", []), + NewTimers = NewState#state.timers, NewKeepAlive = queue:in(Request, State#state.keep_alive), - NewSession = + NewSession = Session#tcp_session{queue_length = %% Queue + current queue:len(NewKeepAlive) + 1, client_close = ClientClose}, httpc_manager:insert_session(NewSession, ProfileName), + ?hcrd("session updated", []), {reply, ok, State#state{keep_alive = NewKeepAlive, - session = NewSession, - timers = NewTimers}}; + session = NewSession, + timers = NewTimers}}; undefined -> %% Note: tcp-message reciving has already been %% activated by handle_pipeline/2. + ?hcrd("no current request", []), cancel_timer(Timers#timers.queue_timer, timeout_queue), NewSession = @@ -337,16 +405,23 @@ handle_call(Request, _, #state{session = Session = httpc_manager:insert_session(NewSession, ProfileName), Relaxed = (Request#request.settings)#http_options.relaxed, - {reply, ok, - NewState#state{request = Request, - session = NewSession, - mfa = {httpc_response, parse, - [State#state.max_header_size, - Relaxed]}}} + MFA = {httpc_response, parse, + [State#state.max_header_size, Relaxed]}, + {reply, ok, NewState#state{request = Request, + session = NewSession, + mfa = MFA}} end; - {error, Reason} -> + + {error, Reason} -> + ?hcri("failed sending request", [{reason, Reason}]), {reply, {request_failed, Reason}, State} - end. + end; + + +handle_call(info, _, State) -> + Info = handler_info(State), + {reply, Info, State}. + %%-------------------------------------------------------------------- %% Function: handle_cast(Msg, State) -> {noreply, State} | @@ -367,19 +442,30 @@ handle_call(Request, _, #state{session = Session = %% handle_keep_alive_queue/2 on the other hand will just skip the %% request as if it was never issued as in this case the request will %% not have been sent. -handle_cast({cancel, RequestId}, State = #state{request = Request = - #request{id = RequestId}, - profile_name = ProfileName}) -> +handle_cast({cancel, RequestId}, + #state{request = #request{id = RequestId} = Request, + profile_name = ProfileName, + canceled = Canceled} = State) -> + ?hcrv("cancel current request", [{request_id, RequestId}, + {profile, ProfileName}, + {canceled, Canceled}]), httpc_manager:request_canceled(RequestId, ProfileName), + ?hcrv("canceled", []), {stop, normal, - State#state{canceled = [RequestId | State#state.canceled], - request = Request#request{from = answer_sent}}}; -handle_cast({cancel, RequestId}, State = #state{profile_name = ProfileName}) -> + State#state{canceled = [RequestId | Canceled], + request = Request#request{from = answer_sent}}}; +handle_cast({cancel, RequestId}, + #state{profile_name = ProfileName, + canceled = Canceled} = State) -> + ?hcrv("cancel", [{request_id, RequestId}, + {profile, ProfileName}, + {canceled, Canceled}]), httpc_manager:request_canceled(RequestId, ProfileName), - {noreply, State#state{canceled = [RequestId | State#state.canceled]}}; + ?hcrv("canceled", []), + {noreply, State#state{canceled = [RequestId | Canceled]}}; + handle_cast(stream_next, #state{session = Session} = State) -> - http_transport:setopts(socket_type(Session#tcp_session.scheme), - Session#tcp_session.socket, [{active, once}]), + activate_once(Session), {noreply, State#state{once = once}}. @@ -390,7 +476,7 @@ handle_cast(stream_next, #state{session = Session} = State) -> %% Description: Handling all non call/cast messages %%-------------------------------------------------------------------- handle_info({Proto, _Socket, Data}, - #state{mfa = {Module, Function, Args} = MFA, + #state{mfa = {Module, Function, Args}, request = #request{method = Method, stream = Stream} = Request, session = Session, @@ -399,64 +485,68 @@ handle_info({Proto, _Socket, Data}, (Proto =:= ssl) orelse (Proto =:= httpc_handler) -> - ?hcri("received data", [{proto, Proto}, {data, Data}, {mfa, MFA}, {method, Method}, {stream, Stream}, {session, Session}, {status_line, StatusLine}]), + ?hcri("received data", [{proto, Proto}, + {module, Module}, + {function, Function}, + {method, Method}, + {stream, Stream}, + {session, Session}, + {status_line, StatusLine}]), FinalResult = try Module:Function([Data | Args]) of {ok, Result} -> - ?hcrd("data processed - ok", [{result, Result}]), + ?hcrd("data processed - ok", []), handle_http_msg(Result, State); {_, whole_body, _} when Method =:= head -> ?hcrd("data processed - whole body", []), handle_response(State#state{body = <<>>}); {Module, whole_body, [Body, Length]} -> - ?hcrd("data processed - whole body", [{module, Module}, {body, Body}, {length, Length}]), + ?hcrd("data processed - whole body", [{length, Length}]), {_, Code, _} = StatusLine, {NewBody, NewRequest} = stream(Body, Request, Code), %% When we stream we will not keep the already %% streamed data, that would be a waste of memory. - NewLength = case Stream of - none -> - Length; - _ -> - Length - size(Body) - end, + NewLength = + case Stream of + none -> + Length; + _ -> + Length - size(Body) + end, NewState = next_body_chunk(State), - - {noreply, NewState#state{mfa = {Module, whole_body, - [NewBody, NewLength]}, + NewMFA = {Module, whole_body, [NewBody, NewLength]}, + {noreply, NewState#state{mfa = NewMFA, request = NewRequest}}; NewMFA -> - ?hcrd("data processed", [{new_mfa, NewMFA}]), - http_transport:setopts(socket_type(Session#tcp_session.scheme), - Session#tcp_session.socket, - [{active, once}]), + ?hcrd("data processed - new mfa", []), + activate_once(Session), {noreply, State#state{mfa = NewMFA}} catch - exit:_ -> - ClientErrMsg = httpc_response:error(Request, - {could_not_parse_as_http, - Data}), - NewState = answer_request(Request, ClientErrMsg, State), + exit:_Exit -> + ?hcrd("data processing exit", [{exit, _Exit}]), + ClientReason = {could_not_parse_as_http, Data}, + ClientErrMsg = httpc_response:error(Request, ClientReason), + NewState = answer_request(Request, ClientErrMsg, State), {stop, normal, NewState}; - error:_ -> - ClientErrMsg = httpc_response:error(Request, - {could_not_parse_as_http, - Data}), - NewState = answer_request(Request, ClientErrMsg, State), + error:_Error -> + ?hcrd("data processing error", [{error, _Error}]), + ClientReason = {could_not_parse_as_http, Data}, + ClientErrMsg = httpc_response:error(Request, ClientReason), + NewState = answer_request(Request, ClientErrMsg, State), {stop, normal, NewState} end, - ?hcri("data processed", [{result, FinalResult}]), + ?hcri("data processed", []), FinalResult; handle_info({Proto, Socket, Data}, - #state{mfa = MFA, - request = Request, - session = Session, - status = Status, + #state{mfa = MFA, + request = Request, + session = Session, + status = Status, status_line = StatusLine, profile_name = Profile} = State) when (Proto =:= tcp) orelse @@ -474,6 +564,7 @@ handle_info({Proto, Socket, Data}, "~n", [Proto, Socket, Data, MFA, Request, Session, Status, StatusLine, Profile]), + {noreply, State}; @@ -513,28 +604,35 @@ handle_info({ssl_error, _, _} = Reason, State) -> handle_info({timeout, RequestId}, #state{request = #request{id = RequestId} = Request, canceled = Canceled} = State) -> + ?hcri("timeout of current request", [{id, RequestId}]), httpc_response:send(Request#request.from, - httpc_response:error(Request,timeout)), + httpc_response:error(Request, timeout)), + ?hcrv("response (timeout) sent - now terminate", []), {stop, normal, State#state{request = Request#request{from = answer_sent}, canceled = [RequestId | Canceled]}}; handle_info({timeout, RequestId}, #state{canceled = Canceled} = State) -> + ?hcri("timeout", [{id, RequestId}]), Filter = fun(#request{id = Id, from = From} = Request) when Id =:= RequestId -> + ?hcrv("found request", [{id, Id}, {from, From}]), %% Notify the owner Response = httpc_response:error(Request, timeout), httpc_response:send(From, Response), + ?hcrv("response (timeout) sent", []), [Request#request{from = answer_sent}]; (_) -> true end, case State#state.status of pipeline -> + ?hcrd("pipeline", []), Pipeline = queue:filter(Filter, State#state.pipeline), {noreply, State#state{canceled = [RequestId | Canceled], pipeline = Pipeline}}; keep_alive -> + ?hcrd("keep_alive", []), KeepAlive = queue:filter(Filter, State#state.keep_alive), {noreply, State#state{canceled = [RequestId | Canceled], keep_alive = KeepAlive}} @@ -577,9 +675,10 @@ terminate(normal, #state{session = undefined}) -> %% Init error sending, no session information has been setup but %% there is a socket that needs closing. -terminate(normal, #state{request = Request, - session = #tcp_session{id = undefined, - socket = Socket}}) -> +terminate(normal, + #state{request = Request, + session = #tcp_session{id = undefined, + socket = Socket}}) -> http_transport:close(socket_type(Request), Socket); %% Socket closed remotely @@ -590,6 +689,9 @@ terminate(normal, request = Request, timers = Timers, pipeline = Pipeline}) -> + ?hcrt("terminate(normal) - remote close", + [{id, Id}, {profile, ProfileName}]), + %% Clobber session (catch httpc_manager:delete_session(Id, ProfileName)), @@ -605,23 +707,28 @@ terminate(normal, %% And, just in case, close our side (**really** overkill) http_transport:close(socket_type(Request), Socket); -terminate(_, State = #state{session = Session, - request = undefined, - profile_name = ProfileName, - timers = Timers, - pipeline = Pipeline, - keep_alive = KeepAlive}) -> - catch httpc_manager:delete_session(Session#tcp_session.id, - ProfileName), +terminate(_, #state{session = #tcp_session{id = Id, + socket = Socket, + scheme = Scheme}, + request = undefined, + profile_name = ProfileName, + timers = Timers, + pipeline = Pipeline, + keep_alive = KeepAlive} = State) -> + (catch httpc_manager:delete_session(Id, ProfileName)), maybe_retry_queue(Pipeline, State), maybe_retry_queue(KeepAlive, State), cancel_timer(Timers#timers.queue_timer, timeout_queue), - Socket = Session#tcp_session.socket, - http_transport:close(socket_type(Session#tcp_session.scheme), Socket); + http_transport:close(socket_type(Scheme), Socket); + +terminate(Reason, #state{request = undefined}) -> + ?hcrt("terminate", [{reason, Reason}]), + ok; -terminate(Reason, State = #state{request = Request}) -> +terminate(Reason, #state{request = Request} = State) -> + ?hcrd("terminate", [{reason, Reason}, {request, Request}]), NewState = maybe_send_answer(Request, httpc_response:error(Request, Reason), State), @@ -641,13 +748,16 @@ maybe_send_answer(Request, Answer, State) -> answer_request(Request, Answer, State). deliver_answers([]) -> + ?hcrd("deliver answer done", []), ok; -deliver_answers([#request{from = From} = Request | Requests]) +deliver_answers([#request{id = Id, from = From} = Request | Requests]) when is_pid(From) -> Response = httpc_response:error(Request, socket_closed_remotely), + ?hcrd("deliver answer", [{id, Id}, {from, From}, {response, Response}]), httpc_response:send(From, Response), deliver_answers(Requests); -deliver_answers([_|Requests]) -> +deliver_answers([Request|Requests]) -> + ?hcrd("skip deliver answer", [{request, Request}]), deliver_answers(Requests). @@ -691,19 +801,22 @@ new_queue(Queue, Fun) -> end, List), queue:from_list(NewList). -%%-------------------------------------------------------------------- + +%%%-------------------------------------------------------------------- %%% Internal functions -%%-------------------------------------------------------------------- +%%%-------------------------------------------------------------------- -connect(SocketType, ToAddress, #options{ipfamily = IpFamily, - ip = FromAddress, - port = FromPort}, Timeout) -> +connect(SocketType, ToAddress, + #options{ipfamily = IpFamily, + ip = FromAddress, + port = FromPort, + socket_opts = Opts0}, Timeout) -> Opts1 = case FromPort of default -> - []; + Opts0; _ -> - [{port, FromPort}] + [{port, FromPort} | Opts0] end, Opts2 = case FromAddress of @@ -728,101 +841,157 @@ connect(SocketType, ToAddress, #options{ipfamily = IpFamily, http_transport:connect(SocketType, ToAddress, Opts3, Timeout) end. - -send_first_request(Address, Request, #state{options = Options} = State) -> - SocketType = socket_type(Request), - ConnTimeout = (Request#request.settings)#http_options.connect_timeout, - ?hcri("connect", +connect_and_send_first_request(Address, + #request{settings = Settings, + headers = Headers, + address = OrigAddress, + scheme = Scheme} = Request, + #state{options = Options} = State) -> + + ?hcrd("connect", [{address, Address}, {request, Request}, {options, Options}]), + + SocketType = socket_type(Request), + ConnTimeout = Settings#http_options.connect_timeout, case connect(SocketType, Address, Options, ConnTimeout) of {ok, Socket} -> - ?hcri("connected - now send first request", [{socket, Socket}]), + ?hcrd("connected - now send first request", [{socket, Socket}]), case httpc_request:send(Address, Request, Socket) of ok -> - ?hcri("first request sent", []), + ?hcrd("first request sent", []), ClientClose = - httpc_request:is_client_closing( - Request#request.headers), + httpc_request:is_client_closing(Headers), SessionType = httpc_manager:session_type(Options), Session = - #tcp_session{id = {Request#request.address, self()}, - scheme = Request#request.scheme, - socket = Socket, + #tcp_session{id = {OrigAddress, self()}, + scheme = Scheme, + socket = Socket, client_close = ClientClose, - type = SessionType}, - TmpState = State#state{request = Request, - session = Session, - mfa = init_mfa(Request, State), - status_line = - init_status_line(Request), - headers = undefined, - body = undefined, - status = new}, - http_transport:setopts(SocketType, - Socket, [{active, once}]), + type = SessionType}, + TmpState = + State#state{request = Request, + session = Session, + mfa = init_mfa(Request, State), + status_line = init_status_line(Request), + headers = undefined, + body = undefined, + status = new}, + ?hcrt("activate socket", []), + activate_once(Session), NewState = activate_request_timeout(TmpState), {ok, NewState}; - {error, Reason} -> - %% Commented out in wait of ssl support to avoid - %% dialyzer warning - %%case State#state.status of - %% new -> % Called from init/1 - self() ! {init_error, error_sending, - httpc_response:error(Request, Reason)}, - {ok, State#state{request = Request, - session = - #tcp_session{socket = Socket}}} - %%ssl_tunnel -> % Not called from init/1 - %% NewState = - %% answer_request(Request, - %%httpc_response:error(Request, - %%Reason), - %% State), - %% {stop, normal, NewState} - %% end + {error, Reason} -> + ?hcrv("failed sending request", [{reason, Reason}]), + Error = {error, {send_failed, + httpc_response:error(Request, Reason)}}, + {stop, Error, State#state{request = Request}} end; - {error, Reason} -> - %% Commented out in wait of ssl support to avoid - %% dialyzer warning - %% case State#state.status of - %% new -> % Called from init/1 - self() ! {init_error, error_connecting, - httpc_response:error(Request, Reason)}, - {ok, State#state{request = Request}} - %% ssl_tunnel -> % Not called from init/1 - %% NewState = - %% answer_request(Request, - %% httpc_response:error(Request, - %% Reason), - %% State), - %% {stop, normal, NewState} - %%end + {error, Reason} -> + ?hcri("connect failed", [{reason, Reason}]), + Error = {error, {connect_failed, + httpc_response:error(Request, Reason)}}, + {stop, Error, State#state{request = Request}} end. + +handler_info(#state{request = Request, + session = Session, + status_line = _StatusLine, + pipeline = Pipeline, + keep_alive = KeepAlive, + status = Status, + canceled = _Canceled, + options = _Options, + timers = _Timers} = _State) -> + + ?hcrt("handler info", [{request, Request}, + {session, Session}, + {pipeline, Pipeline}, + {keep_alive, KeepAlive}, + {status, Status}]), + + %% Info about the current request + RequestInfo = + case Request of + undefined -> + []; + #request{id = Id, + started = ReqStarted} -> + [{id, Id}, {started, ReqStarted}] + end, + + ?hcrt("handler info", [{request_info, RequestInfo}]), + + %% Info about the current session/socket + SessionType = Session#tcp_session.type, + QueueLen = case Session#tcp_session.type of + pipeline -> + queue:len(Pipeline); + keep_alive -> + queue:len(KeepAlive) + end, + Socket = Session#tcp_session.socket, + Scheme = Session#tcp_session.scheme, + SocketType = socket_type(Scheme), + + ?hcrt("handler info", [{session_type, SessionType}, + {queue_length, QueueLen}, + {scheme, Scheme}, + {socket_type, SocketType}, + {socket, Socket}]), + + SocketOpts = http_transport:getopts(SocketType, Socket), + SocketStats = http_transport:getstat(SocketType, Socket), + + Remote = http_transport:peername(SocketType, Socket), + Local = http_transport:sockname(SocketType, Socket), + + ?hcrt("handler info", [{remote, Remote}, + {local, Local}, + {socket_opts, SocketOpts}, + {socket_stats, SocketStats}]), + + SocketInfo = [{remote, Remote}, + {local, Local}, + {socket_opts, SocketOpts}, + {socket_stats, SocketStats}], + + SessionInfo = + [{type, SessionType}, + {queue_length, QueueLen}, + {scheme, Scheme}, + {socket_info, SocketInfo}], + + [{status, Status}, + {current_request, RequestInfo}, + {session, SessionInfo}]. + + + handle_http_msg({Version, StatusCode, ReasonPharse, Headers, Body}, State = #state{request = Request}) -> - ?hcrt("handle_http_msg", [{body, Body}]), + ?hcrt("handle_http_msg", [{headers, Headers}]), case Headers#http_response_h.'content-type' of "multipart/byteranges" ++ _Param -> - exit(not_yet_implemented); + exit({not_yet_implemented, multypart_byteranges}); _ -> - StatusLine = {Version, StatusCode, ReasonPharse}, + StatusLine = {Version, StatusCode, ReasonPharse}, {ok, NewRequest} = start_stream(StatusLine, Headers, Request), handle_http_body(Body, State#state{request = NewRequest, status_line = StatusLine, headers = Headers}) end; -handle_http_msg({ChunkedHeaders, Body}, - State = #state{headers = Headers}) -> - ?hcrt("handle_http_msg", [{chunked_headers, ChunkedHeaders}, {body, Body}]), +handle_http_msg({ChunkedHeaders, Body}, #state{headers = Headers} = State) -> + ?hcrt("handle_http_msg", + [{chunked_headers, ChunkedHeaders}, {headers, Headers}]), NewHeaders = http_chunk:handle_headers(Headers, ChunkedHeaders), handle_response(State#state{headers = NewHeaders, body = Body}); -handle_http_msg(Body, State = #state{status_line = {_,Code, _}}) -> - ?hcrt("handle_http_msg", [{body, Body}, {code, Code}]), - {NewBody, NewRequest}= stream(Body, State#state.request, Code), +handle_http_msg(Body, #state{status_line = {_,Code, _}} = State) -> + ?hcrt("handle_http_msg", [{code, Code}]), + {NewBody, NewRequest} = stream(Body, State#state.request, Code), handle_response(State#state{body = NewBody, request = NewRequest}). handle_http_body(<<>>, State = #state{status_line = {_,304, _}}) -> @@ -837,11 +1006,12 @@ handle_http_body(<<>>, State = #state{request = #request{method = head}}) -> ?hcrt("handle_http_body - head", []), handle_response(State#state{body = <<>>}); -handle_http_body(Body, State = #state{headers = Headers, - max_body_size = MaxBodySize, - status_line = {_,Code, _}, - request = Request}) -> - ?hcrt("handle_http_body", [{body, Body}, {max_body_size, MaxBodySize}, {code, Code}]), +handle_http_body(Body, #state{headers = Headers, + max_body_size = MaxBodySize, + status_line = {_,Code, _}, + request = Request} = State) -> + ?hcrt("handle_http_body", + [{max_body_size, MaxBodySize}, {headers, Headers}, {code, Code}]), TransferEnc = Headers#http_response_h.'transfer-encoding', case case_insensitive_header(TransferEnc) of "chunked" -> @@ -850,12 +1020,17 @@ handle_http_body(Body, State = #state{headers = Headers, State#state.max_header_size, {Code, Request}) of {Module, Function, Args} -> - ?hcrt("handle_http_body - new mfa", [{module, Module}, {function, Function}, {args, Args}]), + ?hcrt("handle_http_body - new mfa", + [{module, Module}, + {function, Function}, + {args, Args}]), NewState = next_body_chunk(State), {noreply, NewState#state{mfa = {Module, Function, Args}}}; {ok, {ChunkedHeaders, NewBody}} -> - ?hcrt("handle_http_body - nyew body", [{chunked_headers, ChunkedHeaders}, {new_body, NewBody}]), + ?hcrt("handle_http_body - new body", + [{chunked_headers, ChunkedHeaders}, + {new_body, NewBody}]), NewHeaders = http_chunk:handle_headers(Headers, ChunkedHeaders), handle_response(State#state{headers = NewHeaders, @@ -872,12 +1047,13 @@ handle_http_body(Body, State = #state{headers = Headers, ?hcrt("handle_http_body - other", []), Length = list_to_integer(Headers#http_response_h.'content-length'), - case ((Length =< MaxBodySize) or (MaxBodySize == nolimit)) of + case ((Length =< MaxBodySize) orelse (MaxBodySize =:= nolimit)) of true -> case httpc_response:whole_body(Body, Length) of {ok, Body} -> - {NewBody, NewRequest}= stream(Body, Request, Code), - handle_response(State#state{body = NewBody, + {NewBody, NewRequest} = + stream(Body, Request, Code), + handle_response(State#state{body = NewBody, request = NewRequest}); MFA -> NewState = next_body_chunk(State), @@ -893,90 +1069,79 @@ handle_http_body(Body, State = #state{headers = Headers, end end. -%%% Normaly I do not comment out code, I throw it away. But this might -%%% actually be used on day if ssl is improved. -%% handle_response(State = #state{status = ssl_tunnel, -%% request = Request, -%% options = Options, -%% session = #tcp_session{socket = Socket, -%% scheme = Scheme}, -%% status_line = {_, 200, _}}) -> -%% %%% Insert code for upgrading the socket if and when ssl supports this. -%% Address = handle_proxy(Request#request.address, Options#options.proxy), -%% send_first_request(Address, Request, State); -%% handle_response(State = #state{status = ssl_tunnel, -%% request = Request}) -> -%% NewState = answer_request(Request, -%% httpc_response:error(Request, -%% ssl_proxy_tunnel_failed), -%% State), -%% {stop, normal, NewState}; - -handle_response(State = #state{status = new}) -> - handle_response(try_to_enable_pipeline_or_keep_alive(State)); - -handle_response(State = - #state{request = Request, +handle_response(#state{status = new} = State) -> + ?hcrd("handle response - status = new", []), + handle_response(try_to_enable_pipeline_or_keep_alive(State)); + +handle_response(#state{request = Request, status = Status, session = Session, status_line = StatusLine, headers = Headers, body = Body, options = Options, - profile_name = ProfileName}) when Status =/= new -> - ?hcrt("handle response", [{status, Status}, {session, Session}, {status_line, StatusLine}, {profile_name, ProfileName}]), + profile_name = ProfileName} = State) + when Status =/= new -> + + ?hcrd("handle response", [{profile, ProfileName}, + {status, Status}, + {request, Request}, + {session, Session}, + {status_line, StatusLine}]), + handle_cookies(Headers, Request, Options, ProfileName), case httpc_response:result({StatusLine, Headers, Body}, Request) of %% 100-continue continue -> + ?hcrd("handle response - continue", []), %% Send request body {_, RequestBody} = Request#request.content, http_transport:send(socket_type(Session#tcp_session.scheme), Session#tcp_session.socket, RequestBody), %% Wait for next response - http_transport:setopts(socket_type(Session#tcp_session.scheme), - Session#tcp_session.socket, - [{active, once}]), + activate_once(Session), Relaxed = (Request#request.settings)#http_options.relaxed, - {noreply, - State#state{mfa = {httpc_response, parse, - [State#state.max_header_size, - Relaxed]}, - status_line = undefined, - headers = undefined, - body = undefined - }}; + MFA = {httpc_response, parse, + [State#state.max_header_size, Relaxed]}, + {noreply, State#state{mfa = MFA, + status_line = undefined, + headers = undefined, + body = undefined}}; + %% Ignore unexpected 100-continue response and receive the %% actual response that the server will send right away. {ignore, Data} -> + ?hcrd("handle response - ignore", [{data, Data}]), Relaxed = (Request#request.settings)#http_options.relaxed, - NewState = State#state{mfa = - {httpc_response, parse, - [State#state.max_header_size, - Relaxed]}, + MFA = {httpc_response, parse, + [State#state.max_header_size, Relaxed]}, + NewState = State#state{mfa = MFA, status_line = undefined, - headers = undefined, - body = undefined}, + headers = undefined, + body = undefined}, handle_info({httpc_handler, dummy, Data}, NewState); + %% On a redirect or retry the current request becomes %% obsolete and the manager will create a new request %% with the same id as the current. {redirect, NewRequest, Data} -> - ?hcrt("handle response - redirect", [{new_request, NewRequest}, {data, Data}]), + ?hcrt("handle response - redirect", + [{new_request, NewRequest}, {data, Data}]), ok = httpc_manager:redirect_request(NewRequest, ProfileName), handle_queue(State#state{request = undefined}, Data); {retry, TimeNewRequest, Data} -> - ?hcrt("handle response - retry", [{time_new_request, TimeNewRequest}, {data, Data}]), + ?hcrt("handle response - retry", + [{time_new_request, TimeNewRequest}, {data, Data}]), ok = httpc_manager:retry_request(TimeNewRequest, ProfileName), handle_queue(State#state{request = undefined}, Data); {ok, Msg, Data} -> - ?hcrt("handle response - result ok", [{msg, Msg}, {data, Data}]), + ?hcrd("handle response - ok", []), end_stream(StatusLine, Request), NewState = answer_request(Request, Msg, State), handle_queue(NewState, Data); {stop, Msg} -> - ?hcrt("handle response - result stop", [{msg, Msg}]), + ?hcrd("handle response - stop", [{msg, Msg}]), end_stream(StatusLine, Request), NewState = answer_request(Request, Msg, State), {stop, normal, NewState} @@ -990,60 +1155,67 @@ handle_cookies(_,_, #options{cookies = verify}, _) -> ok; handle_cookies(Headers, Request, #options{cookies = enabled}, ProfileName) -> {Host, _ } = Request#request.address, - Cookies = http_cookie:cookies(Headers#http_response_h.other, + Cookies = httpc_cookie:cookies(Headers#http_response_h.other, Request#request.path, Host), httpc_manager:store_cookies(Cookies, Request#request.address, ProfileName). %% This request could not be pipelined or used as sequential keept alive %% queue -handle_queue(State = #state{status = close}, _) -> +handle_queue(#state{status = close} = State, _) -> {stop, normal, State}; -handle_queue(State = #state{status = keep_alive}, Data) -> +handle_queue(#state{status = keep_alive} = State, Data) -> handle_keep_alive_queue(State, Data); -handle_queue(State = #state{status = pipeline}, Data) -> +handle_queue(#state{status = pipeline} = State, Data) -> handle_pipeline(State, Data). -handle_pipeline(State = - #state{status = pipeline, session = Session, +handle_pipeline(#state{status = pipeline, + session = Session, profile_name = ProfileName, - options = #options{pipeline_timeout = TimeOut}}, - Data) -> + options = #options{pipeline_timeout = TimeOut}} = + State, + Data) -> + + ?hcrd("handle pipeline", [{profile, ProfileName}, + {session, Session}, + {timeout, TimeOut}]), + case queue:out(State#state.pipeline) of {empty, _} -> + ?hcrd("epmty pipeline queue", []), + %% The server may choose too teminate an idle pipeline %% in this case we want to receive the close message %% at once and not when trying to pipeline the next %% request. - http_transport:setopts(socket_type(Session#tcp_session.scheme), - Session#tcp_session.socket, - [{active, once}]), + activate_once(Session), + %% If a pipeline that has been idle for some time is not %% closed by the server, the client may want to close it. - NewState = activate_queue_timeout(TimeOut, State), + NewState = activate_queue_timeout(TimeOut, State), NewSession = Session#tcp_session{queue_length = 0}, httpc_manager:insert_session(NewSession, ProfileName), %% Note mfa will be initilized when a new request %% arrives. {noreply, - NewState#state{request = undefined, - mfa = undefined, + NewState#state{request = undefined, + mfa = undefined, status_line = undefined, - headers = undefined, - body = undefined - } - }; + headers = undefined, + body = undefined}}; {{value, NextRequest}, Pipeline} -> case lists:member(NextRequest#request.id, State#state.canceled) of true -> + ?hcrv("next request had been cancelled", []), %% See comment for handle_cast({cancel, RequestId}) {stop, normal, State#state{request = NextRequest#request{from = answer_sent}}}; false -> + ?hcrv("next request", [{request, NextRequest}]), NewSession = Session#tcp_session{queue_length = %% Queue + current @@ -1051,21 +1223,19 @@ handle_pipeline(State = httpc_manager:insert_session(NewSession, ProfileName), Relaxed = (NextRequest#request.settings)#http_options.relaxed, + MFA = {httpc_response, + parse, + [State#state.max_header_size, Relaxed]}, NewState = - State#state{pipeline = Pipeline, - request = NextRequest, - mfa = {httpc_response, parse, - [State#state.max_header_size, - Relaxed]}, + State#state{pipeline = Pipeline, + request = NextRequest, + mfa = MFA, status_line = undefined, - headers = undefined, - body = undefined}, + headers = undefined, + body = undefined}, case Data of <<>> -> - http_transport:setopts( - socket_type(Session#tcp_session.scheme), - Session#tcp_session.socket, - [{active, once}]), + activate_once(Session), {noreply, NewState}; _ -> %% If we already received some bytes of @@ -1076,22 +1246,25 @@ handle_pipeline(State = end end. -handle_keep_alive_queue(State = #state{status = keep_alive, - session = Session, - profile_name = ProfileName, - options = #options{keep_alive_timeout - = TimeOut} - }, - Data) -> +handle_keep_alive_queue( + #state{status = keep_alive, + session = Session, + profile_name = ProfileName, + options = #options{keep_alive_timeout = TimeOut}} = State, + Data) -> + + ?hcrd("handle keep_alive", [{profile, ProfileName}, + {session, Session}, + {timeout, TimeOut}]), + case queue:out(State#state.keep_alive) of {empty, _} -> + ?hcrd("empty keep_alive queue", []), %% The server may choose too terminate an idle keep_alive session %% in this case we want to receive the close message %% at once and not when trying to send the next %% request. - http_transport:setopts(socket_type(Session#tcp_session.scheme), - Session#tcp_session.socket, - [{active, once}]), + activate_once(Session), %% If a keep_alive session has been idle for some time is not %% closed by the server, the client may want to close it. NewState = activate_queue_timeout(TimeOut, State), @@ -1111,25 +1284,25 @@ handle_keep_alive_queue(State = #state{status = keep_alive, case lists:member(NextRequest#request.id, State#state.canceled) of true -> - handle_keep_alive_queue(State#state{keep_alive = - KeepAlive}, Data); + ?hcrv("next request has already been canceled", []), + handle_keep_alive_queue( + State#state{keep_alive = KeepAlive}, Data); false -> + ?hcrv("next request", [{request, NextRequest}]), Relaxed = (NextRequest#request.settings)#http_options.relaxed, + MFA = {httpc_response, parse, + [State#state.max_header_size, Relaxed]}, NewState = - State#state{request = NextRequest, - keep_alive = KeepAlive, - mfa = {httpc_response, parse, - [State#state.max_header_size, - Relaxed]}, + State#state{request = NextRequest, + keep_alive = KeepAlive, + mfa = MFA, status_line = undefined, - headers = undefined, - body = undefined}, + headers = undefined, + body = undefined}, case Data of <<>> -> - http_transport:setopts( - socket_type(Session#tcp_session.scheme), - Session#tcp_session.socket, [{active, once}]), + activate_once(Session), {noreply, NewState}; _ -> %% If we already received some bytes of @@ -1140,11 +1313,6 @@ handle_keep_alive_queue(State = #state{status = keep_alive, end end. -call(Msg, Pid, Timeout) -> - gen_server:call(Pid, Msg, Timeout). - -cast(Msg, Pid) -> - gen_server:cast(Pid, Msg). case_insensitive_header(Str) when is_list(Str) -> http_util:to_lower(Str); @@ -1152,20 +1320,34 @@ case_insensitive_header(Str) when is_list(Str) -> case_insensitive_header(Str) -> Str. -activate_request_timeout(State = #state{request = Request}) -> - Time = (Request#request.settings)#http_options.timeout, - case Time of +activate_once(#tcp_session{scheme = Scheme, socket = Socket}) -> + SocketType = socket_type(Scheme), + http_transport:setopts(SocketType, Socket, [{active, once}]). + +activate_request_timeout( + #state{request = #request{timer = undefined} = Request} = State) -> + Timeout = (Request#request.settings)#http_options.timeout, + case Timeout of infinity -> State; _ -> - Ref = erlang:send_after(Time, self(), - {timeout, Request#request.id}), - State#state - {timers = - #timers{request_timers = - [{Request#request.id, Ref}| - (State#state.timers)#timers.request_timers]}} - end. + ReqId = Request#request.id, + ?hcrt("activate request timer", + [{request_id, ReqId}, + {time_consumed, t() - Request#request.started}, + {timeout, Timeout}]), + Msg = {timeout, ReqId}, + Ref = erlang:send_after(Timeout, self(), Msg), + Request2 = Request#request{timer = Ref}, + ReqTimers = [{Request#request.id, Ref} | + (State#state.timers)#timers.request_timers], + Timers = #timers{request_timers = ReqTimers}, + State#state{request = Request2, timers = Timers} + end; + +%% Timer is already running! This is the case for a redirect or retry +activate_request_timeout(State) -> + State. activate_queue_timeout(infinity, State) -> State; @@ -1187,18 +1369,21 @@ is_keep_alive_enabled_server("HTTP/1.0", is_keep_alive_enabled_server(_,_) -> false. -is_keep_alive_connection(Headers, Session) -> - (not ((Session#tcp_session.client_close) or - httpc_response:is_server_closing(Headers))). - -try_to_enable_pipeline_or_keep_alive(State = - #state{session = Session, - request = #request{method = Method}, - status_line = {Version, _, _}, - headers = Headers, - profile_name = ProfileName}) -> - case (is_keep_alive_enabled_server(Version, Headers) andalso - is_keep_alive_connection(Headers, Session)) of +is_keep_alive_connection(Headers, #tcp_session{client_close = ClientClose}) -> + (not ((ClientClose) orelse httpc_response:is_server_closing(Headers))). + +try_to_enable_pipeline_or_keep_alive( + #state{session = Session, + request = #request{method = Method}, + status_line = {Version, _, _}, + headers = Headers, + profile_name = ProfileName} = State) -> + ?hcrd("try to enable pipeline or keep-alive", + [{version, Version}, + {headers, Headers}, + {session, Session}]), + case is_keep_alive_enabled_server(Version, Headers) andalso + is_keep_alive_connection(Headers, Session) of true -> case (is_pipeline_enabled_client(Session) andalso httpc_request:is_idempotent(Method)) of @@ -1209,15 +1394,16 @@ try_to_enable_pipeline_or_keep_alive(State = httpc_manager:insert_session(Session, ProfileName), %% Make sure type is keep_alive in session %% as it in this case might be pipeline - State#state{status = keep_alive, - session = - Session#tcp_session{type = keep_alive}} + NewSession = Session#tcp_session{type = keep_alive}, + State#state{status = keep_alive, + session = NewSession} end; false -> State#state{status = close} end. -answer_request(Request, Msg, #state{timers = Timers} = State) -> +answer_request(Request, Msg, #state{timers = Timers} = State) -> + ?hcrt("answer request", [{request, Request}]), httpc_response:send(Request#request.from, Msg), RequestTimers = Timers#timers.request_timers, TimerRef = @@ -1253,14 +1439,14 @@ retry_pipeline([Request | PipeLine], case (catch httpc_manager:retry_request(Request, ProfileName)) of ok -> RequestTimers = Timers#timers.request_timers, + ReqId = Request#request.id, TimerRef = - proplists:get_value(Request#request.id, RequestTimers, - undefined), - cancel_timer(TimerRef, {timeout, Request#request.id}), - State#state{timers = Timers#timers{request_timers = - lists:delete({Request#request.id, - TimerRef}, - RequestTimers)}}; + proplists:get_value(ReqId, RequestTimers, undefined), + cancel_timer(TimerRef, {timeout, ReqId}), + NewReqsTimers = lists:delete({ReqId, TimerRef}, RequestTimers), + NewTimers = Timers#timers{request_timers = NewReqsTimers}, + State#state{timers = NewTimers}; + Error -> answer_request(Request#request.from, httpc_response:error(Request, Error), State) @@ -1345,12 +1531,14 @@ socket_type(#request{scheme = https, settings = Settings}) -> socket_type(http) -> ip_comm; socket_type(https) -> - {ssl, []}. %% Dummy value ok for ex setops that does not use this value + {ssl, []}. %% Dummy value ok for ex setopts that does not use this value -start_stream({_Version, _Code, _ReasonPhrase}, _Headers, #request{stream = none} = Request) -> +start_stream({_Version, _Code, _ReasonPhrase}, _Headers, + #request{stream = none} = Request) -> ?hcrt("start stream - none", []), {ok, Request}; -start_stream({_Version, Code, _ReasonPhrase}, Headers, #request{stream = self} = Request) +start_stream({_Version, Code, _ReasonPhrase}, Headers, + #request{stream = self} = Request) when (Code =:= 200) orelse (Code =:= 206) -> ?hcrt("start stream - self", [{code, Code}]), Msg = httpc_response:stream_start(Headers, Request, ignore), @@ -1363,7 +1551,8 @@ start_stream({_Version, Code, _ReasonPhrase}, Headers, Msg = httpc_response:stream_start(Headers, Request, self()), httpc_response:send(Request#request.from, Msg), {ok, Request}; -start_stream({_Version, Code, _ReasonPhrase}, _Headers, #request{stream = Filename} = Request) +start_stream({_Version, Code, _ReasonPhrase}, _Headers, + #request{stream = Filename} = Request) when ((Code =:= 200) orelse (Code =:= 206)) andalso is_list(Filename) -> ?hcrt("start stream", [{code, Code}, {filename, Filename}]), case file:open(Filename, [write, raw, append, delayed_write]) of @@ -1436,6 +1625,7 @@ handle_verbose(trace) -> handle_verbose(_) -> ok. + %%% Normaly I do not comment out code, I throw it away. But this might %%% actually be used one day if ssl is improved. %% send_ssl_tunnel_request(Address, Request = #request{address = {Host, Port}}, @@ -1497,3 +1687,21 @@ handle_verbose(_) -> %% d(_, _, _) -> %% ok. + +call(Msg, Pid) -> + Timeout = infinity, + call(Msg, Pid, Timeout). +call(Msg, Pid, Timeout) -> + gen_server:call(Pid, Msg, Timeout). + +cast(Msg, Pid) -> + gen_server:cast(Pid, Msg). + + +%% to(To, Start) when is_integer(Start) andalso (Start >= 0) -> +%% http_util:timeout(To, Start); +%% to(To, _Start) -> +%% http_util:timeout(To, t()). + +t() -> + http_util:timestamp(). diff --git a/lib/inets/src/http_client/httpc_handler_sup.erl b/lib/inets/src/http_client/httpc_handler_sup.erl index d9edaa0599..2a69fd15d0 100644 --- a/lib/inets/src/http_client/httpc_handler_sup.erl +++ b/lib/inets/src/http_client/httpc_handler_sup.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2007-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2007-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -23,7 +23,7 @@ %% API -export([start_link/0]). --export([start_child/1]). +-export([start_child/2]). %% Supervisor callback -export([init/1]). @@ -34,25 +34,28 @@ start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). -start_child(Args) -> +start_child(Options, Profile) -> + Args = [Options, Profile], supervisor:start_child(?MODULE, Args). - + + %%%========================================================================= %%% Supervisor callback %%%========================================================================= init(Args) -> + RestartStrategy = simple_one_for_one, MaxR = 0, MaxT = 3600, - Name = undefined, % As simple_one_for_one is used. + Name = undefined, % As simple_one_for_one is used. StartFunc = {httpc_handler, start_link, Args}, - Restart = temporary, % E.g. should not be restarted - Shutdown = 4000, - Modules = [httpc_handler], - Type = worker, - + Restart = temporary, % E.g. should not be restarted + Shutdown = 4000, + Modules = [httpc_handler], + Type = worker, ChildSpec = {Name, StartFunc, Restart, Shutdown, Type, Modules}, + {ok, {{RestartStrategy, MaxR, MaxT}, [ChildSpec]}}. diff --git a/lib/inets/src/http_client/httpc_internal.hrl b/lib/inets/src/http_client/httpc_internal.hrl index ec709b9860..4d76c4beb3 100644 --- a/lib/inets/src/http_client/httpc_internal.hrl +++ b/lib/inets/src/http_client/httpc_internal.hrl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2005-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2005-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -46,7 +46,7 @@ %% bool() - true if auto redirect on 30x response autoredirect = true, - %% Ssl socket options + %% ssl socket options ssl = [], %% {User, Password} = {string(), string()} @@ -63,41 +63,45 @@ %%% HTTP Client per profile setting. -record(options, { - proxy = {undefined, []}, % {{ProxyHost, ProxyPort}, [NoProxy]}, - %% 0 means persistent connections are used without pipelining - pipeline_timeout = ?HTTP_PIPELINE_TIMEOUT, - max_pipeline_length = ?HTTP_PIPELINE_LENGTH, - max_keep_alive_length = ?HTTP_KEEP_ALIVE_LENGTH, - keep_alive_timeout = ?HTTP_KEEP_ALIVE_TIMEOUT, % Used when pipeline_timeout = 0 - max_sessions = ?HTTP_MAX_TCP_SESSIONS, - cookies = disabled, % enabled | disabled | verify - verbose = false, - ipfamily = inet, % inet | inet6 | inet6fb4 - ip = default, % specify local interface - port = default % specify local port - } + proxy = {undefined, []}, % {{ProxyHost, ProxyPort}, [NoProxy]}, + %% 0 means persistent connections are used without pipelining + pipeline_timeout = ?HTTP_PIPELINE_TIMEOUT, + max_pipeline_length = ?HTTP_PIPELINE_LENGTH, + max_keep_alive_length = ?HTTP_KEEP_ALIVE_LENGTH, + keep_alive_timeout = ?HTTP_KEEP_ALIVE_TIMEOUT, % Used when pipeline_timeout = 0 + max_sessions = ?HTTP_MAX_TCP_SESSIONS, + cookies = disabled, % enabled | disabled | verify + verbose = false, + ipfamily = inet, % inet | inet6 | inet6fb4 + ip = default, % specify local interface + port = default, % specify local port + socket_opts = [] % other socket options + } ). %%% All data associated to a specific HTTP request -record(request, { - id, % ref() - Request Id - from, % pid() - Caller - redircount = 0,% Number of redirects made for this request - scheme, % http | https - address, % ({Host,Port}) Destination Host and Port - path, % string() - Path of parsed URL - pquery, % string() - Rest of parsed URL - method, % atom() - HTTP request Method - headers, % #http_request_h{} - content, % {ContentType, Body} - Current HTTP request - settings, % #http_options{} - User defined settings - abs_uri, % string() ex: "http://www.erlang.org" - userinfo, % string() - optinal "<userinfo>@<host>:<port>" - stream, % Boolean() - stream async reply? - headers_as_is % Boolean() - workaround for servers that does - %% not honor the http standard, can also be used for testing purposes. - } + id, % ref() - Request Id + from, % pid() - Caller + redircount = 0,% Number of redirects made for this request + scheme, % http | https + address, % ({Host,Port}) Destination Host and Port + path, % string() - Path of parsed URL + pquery, % string() - Rest of parsed URL + method, % atom() - HTTP request Method + headers, % #http_request_h{} + content, % {ContentType, Body} - Current HTTP request + settings, % #http_options{} - User defined settings + abs_uri, % string() ex: "http://www.erlang.org" + userinfo, % string() - optinal "<userinfo>@<host>:<port>" + stream, % Boolean() - stream async reply? + headers_as_is, % Boolean() - workaround for servers that does + % not honor the http standard, can also be used for testing purposes. + started, % integer() > 0 - When we started processing the request + timer, % undefined | ref() + socket_opts % undefined | [socket_option()] + } ). -record(tcp_session, @@ -107,7 +111,7 @@ scheme, % http (HTTP/TCP) | https (HTTP/SSL/TCP) socket, % Open socket, used by connection queue_length = 1, % Current length of pipeline or keep alive queue - type % pipeline | keep_alive (wait for response before sending new request) + type % pipeline | keep_alive (wait for response before sending new request) }). -record(http_cookie, diff --git a/lib/inets/src/http_client/httpc_manager.erl b/lib/inets/src/http_client/httpc_manager.erl index 63b00c7dce..f8fc6322ed 100644 --- a/lib/inets/src/http_client/httpc_manager.erl +++ b/lib/inets/src/http_client/httpc_manager.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2002-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2002-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -25,51 +25,85 @@ -include("http_internal.hrl"). %% Internal Application API --export([start_link/1, start_link/2, request/2, cancel_request/2, - request_canceled/2, retry_request/2, redirect_request/2, - insert_session/2, delete_session/2, set_options/2, store_cookies/3, - cookies/2, session_type/1]). +-export([ + start_link/3, + request/2, + cancel_request/2, + request_canceled/2, + retry_request/2, + redirect_request/2, + insert_session/2, + delete_session/2, + set_options/2, + store_cookies/3, + which_cookies/1, which_cookies/2, + reset_cookies/1, + session_type/1, + info/1 + ]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). --record(state, { +-record(state, + { cancel = [], % [{RequestId, HandlerPid, ClientPid}] - handler_db, % ets() - Entry: {Requestid, HandlerPid, ClientPid} - cookie_db, % {ets(), dets()} - {session_cookie_db, cookie_db} + handler_db, % ets() - Entry: #handler_info{} + cookie_db, % cookie_db() session_db, % ets() - Entry: #tcp_session{} profile_name, % atom() options = #options{} }). +-record(handler_info, + { + id, % Id of the request: request_id() + starter, % Pid of the handler starter process (temp): pid() + handler, % Pid of the handler process: pid() + from, % From for the request: from() + state % State of the handler: initiating | operational | canceled + }). + +%% Entries in the handler / request cross-ref table +%% -record(request_info, +%% { +%% id, % Id of the request +%% handler, % Pid of the handler process +%% from, % The From value for the caller +%% mref % Monitor ref for the caller +%% }). + %%==================================================================== %% Internal Application API %%==================================================================== %%-------------------------------------------------------------------- -%% Function: start_link({ProfileName, CookieDir}) -> {ok, Pid} +%% Function: start_link(ProfileName, CookieDir, ManagedHow) -> {ok, Pid} %% %% ProfileName - httpc_manager_<Profile> %% CookieDir - directory() +%% ManagedHow - stand_alone | inets %% -%% Description: Starts the http request manger process. (Started by -%% the intes supervisor.) -%%-------------------------------------------------------------------- -start_link({default, CookieDir}) -> - gen_server:start_link({local, ?MODULE}, ?MODULE, - [?MODULE, {http_default_cookie_db, CookieDir}], - []); -start_link({Profile, CookieDir}) -> - ProfileName = list_to_atom("httpc_manager_" ++ atom_to_list(Profile)), - gen_server:start_link({local, ProfileName}, ?MODULE, - [ProfileName, - {http_default_cookie_db, CookieDir}], []). -start_link({Profile, CookieDir}, stand_alone) -> - ProfileName = list_to_atom("stand_alone_" ++ atom_to_list(Profile)), - gen_server:start_link(?MODULE, [ProfileName, - {http_default_cookie_db, CookieDir}], - []). +%% Description: Starts the http request manager process. +%% (If ManagedHow = inets then started by the inets supervisor.) +%%-------------------------------------------------------------------- + +start_link(Profile, CookieDir, stand_alone) -> + ProfileName = httpc:profile_name("stand_alone_", Profile), + Args = [ProfileName, CookieDir], + Opts = [], + %% Opts = [{debug, [log, statistics]}], + gen_server:start_link(?MODULE, Args, Opts); +start_link(Profile, CookieDir, _) -> + ProfileName = httpc:profile_name(Profile), + Server = {local, ProfileName}, + Args = [ProfileName, CookieDir], + Opts = [], + %% Opts = [{debug, [log, statistics]}], + gen_server:start_link(Server, ?MODULE, Args, Opts). + + %%-------------------------------------------------------------------- %% Function: request(Request, ProfileName) -> %% {ok, Requestid} | {error, Reason} @@ -78,8 +112,10 @@ start_link({Profile, CookieDir}, stand_alone) -> %% %% Description: Sends a request to the httpc manager process. %%-------------------------------------------------------------------- + request(Request, ProfileName) -> - call(ProfileName, {request, Request}, infinity). + call(ProfileName, {request, Request}). + %%-------------------------------------------------------------------- %% Function: retry_request(Request, ProfileName) -> _ @@ -90,9 +126,11 @@ request(Request, ProfileName) -> %% to be called by the httpc handler process if it has to terminate with %% a non empty pipeline. %%-------------------------------------------------------------------- + retry_request(Request, ProfileName) -> cast(ProfileName, {retry_or_redirect_request, Request}). + %%-------------------------------------------------------------------- %% Function: redirect_request(Request, ProfileName) -> _ %% Request = #request{} @@ -102,9 +140,11 @@ retry_request(Request, ProfileName) -> %% manager process, intended to be called by the httpc handler process %% when the automatic redirect option is set. %%-------------------------------------------------------------------- + redirect_request(Request, ProfileName) -> cast(ProfileName, {retry_or_redirect_request, Request}). + %%-------------------------------------------------------------------- %% Function: cancel_request(RequestId, ProfileName) -> ok %% RequestId - ref() @@ -112,8 +152,10 @@ redirect_request(Request, ProfileName) -> %% %% Description: Cancels the request with <RequestId>. %%-------------------------------------------------------------------- + cancel_request(RequestId, ProfileName) -> - call(ProfileName, {cancel_request, RequestId}, infinity). + call(ProfileName, {cancel_request, RequestId}). + %%-------------------------------------------------------------------- %% Function: request_canceled(RequestId, ProfileName) -> ok @@ -123,9 +165,11 @@ cancel_request(RequestId, ProfileName) -> %% Description: Confirms that a request has been canceld. Intended to %% be called by the httpc handler process. %%-------------------------------------------------------------------- + request_canceled(RequestId, ProfileName) -> cast(ProfileName, {request_canceled, RequestId}). + %%-------------------------------------------------------------------- %% Function: insert_session(Session, ProfileName) -> _ %% Session - #tcp_session{} @@ -135,9 +179,12 @@ request_canceled(RequestId, ProfileName) -> %% table <ProfileName>_session_db. Intended to be called by %% the httpc request handler process. %%-------------------------------------------------------------------- + insert_session(Session, ProfileName) -> - Db = list_to_atom(atom_to_list(ProfileName) ++ "_session_db"), - ets:insert(Db, Session). + SessionDbName = session_db_name(ProfileName), + ?hcrt("insert session", [{session, Session}, {profile, ProfileName}]), + ets:insert(SessionDbName, Session). + %%-------------------------------------------------------------------- %% Function: delete_session(SessionId, ProfileName) -> _ @@ -148,9 +195,12 @@ insert_session(Session, ProfileName) -> %% table httpc_manager_session_db_<Profile>. Intended to be called by %% the httpc request handler process. %%-------------------------------------------------------------------- + delete_session(SessionId, ProfileName) -> - Db = list_to_atom(atom_to_list(ProfileName) ++ "_session_db"), - ets:delete(Db, SessionId). + SessionDbName = session_db_name(ProfileName), + ?hcrt("delete session", [{session_is, SessionId}, {profile, ProfileName}]), + ets:delete(SessionDbName, SessionId). + %%-------------------------------------------------------------------- %% Function: set_options(Options, ProfileName) -> ok @@ -166,9 +216,11 @@ delete_session(SessionId, ProfileName) -> %% %% Description: Sets the options to be used by the client. %%-------------------------------------------------------------------- + set_options(Options, ProfileName) -> cast(ProfileName, {set_options, Options}). + %%-------------------------------------------------------------------- %% Function: store_cookies(Cookies, Address, ProfileName) -> ok %% @@ -183,8 +235,22 @@ store_cookies([], _, _) -> store_cookies(Cookies, Address, ProfileName) -> cast(ProfileName, {store_cookies, {Cookies, Address}}). + +%%-------------------------------------------------------------------- +%% Function: reset_cookies(ProfileName) -> void() +%% +%% Url = string() +%% ProfileName = atom() +%% +%% Description: Resets the cookie database +%%-------------------------------------------------------------------- + +reset_cookies(ProfileName) -> + call(ProfileName, reset_cookies). + + %%-------------------------------------------------------------------- -%% Function: cookies(Url, ProfileName) -> ok +%% Function: which_cookies(Url, ProfileName) -> [cookie()] %% %% Url = string() %% ProfileName = atom() @@ -192,8 +258,25 @@ store_cookies(Cookies, Address, ProfileName) -> %% Description: Retrieves the cookies that would be sent when %% requesting <Url>. %%-------------------------------------------------------------------- -cookies(Url, ProfileName) -> - call(ProfileName, {cookies, Url}, infinity). + +which_cookies(ProfileName) -> + call(ProfileName, which_cookies). +which_cookies(Url, ProfileName) -> + call(ProfileName, {which_cookies, Url}). + + +%%-------------------------------------------------------------------- +%% Function: info(ProfileName) -> list() +%% +%% ProfileName = atom() +%% +%% Description: Retrieves various info about the manager and the +%% handlers it manages +%%-------------------------------------------------------------------- + +info(ProfileName) -> + call(ProfileName, info). + %%-------------------------------------------------------------------- %% Function: session_type(Options) -> ok @@ -202,11 +285,13 @@ cookies(Url, ProfileName) -> %% %% Description: Determines if to use pipelined sessions or not. %%-------------------------------------------------------------------- + session_type(#options{pipeline_timeout = 0}) -> keep_alive; session_type(_) -> pipeline. + %%==================================================================== %% gen_server callback functions %%==================================================================== @@ -216,19 +301,46 @@ session_type(_) -> %% {ok, State, Timeout} | ignore |{stop, Reason} %% Description: Initiates the httpc_manger process %%-------------------------------------------------------------------- -init([ProfileName, CookiesConf | _]) -> +init([ProfileName, CookiesDir]) -> process_flag(trap_exit, true), - SessionDb = list_to_atom(atom_to_list(ProfileName) ++ "_session_db"), - ets:new(SessionDb, + ?hcrv("starting", [{profile, ProfileName}]), + case (catch do_init(ProfileName, CookiesDir)) of + {ok, _} = OK -> + ?hcrd("started", [OK]), + OK; + {error, Reason} -> + {stop, Reason}; + Crap -> + {stop, Crap} + end. + + +do_init(ProfileName, CookiesDir) -> + %% Create session db + ?hcrt("create session db", []), + SessionDbName = session_db_name(ProfileName), + ets:new(SessionDbName, [public, set, named_table, {keypos, #tcp_session.id}]), - ?hcri("starting", [{profile, ProfileName}]), - {ok, #state{handler_db = ets:new(handler_db, [protected, set]), - cookie_db = - http_cookie:open_cookie_db({CookiesConf, - http_session_cookie_db}), - session_db = SessionDb, - profile_name = ProfileName - }}. + + %% Create handler db + ?hcrt("create handler/request db", []), + HandlerDbName = handler_db_name(ProfileName), + ets:new(HandlerDbName, + [protected, set, named_table, {keypos, #handler_info.id}]), + + %% Cookie DB + ?hcrt("create cookie db", []), + SessionCookieDbName = session_cookie_db_name(ProfileName), + CookieDbName = cookie_db_name(ProfileName), + CookieDb = httpc_cookie:open_db(CookieDbName, CookiesDir, + SessionCookieDbName), + + State = #state{handler_db = HandlerDbName, + cookie_db = CookieDb, + session_db = SessionDbName, + profile_name = ProfileName}, + {ok, State}. + %%-------------------------------------------------------------------- %% Function: handle_call(Request, From, State) -> {reply, Reply, State} | @@ -239,45 +351,80 @@ init([ProfileName, CookiesConf | _]) -> %% {stop, Reason, State} (terminate/2 is called) %% Description: Handling call messages %%-------------------------------------------------------------------- -handle_call({request, Request}, _, State) -> - ?hcri("request", [{request, Request}]), +handle_call({request, Request}, _From, State) -> + ?hcrv("request", [{request, Request}]), case (catch handle_request(Request, State)) of - {reply, Msg, NewState} -> - {reply, Msg, NewState}; + {ok, ReqId, NewState} -> + {reply, {ok, ReqId}, NewState}; + Error -> - {stop, Error, httpc_response:error(Request, Error), State} + NewError = {error, {failed_process_request, Error}}, + {reply, NewError, State} end; -handle_call({cancel_request, RequestId}, From, State) -> - ?hcri("cancel_request", [{request_id, RequestId}]), +handle_call({cancel_request, RequestId}, From, + #state{handler_db = HandlerDb} = State) -> + ?hcrv("cancel_request", [{request_id, RequestId}]), case ets:lookup(State#state.handler_db, RequestId) of [] -> - ok, %% Nothing to cancel - {reply, ok, State}; - [{_, Pid, _}] -> + ?hcrd("nothing to cancel", []), + Reply = ok, %% Nothing to cancel + {reply, Reply, State}; + + [#handler_info{handler = Pid}] when is_pid(Pid) -> + ?hcrd("found operational handler for this request", + [{handler, Pid}]), httpc_handler:cancel(RequestId, Pid), {noreply, State#state{cancel = - [{RequestId, Pid, From} | + [{RequestId, Pid, From} | + State#state.cancel]}}; + + [#handler_info{starter = Pid, state = HandlerState}] + when is_pid(Pid) -> + ?hcri("found *initiating* handler for this request", + [{starter, Pid}, {state, HandlerState}]), + ets:update_element(HandlerDb, RequestId, + {#handler_info.state, canceled}), + {noreply, State#state{cancel = + [{RequestId, Pid, From} | State#state.cancel]}} + end; -handle_call({cookies, Url}, _, State) -> +handle_call(reset_cookies, _, #state{cookie_db = CookieDb} = State) -> + ?hcrv("reset cookies", []), + httpc_cookie:reset_db(CookieDb), + {reply, ok, State}; + +handle_call(which_cookies, _, #state{cookie_db = CookieDb} = State) -> + ?hcrv("which cookies", []), + CookieHeaders = httpc_cookie:which_cookies(CookieDb), + {reply, CookieHeaders, State}; + +handle_call({which_cookies, Url}, _, #state{cookie_db = CookieDb} = State) -> + ?hcrv("which cookies", [{url, Url}]), case http_uri:parse(Url) of {Scheme, _, Host, Port, Path, _} -> CookieHeaders = - http_cookie:header(Scheme, {Host, Port}, - Path, State#state.cookie_db), + httpc_cookie:header(CookieDb, Scheme, {Host, Port}, Path), {reply, CookieHeaders, State}; Msg -> {reply, Msg, State} end; -handle_call(Msg, From, State) -> - Report = io_lib:format("HTTPC_MANAGER recived unkown call: ~p" - "from: ~p~n", [Msg, From]), - error_logger:error_report(Report), +handle_call(info, _, State) -> + ?hcrv("info", []), + Info = get_manager_info(State), + {reply, Info, State}; + +handle_call(Req, From, #state{profile_name = ProfileName} = State) -> + error_report(ProfileName, + "received unkown request" + "~n Req: ~p" + "~n From: ~p", [Req, From]), {reply, {error, 'API_violation'}, State}. + %%-------------------------------------------------------------------- %% Function: handle_cast(Msg, State) -> {noreply, State} | %% {noreply, State, Timeout} | @@ -286,29 +433,61 @@ handle_call(Msg, From, State) -> %%-------------------------------------------------------------------- handle_cast({retry_or_redirect_request, {Time, Request}}, #state{profile_name = ProfileName} = State) -> - {ok, _} = timer:apply_after(Time, ?MODULE, retry_request, [Request, ProfileName]), - {noreply, State}; + ?hcrv("retry or redirect request", [{time, Time}, {request, Request}]), + case timer:apply_after(Time, ?MODULE, retry_request, + [Request, ProfileName]) of + {ok, _} -> + {noreply, State}; + {error, Reason} -> + error_report(ProfileName, + "failed scheduling retry/redirect request" + "~n Time: ~p" + "~n Request: ~p" + "~n Reason: ~p", [Time, Request, Reason]), + {noreply, State} + end; -handle_cast({retry_or_redirect_request, Request}, State) -> +handle_cast({retry_or_redirect_request, Request}, + #state{profile_name = Profile, + handler_db = HandlerDb} = State) -> + ?hcrv("retry or redirect request", [{request, Request}]), case (catch handle_request(Request, State)) of - {reply, {ok, _}, NewState} -> + {ok, _, NewState} -> {noreply, NewState}; + Error -> - httpc_response:error(Request, Error), - {stop, Error, State} + ReqId = Request#request.id, + error_report(Profile, + "failed to retry or redirect request ~p" + "~n Error: ~p", [ReqId, Error]), + case ets:lookup(HandlerDb, ReqId) of + [#handler_info{from = From}] -> + Error2 = httpc_response:error(Request, Error), + httpc_response:send(From, Error2), + ok; + + _ -> + ok + end, + {noreply, State} end; handle_cast({request_canceled, RequestId}, State) -> + ?hcrv("request canceled", [{request_id, RequestId}]), ets:delete(State#state.handler_db, RequestId), case lists:keysearch(RequestId, 1, State#state.cancel) of {value, Entry = {RequestId, _, From}} -> + ?hcrt("found in cancel", [{from, From}]), gen_server:reply(From, ok), {noreply, State#state{cancel = lists:delete(Entry, State#state.cancel)}}; - _ -> + Else -> + ?hcrt("not found in cancel", [{else, Else}]), {noreply, State} end; + handle_cast({set_options, Options}, State = #state{options = OldOptions}) -> + ?hcrv("set options", [{options, Options}, {old_options, OldOptions}]), NewOptions = #options{proxy = get_proxy(Options, OldOptions), pipeline_timeout = get_pipeline_timeout(Options, OldOptions), @@ -320,7 +499,8 @@ handle_cast({set_options, Options}, State = #state{options = OldOptions}) -> ipfamily = get_ipfamily(Options, OldOptions), ip = get_ip(Options, OldOptions), port = get_port(Options, OldOptions), - verbose = get_verbose(Options, OldOptions) + verbose = get_verbose(Options, OldOptions), + socket_opts = get_socket_opts(Options, OldOptions) }, case {OldOptions#options.verbose, NewOptions#options.verbose} of {Same, Same} -> @@ -345,10 +525,10 @@ handle_cast({store_cookies, {Cookies, _}}, State) -> ok = do_store_cookies(Cookies, State), {noreply, State}; -handle_cast(Msg, State) -> - Report = io_lib:format("HTTPC_MANAGER recived unkown cast: ~p", - [Msg]), - error_logger:error_report(Report), +handle_cast(Msg, #state{profile_name = ProfileName} = State) -> + error_report(ProfileName, + "recived unknown message" + "~n Msg: ~p", [Msg]), {noreply, State}. @@ -359,17 +539,32 @@ handle_cast(Msg, State) -> %% {stop, Reason, State} (terminate/2 is called) %% Description: Handling all non call/cast messages %%--------------------------------------------------------- -handle_info({'EXIT', _, _}, State) -> - %% Handled in DOWN +handle_info({connect_and_send, StarterPid, ReqId, HandlerPid, Res}, State) -> + handle_connect_and_send(StarterPid, ReqId, HandlerPid, Res, State), + {noreply, State}; + +handle_info({failed_starting_handler, StarterPid, ReqId, Res}, State) -> + handle_failed_starting_handler(StarterPid, ReqId, Res, State), + {noreply, State}; + +handle_info({'EXIT', Pid, Reason}, #state{handler_db = HandlerDb} = State) -> + maybe_handle_terminating_starter(Pid, Reason, HandlerDb), {noreply, State}; + handle_info({'DOWN', _, _, Pid, _}, State) -> - ets:match_delete(State#state.handler_db, {'_', Pid, '_'}), + + %% + %% Check what happens to waiting requests! Chall we not send a reply? + %% + + Pattern = #handler_info{handler = Pid, _ = '_'}, + ets:match_delete(State#state.handler_db, Pattern), %% If there where any canceled request, handled by the %% the process that now has terminated, the %% cancelation can be viewed as sucessfull! - NewCanceldList = - lists:foldl(fun(Entry = {_, HandlerPid, From}, Acc) -> + NewCanceledList = + lists:foldl(fun({_, HandlerPid, From} = Entry, Acc) -> case HandlerPid of Pid -> gen_server:reply(From, ok), @@ -378,21 +573,25 @@ handle_info({'DOWN', _, _, Pid, _}, State) -> Acc end end, State#state.cancel, State#state.cancel), - {noreply, State#state{cancel = NewCanceldList}}; -handle_info(Info, State) -> - Report = io_lib:format("Unknown message in " - "httpc_manager:handle_info ~p~n", [Info]), - error_logger:error_report(Report), + {noreply, State#state{cancel = NewCanceledList}}; + +handle_info(Info, #state{profile_name = ProfileName} = State) -> + error_report(ProfileName, + "received unknown info" + "~n Info: ~p", [Info]), {noreply, State}. + + %%-------------------------------------------------------------------- %% Function: terminate(Reason, State) -> _ (ignored by gen_server) %% Description: Shutdown the httpc_handler %%-------------------------------------------------------------------- terminate(_, State) -> - http_cookie:close_cookie_db(State#state.cookie_db), + httpc_cookie:close_db(State#state.cookie_db), ets:delete(State#state.session_db), ets:delete(State#state.handler_db). + %%-------------------------------------------------------------------- %% Func: code_change(_OldVsn, State, Extra) -> {ok, NewState} %% Purpose: Convert process state when code is changed @@ -400,70 +599,217 @@ terminate(_, State) -> code_change(_OldVsn, State, _Extra) -> {ok, State}. + %%-------------------------------------------------------------------- %% Internal functions %%-------------------------------------------------------------------- -handle_request(#request{settings = - #http_options{version = "HTTP/0.9"}} = Request, - State) -> - %% Act as an HTTP/0.9 client that does not know anything - %% about persistent connections - NewRequest = handle_cookies(generate_request_id(Request), State), - NewHeaders = - (NewRequest#request.headers)#http_request_h{connection - = undefined}, - start_handler(NewRequest#request{headers = NewHeaders}, State), - {reply, {ok, NewRequest#request.id}, State}; +get_manager_info(#state{handler_db = HDB, + cookie_db = CDB} = _State) -> + HandlerInfo = get_handler_info(HDB), + CookieInfo = httpc_cookie:which_cookies(CDB), + [{handlers, HandlerInfo}, {cookies, CookieInfo}]. + +get_handler_info(Tab) -> + Pattern = #handler_info{handler = '$1', + state = '$2', + _ = '_'}, + Handlers1 = [{Pid, State} || [Pid, State] <- ets:match(Tab, Pattern)], + F = fun({Pid, State} = Elem, Acc) when State =/= canceled -> + case lists:keymember(Pid, 1, Acc) of + true -> + Acc; + false -> + [Elem | Acc] + end; + (_, Acc) -> + Acc + end, + Handlers2 = lists:foldl(F, [], Handlers1), + Handlers3 = [{Pid, State, httpc_handler:info(Pid)} || + {Pid, State} <- Handlers2], + Handlers3. + + +%% +%% The request handler process is started asynchronously by a +%% "starter process". When that process terminates it sends +%% one of two messages. These ara handled by the two functions +%% below. +%% + +handle_connect_and_send(_StarterPid, ReqId, HandlerPid, Result, + #state{profile_name = Profile, + handler_db = HandlerDb}) -> + case ets:lookup(HandlerDb, ReqId) of + [#handler_info{state = initiating} = HandlerInfo] when Result =:= ok -> + ?hcri("received connect-and-send ack for initiating handler", []), + HandlerInfo2 = HandlerInfo#handler_info{starter = undefined, + handler = HandlerPid, + state = operational}, + ets:insert(HandlerDb, HandlerInfo2), + ok; + + [#handler_info{state = canceled} = HandlerInfo] when Result =:= ok -> + ?hcri("received connect-and-send ack for canceled handler", []), + httpc_handler:cancel(ReqId, HandlerPid), + HandlerInfo2 = HandlerInfo#handler_info{starter = undefined, + handler = HandlerPid}, + ets:insert(HandlerDb, HandlerInfo2), + ok; + + [#handler_info{from = From}] -> + error_report(Profile, + "handler (~p) failed to connect and/or " + "send request ~p" + "~n Error: ~p", [HandlerPid, ReqId, Result]), + ?hcri("received connect-and-send error", [{result, Result}]), + Reason2 = + case Result of + {error, Reason} -> + {failed_connecting, Reason}; + _ -> + {failed_connecting, Result} + end, + DummyReq = #request{id = ReqId}, + httpc_response:send(From, httpc_response:error(DummyReq, Reason2)), + %% gen_server:reply(From, Error), + ets:delete(HandlerDb, ReqId), + ok; + + [] -> + error_report(Profile, + "handler successfully (~p) started for unknown request ~p", + [HandlerPid, ReqId]), + httpc_handler:cancel(ReqId, HandlerPid) + end. + +handle_failed_starting_handler(_StarterPid, ReqId, Error, + #state{profile_name = Profile, + handler_db = HandlerDb}) -> + case ets:lookup(HandlerDb, ReqId) of + [#handler_info{state = canceled, + from = From}] -> + error_report(Profile, + "failed starting handler for request ~p" + "~n Error: ~p", [ReqId, Error]), + request_canceled(Profile, ReqId), % Fake signal from handler + gen_server:reply(From, Error), + ets:delete(HandlerDb, ReqId), + ok; + + [#handler_info{from = From}] -> + error_report(Profile, + "failed starting handler for request ~p" + "~n Error: ~p", [ReqId, Error]), + gen_server:reply(From, Error), + ets:delete(HandlerDb, ReqId), + ok; + + [] -> + error_report(Profile, + "failed starting handler for unknown request ~p" + "~n Error: ~p", [ReqId, Error]), + ok + end. + + +maybe_handle_terminating_starter(MeybeStarterPid, Reason, HandlerDb) -> + Pattern = #handler_info{starter = MeybeStarterPid, _ = '_'}, + case ets:match_object(HandlerDb, Pattern) of + [#handler_info{id = ReqId, from = From, state = initiating}] -> + Error = {error, {failed_starting_request_handler, Reason}}, + gen_server:reply(From, Error), + ets:delete(HandlerDb, ReqId), + ok; + _ -> + ok + end. + + +%% ----- +%% Act as an HTTP/0.9 client that does not know anything +%% about persistent connections handle_request(#request{settings = - #http_options{version = "HTTP/1.0"}} = Request, + #http_options{version = "HTTP/0.9"}} = Request0, State) -> - %% Act as an HTTP/1.0 client that does not - %% use persistent connections - - NewRequest = handle_cookies(generate_request_id(Request), State), - NewHeaders = - (NewRequest#request.headers)#http_request_h{connection - = "close"}, - start_handler(NewRequest#request{headers = NewHeaders}, State), - {reply, {ok, NewRequest#request.id}, State}; - -handle_request(Request, State = #state{options = Options}) -> - - NewRequest = handle_cookies(generate_request_id(Request), State), - SessionType = session_type(Options), - case select_session(Request#request.method, - Request#request.address, - Request#request.scheme, SessionType, State) of + Request1 = handle_cookies(generate_request_id(Request0), State), + Hdrs0 = Request1#request.headers, + Hdrs1 = Hdrs0#http_request_h{connection = undefined}, + Request2 = Request1#request{headers = Hdrs1}, + create_handler_starter(Request2, State), + {ok, Request2#request.id, State}; + +%% ----- +%% Act as an HTTP/1.0 client that does not +%% use persistent connections +handle_request(#request{settings = + #http_options{version = "HTTP/1.0"}} = Request0, + State) -> + Request1 = handle_cookies(generate_request_id(Request0), State), + Hdrs0 = Request1#request.headers, + Hdrs1 = Hdrs0#http_request_h{connection = "close"}, + Request2 = Request1#request{headers = Hdrs1}, + create_handler_starter(Request2, State), + {ok, Request2#request.id, State}; + + +%% ----- +handle_request(#request{method = Method, + address = Address, + scheme = Scheme} = Request0, + #state{options = Opts} = State) -> + Request1 = handle_cookies(generate_request_id(Request0), State), + SessionType = session_type(Opts), + case select_session(Method, Address, Scheme, SessionType, State) of {ok, HandlerPid} -> - pipeline_or_keep_alive(NewRequest, HandlerPid, State); + pipeline_or_keep_alive(Request1, HandlerPid, State); no_connection -> - start_handler(NewRequest, State); - {no_session, OpenSessions} when OpenSessions - < Options#options.max_sessions -> - start_handler(NewRequest, State); + create_handler_starter(Request1, State); + {no_session, OpenSessions} + when OpenSessions < Opts#options.max_sessions -> + create_handler_starter(Request1, State); {no_session, _} -> %% Do not start any more persistent connections %% towards this server. - NewHeaders = - (NewRequest#request.headers)#http_request_h{connection - = "close"}, - start_handler(NewRequest#request{headers = NewHeaders}, State) + Hdrs0 = Request1#request.headers, + Hdrs1 = Hdrs0#http_request_h{connection = "close"}, + Request2 = Request1#request{headers = Hdrs1}, + create_handler_starter(Request2, State) end, - {reply, {ok, NewRequest#request.id}, State}. + {ok, Request1#request.id, State}. -select_session(Method, HostPort, Scheme, SessionTyp, - #state{options = #options{max_pipeline_length = - MaxPipe, + +select_session(Method, HostPort, Scheme, SessionType, + #state{options = #options{max_pipeline_length = MaxPipe, max_keep_alive_length = MaxKeepAlive}, session_db = SessionDb}) -> - case httpc_request:is_idempotent(Method) or (SessionTyp == keep_alive) of + ?hcrd("select session", [{session_type, SessionType}, + {max_pipeline_length, MaxPipe}, + {max_keep_alive_length, MaxKeepAlive}]), + case httpc_request:is_idempotent(Method) orelse + (SessionType =:= keep_alive) of true -> - Candidates = ets:match(SessionDb, - {'_', {HostPort, '$1'}, - false, Scheme, '_', '$2', SessionTyp}), - select_session(Candidates, MaxKeepAlive, MaxPipe, SessionTyp); + %% Look for handlers connecting to this host (HostPort) + %% tcp_session with record name field (tcp_session) and + %% socket fields ignored. The fields id (part of: HostPort), + %% client_close, scheme and type specified. + %% The fields id (part of: HandlerPid) and queue_length + %% specified. + Pattern = #tcp_session{id = {HostPort, '$1'}, + client_close = false, + scheme = Scheme, + socket = '_', + queue_length = '$2', + type = SessionType}, + %% {'_', {HostPort, '$1'}, false, Scheme, '_', '$2', SessionTyp}, + Candidates = ets:match(SessionDb, Pattern), + ?hcrd("select session", [{host_port, HostPort}, + {scheme, Scheme}, + {type, SessionType}, + {candidates, Candidates}]), + select_session(Candidates, MaxKeepAlive, MaxPipe, SessionType); false -> no_connection end. @@ -473,51 +819,102 @@ select_session(Candidates, Max, _, keep_alive) -> select_session(Candidates, _, Max, pipeline) -> select_session(Candidates, Max). +select_session([] = _Candidates, _Max) -> + ?hcrd("select session - no candicate", []), + no_connection; select_session(Candidates, Max) -> - case Candidates of + NewCandidates = + [{Pid, Length} || [Pid, Length] <- Candidates, Length =< Max], + case lists:keysort(2, NewCandidates) of [] -> - no_connection; - _ -> - NewCandidates = - lists:foldl( - fun([Pid, Length], Acc) when Length =< Max -> - [{Pid, Length} | Acc]; - (_, Acc) -> - Acc - end, [], Candidates), - - case lists:keysort(2, NewCandidates) of - [] -> - {no_session, length(Candidates)}; - [{HandlerPid, _} | _] -> - {ok, HandlerPid} - end + {no_session, length(Candidates)}; + [{HandlerPid, _} | _] -> + ?hcrd("select session - found one", [{handler, HandlerPid}]), + {ok, HandlerPid} end. -pipeline_or_keep_alive(Request, HandlerPid, State) -> +pipeline_or_keep_alive(#request{id = Id} = Request, HandlerPid, State) -> + ?hcrd("pipeline of keep-alive", [{id, Id}, {handler, HandlerPid}]), case (catch httpc_handler:send(Request, HandlerPid)) of ok -> - ets:insert(State#state.handler_db, {Request#request.id, - HandlerPid, - Request#request.from}); - _ -> %timeout pipelining failed - start_handler(Request, State) + ?hcrd("pipeline or keep-alive - successfully sent", []), + Entry = #handler_info{id = Id, + handler = HandlerPid, + state = operational}, + ets:insert(State#state.handler_db, Entry); + + _ -> %% timeout pipelining failed + ?hcrd("pipeline or keep-alive - failed sending -> " + "start a new handler", []), + create_handler_starter(Request, State) end. -start_handler(Request, State) -> - {ok, Pid} = - case is_inets_manager() of - true -> - httpc_handler_sup:start_child([Request, State#state.options, - State#state.profile_name]); - false -> - httpc_handler:start_link(Request, State#state.options, - State#state.profile_name) - end, - ets:insert(State#state.handler_db, {Request#request.id, - Pid, Request#request.from}), - erlang:monitor(process, Pid). +create_handler_starter(#request{socket_opts = SocketOpts} = Request, + #state{options = Options} = State) + when is_list(SocketOpts) -> + %% The user provided us with (override) socket options + ?hcrt("create handler starter", [{socket_opts, SocketOpts}, {options, Options}]), + Options2 = Options#options{socket_opts = SocketOpts}, + create_handler_starter(Request#request{socket_opts = undefined}, + State#state{options = Options2}); + +create_handler_starter(#request{id = Id, + from = From} = Request, + #state{profile_name = ProfileName, + options = Options, + handler_db = HandlerDb} = _State) -> + ?hcrv("create handler starter", [{id, Id}, {profile, ProfileName}]), + IsInetsManager = is_inets_manager(), + ManagerPid = self(), + StarterFun = + fun() -> + ?hcrd("handler starter - start", + [{id, Id}, + {profile, ProfileName}, + {inets_manager, IsInetsManager}]), + Result1 = + case IsInetsManager of + true -> + httpc_handler_sup:start_child(Options, + ProfileName); + false -> + httpc_handler:start_link(Options, + ProfileName) + end, + ?hcrd("handler starter - maybe connect and send", + [{id, Id}, {profile, ProfileName}, {result, Result1}]), + case Result1 of + {ok, HandlerPid} -> + Result2 = httpc_handler:connect_and_send(Request, + HandlerPid), + ?hcrd("handler starter - connected and sent", + [{id, Id}, {profile, ProfileName}, + {handler, HandlerPid}, {result, Result2}]), + ConnAndSendMessage = + {connect_and_send, + self(), Id, HandlerPid, Result2}, + ManagerPid ! ConnAndSendMessage; + {error, Reason} -> + StartFailureMessage = + {failed_starting_handler, self(), Id, Reason}, + ManagerPid ! StartFailureMessage; + _ -> + StartFailureMessage = + {failed_starting_handler, self(), Id, Result1}, + ManagerPid ! StartFailureMessage + end + end, + Starter = erlang:spawn_link(StarterFun), + ?hcrd("create handler starter - started", [{id, Id}, {starter, Starter}]), + Entry = #handler_info{id = Id, + starter = Starter, + from = From, + state = initiating}, + ets:insert(HandlerDb, Entry), + ok. + + is_inets_manager() -> case get('$ancestors') of [httpc_profile_sup | _] -> @@ -532,32 +929,55 @@ generate_request_id(Request) -> RequestId = make_ref(), Request#request{id = RequestId}; _ -> - %% This is an automatic redirect or a retryed pipelined - %% request keep the old id. + %% This is an automatic redirect or a retryed pipelined request + %% => keep the old id. Request end. handle_cookies(Request, #state{options = #options{cookies = disabled}}) -> Request; -handle_cookies(Request = #request{scheme = Scheme, address = Address, - path = Path, headers = - Headers = #http_request_h{other = Other}}, - #state{cookie_db = Db}) -> - case http_cookie:header(Scheme, Address, Path, Db) of +handle_cookies( + #request{scheme = Scheme, + address = Address, + path = Path, + headers = #http_request_h{other = Other} = Hdrs} = Request, + #state{cookie_db = CookieDb}) -> + case httpc_cookie:header(CookieDb, Scheme, Address, Path) of {"cookie", ""} -> Request; CookieHeader -> - NewHeaders = - Headers#http_request_h{other = [CookieHeader | Other]}, + NewHeaders = Hdrs#http_request_h{other = [CookieHeader | Other]}, Request#request{headers = NewHeaders} end. do_store_cookies([], _) -> ok; -do_store_cookies([Cookie | Cookies], State) -> - ok = http_cookie:insert(Cookie, State#state.cookie_db), +do_store_cookies([Cookie | Cookies], #state{cookie_db = CookieDb} = State) -> + ok = httpc_cookie:insert(CookieDb, Cookie), do_store_cookies(Cookies, State). + + +session_db_name(ProfileName) -> + make_db_name(ProfileName, "__session_db"). + +cookie_db_name(ProfileName) -> + make_db_name(ProfileName, "__cookie_db"). + +session_cookie_db_name(ProfileName) -> + make_db_name(ProfileName, "__session_cookie_db"). + +handler_db_name(ProfileName) -> + make_db_name(ProfileName, "__handler_db"). + +make_db_name(ProfileName, Post) -> + list_to_atom(atom_to_list(ProfileName) ++ Post). + + + +call(ProfileName, Msg) -> + Timeout = infinity, + call(ProfileName, Msg, Timeout). call(ProfileName, Msg, Timeout) -> gen_server:call(ProfileName, Msg, Timeout). @@ -611,6 +1031,9 @@ get_port(Opts, #options{port = Default}) -> get_verbose(Opts, #options{verbose = Default}) -> proplists:get_value(verbose, Opts, Default). +get_socket_opts(Opts, #options{socket_opts = Default}) -> + proplists:get_value(socket_opts, Opts, Default). + handle_verbose(debug) -> dbg:p(self(), [call]), @@ -621,6 +1044,12 @@ handle_verbose(trace) -> handle_verbose(_) -> ok. + +error_report(Profile, F, A) -> + Report = io_lib:format("HTTPC-MANAGER<~p> " ++ F ++ "~n", [Profile | A]), + error_logger:error_report(Report). + + %% d(F) -> %% d(F, []). diff --git a/lib/inets/src/http_client/httpc_profile_sup.erl b/lib/inets/src/http_client/httpc_profile_sup.erl index 2351083435..29f86aa373 100644 --- a/lib/inets/src/http_client/httpc_profile_sup.erl +++ b/lib/inets/src/http_client/httpc_profile_sup.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2007-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2007-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -28,6 +28,7 @@ %% Supervisor callback -export([init/1]). + %%%========================================================================= %%% API %%%========================================================================= @@ -39,7 +40,8 @@ start_child(PropList) -> undefined -> {error, no_profile}; Profile -> - Dir = proplists:get_value(data_dir, PropList, only_session_cookies), + Dir = + proplists:get_value(data_dir, PropList, only_session_cookies), Spec = httpc_child_spec(Profile, Dir), supervisor:start_child(?MODULE, Spec) end. @@ -63,12 +65,12 @@ stop_child(Profile) -> end. id(Profile) -> - DefaultProfile = http:default_profile(), + DefaultProfile = httpc:default_profile(), case Profile of DefaultProfile -> httpc_manager; _ -> - {http, Profile} + {httpc, Profile} end. @@ -98,7 +100,7 @@ child_spec([{httpc, PropList} | Rest], Acc) when is_list(PropList) -> httpc_child_spec(Profile, Dir) -> Name = id(Profile), - StartFunc = {httpc_manager, start_link, [{Profile, Dir}]}, + StartFunc = {httpc_manager, start_link, [Profile, Dir, inets]}, Restart = permanent, Shutdown = 4000, Modules = [httpc_manager], diff --git a/lib/inets/src/http_client/httpc_request.erl b/lib/inets/src/http_client/httpc_request.erl index 3d66638d66..55e0af4b42 100644 --- a/lib/inets/src/http_client/httpc_request.erl +++ b/lib/inets/src/http_client/httpc_request.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2004-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2004-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% @@ -39,14 +39,47 @@ %% %% Description: Composes and sends a HTTP-request. %%------------------------------------------------------------------------- -send(SendAddr, #request{method = Method, scheme = Scheme, - path = Path, pquery = Query, headers = Headers, - content = Content, address = Address, - abs_uri = AbsUri, headers_as_is = HeadersAsIs, - settings = HttpOptions, - userinfo = UserInfo}, - Socket) -> +send(SendAddr, #request{scheme = Scheme, socket_opts = SocketOpts} = Request, + Socket) + when is_list(SocketOpts) -> + SocketType = socket_type(Scheme), + case http_transport:setopts(SocketType, Socket, SocketOpts) of + ok -> + send(SendAddr, Socket, SocketType, + Request#request{socket_opts = undefined}); + {error, Reason} -> + {error, {setopts_failed, Reason}} + end; +send(SendAddr, #request{scheme = Scheme} = Request, Socket) -> + SocketType = socket_type(Scheme), + send(SendAddr, Socket, SocketType, Request). + +send(SendAddr, Socket, SocketType, + #request{method = Method, + path = Path, + pquery = Query, + headers = Headers, + content = Content, + address = Address, + abs_uri = AbsUri, + headers_as_is = HeadersAsIs, + settings = HttpOptions, + userinfo = UserInfo}) -> + ?hcrt("send", + [{send_addr, SendAddr}, + {socket, Socket}, + {method, Method}, + {path, Path}, + {pquery, Query}, + {headers, Headers}, + {content, Content}, + {address, Address}, + {abs_uri, AbsUri}, + {headers_as_is, HeadersAsIs}, + {settings, HttpOptions}, + {userinfo, UserInfo}]), + TmpHeaders = handle_user_info(UserInfo, Headers), {TmpHeaders2, Body} = @@ -70,9 +103,14 @@ send(SendAddr, #request{method = Method, scheme = Scheme, Version = HttpOptions#http_options.version, Message = [method(Method), " ", Uri, " ", - version(Version), ?CRLF, headers(FinalHeaders, Version), ?CRLF, Body], + version(Version), ?CRLF, + headers(FinalHeaders, Version), ?CRLF, Body], + + ?hcrd("send", [{message, Message}]), - http_transport:send(socket_type(Scheme), Socket, lists:append(Message)). + http_transport:send(SocketType, Socket, lists:append(Message)). + + %%------------------------------------------------------------------------- %% is_idempotent(Method) -> @@ -123,7 +161,7 @@ is_client_closing(Headers) -> %%% Internal functions %%%======================================================================== post_data(Method, Headers, {ContentType, Body}, HeadersAsIs) - when Method == post; Method == put -> + when (Method =:= post) orelse (Method =:= put) -> ContentLength = body_length(Body), NewBody = case Headers#http_request_h.expect of "100-continue" -> diff --git a/lib/inets/src/http_client/httpc_response.erl b/lib/inets/src/http_client/httpc_response.erl index e2ba66f730..df7d40a33e 100644 --- a/lib/inets/src/http_client/httpc_response.erl +++ b/lib/inets/src/http_client/httpc_response.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2004-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2004-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% @@ -132,8 +132,13 @@ result(Response = {{_,Code,_}, _, _}, Request) when (Code div 100) =:= 5 -> result(Response, Request) -> transparent(Response, Request). -send(To, Msg) -> - To ! {http, Msg}. +send(Receiver, Msg) when is_pid(Receiver) -> + Receiver ! {http, Msg}; +send(Receiver, Msg) when is_function(Receiver) -> + (catch Receiver(Msg)); +send({Module, Function, Args}, Msg) -> + (catch apply(Module, Function, [Msg | Args])). + %%%======================================================================== %%% Internal functions diff --git a/lib/inets/src/http_lib/Makefile b/lib/inets/src/http_lib/Makefile index 27e7ee65c5..7f4c92861c 100644 --- a/lib/inets/src/http_lib/Makefile +++ b/lib/inets/src/http_lib/Makefile @@ -1,19 +1,19 @@ # # %CopyrightBegin% -# -# Copyright Ericsson AB 2005-2009. All Rights Reserved. -# +# +# Copyright Ericsson AB 2005-2010. All Rights Reserved. +# # The contents of this file are subject to the Erlang Public License, # Version 1.1, (the "License"); you may not use this file except in # compliance with the License. You should have received a copy of the # Erlang Public License along with this software. If not, it can be # retrieved online at http://www.erlang.org/. -# +# # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. -# +# # %CopyrightEnd% # # @@ -22,6 +22,7 @@ include $(ERL_TOP)/make/target.mk EBIN = ../../ebin include $(ERL_TOP)/make/$(TARGET)/otp.mk + # ---------------------------------------------------- # Application version # ---------------------------------------------------- @@ -29,10 +30,12 @@ include ../../vsn.mk VSN = $(INETS_VSN) + # ---------------------------------------------------- # Release directory specification # ---------------------------------------------------- -RELSYSDIR = $(RELEASE_PATH)/lib/inets-$(VSN) +RELSYSDIR = $(RELEASE_PATH)/lib/$(APPLICATION)-$(VSN) + # ---------------------------------------------------- # Target Specs @@ -50,10 +53,12 @@ ERL_FILES = $(MODULES:%=%.erl) TARGET_FILES= $(MODULES:%=$(EBIN)/%.$(EMULATOR)) + # ---------------------------------------------------- # INETS FLAGS # ---------------------------------------------------- -INETS_FLAGS = -D'SERVER_SOFTWARE="inets/$(VSN)"' \ +INETS_FLAGS = -D'SERVER_SOFTWARE="$(APPLICATION)/$(VSN)"' + # ---------------------------------------------------- # FLAGS @@ -82,6 +87,7 @@ clean: docs: + # ---------------------------------------------------- # Release Target # ---------------------------------------------------- @@ -96,6 +102,8 @@ release_spec: opt release_docs_spec: info: + @echo "APPLICATION = $(APPLICATION)" @echo "INETS_DEBUG = $(INETS_DEBUG)" @echo "INETS_FLAGS = $(INETS_FLAGS)" @echo "ERL_COMPILE_FLAGS = $(ERL_COMPILE_FLAGS)" + diff --git a/lib/inets/src/http_lib/http_chunk.erl b/lib/inets/src/http_lib/http_chunk.erl index cd20dce9d5..621bc68eae 100644 --- a/lib/inets/src/http_lib/http_chunk.erl +++ b/lib/inets/src/http_lib/http_chunk.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2004-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2004-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% Description: Implements chunked transfer encoding see RFC2616 section @@ -186,13 +186,6 @@ decode_data(ChunkSize, TotalChunk, Info = {MaxBodySize, BodySoFar, AccLength, MaxHeaderSize, Stream}) when ChunkSize =< size(TotalChunk) -> case TotalChunk of - %% Potential last chunk - <<_:ChunkSize/binary, ?CR, ?LF, "0">> -> - {?MODULE, decode_data, [ChunkSize, TotalChunk, Info]}; - <<_:ChunkSize/binary, ?CR, ?LF, "0", ?CR>> -> - {?MODULE, decode_data, [ChunkSize, TotalChunk, Info]}; - <<_:ChunkSize/binary, ?CR, ?LF>> -> - {?MODULE, decode_data, [ChunkSize, TotalChunk, Info]}; %% Last chunk <<Data:ChunkSize/binary, ?CR, ?LF, "0", ";">> -> %% Note ignore_extensions will call decode_trailer/1 @@ -223,6 +216,10 @@ decode_data(ChunkSize, TotalChunk, NewBody, integer_to_list(AccLength)); %% There are more chunks, so here we go agin... + <<Data:ChunkSize/binary, ?CR, ?LF>> -> + {NewBody, NewStream} = + stream(<<BodySoFar/binary, Data/binary>>, Stream), + {?MODULE, decode_size, [<<>>, [], {MaxBodySize, NewBody, AccLength, MaxHeaderSize, NewStream}]}; <<Data:ChunkSize/binary, ?CR, ?LF, Rest/binary>> when (AccLength < MaxBodySize) or (MaxBodySize == nolimit) -> {NewBody, NewStream} = diff --git a/lib/inets/src/http_lib/http_transport.erl b/lib/inets/src/http_lib/http_transport.erl index 8100d7183a..7c2ac626e6 100644 --- a/lib/inets/src/http_lib/http_transport.erl +++ b/lib/inets/src/http_lib/http_transport.erl @@ -1,32 +1,48 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2004-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2004-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% -% + -module(http_transport). % Internal application API --export([start/1, connect/3, connect/4, listen/2, listen/3, - accept/2, accept/3, close/2, - send/3, controlling_process/3, setopts/3, - peername/2, resolve/0]). +-export([ + start/1, + connect/3, connect/4, + listen/2, listen/3, + accept/2, accept/3, + close/2, + send/3, + controlling_process/3, + setopts/3, getopts/2, getopts/3, + getstat/2, + peername/2, sockname/2, + resolve/0 + ]). -export([negotiate/3]). +-include("inets_internal.hrl"). +-define(SERVICE, httpl). +-define(hlri(Label, Content), ?report_important(Label, ?SERVICE, Content)). +-define(hlrv(Label, Content), ?report_verbose(Label, ?SERVICE, Content)). +-define(hlrd(Label, Content), ?report_debug(Label, ?SERVICE, Content)). +-define(hlrt(Label, Content), ?report_trace(Label, ?SERVICE, Content)). + %%%========================================================================= %%% Internal application API @@ -77,14 +93,22 @@ connect(SocketType, Address, Opts) -> connect(ip_comm = _SocketType, {Host, Port}, Opts0, Timeout) when is_list(Opts0) -> Opts = [binary, {packet, 0}, {active, false}, {reuseaddr, true} | Opts0], + ?hlrt("connect using gen_tcp", + [{host, Host}, {port, Port}, {opts, Opts}, {timeout, Timeout}]), gen_tcp:connect(Host, Port, Opts, Timeout); connect({ssl, SslConfig}, {Host, Port}, _, Timeout) -> Opts = [binary, {active, false}] ++ SslConfig, + ?hlrt("connect using ssl", + [{host, Host}, {port, Port}, {ssl_config, SslConfig}, + {timeout, Timeout}]), ssl:connect(Host, Port, Opts, Timeout); connect({erl_ssl, SslConfig}, {Host, Port}, _, Timeout) -> Opts = [binary, {active, false}, {ssl_imp, new}] ++ SslConfig, + ?hlrt("connect using erl_ssl", + [{host, Host}, {port, Port}, {ssl_config, SslConfig}, + {timeout, Timeout}]), ssl:connect(Host, Port, Opts, Timeout). @@ -209,6 +233,7 @@ accept(ip_comm, ListenSocket, Timeout) -> accept({ssl,_SSLConfig}, ListenSocket, Timeout) -> ssl:transport_accept(ListenSocket, Timeout). + %%------------------------------------------------------------------------- %% controlling_process(SocketType, Socket, NewOwner) -> ok | {error, Reason} %% SocketType = ip_comm | {ssl, _} @@ -222,6 +247,7 @@ controlling_process(ip_comm, Socket, NewOwner) -> controlling_process({ssl, _}, Socket, NewOwner) -> ssl:controlling_process(Socket, NewOwner). + %%------------------------------------------------------------------------- %% setopts(SocketType, Socket, Options) -> ok | {error, Reason} %% SocketType = ip_comm | {ssl, _} @@ -231,10 +257,62 @@ controlling_process({ssl, _}, Socket, NewOwner) -> %% gen_tcp or ssl. %%------------------------------------------------------------------------- setopts(ip_comm, Socket, Options) -> - inet:setopts(Socket,Options); + ?hlrt("ip_comm setopts", [{socket, Socket}, {options, Options}]), + inet:setopts(Socket, Options); setopts({ssl, _}, Socket, Options) -> + ?hlrt("ssl setopts", [{socket, Socket}, {options, Options}]), ssl:setopts(Socket, Options). + +%%------------------------------------------------------------------------- +%% getopts(SocketType, Socket [, Opts]) -> ok | {error, Reason} +%% SocketType = ip_comm | {ssl, _} +%% Socket = socket() +%% Opts = socket_options() +%% Description: Gets the values for some options. +%%------------------------------------------------------------------------- +getopts(SocketType, Socket) -> + Opts = [packet, packet_size, recbuf, sndbuf, priority, tos, send_timeout], + getopts(SocketType, Socket, Opts). + +getopts(ip_comm, Socket, Options) -> + ?hlrt("ip_comm getopts", [{socket, Socket}, {options, Options}]), + case inet:getopts(Socket, Options) of + {ok, SocketOpts} -> + SocketOpts; + {error, _} -> + [] + end; +getopts({ssl, _}, Socket, Options) -> + ?hlrt("ssl getopts", [{socket, Socket}, {options, Options}]), + case ssl:getopts(Socket, Options) of + {ok, SocketOpts} -> + SocketOpts; + {error, _} -> + [] + end. + + +%%------------------------------------------------------------------------- +%% getstat(SocketType, Socket) -> socket_stats() +%% SocketType = ip_comm | {ssl, _} +%% Socket = socket() +%% socket_stats() = list() +%% Description: Gets the socket stats values for the socket +%%------------------------------------------------------------------------- +getstat(ip_comm = _SocketType, Socket) -> + ?hlrt("ip_comm getstat", [{socket, Socket}]), + case inet:getstat(Socket) of + {ok, Stats} -> + Stats; + {error, _} -> + [] + end; +getstat({ssl, _} = _SocketType, _Socket) -> + %% ?hlrt("ssl getstat", [{socket, Socket}]), + []. + + %%------------------------------------------------------------------------- %% send(RequestOrSocketType, Socket, Message) -> ok | {error, Reason} %% SocketType = ip_comm | {ssl, _} @@ -247,6 +325,7 @@ send(ip_comm, Socket, Message) -> send({ssl, _}, Socket, Message) -> ssl:send(Socket, Message). + %%------------------------------------------------------------------------- %% close(SocketType, Socket) -> ok | {error, Reason} %% SocketType = ip_comm | {ssl, _} @@ -260,9 +339,11 @@ close({ssl, _}, Socket) -> ssl:close(Socket). %%------------------------------------------------------------------------- -%% peername(SocketType, Socket) -> ok | {error, Reason} +%% peername(SocketType, Socket) -> {Port, SockName} %% SocketType = ip_comm | {ssl, _} %% Socket = socket() +%% Port = integer() (-1 if error occured) +%% PeerName = string() %% %% Description: Returns the address and port for the other end of a %% connection, usning either gen_tcp or ssl. @@ -297,6 +378,48 @@ peername({ssl, _}, Socket) -> {-1, "unknown"} end. + +%%------------------------------------------------------------------------- +%% sockname(SocketType, Socket) -> {Port, SockName} +%% SocketType = ip_comm | {ssl, _} +%% Socket = socket() +%% Port = integer() (-1 if error occured) +%% SockName = string() +%% +%% Description: Returns the address and port for the local (our) end +%% other end of connection, using either gen_tcp or ssl. +%%------------------------------------------------------------------------- +sockname(ip_comm, Socket) -> + case inet:sockname(Socket) of + {ok,{{A, B, C, D}, Port}} -> + SockName = integer_to_list(A)++"."++integer_to_list(B)++"."++ + integer_to_list(C)++"."++integer_to_list(D), + {Port, SockName}; + {ok,{{A, B, C, D, E, F, G, H}, Port}} -> + SockName = http_util:integer_to_hexlist(A) ++ ":"++ + http_util:integer_to_hexlist(B) ++ ":" ++ + http_util:integer_to_hexlist(C) ++ ":" ++ + http_util:integer_to_hexlist(D) ++ ":" ++ + http_util:integer_to_hexlist(E) ++ ":" ++ + http_util:integer_to_hexlist(F) ++ ":" ++ + http_util:integer_to_hexlist(G) ++":"++ + http_util:integer_to_hexlist(H), + {Port, SockName}; + {error, _} -> + {-1, "unknown"} + end; + +sockname({ssl, _}, Socket) -> + case ssl:sockname(Socket) of + {ok,{{A, B, C, D}, Port}} -> + SockName = integer_to_list(A)++"."++integer_to_list(B)++"."++ + integer_to_list(C)++"."++integer_to_list(D), + {Port, SockName}; + {error, _} -> + {-1, "unknown"} + end. + + %%------------------------------------------------------------------------- %% resolve() -> HostName %% HostName = string() diff --git a/lib/inets/src/http_lib/http_util.erl b/lib/inets/src/http_lib/http_util.erl index b03b780cf8..ddb58c7116 100644 --- a/lib/inets/src/http_lib/http_util.erl +++ b/lib/inets/src/http_lib/http_util.erl @@ -1,27 +1,33 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2005-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2005-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% -module(http_util). --export([to_upper/1, to_lower/1, convert_netscapecookie_date/1, +-export([ + to_upper/1, to_lower/1, + convert_netscapecookie_date/1, hexlist_to_integer/1, integer_to_hexlist/1, - convert_month/1, is_hostname/1]). + convert_month/1, + is_hostname/1, + timestamp/0, timeout/2 + ]). + %%%========================================================================= %%% Internal application API @@ -100,6 +106,21 @@ convert_month("Dec") -> 12. is_hostname(Dest) -> inet_parse:domain(Dest). + +timestamp() -> + {A,B,C} = os:timestamp(), + A*1000000000+B*1000+(C div 1000). + +timeout(Timeout, Started) -> + %% NewTimeout = Timeout - (timestamp() - Started), + case Timeout - (timestamp() - Started) of + NewTimeout when Timeout > 0 -> + NewTimeout; + _ -> + 0 + end. + + %%%======================================================================== %%% Internal functions %%%======================================================================== diff --git a/lib/inets/src/http_server/Makefile b/lib/inets/src/http_server/Makefile index 4bbd23df3f..ce1405011e 100644 --- a/lib/inets/src/http_server/Makefile +++ b/lib/inets/src/http_server/Makefile @@ -1,19 +1,19 @@ # # %CopyrightBegin% -# -# Copyright Ericsson AB 2005-2009. All Rights Reserved. -# +# +# Copyright Ericsson AB 2005-2010. All Rights Reserved. +# # The contents of this file are subject to the Erlang Public License, # Version 1.1, (the "License"); you may not use this file except in # compliance with the License. You should have received a copy of the # Erlang Public License along with this software. If not, it can be # retrieved online at http://www.erlang.org/. -# +# # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. -# +# # %CopyrightEnd% # # @@ -33,7 +33,7 @@ VSN = $(INETS_VSN) # ---------------------------------------------------- # Release directory specification # ---------------------------------------------------- -RELSYSDIR = $(RELEASE_PATH)/lib/inets-$(VSN) +RELSYSDIR = $(RELEASE_PATH)/lib/$(APPLICATION)-$(VSN) # ---------------------------------------------------- @@ -92,7 +92,7 @@ TARGET_FILES= $(MODULES:%=$(EBIN)/%.$(EMULATOR)) # ---------------------------------------------------- # INETS FLAGS # ---------------------------------------------------- -INETS_FLAGS = -D'SERVER_SOFTWARE="inets/$(VSN)"' +INETS_FLAGS = -D'SERVER_SOFTWARE="$(APPLICATION)/$(VSN)"' # ---------------------------------------------------- @@ -133,6 +133,7 @@ release_spec: opt release_docs_spec: info: + @echo "APPLICATION = $(APPLICATION)" @echo "INETS_DEBUG = $(INETS_DEBUG)" @echo "INETS_FLAGS = $(INETS_FLAGS)" @echo "ERL_COMPILE_FLAGS = $(ERL_COMPILE_FLAGS)" diff --git a/lib/inets/src/http_server/httpd.erl b/lib/inets/src/http_server/httpd.erl index 554f162fc5..8fe54ccef6 100644 --- a/lib/inets/src/http_server/httpd.erl +++ b/lib/inets/src/http_server/httpd.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1997-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 1997-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -358,7 +358,7 @@ foreach([KeyValue|Rest]) -> get_addr_and_port(ConfigFile) -> case httpd_conf:load(ConfigFile) of {ok, ConfigList} -> - case httpd_conf:validate_properties(ConfigList) of + case (catch httpd_conf:validate_properties(ConfigList)) of {ok, Config} -> Address = proplists:get_value(bind_address, Config, any), Port = proplists:get_value(port, Config, 80), @@ -506,7 +506,7 @@ get_status(Addr,Port,Timeout) when is_integer(Port) -> end. do_reload_config(ConfigList, Mode) -> - case httpd_conf:validate_properties(ConfigList) of + case (catch httpd_conf:validate_properties(ConfigList)) of {ok, Config} -> Address = proplists:get_value(bind_address, Config, any), Port = proplists:get_value(port, Config, 80), diff --git a/lib/inets/src/http_server/httpd_conf.erl b/lib/inets/src/http_server/httpd_conf.erl index 9c93e2c5fe..3e498d1db7 100644 --- a/lib/inets/src/http_server/httpd_conf.erl +++ b/lib/inets/src/http_server/httpd_conf.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1997-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 1997-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -864,17 +864,22 @@ load_traverse(Line, [Context|Contexts], [Module|Modules], NewContexts, {'EXIT', {undef, _}} -> ?hdrt("does not implement load", []), load_traverse(Line, Contexts, Modules, - [Context|NewContexts], ConfigList,yes); + [Context|NewContexts], ConfigList, yes); {'EXIT', Reason} -> error_logger:error_report({'EXIT', Reason}), load_traverse(Line, Contexts, Modules, [Context|NewContexts], ConfigList, State); + ok -> + ?hdrt("line processed", []), + load_traverse(Line, Contexts, Modules, + [Context|NewContexts], ConfigList, yes); + {ok, NewContext} -> ?hdrt("line processed", [{new_context, NewContext}]), load_traverse(Line, Contexts, Modules, - [NewContext|NewContexts], ConfigList,yes); + [NewContext|NewContexts], ConfigList, yes); {ok, NewContext, ConfigEntry} when is_tuple(ConfigEntry) -> ?hdrt("line processed", diff --git a/lib/inets/src/http_server/httpd_instance_sup.erl b/lib/inets/src/http_server/httpd_instance_sup.erl index 3b5464132c..0aaeb838c2 100644 --- a/lib/inets/src/http_server/httpd_instance_sup.erl +++ b/lib/inets/src/http_server/httpd_instance_sup.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2001-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2001-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -37,7 +37,7 @@ %%% Internal Application API %%%========================================================================= start_link([{_, _}| _] = Config, AcceptTimeout, Debug) -> - case httpd_conf:validate_properties(Config) of + case (catch httpd_conf:validate_properties(Config)) of {ok, Config2} -> Address = proplists:get_value(bind_address, Config2), Port = proplists:get_value(port, Config2), @@ -66,7 +66,7 @@ start_link(ConfigFile, AcceptTimeout, Debug) -> start_link([{_, _}| _] = Config, AcceptTimeout, ListenInfo, Debug) -> - case httpd_conf:validate_properties(Config) of + case (catch httpd_conf:validate_properties(Config)) of {ok, Config2} -> Address = proplists:get_value(bind_address, Config2), Port = proplists:get_value(port, Config2), @@ -154,7 +154,7 @@ make_name(Address,Port) -> file_2_config(ConfigFile) -> case httpd_conf:load(ConfigFile) of {ok, ConfigList} -> - case httpd_conf:validate_properties(ConfigList) of + case (catch httpd_conf:validate_properties(ConfigList)) of {ok, Config} -> Address = proplists:get_value(bind_address, ConfigList), Port = proplists:get_value(port, ConfigList), diff --git a/lib/inets/src/http_server/httpd_request.erl b/lib/inets/src/http_server/httpd_request.erl index ad2cc4bda3..8eee08e766 100644 --- a/lib/inets/src/http_server/httpd_request.erl +++ b/lib/inets/src/http_server/httpd_request.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2005-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2005-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% @@ -68,10 +68,11 @@ body_data(Headers, Body) -> {binary_to_list(BodyThisReq), Next} end. + %%------------------------------------------------------------------------- %% validate(Method, Uri, Version) -> ok | {error, {bad_request, Reason} | %% {error, {not_supported, {Method, Uri, Version}} -%% Method = "HEAD" | "GET" | "POST" | "TRACE" +%% Method = "HEAD" | "GET" | "POST" | "TRACE" | "PUT" | "DELETE" %% Uri = uri() %% Version = "HTTP/N.M" %% Description: Checks that HTTP-request-line is valid. @@ -84,6 +85,10 @@ validate("GET", Uri, "HTTP/0.9") -> validate_uri(Uri); validate("GET", Uri, "HTTP/1." ++ _N) -> validate_uri(Uri); +validate("PUT", Uri, "HTTP/1." ++ _N) -> + validate_uri(Uri); +validate("DELETE", Uri, "HTTP/1." ++ _N) -> + validate_uri(Uri); validate("POST", Uri, "HTTP/1." ++ _N) -> validate_uri(Uri); validate("TRACE", Uri, "HTTP/1." ++ N) when hd(N) >= $1 -> diff --git a/lib/inets/src/http_server/httpd_sup.erl b/lib/inets/src/http_server/httpd_sup.erl index fc41994727..3399f78b53 100644 --- a/lib/inets/src/http_server/httpd_sup.erl +++ b/lib/inets/src/http_server/httpd_sup.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2004-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2004-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -169,7 +169,7 @@ httpd_child_spec([Value| _] = Config, AcceptTimeout, Debug) httpd_child_spec(ConfigFile, AcceptTimeout, Debug) -> case httpd_conf:load(ConfigFile) of {ok, ConfigList} -> - case httpd_conf:validate_properties(ConfigList) of + case (catch httpd_conf:validate_properties(ConfigList)) of {ok, Config} -> Address = proplists:get_value(bind_address, Config, any), Port = proplists:get_value(port, Config, 80), diff --git a/lib/inets/src/http_server/mod_alias.erl b/lib/inets/src/http_server/mod_alias.erl index 7073f5405d..ec0a12242f 100644 --- a/lib/inets/src/http_server/mod_alias.erl +++ b/lib/inets/src/http_server/mod_alias.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1997-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 1997-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -28,44 +28,51 @@ path/3]). -include("httpd.hrl"). +-include("httpd_internal.hrl"). -define(VMODULE,"ALIAS"). %% do -do(Info) -> - case proplists:get_value(status, Info#mod.data) of +do(#mod{data = Data} = Info) -> + ?hdrt("do", []), + case proplists:get_value(status, Data) of %% A status code has been generated! {_StatusCode, _PhraseArgs, _Reason} -> - {proceed,Info#mod.data}; + {proceed, Data}; %% No status code has been generated! undefined -> - case proplists:get_value(response, Info#mod.data) of + case proplists:get_value(response, Data) of %% No response has been generated! undefined -> do_alias(Info); %% A response has been generated or sent! _Response -> - {proceed, Info#mod.data} + {proceed, Data} end end. -do_alias(Info) -> - {ShortPath, Path, AfterPath} = - real_name(Info#mod.config_db, - Info#mod.request_uri, - httpd_util:multi_lookup(Info#mod.config_db,alias)), +do_alias(#mod{config_db = ConfigDB, + request_uri = ReqURI, + data = Data}) -> + {ShortPath, Path, AfterPath} = + real_name(ConfigDB, ReqURI, which_alias(ConfigDB)), + ?hdrt("real name", + [{request_uri, ReqURI}, + {short_path, ShortPath}, + {path, Path}, + {after_path, AfterPath}]), %% Relocate if a trailing slash is missing else proceed! LastChar = lists:last(ShortPath), case file:read_file_info(ShortPath) of - {ok, FileInfo} when FileInfo#file_info.type == directory, - LastChar /= $/ -> - ServerName = httpd_util:lookup(Info#mod.config_db, server_name), - Port = port_string(httpd_util:lookup(Info#mod.config_db,port, 80)), - URL = "http://" ++ ServerName ++ Port ++ - Info#mod.request_uri ++ "/", + {ok, FileInfo} when ((FileInfo#file_info.type =:= directory) andalso + (LastChar =/= $/)) -> + ?hdrt("directory and last-char is a /", []), + ServerName = which_server_name(ConfigDB), + Port = port_string( which_port(ConfigDB) ), + URL = "http://" ++ ServerName ++ Port ++ ReqURI ++ "/", ReasonPhrase = httpd_util:reason_phrase(301), - Message = httpd_util:message(301, URL, Info#mod.config_db), + Message = httpd_util:message(301, URL, ConfigDB), {proceed, [{response, {301, ["Location: ", URL, "\r\n" @@ -76,25 +83,26 @@ do_alias(Info) -> "<BODY>\n<H1>",ReasonPhrase, "</H1>\n", Message, "\n</BODY>\n</HTML>\n"]}}| - [{real_name, {Path, AfterPath}} | Info#mod.data]]}; + [{real_name, {Path, AfterPath}} | Data]]}; _NoFile -> - {proceed,[{real_name, {Path, AfterPath}} | Info#mod.data]} + {proceed, [{real_name, {Path, AfterPath}} | Data]} end. port_string(80) -> ""; port_string(Port) -> - ":"++integer_to_list(Port). + ":" ++ integer_to_list(Port). %% real_name real_name(ConfigDB, RequestURI, []) -> - DocumentRoot = httpd_util:lookup(ConfigDB, document_root, ""), + DocumentRoot = which_document_root(ConfigDB), RealName = DocumentRoot ++ RequestURI, {ShortPath, _AfterPath} = httpd_util:split_path(RealName), - {Path, AfterPath} = httpd_util:split_path(default_index(ConfigDB, - RealName)), + {Path, AfterPath} = + httpd_util:split_path(default_index(ConfigDB, RealName)), {ShortPath, Path, AfterPath}; + real_name(ConfigDB, RequestURI, [{FakeName,RealName}|Rest]) -> case inets_regexp:match(RequestURI, "^" ++ FakeName) of {match, _, _} -> @@ -105,7 +113,7 @@ real_name(ConfigDB, RequestURI, [{FakeName,RealName}|Rest]) -> httpd_util:split_path(default_index(ConfigDB, ActualName)), {ShortPath, Path, AfterPath}; nomatch -> - real_name(ConfigDB,RequestURI,Rest) + real_name(ConfigDB, RequestURI, Rest) end. %% real_script_name @@ -113,20 +121,21 @@ real_name(ConfigDB, RequestURI, [{FakeName,RealName}|Rest]) -> real_script_name(_ConfigDB, _RequestURI, []) -> not_a_script; real_script_name(ConfigDB, RequestURI, [{FakeName,RealName} | Rest]) -> - case inets_regexp:match(RequestURI,"^"++FakeName) of + case inets_regexp:match(RequestURI, "^" ++ FakeName) of {match,_,_} -> - {ok,ActualName,_}=inets_regexp:sub(RequestURI,"^"++FakeName,RealName), - httpd_util:split_script_path(default_index(ConfigDB,ActualName)); + {ok, ActualName, _} = + inets_regexp:sub(RequestURI, "^" ++ FakeName, RealName), + httpd_util:split_script_path(default_index(ConfigDB, ActualName)); nomatch -> - real_script_name(ConfigDB,RequestURI,Rest) + real_script_name(ConfigDB, RequestURI, Rest) end. %% default_index default_index(ConfigDB, Path) -> case file:read_file_info(Path) of - {ok, FileInfo} when FileInfo#file_info.type == directory -> - DirectoryIndex = httpd_util:lookup(ConfigDB, directory_index, []), + {ok, FileInfo} when FileInfo#file_info.type =:= directory -> + DirectoryIndex = which_directory_index(ConfigDB), append_index(Path, DirectoryIndex); _ -> Path @@ -147,9 +156,9 @@ append_index(RealName, [Index | Rest]) -> path(Data, ConfigDB, RequestURI) -> case proplists:get_value(real_name, Data) of undefined -> - DocumentRoot = httpd_util:lookup(ConfigDB, document_root, ""), + DocumentRoot = which_document_root(ConfigDB), {Path, _AfterPath} = - httpd_util:split_path(DocumentRoot++RequestURI), + httpd_util:split_path(DocumentRoot ++ RequestURI), Path; {Path, _AfterPath} -> Path @@ -164,7 +173,7 @@ path(Data, ConfigDB, RequestURI) -> load("DirectoryIndex " ++ DirectoryIndex, []) -> {ok, DirectoryIndexes} = inets_regexp:split(DirectoryIndex," "), {ok,[], {directory_index, DirectoryIndexes}}; -load("Alias " ++ Alias,[]) -> +load("Alias " ++ Alias, []) -> case inets_regexp:split(Alias," ") of {ok, [FakeName, RealName]} -> {ok,[],{alias,{FakeName,RealName}}}; @@ -191,13 +200,13 @@ store({directory_index, Value} = Conf, _) when is_list(Value) -> end; store({directory_index, Value}, _) -> {error, {wrong_type, {directory_index, Value}}}; -store({alias, {Fake, Real}} = Conf, _) when is_list(Fake), - is_list(Real) -> +store({alias, {Fake, Real}} = Conf, _) + when is_list(Fake) andalso is_list(Real) -> {ok, Conf}; store({alias, Value}, _) -> {error, {wrong_type, {alias, Value}}}; -store({script_alias, {Fake, Real}} = Conf, _) when is_list(Fake), - is_list(Real) -> +store({script_alias, {Fake, Real}} = Conf, _) + when is_list(Fake) andalso is_list(Real) -> {ok, Conf}; store({script_alias, Value}, _) -> {error, {wrong_type, {script_alias, Value}}}. @@ -208,3 +217,21 @@ is_directory_index_list([Head | Tail]) when is_list(Head) -> is_directory_index_list(Tail); is_directory_index_list(_) -> false. + + +%% --------------------------------------------------------------------- + +which_alias(ConfigDB) -> + httpd_util:multi_lookup(ConfigDB, alias). + +which_server_name(ConfigDB) -> + httpd_util:lookup(ConfigDB, server_name). + +which_port(ConfigDB) -> + httpd_util:lookup(ConfigDB, port, 80). + +which_document_root(ConfigDB) -> + httpd_util:lookup(ConfigDB, document_root, ""). + +which_directory_index(ConfigDB) -> + httpd_util:lookup(ConfigDB, directory_index, []). diff --git a/lib/inets/src/http_server/mod_cgi.erl b/lib/inets/src/http_server/mod_cgi.erl index ab12a3b57b..33605b9698 100644 --- a/lib/inets/src/http_server/mod_cgi.erl +++ b/lib/inets/src/http_server/mod_cgi.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1997-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 1997-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -335,6 +335,8 @@ script_elements(#mod{method = "GET"}, {PathInfo, QueryString}) -> [{query_string, QueryString}, {path_info, PathInfo}]; script_elements(#mod{method = "POST", entity_body = Body}, _) -> [{entity_body, Body}]; +script_elements(#mod{method = "PUT", entity_body = Body}, _) -> + [{entity_body, Body}]; script_elements(_, _) -> []. diff --git a/lib/inets/src/http_server/mod_esi.erl b/lib/inets/src/http_server/mod_esi.erl index dd6f62ae2d..484d4b3fb4 100644 --- a/lib/inets/src/http_server/mod_esi.erl +++ b/lib/inets/src/http_server/mod_esi.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1997-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 1997-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -249,7 +249,24 @@ erl(#mod{method = Method} = ModData, ESIBody, Modules) {proceed, [{status,{400, none, BadRequest}} | ModData#mod.data]} end; -erl(#mod{method = "POST", entity_body = Body} = ModData, ESIBody, Modules) -> +erl(#mod{request_uri = ReqUri, + method = "PUT", + http_version = Version, + data = Data}, _ESIBody, _Modules) -> + {proceed, [{status,{501,{"PUT", ReqUri, Version}, + ?NICE("Erl mechanism doesn't support method PUT")}}| + Data]}; + +erl(#mod{request_uri = ReqUri, + method = "DELETE", + http_version = Version, + data = Data}, _ESIBody, _Modules) -> + {proceed,[{status,{501,{"DELETE", ReqUri, Version}, + ?NICE("Erl mechanism doesn't support method DELETE")}}| + Data]}; + +erl(#mod{method = "POST", + entity_body = Body} = ModData, ESIBody, Modules) -> case httpd_util:split(ESIBody,":|%3A|/",2) of {ok,[ModuleName, Function]} -> generate_webpage(ModData, ESIBody, Modules, @@ -444,8 +461,26 @@ input_type([_First|Rest]) -> %%------------------------ Eval mechanism -------------------------------- -eval(#mod{request_uri = ReqUri, method = "POST", - http_version = Version, data = Data}, _ESIBody, _Modules) -> +eval(#mod{request_uri = ReqUri, + method = "PUT", + http_version = Version, + data = Data}, _ESIBody, _Modules) -> + {proceed,[{status,{501,{"PUT", ReqUri, Version}, + ?NICE("Eval mechanism doesn't support method PUT")}}| + Data]}; + +eval(#mod{request_uri = ReqUri, + method = "DELETE", + http_version = Version, + data = Data}, _ESIBody, _Modules) -> + {proceed,[{status,{501,{"DELETE", ReqUri, Version}, + ?NICE("Eval mechanism doesn't support method DELETE")}}| + Data]}; + +eval(#mod{request_uri = ReqUri, + method = "POST", + http_version = Version, + data = Data}, _ESIBody, _Modules) -> {proceed,[{status,{501,{"POST", ReqUri, Version}, ?NICE("Eval mechanism doesn't support method POST")}}| Data]}; diff --git a/lib/inets/src/inets_app/Makefile b/lib/inets/src/inets_app/Makefile index 2dab99386a..33c9e34a3a 100644 --- a/lib/inets/src/inets_app/Makefile +++ b/lib/inets/src/inets_app/Makefile @@ -1,19 +1,19 @@ # # %CopyrightBegin% -# -# Copyright Ericsson AB 2005-2009. All Rights Reserved. -# +# +# Copyright Ericsson AB 2005-2010. All Rights Reserved. +# # The contents of this file are subject to the Erlang Public License, # Version 1.1, (the "License"); you may not use this file except in # compliance with the License. You should have received a copy of the # Erlang Public License along with this software. If not, it can be # retrieved online at http://www.erlang.org/. -# +# # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. -# +# # %CopyrightEnd% # # @@ -22,6 +22,7 @@ include $(ERL_TOP)/make/target.mk EBIN = ../../ebin include $(ERL_TOP)/make/$(TARGET)/otp.mk + # ---------------------------------------------------- # Application version # ---------------------------------------------------- @@ -32,12 +33,13 @@ VSN = $(INETS_VSN) # ---------------------------------------------------- # Release directory specification # ---------------------------------------------------- -RELSYSDIR = $(RELEASE_PATH)/lib/inets-$(VSN) +RELSYSDIR = $(RELEASE_PATH)/lib/$(APPLICATION)-$(VSN) # ---------------------------------------------------- # Target Specs # ---------------------------------------------------- + MODULES = \ inets_service \ inets \ @@ -49,7 +51,8 @@ HRL_FILES = inets_internal.hrl ERL_FILES = $(MODULES:%=%.erl) -TARGET_FILES= $(MODULES:%=$(EBIN)/%.$(EMULATOR)) \ +TARGET_FILES= \ + $(MODULES:%=$(EBIN)/%.$(EMULATOR)) \ $(APP_TARGET) \ $(APPUP_TARGET) @@ -66,7 +69,7 @@ APPUP_TARGET = $(EBIN)/$(APPUP_FILE) # ---------------------------------------------------- # INETS FLAGS # ---------------------------------------------------- -INETS_FLAGS = -D'SERVER_SOFTWARE="inets/$(VSN)"' \ +INETS_FLAGS = -D'SERVER_SOFTWARE="$(APPLICATION)/$(VSN)"' # ---------------------------------------------------- @@ -108,14 +111,15 @@ $(APPUP_TARGET): $(APPUP_SRC) ../../vsn.mk include $(ERL_TOP)/make/otp_release_targets.mk release_spec: opt - $(INSTALL_DIR) $(RELSYSDIR)/src + $(INSTALL_DIR) $(RELSYSDIR)/src $(INSTALL_DATA) $(HRL_FILES) $(ERL_FILES) $(RELSYSDIR)/src - $(INSTALL_DIR) $(RELSYSDIR)/ebin + $(INSTALL_DIR) $(RELSYSDIR)/ebin $(INSTALL_DATA) $(TARGET_FILES) $(RELSYSDIR)/ebin release_docs_spec: info: + @echo "APPLICATION = $(APPLICATION)" @echo "INETS_DEBUG = $(INETS_DEBUG)" @echo "INETS_FLAGS = $(INETS_FLAGS)" @echo "ERL_COMPILE_FLAGS = $(ERL_COMPILE_FLAGS)" diff --git a/lib/inets/src/inets_app/inets.app.src b/lib/inets/src/inets_app/inets.app.src index 6524c3b19b..04f6365b98 100644 --- a/lib/inets/src/inets_app/inets.app.src +++ b/lib/inets/src/inets_app/inets.app.src @@ -1,19 +1,19 @@ %% This is an -*- erlang -*- file. %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1997-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 1997-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% @@ -34,7 +34,8 @@ ftp_sup, %% HTTP client: - http, + http, %% Old client API module + httpc, %% New client API module httpc_handler, httpc_handler_sup, httpc_manager, @@ -42,7 +43,7 @@ httpc_request, httpc_response, httpc_sup, - http_cookie, + httpc_cookie, http_uri, %% Proably will by used by server also in the future diff --git a/lib/inets/src/inets_app/inets.appup.src b/lib/inets/src/inets_app/inets.appup.src index 0112a64239..2efa7ccb60 100644 --- a/lib/inets/src/inets_app/inets.appup.src +++ b/lib/inets/src/inets_app/inets.appup.src @@ -1,34 +1,31 @@ %% This is an -*- erlang -*- file. %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1999-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 1999-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% {"%VSN%", [ {"5.2", [ - {load_module, inets, soft_purge, soft_purge, []} + {restart_application, inets} ] }, {"5.1.3", [ - {load_module, httpd_response, soft_purge, soft_purge, []}, - {update, ftp, {advanced, upgrade_from_pre_5_12}, - soft_purge, soft_purge, []}, - {update, httpc_handler, soft, soft_purge, soft_purge, []} + {restart_application, inets} ] }, {"5.1.2", @@ -40,15 +37,12 @@ [ {"5.2", [ - {load_module, inets, soft_purge, soft_purge, []} + {restart_application, inets} ] }, {"5.1.3", [ - {load_module, httpd_response, soft_purge, soft_purge, []}, - {update, ftp, {advanced, downgrade_to_pre_5_12}, - soft_purge, soft_purge, []}, - {update, httpc_handler, soft, soft_purge, soft_purge, []} + {restart_application, inets} ] }, {"5.1.2", diff --git a/lib/inets/src/inets_app/inets.erl b/lib/inets/src/inets_app/inets.erl index 77cb14cc20..7e3f862ee7 100644 --- a/lib/inets/src/inets_app/inets.erl +++ b/lib/inets/src/inets_app/inets.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2006-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2006-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -522,9 +522,7 @@ change_pattern({Mod, Service, Pattern}) catch exit:{Where, Reason} -> {error, {Where, Reason}} - end; - _ -> - exit({bad_pattern, Pattern}) + end end, ok. @@ -728,7 +726,7 @@ call_service(Service, Call, Args) -> service_module(tftpd) -> tftp; service_module(httpc) -> - http; + httpc; service_module(ftpc) -> ftp; service_module(Service) -> diff --git a/lib/inets/src/tftp/Makefile b/lib/inets/src/tftp/Makefile index 63f70f7943..b4339da1e2 100644 --- a/lib/inets/src/tftp/Makefile +++ b/lib/inets/src/tftp/Makefile @@ -1,19 +1,19 @@ # # %CopyrightBegin% -# -# Copyright Ericsson AB 2005-2009. All Rights Reserved. -# +# +# Copyright Ericsson AB 2005-2010. All Rights Reserved. +# # The contents of this file are subject to the Erlang Public License, # Version 1.1, (the "License"); you may not use this file except in # compliance with the License. You should have received a copy of the # Erlang Public License along with this software. If not, it can be # retrieved online at http://www.erlang.org/. -# +# # Software distributed under the License is distributed on an "AS IS" # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See # the License for the specific language governing rights and limitations # under the License. -# +# # %CopyrightEnd% # # @@ -33,7 +33,8 @@ VSN = $(INETS_VSN) # ---------------------------------------------------- # Release directory specification # ---------------------------------------------------- -RELSYSDIR = $(RELEASE_PATH)/lib/inets-$(VSN) +RELSYSDIR = $(RELEASE_PATH)/lib/$(APPLICATION)-$(VSN) + # ---------------------------------------------------- # Target Specs @@ -53,10 +54,12 @@ ERL_FILES = $(MODULES:%=%.erl) TARGET_FILES= $(MODULES:%=$(EBIN)/%.$(EMULATOR)) + # ---------------------------------------------------- # INETS FLAGS # ---------------------------------------------------- -INETS_FLAGS = -D'SERVER_SOFTWARE="inets/$(VSN)"' \ +INETS_FLAGS = -D'SERVER_SOFTWARE="$(APPLICATION)/$(VSN)"' + # ---------------------------------------------------- # FLAGS @@ -64,6 +67,8 @@ INETS_FLAGS = -D'SERVER_SOFTWARE="inets/$(VSN)"' \ ERL_COMPILE_FLAGS += $(INETS_FLAGS) \ +'{parse_transform,sys_pre_attributes}' \ +'{attribute,insert,app_vsn,$(APP_VSN)}' + + # ---------------------------------------------------- # Targets # ---------------------------------------------------- @@ -90,6 +95,7 @@ release_spec: opt release_docs_spec: info: + @echo "APPLICATION = $(APPLICATION)" @echo "INETS_DEBUG = $(INETS_DEBUG)" @echo "INETS_FLAGS = $(INETS_FLAGS)" @echo "ERL_COMPILE_FLAGS = $(ERL_COMPILE_FLAGS)" |