Skip to content

Commit

Permalink
No need to handle nil header values.
Browse files Browse the repository at this point in the history
  • Loading branch information
DevL committed Apr 5, 2015
1 parent 04e7cb1 commit 67184fa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ defp deps do
end
```

Add the plug to e.g. a pipeline in a [Phoenix](http://www.phoenixframework.org/) controller. In this case we will require the request header `x-api-key` to be set, extract its first value and assign it the connection (a `Plug.Conn`) for later use in another plug or action.
Add the plug to e.g. a pipeline in a [Phoenix](http://www.phoenixframework.org/)
controller. In this case we will require the request header `x-api-key` to be set,
extract its first value and assign it the connection (a `Plug.Conn`) for later use
in another plug or action.
```elixir
defmodule MyPhoenixApp.MyController do
use MyPhoenixApp.Web, :controller
Expand All @@ -31,9 +34,14 @@ defmodule MyPhoenixApp.MyController do
end
end
```
Notice how the first value required header `"x-api-key"` has been extracted and can be retrieved using `conn.assigns[:api_key]`. An alternative is to use `Plug.Conn.get_req_header/2` to get all the values associated with a given header.
Notice how the first value required header `"x-api-key"` has been extracted
and can be retrieved using `conn.assigns[:api_key]`. An alternative is to use
`Plug.Conn.get_req_header/2` to get all the values associated with a given header.

By default, a missing header will return a status code of 403 (forbidden) and halt the plug pipeline, i.e. no subsequent plugs will be executed. The same is true if the required header is explicitly set to `nil`. This behaviour however is configurable.
By default, a missing header will return a status code of 403 (forbidden) and halt
the plug pipeline, i.e. no subsequent plugs will be executed. The same is true if
the required header is explicitly set to `nil` as the underlying HTTP server will
not include the header. This behaviour however is configurable.
```elixir
defmodule MyPhoenixApp.MyOtherController do
use MyPhoenixApp.Web, :controller
Expand All @@ -55,7 +63,10 @@ defmodule MyPhoenixApp.MyOtherController do
end
end
```
If the header is missing or set to `nil` the status code, a status code of 400 (bad request) will be returned before the plug pipeline is halted. Notice that the function specified as a callback needs to be a public function as it'll be invoked from another module.
If the header is missing or set to `nil` the status code, a status code of 400
(bad request) will be returned before the plug pipeline is halted. Notice that
the function specified as a callback needs to be a public function as it'll be
invoked from another module.

Lastly, it's possible to extract multiple headers at the same time.

Expand Down
2 changes: 1 addition & 1 deletion lib/plug_require_header.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule PlugRequireHeader do
import Plug.Conn
alias Plug.Conn.Status

@vsn "0.3.0"
@vsn "0.3.1"
@doc false
def version, do: @vsn

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule PlugRequireHeader.Mixfile do
def project do
[
app: :plug_require_header,
version: "0.3.0",
version: "0.3.1",
name: "PlugRequireHeader",
source_url: "https://github.com/DevL/plug_require_header",
elixir: "~> 1.0",
Expand Down

0 comments on commit 67184fa

Please sign in to comment.