Packages
plug
1.4.0
1.20.3
1.20.2
1.20.1
retired
1.20.0
retired
1.19.5
1.19.4
1.19.3
1.19.2
1.19.1
1.19.0
1.18.5
1.18.4
1.18.3
1.18.2
1.18.1
1.18.0
1.17.4
1.17.3
1.17.2
1.17.1
1.17.0
1.16.6
1.16.5
1.16.4
1.16.3
1.16.2
1.16.1
1.16.0
1.15.6
1.15.5
1.15.4
1.15.3
1.15.2
1.15.1
1.15.0
1.14.2
1.14.1
1.14.0
1.13.6
1.13.5
1.13.4
1.13.3
1.13.2
1.13.1
retired
1.13.0
retired
1.12.1
1.12.0
1.11.1
1.11.0
1.10.4
1.10.3
1.10.2
1.10.1
1.10.0
1.9.0
1.8.3
1.8.2
1.8.1
1.8.0
1.7.2
1.7.1
1.7.0
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.5.0-rc.2
1.5.0-rc.1
1.5.0-rc.0
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.2.0-rc.0
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.14.0
0.13.1
0.13.0
0.12.2
0.12.1
0.12.0
0.11.3
0.11.2
0.11.1
0.11.0
0.10.0
0.9.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.4
0.4.3
0.4.2
0.4.1
Compose web applications with functions
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/plug/conn/status.ex
defmodule Plug.Conn.Status do
@moduledoc """
Conveniences for working with status codes.
"""
custom_statuses = Application.get_env(:plug, :statuses, %{})
statuses = %{
100 => "Continue",
101 => "Switching Protocols",
102 => "Processing",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
207 => "Multi-Status",
208 => "Already Reported",
226 => "IM Used",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
306 => "Switch Proxy",
307 => "Temporary Redirect",
308 => "Permanent Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Long",
415 => "Unsupported Media Type",
416 => "Requested Range Not Satisfiable",
417 => "Expectation Failed",
418 => "I'm a teapot",
421 => "Misdirected Request",
422 => "Unprocessable Entity",
423 => "Locked",
424 => "Failed Dependency",
425 => "Unordered Collection",
426 => "Upgrade Required",
428 => "Precondition Required",
429 => "Too Many Requests",
431 => "Request Header Fields Too Large",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported",
506 => "Variant Also Negotiates",
507 => "Insufficient Storage",
508 => "Loop Detected",
510 => "Not Extended",
511 => "Network Authentication Required"
}
reason_phrase_to_atom = fn reason_phrase ->
reason_phrase
|> String.downcase()
|> String.replace("'", "")
|> String.replace(~r/[^a-z0-9]/, "_")
|> String.to_atom()
end
status_map_to_doc = fn statuses ->
statuses
|> Enum.sort_by(&elem(&1, 0))
|> Enum.map(fn {code, reason_phrase} ->
atom = reason_phrase_to_atom.(reason_phrase)
" * `#{inspect atom}` - #{code}\n"
end)
end
custom_status_doc =
if custom_statuses != %{} do
"""
## Custom status codes
#{status_map_to_doc.(custom_statuses)}
"""
end
@doc """
Returns the status code given an integer or a known atom.
## Known status codes
The following status codes can be given as atoms with their
respective value shown next:
#{status_map_to_doc.(statuses)}
#{custom_status_doc}
"""
@spec code(integer | atom) :: integer
def code(integer_or_atom)
def code(integer) when integer in 100..999 do
integer
end
for {code, reason_phrase} <- statuses do
atom = reason_phrase_to_atom.(reason_phrase)
def code(unquote(atom)), do: unquote(code)
end
# This ensures that both the default and custom statuses will work
for {code, reason_phrase} <- custom_statuses do
atom = reason_phrase_to_atom.(reason_phrase)
def code(unquote(atom)), do: unquote(code)
end
@spec reason_phrase(integer) :: String.t
def reason_phrase(integer)
for {code, phrase} <- Map.merge(statuses, custom_statuses) do
def reason_phrase(unquote(code)), do: unquote(phrase)
end
def reason_phrase(code) do
raise ArgumentError, """
unknown status code #{inspect code}
Custom codes can be defined in the configuration for the :plug application,
under the :statuses key (which contains a map of status codes as keys and
reason phrases as values). For example:
config :plug, :statuses, %{451 => "Unavailable For Legal Reasons"}
After defining the config for custom statuses, Plug must be recompiled for
the changes to take place using:
MIX_ENV=dev mix deps.compile plug
Doing this will allow the use of the integer status code 451 as
well as the atom :unavailable_for_legal_reasons in many Plug functions.
For example:
put_status(conn, :unavailable_for_legal_reasons)
"""
end
end