z_sites_dispatcher (zotonic_core v1.0.0-rc.17)

Server for matching the request path to correct site and dispatch rule.

Summary

Functions

Convert process state when code is changed

Collect all dispatch rules for all running sites, normalize and filter them.

Collect all dispatch rules for all sites, normalize and filter them.

Match the host and path to a dispatch rule.

Dispatch a path for a host, return the extracted dispatch information and bindings. Used for matching URLs to dispatch rules and ids.

Dispatch an URL, return the extracted dispatch information and bindings. Used for matching URLs to dispatch rules and ids.

Cowboy middleware, route the new request. Continue with the cowmachine, requests a redirect or return a 400 on an unknown host. The cowmachine_proxy middleware must have been called before this.

Fetch dispatch rules for a specific site.

Retrieve the fallback site.

Fetch the site handling the given hostname (with optional port)

Fetch the site handling the given URL. Scheme is not checked.

Load all dispatch rules, if anything changed then recompile the dispatcher(s)

Handling all non call/cast messages

Initiates the server.

Callback for the dispatch compiler, try to bind a language

Starts the server

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

Collect dispatch information from all sites and recompiles the dispatch rules.

Update the host/site mappings

Types

binding/0

-type binding() :: dispatch_compiler:binding() | {atom(), atom()}.

bindings/0

-type bindings() :: [binding()].

dispatch/0

-type dispatch() ::
          #dispatch_controller{dispatch_rule :: atom(),
                               controller :: atom(),
                               controller_options :: list(),
                               path_tokens :: [binary()],
                               bindings :: bindings(),
                               context :: z:context()} |
          #dispatch_nomatch{site :: atom(),
                            host :: binary(),
                            path_tokens :: [binary()],
                            bindings :: bindings(),
                            context ::
                                #context{cowreq :: cowboy_req:req() | undefined,
                                         cowenv :: cowboy_middleware:env() | undefined,
                                         site :: atom(),
                                         controller_module :: atom() | undefined,
                                         client_id :: binary() | undefined,
                                         client_topic :: mqtt_sessions:topic() | undefined,
                                         routing_id :: binary() | undefined,
                                         acl :: term() | admin | undefined,
                                         acl_is_read_only :: boolean(),
                                         user_id :: integer() | authenticated | undefined,
                                         render_state :: undefined | z_render:render_state(),
                                         db :: {atom(), atom()} | undefined,
                                         dbc :: pid() | undefined,
                                         language :: [atom()],
                                         tz :: binary(),
                                         props :: map(),
                                         depcache :: pid() | atom(),
                                         dispatcher :: pid() | atom(),
                                         template_server :: pid() | atom(),
                                         scomp_server :: pid() | atom(),
                                         dropbox_server :: pid() | atom(),
                                         pivot_server :: pid() | atom(),
                                         module_indexer :: pid() | atom(),
                                         translation_table :: atom()} |
                                undefined} |
          redirect() |
          redirect_protocol() |
          stop_request().

dispatch_rsc_rule/0

-type dispatch_rsc_rule() ::
          {Name :: atom(), RscName :: atom(), Handler :: any(), HandlerArgs :: list()}.

dispatch_rule/0

-type dispatch_rule() :: dispatch_compiler:dispatch_rule() | dispatch_rsc_rule().

hostname/0

-type hostname() :: binary() | string().

redirect/0

-type redirect() ::
          #redirect{site :: atom(), location :: binary() | undefined, is_permanent :: boolean()}.

redirect_protocol/0

-type redirect_protocol() ::
          #redirect_protocol{site :: atom(),
                             protocol :: http | https,
                             host :: binary(),
                             is_permanent :: boolean()}.

site_dispatch_list/0

-type site_dispatch_list() ::
          #site_dispatch_list{site :: atom(),
                              hostname :: z_sites_dispatcher:hostname(),
                              smtphost :: z_sites_dispatcher:hostname() | undefined,
                              hostalias :: [z_sites_dispatcher:hostname()],
                              redirect :: boolean(),
                              dispatch_list :: [z_sites_dispatcher:dispatch_rule()],
                              page_paths ::
                                  #{atom() | binary() => z_sites_dispatcher:dispatch_rsc_rule()}}.

stop_request/0

