Skip to content

Commit

Permalink
Update to Elixir master
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Sep 10, 2012
1 parent d7430ca commit 13f89aa
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 35 deletions.
16 changes: 8 additions & 8 deletions lib/dynamo/cowboy/body_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ defmodule Dynamo.Cowboy.BodyParser do
end

defp multipart_entry(headers, body, acc) do
case List.keyfind(headers, "Content-Disposition", 1) do
case List.keyfind(headers, "Content-Disposition", 0) do
{ _, value } ->
[_|parts] = Binary.split(value, ";", global: true)
[_|parts] = String.split(value, ";", global: true)
parts = lc part inlist parts, do: to_multipart_kv(part)

case List.keyfind(parts, "name", 1) do
case List.keyfind(parts, "name", 0) do
{ "name", name } ->
entry =
case List.keyfind(parts, "filename", 1) do
case List.keyfind(parts, "filename", 0) do
{ "filename", filename } ->
{ _, type } = List.keyfind(headers, :"Content-Type", 1) || { :"Content-Type", nil }
{ _, type } = List.keyfind(headers, :"Content-Type", 0) || { :"Content-Type", nil }
{ name, Dynamo.HTTP.File.new(name: name, filename: filename, content_type: type, body: body) }
_ ->
{ name, body }
Expand All @@ -65,20 +65,20 @@ defmodule Dynamo.Cowboy.BodyParser do
end

defp to_multipart_kv(binary) do
case Binary.split(binary, "=") do
case String.split(binary, "=") do
[h] -> { trim(h), nil }
[h,t] -> { trim(h), strip_quotes(t) }
end
end

