defmodule Phoenix.Endpoint do @moduledoc ~S""" Defines a Phoenix endpoint. The endpoint is the boundary where all requests to your web application start. It is also the interface your application provides to the underlying web servers. Overall, an endpoint has three responsibilities: * to provide a wrapper for starting and stopping the endpoint as part of a supervision tree * to define an initial plug pipeline for requests to pass through * to host web specific configuration for your application ## Endpoints An endpoint is simply a module defined with the help of `Phoenix.Endpoint`. If you have used the `mix phx.new` generator, an endpoint was automatically generated as part of your application: defmodule YourAppWeb.Endpoint do use Phoenix.Endpoint, otp_app: :your_app # plug ... # plug ... plug YourApp.Router end Endpoints must be explicitly started as part of your application supervision tree. Endpoints are added by default to the supervision tree in generated applications. Endpoints can be added to the supervision tree as follows: children = [ YourAppWeb.Endpoint ] ## Endpoint configuration All endpoints are configured in your application environment. For example: config :your_app, YourAppWeb.Endpoint, secret_key_base: "kjoy3o1zeidquwy1398juxzldjlksahdk3" Endpoint configuration is split into two categories. Compile-time configuration means the configuration is read during compilation and changing it at runtime has no effect. The compile-time configuration is mostly related to error handling. Runtime configuration, instead, is accessed during or after your application is started and can be read through the `c:config/2` function: YourAppWeb.Endpoint.config(:port) YourAppWeb.Endpoint.config(:some_config, :default_value) ### Dynamic configuration For dynamically configuring the endpoint, such as loading data from environment variables or configuration files, Phoenix invokes the `c:init/2` callback on the endpoint, passing the atom `:supervisor` as the first argument and the endpoint configuration as second. All of Phoenix configuration, except the Compile-time configuration below can be set dynamically from the `c:init/2` callback. ### Compile-time configuration * `:code_reloader` - when `true`, enables code reloading functionality. For the list of code reloader configuration options see `Phoenix.CodeReloader.reload/1`. Keep in mind code reloading is based on the file-system, therefore it is not possible to run two instances of the same app at the same time with code reloading in development, as they will race each other and only one will effectively recompile the files. In such cases, tweak your config files so code reloading is enabled in only one of the apps or set the MIX_BUILD environment variable to give them distinct build directories * `:debug_errors` - when `true`, uses `Plug.Debugger` functionality for debugging failures in the application. Recommended to be set to `true` only in development as it allows listing of the application source code during debugging. Defaults to `false` * `:force_ssl` - ensures no data is ever sent via HTTP, always redirecting to HTTPS. It expects a list of options which are forwarded to `Plug.SSL`. By default it sets the "strict-transport-security" header in HTTPS requests, forcing browsers to always use HTTPS. If an unsafe request (HTTP) is sent, it redirects to the HTTPS version using the `:host` specified in the `:url` configuration. To dynamically redirect to the `host` of the current request, set `:host` in the `:force_ssl` configuration to `nil` ### Runtime configuration * `:adapter` - which webserver adapter to use for serving web requests. See the "Adapter configuration" section below * `:cache_static_manifest` - a path to a json manifest file that contains static files and their digested version. This is typically set to "priv/static/cache_manifest.json" which is the file automatically generated by `mix phx.digest`. It can be either: a string containing a file system path or a tuple containing the application name and the path within that application. * `:cache_static_manifest_latest` - a map of the static files pointing to their digest version. This is automatically loaded from `cache_static_manifest` on boot. However, if you have your own static handling mechanism, you may want to set this value explicitly. This is used by projects such as `LiveView` to detect if the client is running on the latest version of all assets. * `:cache_manifest_skip_vsn` - when true, skips the appended query string "?vsn=d" when generatic paths to static assets. This query string is used by `Plug.Static` to set long expiry dates, therefore, you should set this option to true only if you are not using `Plug.Static` to serve assets, for example, if you are using a CDN. If you are setting this option, you should also consider passing `--no-vsn` to `mix phx.digest`. Defaults to `false`. * `:check_origin` - configure the default `:check_origin` setting for transports. See `socket/3` for options. Defaults to `true`. * `:secret_key_base` - a secret key used as a base to generate secrets for encrypting and signing data. For example, cookies and tokens are signed by default, but they may also be encrypted if desired. Defaults to `nil` as it must be set per application * `:server` - when `true`, starts the web server when the endpoint supervision tree starts. Defaults to `false`. The `mix phx.server` task automatically sets this to `true` * `:url` - configuration for generating URLs throughout the app. Accepts the `:host`, `:scheme`, `:path` and `:port` options. All keys except `:path` can be changed at runtime. Defaults to: [host: "localhost", path: "/"] The `:port` option requires either an integer or string. The `:host` option requires a string. The `:scheme` option accepts `"http"` and `"https"` values. Default value is inferred from top level `:http` or `:https` option. It is useful when hosting Phoenix behind a load balancer or reverse proxy and terminating SSL there. The `:path` option can be used to override root path. Useful when hosting Phoenix behind a reverse proxy with URL rewrite rules * `:static_url` - configuration for generating URLs for static files. It will fallback to `url` if no option is provided. Accepts the same options as `url` * `:watchers` - a set of watchers to run alongside your server. It expects a list of tuples containing the executable and its arguments. Watchers are guaranteed to run in the application directory, but only when the server is enabled (unless `:force_watchers` configuration is set to `true`). For example, the watcher below will run the "watch" mode of the webpack build tool when the server starts. You can configure it to whatever build tool or command you want: [ node: [ "node_modules/webpack/bin/webpack.js", "--mode", "development", "--watch", "--watch-options-stdin" ] ] The `:cd` and `:env` options can be given at the end of the list to customize the watcher: [node: [..., cd: "assets", env: [{"TAILWIND_MODE", "watch"}]]] A watcher can also be a module-function-args tuple that will be invoked accordingly: [another: {Mod, :fun, [arg1, arg2]}] * `:force_watchers` - when `true`, forces your watchers to start even when the `:server` option is set to `false`. * `:live_reload` - configuration for the live reload option. Configuration requires a `:patterns` option which should be a list of file patterns to watch. When these files change, it will trigger a reload. If you are using a tool like [pow](http://pow.cx) in development, you may need to set the `:url` option appropriately. live_reload: [ url: "ws://localhost:4000", patterns: [ ~r{priv/static/.*(js|css|png|jpeg|jpg|gif)$}, ~r{web/views/.*(ex)$}, ~r{web/templates/.*(eex)$} ] ] * `:pubsub_server` - the name of the pubsub server to use in channels and via the Endpoint broadcast functions. The PubSub server is typically started in your supervision tree. * `:render_errors` - responsible for rendering templates whenever there is a failure in the application. For example, if the application crashes with a 500 error during a HTML request, `render("500.html", assigns)` will be called in the view given to `:render_errors`. Defaults to: [view: MyApp.ErrorView, accepts: ~w(html), layout: false, log: :debug] The default format is used when none is set in the connection ### Adapter configuration Phoenix allows you to choose which webserver adapter to use. The default is `Phoenix.Endpoint.Cowboy2Adapter` which can be configured via the following top-level options. * `:http` - the configuration for the HTTP server. It accepts all options as defined by [`Plug.Cowboy`](https://hexdocs.pm/plug_cowboy/). Defaults to `false` * `:https` - the configuration for the HTTPS server. It accepts all options as defined by [`Plug.Cowboy`](https://hexdocs.pm/plug_cowboy/). Defaults to `false` * `:drainer` - a drainer process that triggers when your application is shutting down to wait for any on-going request to finish. It accepts all options as defined by [`Plug.Cowboy.Drainer`](https://hexdocs.pm/plug_cowboy/Plug.Cowboy.Drainer.html). Defaults to `[]`, which will start a drainer process for each configured endpoint, but can be disabled by setting it to `false`. ## Endpoint API In the previous section, we have used the `c:config/2` function that is automatically generated in your endpoint. Here's a list of all the functions that are automatically defined in your endpoint: * for handling paths and URLs: `c:struct_url/0`, `c:url/0`, `c:path/1`, `c:static_url/0`,`c:static_path/1`, and `c:static_integrity/1` * for broadcasting to channels: `c:broadcast/3`, `c:broadcast!/3`, `c:broadcast_from/4`, `c:broadcast_from!/4`, `c:local_broadcast/3`, and `c:local_broadcast_from/4` * for configuration: `c:start_link/1`, `c:config/2`, and `c:config_change/2` * as required by the `Plug` behaviour: `c:Plug.init/1` and `c:Plug.call/2` """ @type topic :: String.t @type event :: String.t @type msg :: map | {:binary, binary} require Logger # Configuration @doc """ Starts the endpoint supervision tree. Starts endpoint's configuration cache and possibly the servers for handling requests. """ @callback start_link(keyword) :: Supervisor.on_start @doc """ Access the endpoint configuration given by key. """ @callback config(key :: atom, default :: term) :: term @doc """ Reload the endpoint configuration on application upgrades. """ @callback config_change(changed :: term, removed :: term) :: term @doc """ Initialize the endpoint configuration. Invoked when the endpoint supervisor starts, allows dynamically configuring the endpoint from system environment or other runtime sources. """ @callback init(:supervisor, config :: Keyword.t) :: {:ok, Keyword.t} # Paths and URLs @doc """ Generates the endpoint base URL, but as a `URI` struct. """ @callback struct_url() :: URI.t @doc """ Generates the endpoint base URL without any path information. """ @callback url() :: String.t @doc """ Generates the path information when routing to this endpoint. """ @callback path(path :: String.t) :: String.t @doc """ Generates the static URL without any path information. """ @callback static_url() :: String.t @doc """ Generates a route to a static file in `priv/static`. """ @callback static_path(path :: String.t) :: String.t @doc """ Generates an integrity hash to a static file in `priv/static`. """ @callback static_integrity(path :: String.t) :: String.t | nil @doc """ Generates a two item tuple containing the `static_path` and `static_integrity`. """ @callback static_lookup(path :: String.t) :: {String.t, String.t} | {String.t, nil} @doc """ Returns the script name from the :url configuration. """ @callback script_name() :: [String.t] @doc """ Returns the host from the :url configuration. """ @callback host() :: String.t # Channels @doc """ Subscribes the caller to the given topic. See `Phoenix.PubSub.subscribe/3` for options. """ @callback subscribe(topic, opts :: Keyword.t) :: :ok | {:error, term} @doc """ Unsubscribes the caller from the given topic. """ @callback unsubscribe(topic) :: :ok | {:error, term} @doc """ Broadcasts a `msg` as `event` in the given `topic` to all nodes. """ @callback broadcast(topic, event, msg) :: :ok | {:error, term} @doc """ Broadcasts a `msg` as `event` in the given `topic` to all nodes. Raises in case of failures. """ @callback broadcast!(topic, event, msg) :: :ok | no_return @doc """ Broadcasts a `msg` from the given `from` as `event` in the given `topic` to all nodes. """ @callback broadcast_from(from :: pid, topic, event, msg) :: :ok | {:error, term} @doc """ Broadcasts a `msg` from the given `from` as `event` in the given `topic` to all nodes. Raises in case of failures. """ @callback broadcast_from!(from :: pid, topic, event, msg) :: :ok | no_return @doc """ Broadcasts a `msg` as `event` in the given `topic` within the current node. """ @callback local_broadcast(topic, event, msg) :: :ok @doc """ Broadcasts a `msg` from the given `from` as `event` in the given `topic` within the current node. """ @callback local_broadcast_from(from :: pid, topic, event, msg) :: :ok @doc false defmacro __using__(opts) do quote do @behaviour Phoenix.Endpoint unquote(config(opts)) unquote(pubsub()) unquote(plug()) unquote(server()) end end defp config(opts) do quote do @otp_app unquote(opts)[:otp_app] || raise "endpoint expects :otp_app to be given" var!(config) = Phoenix.Endpoint.Supervisor.config(@otp_app, __MODULE__) var!(code_reloading?) = var!(config)[:code_reloader] @compile_config Keyword.take(var!(config), Phoenix.Endpoint.Supervisor.compile_config_keys()) @doc false def __compile_config__ do @compile_config end # Avoid unused variable warnings _ = var!(code_reloading?) @doc false def init(_key, config) do {:ok, config} end defoverridable init: 2 end end defp pubsub() do quote do def subscribe(topic, opts \\ []) when is_binary(topic) do Phoenix.PubSub.subscribe(pubsub_server!(), topic, opts) end def unsubscribe(topic) do Phoenix.PubSub.unsubscribe(pubsub_server!(), topic) end def broadcast_from(from, topic, event, msg) do Phoenix.Channel.Server.broadcast_from(pubsub_server!(), from, topic, event, msg) end def broadcast_from!(from, topic, event, msg) do Phoenix.Channel.Server.broadcast_from!(pubsub_server!(), from, topic, event, msg) end def broadcast(topic, event, msg) do Phoenix.Channel.Server.broadcast(pubsub_server!(), topic, event, msg) end def broadcast!(topic, event, msg) do Phoenix.Channel.Server.broadcast!(pubsub_server!(), topic, event, msg) end def local_broadcast(topic, event, msg) do Phoenix.Channel.Server.local_broadcast(pubsub_server!(), topic, event, msg) end def local_broadcast_from(from, topic, event, msg) do Phoenix.Channel.Server.local_broadcast_from(pubsub_server!(), from, topic, event, msg) end defp pubsub_server! do config(:pubsub_server) || raise ArgumentError, "no :pubsub_server configured for #{inspect(__MODULE__)}" end end end defp plug() do quote location: :keep do use Plug.Builder, init_mode: Phoenix.plug_init_mode() import Phoenix.Endpoint Module.register_attribute(__MODULE__, :phoenix_sockets, accumulate: true) if force_ssl = Phoenix.Endpoint.__force_ssl__(__MODULE__, var!(config)) do plug Plug.SSL, force_ssl end if var!(config)[:debug_errors] do use Plug.Debugger, otp_app: @otp_app, banner: {Phoenix.Endpoint.RenderErrors, :__debugger_banner__, []}, style: [ primary: "#EB532D", logo: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJEAAABjCAYAAACbguIxAAAAAXNSR0IArs4c6QAAAAlwSFlzAAALEwAACxMBAJqcGAAAHThJREFUeAHtPWlgVOW197vbLNkTFoFQlixAwpIVQZ8ooE+tRaBWdoK4VF5tfe2r1tb2ta611r6n9b1Xd4GETRGxIuJSoKACAlkIkD0hsiRoIHtmues7J3LpOJ2Z3Jm5yUxi5s+991vOOd+5Z777fWf7CGXA79Ct46ZGmyPnshw9WaX5qTSlJBCKjqU51aoohKVUivaIRqUUmlactEK3iCp1gablTztsnZ9kbK16w2P7wcKw5AAJhKqiBWlzIyIjVrKsnKtQ7HiiqiaGZQOC5Qm/JAkiUekqSha2X7/x2JP1FOXw1G6wLDw4oPvFl94+ZVmk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KXIYm2LrevnocydGCpG9Esh0piFsVoRTMQTkAcUzivT0oNptaG5gvXkYMr64qCSfIWG8sCx9msh0oaNJ/bMmHLFU7BcgjPGSEJvzU5oaWcUOEtKwUOBARPtWUOCRuTGppYeoyQ0+vv7dUAIketLQNeFyLj4H0Es2NUwNyX6sxDH0GnI5iECU2yQ//AcIVKjSHO1YofzJMU4K+0XhJb2aKoN8VkddERUNDuUoUgyy/LZkBA9FRIjTwJfnTjNxbe1SViU+W7hVlf6BuL9gBMi95eEXpR8FD+NIfRkQaFHw0vvTkNM06pNoZmLquxophWqrl2mz3W22o7pTeLgjkd7xoxoIybHrDHxzI8hiDGq9VzzNdN31x3R6gfidcALkZEv7cDNyZmxUZbrBNXZ8Pmxzt095QlAAcazWXsK/jOSxlDAGhQiP7iOkaSWePOdRGZmghfBKAJZrWSacmBKOzgbsxFcaY/YHLZ39WZd8wN1WDcdFKIAX0/Zooz7OAv7EHgJjnYHAX5P7USRPty3t3qN5gjm3mYgPQ8KUZBvs2hB2tzouIh1kIE80R0UhiBDvNnatM3F97jXDaTnQSEy6G1WrMh43WSyrPYEDqMsxhcUTvJUNxDKBoXIwLdYsnTyimizeb2nJBGSIJxKKSgcbyC6sAE1KEQGvwp0gh86JOEouOh2qxJcwQuiUDIhvzDTtWwg3HtWuQ6EkYVoDJjw4PyZC9PRQOtOAs/xGRXLpv3Bvby/Pw8KUS+8was/ri+52NW+UJHAPuL2482mhzAixa24Xz8OClEvvT605jd3tS6ApKHfOGKCEIaaM3NkUS+hDQnYQSHqRbajIH1WeCZRFaVvhCujbqlmdc5LvYi6T0EPLqz7iN14Wjdtivg1C0eha9Z/OB/x0P49lbf0d4XkoBD1kRBpaNChLiYhYY2JUufIrDpCEkkR5FrE3No9ZmnVYITb9f8BhSZnYemqCy4AAAAASUVORK5CYII=" ] end # Compile after the debugger so we properly wrap it. @before_compile Phoenix.Endpoint end end defp server() do quote location: :keep, unquote: false do @doc """ Returns the child specification to start the endpoint under a supervision tree. """ def child_spec(opts) do %{ id: __MODULE__, start: {__MODULE__, :start_link, [opts]}, type: :supervisor } end @doc """ Starts the endpoint supervision tree. ## Options * `:log_access_url` - if the access url should be logged once the endpoint starts All other options are merged into the endpoint configuration. """ def start_link(opts \\ []) do Phoenix.Endpoint.Supervisor.start_link(@otp_app, __MODULE__, opts) end @doc """ Returns the endpoint configuration for `key` Returns `default` if the key does not exist. """ def config(key, default \\ nil) do case :ets.lookup(__MODULE__, key) do [{^key, val}] -> val [] -> default end end @doc """ Reloads the configuration given the application environment changes. """ def config_change(changed, removed) do Phoenix.Endpoint.Supervisor.config_change(__MODULE__, changed, removed) end @doc """ Generates the endpoint base URL without any path information. It uses the configuration under `:url` to generate such. """ def url do Phoenix.Config.cache(__MODULE__, :__phoenix_url__, &Phoenix.Endpoint.Supervisor.url/1) end @doc """ Generates the static URL without any path information. It uses the configuration under `:static_url` to generate such. It falls back to `:url` if `:static_url` is not set. """ def static_url do Phoenix.Config.cache(__MODULE__, :__phoenix_static_url__, &Phoenix.Endpoint.Supervisor.static_url/1) end @doc """ Generates the endpoint base URL but as a `URI` struct. It uses the configuration under `:url` to generate such. Useful for manipulating the URL data and passing it to URL helpers. """ def struct_url do Phoenix.Config.cache(__MODULE__, :__phoenix_struct_url__, &Phoenix.Endpoint.Supervisor.struct_url/1) end @doc """ Returns the host for the given endpoint. """ def host do Phoenix.Config.cache(__MODULE__, :__phoenix_host__, &Phoenix.Endpoint.Supervisor.host/1) end @doc """ Generates the path information when routing to this endpoint. """ def path(path) do Phoenix.Config.cache(__MODULE__, :__phoenix_path__, &Phoenix.Endpoint.Supervisor.path/1) <> path end @doc """ Generates the script name. """ def script_name do Phoenix.Config.cache(__MODULE__, :__phoenix_script_name__, &Phoenix.Endpoint.Supervisor.script_name/1) end @doc """ Generates a route to a static file in `priv/static`. """ def static_path(path) do Phoenix.Config.cache(__MODULE__, :__phoenix_static__, &Phoenix.Endpoint.Supervisor.static_path/1) <> elem(static_lookup(path), 0) end @doc """ Generates a base64-encoded cryptographic hash (sha512) to a static file in `priv/static`. Meant to be used for Subresource Integrity with CDNs. """ def static_integrity(path) do elem(static_lookup(path), 1) end @doc """ Returns a two item tuple with the first item being the `static_path` and the second item being the `static_integrity`. """ def static_lookup(path) do Phoenix.Config.cache(__MODULE__, {:__phoenix_static__, path}, &Phoenix.Endpoint.Supervisor.static_lookup(&1, path)) end end end @doc false def __force_ssl__(module, config) do if force_ssl = config[:force_ssl] do Keyword.put_new(force_ssl, :host, {module, :host, []}) end end @doc false defmacro __before_compile__(%{module: module}) do sockets = Module.get_attribute(module, :phoenix_sockets) dispatches = for {path, socket, socket_opts} <- sockets, {path, type, conn_ast, socket, opts} <- socket_paths(module, path, socket, socket_opts) do quote do defp do_handler(unquote(path), conn, _opts) do {unquote(type), unquote(conn_ast), unquote(socket), unquote(Macro.escape(opts))} end end end quote do defoverridable [call: 2] # Inline render errors so we set the endpoint before calling it. def call(conn, opts) do conn = put_in conn.secret_key_base, config(:secret_key_base) conn = put_in conn.script_name, script_name() conn = Plug.Conn.put_private(conn, :phoenix_endpoint, __MODULE__) try do super(conn, opts) rescue e in Plug.Conn.WrapperError -> %{conn: conn, kind: kind, reason: reason, stack: stack} = e Phoenix.Endpoint.RenderErrors.__catch__(conn, kind, reason, stack, config(:render_errors)) catch kind, reason -> stack = __STACKTRACE__ Phoenix.Endpoint.RenderErrors.__catch__(conn, kind, reason, stack, config(:render_errors)) end end @doc false def __sockets__, do: unquote(Macro.escape(sockets)) @doc false def __handler__(%{path_info: path} = conn, opts), do: do_handler(path, conn, opts) unquote(dispatches) defp do_handler(_path, conn, opts), do: {:plug, conn, __MODULE__, opts} end end defp socket_paths(endpoint, path, socket, opts) do paths = [] websocket = Keyword.get(opts, :websocket, true) longpoll = Keyword.get(opts, :longpoll, false) paths = if websocket do config = Phoenix.Socket.Transport.load_config(websocket, Phoenix.Transports.WebSocket) {conn_ast, match_path} = socket_path(path, config) [{match_path, :websocket, conn_ast, socket, config} | paths] else paths end paths = if longpoll do config = Phoenix.Socket.Transport.load_config(longpoll, Phoenix.Transports.LongPoll) plug_init = {endpoint, socket, config} {conn_ast, match_path} = socket_path(path, config) [{match_path, :plug, conn_ast, Phoenix.Transports.LongPoll, plug_init} | paths] else paths end paths end defp socket_path(path, config) do end_path_fragment = Keyword.fetch!(config, :path) {vars, path} = String.split(path <> "/" <> end_path_fragment, "/", trim: true) |> Enum.join("/") |> Plug.Router.Utils.build_path_match() conn_ast = if vars == [] do quote do conn end else params_map = {:%{}, [], Plug.Router.Utils.build_path_params_match(vars)} quote do params = unquote(params_map) %Plug.Conn{conn | path_params: params, params: params} end end {conn_ast, path} end ## API @doc """ Defines a websocket/longpoll mount-point for a socket. ## Options * `:websocket` - controls the websocket configuration. Defaults to `true`. May be false or a keyword list of options. See "Common configuration" and "WebSocket configuration" for the whole list * `:longpoll` - controls the longpoll configuration. Defaults to `false`. May be true or a keyword list of options. See "Common configuration" and "Longpoll configuration" for the whole list If your socket is implemented using `Phoenix.Socket`, you can also pass to each transport above all options accepted on `use Phoenix.Socket`. An option given here will override the value in `use Phoenix.Socket`. ## Examples socket "/ws", MyApp.UserSocket socket "/ws/admin", MyApp.AdminUserSocket, longpoll: true, websocket: [compress: true] ## Path params It is possible to include variables in the path, these will be available in the `params` that are passed to the socket. socket "/ws/:user_id", MyApp.UserSocket, websocket: [path: "/project/:project_id"] ## Common configuration The configuration below can be given to both `:websocket` and `:longpoll` keys: * `:path` - the path to use for the transport. Will default to the transport name ("/websocket" or "/longpoll") * `:serializer` - a list of serializers for messages. See `Phoenix.Socket` for more information * `:transport_log` - if the transport layer itself should log and, if so, the level * `:check_origin` - if the transport should check the origin of requests when the `origin` header is present. May be `true`, `false`, a list of hosts that are allowed, or a function provided as MFA tuple. Defaults to `:check_origin` setting at endpoint configuration. If `true`, the header is checked against `:host` in `YourAppWeb.Endpoint.config(:url)[:host]`. If `false`, your app is vulnerable to Cross-Site WebSocket Hijacking (CSWSH) attacks. Only use in development, when the host is truly unknown or when serving clients that do not send the `origin` header, such as mobile apps. You can also specify a list of explicitly allowed origins. Wildcards are supported. check_origin: [ "https://example.com", "//another.com:888", "//*.other.com" ] Or to accept any origin matching the request connection's host, port, and scheme: check_origin: :conn Or a custom MFA function: check_origin: {MyAppWeb.Auth, :my_check_origin?, []} The MFA is invoked with the request `%URI{}` as the first argument, followed by arguments in the MFA list. * `:code_reloader` - enable or disable the code reloader. Defaults to your endpoint configuration * `:connect_info` - a list of keys that represent data to be copied from the transport to be made available in the user socket `connect/3` callback The valid keys are: * `:peer_data` - the result of `Plug.Conn.get_peer_data/1` * `:trace_context_headers` - a list of all trace context headers. Supported headers are defined by the [W3C Trace Context Specification](https://www.w3.org/TR/trace-context-1/). These headers are necessary for libraries such as [OpenTelemetry](https://opentelemetry.io/) to extract trace propagation information to know this request is part of a larger trace in progress. * `:x_headers` - all request headers that have an "x-" prefix * `:uri` - a `%URI{}` with information from the conn * `:user_agent` - the value of the "user-agent" request header * `{:session, session_config}` - the session information from `Plug.Conn`. The `session_config` is an exact copy of the arguments given to `Plug.Session`. This requires the "_csrf_token" to be given as request parameter with the value of `URI.encode_www_form(Plug.CSRFProtection.get_csrf_token())` when connecting to the socket. It can also be a MFA to allow loading config in runtime `{MyAppWeb.Auth, :get_session_config, []}`. Otherwise the session will be `nil`. Arbitrary keywords may also appear following the above valid keys, which is useful for passing custom connection information to the socket. For example: socket "/socket", AppWeb.UserSocket, websocket: [ connect_info: [:peer_data, :trace_context_headers, :x_headers, :uri, session: [store: :cookie]] ] With arbitrary keywords: socket "/socket", AppWeb.UserSocket, websocket: [ connect_info: [:uri, custom_value: "abcdef"] ] ## Websocket configuration The following configuration applies only to `:websocket`. * `:timeout` - the timeout for keeping websocket connections open after it last received data, defaults to 60_000ms * `:max_frame_size` - the maximum allowed frame size in bytes, defaults to "infinity" * `:fullsweep_after` - the maximum number of garbage collections before forcing a fullsweep for the socket process. You can set it to `0` to force more frequent cleanups of your websocket transport processes. Setting this option requires Erlang/OTP 24 * `:compress` - whether to enable per message compression on all data frames, defaults to false * `:subprotocols` - a list of supported websocket subprotocols. Used for handshake `Sec-WebSocket-Protocol` response header, defaults to nil. For example: subprotocols: ["sip", "mqtt"] * `:error_handler` - custom error handler for connection errors. If `c:Phoenix.Socket.connect/3` returns an `{:error, reason}` tuple, the error handler will be called with the error reason. For WebSockets, the error handler must be a MFA tuple that receives a `Plug.Conn`, the error reason, and returns a `Plug.Conn` with a response. For example: error_handler: {MySocket, :handle_error, []} and a `{:error, :rate_limit}` return may be handled on `MySocket` as: def handle_error(conn, :rate_limit), do: Plug.Conn.send_resp(conn, 429, "Too many requests") ## Longpoll configuration The following configuration applies only to `:longpoll`: * `:window_ms` - how long the client can wait for new messages in its poll request, defaults to 10_000ms. * `:pubsub_timeout_ms` - how long a request can wait for the pubsub layer to respond, defaults to 2000ms. * `:crypto` - options for verifying and signing the token, accepted by `Phoenix.Token`. By default tokens are valid for 2 weeks """ defmacro socket(path, module, opts \\ []) do module = Macro.expand(module, %{__CALLER__ | function: {:__handler__, 2}}) quote do @phoenix_sockets {unquote(path), unquote(module), unquote(opts)} end end @doc false @deprecated "Phoenix.Endpoint.instrument/4 is deprecated and has no effect. Use :telemetry instead" defmacro instrument(_endpoint_or_conn_or_socket, _event, _runtime, _fun) do :ok end @doc """ Checks if Endpoint's web server has been configured to start. * `otp_app` - The OTP app running the endpoint, for example `:my_app` * `endpoint` - The endpoint module, for example `MyAppWeb.Endpoint` ## Examples iex> Phoenix.Endpoint.server?(:my_app, MyAppWeb.Endpoint) true """ def server?(otp_app, endpoint) when is_atom(otp_app) and is_atom(endpoint) do Phoenix.Endpoint.Supervisor.server?(otp_app, endpoint) end end