-type stop_request() :: #stop_request{status :: pos_integer()}.

trace/0

-type trace() ::
          #{path := binary() | [binary()] | undefined,
            step := trace_step(),
            args := proplists:proplist()}.

trace_step/0

-type trace_step() ::
          undefined | match | try_match | dispatch_rewrite | forced_protocol_switch | notify_dispatch |
          rewrite_id | rewrite_match | rewrite_nomatch.

Functions

code_change(OldVsn, State, Extra)

-spec code_change(term(), term(), term()) -> {ok, term()}.

Convert process state when code is changed

collect_dispatchrules()

Collect all dispatch rules for all running sites, normalize and filter them.

collect_dispatchrules(Site)

Collect all dispatch rules for all sites, normalize and filter them.

dispatch(Req, Env)

-spec dispatch(cowboy_req:req(), cowboy_middleware:env()) -> dispatch().

Match the host and path to a dispatch rule.

dispatch(Method, Host, Path, IsSsl, OptTracerPid)

-spec dispatch(binary() | string(),
               binary() | string(),
               binary() | string(),
               boolean(),
               pid() | undefined) ->
                  dispatch().

dispatch_path(Path, Context)

-spec dispatch_path(binary() | string(), z:context()) ->
                       {ok, map()} | {error, non_neg_integer() | invalid}.

Dispatch a path for a host, return the extracted dispatch information and bindings. Used for matching URLs to dispatch rules and ids.

dispatch_trace(Path, Context)

-spec dispatch_trace(binary(), z:context()) -> {ok, [trace()]} | {error, timeout}.

dispatch_trace(Protocol, Path, Context)

-spec dispatch_trace(http | https, binary(), z:context()) -> {ok, [trace()]} | {error, timeout}.

dispatch_url(Url)

-spec dispatch_url(binary() | string()) -> {ok, map()} | {error, non_neg_integer() | invalid}.

Dispatch an URL, return the extracted dispatch information and bindings. Used for matching URLs to dispatch rules and ids.

execute(Req, Env)

-spec execute(Req, Env) -> Result
                 when
                     Req :: cowboy_req:req(),
                     Env :: cowboy_middleware:env(),
                     Result :: {ok, Req1, Env1} | {stop, Req1},
                     Req1 :: cowboy_req:req(),
                     Env1 :: cowboy_middleware:env().

Cowboy middleware, route the new request. Continue with the cowmachine, requests a redirect or return a 400 on an unknown host. The cowmachine_proxy middleware must have been called before this.

fetch_dispatchinfo(SiteOrContext)

Fetch dispatch rules for a specific site.

get_fallback_site()

-spec get_fallback_site() -> {ok, atom()} | undefined.

Retrieve the fallback site.

get_site_for_hostname(Hostname)

-spec get_site_for_hostname(string() | binary() | '*') -> {ok, atom()} | undefined.

Fetch the site handling the given hostname (with optional port)

get_site_for_url(Url)

-spec get_site_for_url(binary() | string()) -> {ok, atom()} | undefined.

Fetch the site handling the given URL. Scheme is not checked.

handle_call(Message, From, State)

Trap unknown calls

handle_cast(Message, State)

-spec handle_cast(term(), term()) ->
                     {noreply, term()} |
                     {noreply, term(), timeout() | hibernate} |
                     {stop, term(), term()}.

Load all dispatch rules, if anything changed then recompile the dispatcher(s)

handle_info(Info, State)

Handling all non call/cast messages

init(Args)

-spec init(term()) -> {ok, term()} | {ok, term(), timeout() | hibernate} | ignore | {stop, term()}.

Initiates the server.

is_bind_language(Match, Context)

Callback for the dispatch compiler, try to bind a language

start_link()

-spec start_link() -> {ok, pid()} | ignore | {error, term()}.

Starts the server

start_link(Args)

-spec start_link(list()) -> {ok, pid()} | ignore | {error, term()}.

terminate(Reason, State)

-spec terminate(term(), term()) -> ok.

This function is called by a gen_server when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, the gen_server terminates with Reason. The return value is ignored.

update_dispatchinfo()

-spec update_dispatchinfo() -> ok.

Collect dispatch information from all sites and recompiles the dispatch rules.

update_hosts()

-spec update_hosts() -> ok.

Update the host/site mappings