Skip to content

Commit

Permalink
Add querystring decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
mme committed Apr 16, 2013
1 parent bde38f8 commit a7094c4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
38 changes: 34 additions & 4 deletions lib/elli_request.ex
Expand Up @@ -22,15 +22,15 @@ defmodule Elli.HTTPRequest do

def get_header( key, default, {__MODULE__, elli_req} ), do: :elli_request.get_range(key, default, elli_req)

def get_arg( key, {__MODULE__, elli_req} ), do: :elli_request.get_arg(key, elli_req)
def get_arg( key, {__MODULE__, elli_req} ), do: arg_val(:elli_request.get_arg(key, elli_req))

def get_arg( key, default, {__MODULE__, elli_req}), do: :elli_request.get_header(key, default, elli_req)
def get_arg( key, default, {__MODULE__, elli_req}), do: arg_val(:elli_request.get_header(key, default, elli_req))

def get_args( {__MODULE__, elli_req} ), do: :elli_request.get_args(elli_req)

def post_arg( key,{__MODULE__, elli_req} ), do: :elli_request.post_arg(key,elli_req)
def post_arg( key,{__MODULE__, elli_req} ), do: arg_val(:elli_request.post_arg(key,elli_req))

def post_arg( key,default,{__MODULE__, elli_req} ), do: :elli_request.post_arg(key,default,elli_req)
def post_arg( key,default,{__MODULE__, elli_req} ), do: arg_val(:elli_request.post_arg(key,default,elli_req))

def body_qs( {__MODULE__, elli_req} ), do: :elli_request.body_qs(elli_req)

Expand All @@ -47,4 +47,34 @@ defmodule Elli.HTTPRequest do
def to_proplist( {__MODULE__, elli_req} ), do: :elli_request.to_proplist(elli_req)


def arg_val(nil), do: nil
def arg_val(a) when is_atom(a), do: a
def arg_val(s), do: urldecode(s)

# stolen from cowboy
# https://github.com/extend/cowboy/blob/master/src/cowboy_http.erl
def urldecode(s), do: urldecode(s, <<>>)

def urldecode(<<?%, h, l, rest :: binary>>, acc) do
g = unhex(h)
m = unhex(l)
if g == :error or m == :error do
urldecode(<<h, h, rest :: binary>>, <<acc :: binary, ?%>>)
else
urldecode(rest, <<acc :: binary, Bitwise.bor(Bitwise.<<<(g,4), m)>>)
end
end


def urldecode(<<?%, rest :: binary>>, acc), do: urldecode(rest, <<acc :: binary, ?%>>)
def urldecode(<<?+, rest :: binary>>, acc), do: urldecode(rest, <<acc :: binary, ? >>)
def urldecode(<<c, rest :: binary>>, acc), do: urldecode(rest, <<acc :: binary, c>>)
def urldecode(<<>>, acc), do: acc


def unhex(c) when c >= ?0 and c <= ?9, do: c - ?0
def unhex(c) when c >= ?A and c <= ?F, do: c - ?A + 10
def unhex(c) when c >= ?a and c <= ?f, do: c - ?a + 10
def unhex(_), do: :error

end
4 changes: 2 additions & 2 deletions test/elli_handler_test.exs
Expand Up @@ -32,8 +32,8 @@ defmodule Elli.Handler.Test do
end

test "post body" do
{:ok, {{_,200,_},_,response}} = :httpc.request(:post, {'http://localhost:3000/hello/post/variable', [], 'application/x-www-form-urlencoded', 'var=hello_hello' }, [], [])
assert response == 'hello_hello'
{:ok, {{_,200,_},_,response}} = :httpc.request(:post, {'http://localhost:3000/hello/post/variable', [], 'application/x-www-form-urlencoded', 'var=hello%20hello' }, [], [])
assert response == 'hello hello'
end

test "halt" do
Expand Down

0 comments on commit a7094c4

Please sign in to comment.