defp strip_quotes(<<?", remaining | :binary>>) do
defp strip_quotes(<<?", remaining :: binary>>) do
binary_part(remaining, 0, size(remaining) - 1)
end

defp strip_quotes(other) do
other
end

defp trim(<<?\s, rest | :binary>>), do: trim(rest)
defp trim(<<?\s, rest :: binary>>), do: trim(rest)
defp trim(rest) when is_binary(rest), do: rest
end
2 changes: 1 addition & 1 deletion lib/dynamo/http/case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ defmodule Dynamo.HTTP.Case do
def process(app, method, path) do
conn = app.service Dynamo.HTTP.Test.new.req(method, path)

if not is_tuple(conn) or not function_exported?(elem(conn, 1), :state, 1) do
if not is_tuple(conn) or not function_exported?(elem(conn, 0), :state, 1) do
raise "#{inspect app}.service did not return a connection, got #{inspect conn}"
end

Expand Down
4 changes: 2 additions & 2 deletions lib/dynamo/http/cookies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Dynamo.HTTP.Cookies do
cookies = Dict.put(cookies, key, value)
end

resp_cookies = List.keydelete(resp_cookies, key, 1)
resp_cookies = List.keydelete(resp_cookies, key, 0)
connection(conn, cookies: cookies, resp_cookies: [{ key, value, opts }|resp_cookies])
end

Expand All @@ -46,7 +46,7 @@ defmodule Dynamo.HTTP.Cookies do
cookies = Dict.delete(cookies, key)
end

resp_cookies = List.keydelete(resp_cookies, key, 1)
resp_cookies = List.keydelete(resp_cookies, key, 0)
connection(conn, cookies: cookies, resp_cookies: [{ key, "", opts }|resp_cookies])
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamo/http/query_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Dynamo.HTTP.QueryParser do
#
case Regex.run(%r"^([^\[]+)\[(.*)\]$", key) do
[_all, key, subpart] ->
[key|Binary.split(subpart, "][", global: true)]
[key|String.split(subpart, "][", global: true)]
_ ->
[key]
end
Expand Down
6 changes: 3 additions & 3 deletions lib/dynamo/router/filters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule Dynamo.Router.Filters do
Matches a filter against the other
"""
def match?(atom, filter) when is_atom(atom) and is_tuple(filter) do
elem(filter, 1) == atom
elem(filter, 0) == atom
end

def match?(atom, atom) when is_atom(atom) do
Expand Down Expand Up @@ -130,7 +130,7 @@ defmodule Dynamo.Router.Filters do
{ h, 0 }
end

defp extract_module_and_arity(h) when is_tuple(h) and is_atom(elem(h, 1)) do
{ elem(h, 1), 1 }
defp extract_module_and_arity(h) when is_tuple(h) and is_atom(elem(h, 0)) do
{ elem(h, 0), 1 }
end
end
4 changes: 2 additions & 2 deletions lib/dynamo/router/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ defmodule Dynamo.Router.Utils do
"""
def split(bin) do
lc segment inlist Binary.split(bin, "/", global: true), segment != "", do: segment
lc segment inlist String.split(bin, "/", global: true), segment != "", do: segment
end

## Helpers
Expand Down Expand Up @@ -127,7 +127,7 @@ defmodule Dynamo.Router.Utils do
end

defp list_split(bin) do
lc segment inlist Binary.split(bin, "/", global: true), segment != "", do: binary_to_list(segment)
lc segment inlist String.split(bin, "/", global: true), segment != "", do: binary_to_list(segment)
end

defp binary_from_buffer(buffer) do
Expand Down
8 changes: 4 additions & 4 deletions lib/dynamo/utils/message_verifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Dynamo.Utils.MessageVerifier do
Decodes and verifies the encoded binary was not tampared with.
"""
def verify(encoded, secret) do
case Binary.split(encoded, "--") do
case String.split(encoded, "--") do
[content, digest] when content != "" and digest != "" ->
if secure_compare(digest(secret, content), digest) do
{ :ok, content /> :base64.decode /> binary_to_term }
Expand All @@ -33,7 +33,7 @@ defmodule Dynamo.Utils.MessageVerifier do
end

defp digest(secret, data) do
<<mac | 160 - :integer>> = :crypto.sha_mac(secret, data)
<<mac :: [integer, size(160)]>> = :crypto.sha_mac(secret, data)
:erlang.integer_to_list(mac, 16) /> list_to_binary
end

Expand All @@ -49,11 +49,11 @@ defmodule Dynamo.Utils.MessageVerifier do
end
end

defp compare_each(<<h, left | :binary>>, <<h, right | :binary>>, acc) do
defp compare_each(<<h, left :: binary>>, <<h, right :: binary>>, acc) do
compare_each(left, right, acc)
end

defp compare_each(<<_, left | :binary>>, <<_, right | :binary>>, _acc) do
defp compare_each(<<_, left :: binary>>, <<_, right :: binary>>, _acc) do
compare_each(left, right, false)
end

Expand Down
8 changes: 4 additions & 4 deletions lib/dynamo/view/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ defmodule Dynamo.View.Handler do
end
end

defp upcase(<<h, t | :binary>>) when h in ?a..?z do
<<h - 32, upcase(t) | :binary>>
defp upcase(<<h, t :: binary>>) when h in ?a..?z do
<<h - 32, upcase(t) :: binary>>
end

defp upcase(<<h, t | :binary>>) do
<<h, upcase(t) | :binary>>
defp upcase(<<h, t :: binary>>) do
<<h, upcase(t) :: binary>>
end

defp upcase(<<>>) do
Expand Down
16 changes: 8 additions & 8 deletions test/dynamo/cowboy/http_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -177,22 +177,22 @@ defmodule Dynamo.Cowboy.HTTPTest do
assert_success response

{ _, headers, _ } = response
assert List.keyfind(headers, "Set-Cookie", 1) == { "Set-Cookie", "foo=bar; Version=1; Path=/hello; HttpOnly" }
assert List.keyfind(headers, "Set-Cookie", 0) == { "Set-Cookie", "foo=bar; Version=1; Path=/hello; HttpOnly" }

headers = List.keydelete(headers, "Set-Cookie", 1)
assert List.keyfind(headers, "Set-Cookie", 1) == { "Set-Cookie","bar=baz; Version=1" }
headers = List.keydelete(headers, "Set-Cookie", 0)
assert List.keyfind(headers, "Set-Cookie", 0) == { "Set-Cookie","bar=baz; Version=1" }
end

test :req_resp_cookies do
response = request :get, "/req_resp_cookies", [{ "Cookie", %b(foo="bar"; baz="bat") }]
assert_success response

{ _, headers, _ } = response
{ "Set-Cookie", contents } = List.keyfind(headers, "Set-Cookie", 1)
{ "Set-Cookie", contents } = List.keyfind(headers, "Set-Cookie", 0)
assert contents =~ %r"foo=; Version=1; Expires=Thu, 01 Jan 1970 \d\d:\d\d:\d\d GMT; Max-Age=0; HttpOnly"

headers = List.keydelete(headers, "Set-Cookie", 1)
assert List.keyfind(headers, "Set-Cookie", 1) == nil
headers = List.keydelete(headers, "Set-Cookie", 0)
assert List.keyfind(headers, "Set-Cookie", 0) == nil
end

## Assigns
Expand Down Expand Up @@ -249,7 +249,7 @@ defmodule Dynamo.Cowboy.HTTPTest do
assert_success response

{ _, headers, _ } = response
assert List.keyfind(headers, "X-Header", 1) == { "X-Header", "Third" }
assert List.keyfind(headers, "X-Header", 0) == { "X-Header", "Third" }
end

## Request Body API
Expand Down Expand Up @@ -291,7 +291,7 @@ defmodule Dynamo.Cowboy.HTTPTest do

test :sendfile do
{ 200, headers, "HELLO" } = request :get, "/sendfile"
assert List.keyfind(headers, "Content-Length", 1) == { "Content-Length", "5" }
assert List.keyfind(headers, "Content-Length", 0) == { "Content-Length", "5" }

assert { 500, _, _ } = request :get, "/invalid_sendfile"
end
Expand Down
4 changes: 2 additions & 2 deletions test/dynamo/utils/message_verifier_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Dynamo.Utils.MessageVerifierTest do
alias Dynamo.Utils.MessageVerifier, as: MV

test "generates a signed message" do
[content, encoded] = Binary.split MV.generate(:hello, "secret"), "--"
[content, encoded] = String.split MV.generate(:hello, "secret"), "--"
assert content /> :base64.decode /> binary_to_term == :hello
assert size(encoded) == 40
end
Expand All @@ -22,7 +22,7 @@ defmodule Dynamo.Utils.MessageVerifierTest do
end

test "does not verify a tampered message" do
[_, encoded] = Binary.split MV.generate(:hello, "secret"), "--"
[_, encoded] = String.split MV.generate(:hello, "secret"), "--"
content = :bye /> term_to_binary /> :base64.encode
assert MV.verify(content <> "--" <> encoded, "secret") == :error
end
Expand Down

0 comments on commit 13f89aa

Please sign in to comment.