forked from elixir-plug/plug
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic send and more req parsed data
- Loading branch information
José Valim
committed
Nov 15, 2013
1 parent
1ddab8a
commit cb835fb
Showing
7 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright (c) 2013 Plataformatec. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,42 @@ | ||
# Plug | ||
|
||
** TODO: Add description ** | ||
Plug is: | ||
|
||
1. An specification for composable modules in between web applications | ||
2. A connection specification and adapters for different web servers in the Erlang VM | ||
|
||
## Connection | ||
|
||
A connection is represented by the `Plug.Conn` record: | ||
|
||
```elixir | ||
Plug.Conn[ | ||
host: "www.example.com", | ||
path_info: ["bar", "baz"], | ||
script_name: ["foo"], | ||
assigns: [], | ||
... | ||
] | ||
``` | ||
|
||
`Plug.Conn` is a record so it can be extended with protocols. Most of the data can be read directly from the record, which is useful for pattern matching. Whenever you want to manipulate the connection, you must use the functions defined in `Plug.Connection`. | ||
|
||
As everything else in Elixir, **`Plug.Conn` is immutable**, so every manipulation returns a new copy of the connection: | ||
|
||
```elixir | ||
conn = assign(conn, :key, value) | ||
conn = send(conn, 200, "OK!") | ||
conn | ||
``` | ||
|
||
Note the connection is a **direct interface to the underlying web server**. When you call `send/3` above, it will immediately send the given status and body back to the client. Furthermore, **parsing the request information is lazy**. For example, if you want to access the request headers, they need to be explicitly fetched before hand: | ||
|
||
```elixir | ||
conn = fetch(conn, :req_headers) | ||
conn.req_headers["content-type"] | ||
``` | ||
|
||
# License | ||
|
||
Plug source code is released under Apache 2 License. | ||
Check LICENSE file for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
defmodule Plug.Connection.Spec do | ||
use Behaviour | ||
|
||
defcallback build(term, term) :: term | ||
alias Plug.Conn | ||
@typep payload :: term | ||
|
||
@doc """ | ||
Sends the given status, headers and body as a response | ||
back to the client. | ||
""" | ||
defcallback send(payload, Conn.status, Conn.headers, Conn.body) :: payload | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.