mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-05 08:12:31 -04:00
Extract allowed_methods to simple macro
This commit is contained in:
parent
ce6b93f779
commit
86d06dc523
23 changed files with 36 additions and 209 deletions
|
@ -2,14 +2,7 @@ defmodule HTTParrot.Base64Handler do
|
|||
@moduledoc """
|
||||
Returns urlsafe base64 decoded data.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"application", "octet-stream", []}, :get_binary}], req, state}
|
||||
|
@ -39,6 +32,4 @@ defmodule HTTParrot.Base64Handler do
|
|||
def get_binary(req, result) do
|
||||
{result, req, result}
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,13 +2,7 @@ defmodule HTTParrot.BasicAuthHandler do
|
|||
@moduledoc """
|
||||
Challenges HTTPBasic Auth
|
||||
"""
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def is_authorized(req, state) do
|
||||
{user, req} = :cowboy_req.binding(:user, req)
|
||||
|
@ -31,6 +25,4 @@ defmodule HTTParrot.BasicAuthHandler do
|
|||
defp response(user) do
|
||||
[authenticated: true, user: user] |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -3,14 +3,7 @@ defmodule HTTParrot.CacheHandler do
|
|||
Returns 200 unless an If-Modified-Since or If-None-Match header is provided, when it returns a 304.
|
||||
"""
|
||||
alias HTTParrot.GeneralRequestInfo
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def last_modified(req, state) do
|
||||
{{{2012, 9, 21}, {22, 36, 14}}, req, state}
|
||||
|
@ -28,6 +21,4 @@ defmodule HTTParrot.CacheHandler do
|
|||
defp response(info) do
|
||||
info |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.CookiesHandler do
|
|||
@moduledoc """
|
||||
Returns cookie data.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"application", "json", []}, :get_json}], req, state}
|
||||
|
@ -24,6 +17,4 @@ defmodule HTTParrot.CookiesHandler do
|
|||
defp response(cookies) do
|
||||
[cookies: cookies] |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
15
lib/httparrot/cowboy.ex
Normal file
15
lib/httparrot/cowboy.ex
Normal file
|
@ -0,0 +1,15 @@
|
|||
defmodule HTTParrot.Cowboy do
|
||||
defmacro __using__(opts) do
|
||||
methods = Keyword.get(opts, :methods, [])
|
||||
|
||||
quote bind_quoted: [methods: methods] do
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{unquote(methods), req, state}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,13 +1,6 @@
|
|||
defmodule HTTParrot.DelayedHandler do
|
||||
alias HTTParrot.GeneralRequestInfo
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def malformed_request(req, state) do
|
||||
{n, req} = :cowboy_req.binding(:n, req)
|
||||
|
@ -32,6 +25,4 @@ defmodule HTTParrot.DelayedHandler do
|
|||
defp response(info) do
|
||||
info |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.DeleteCookiesHandler do
|
|||
@moduledoc """
|
||||
Deletes one or more simple cookies.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def malformed_request(req, state) do
|
||||
{qs_vals, req} = :cowboy_req.qs_vals(req)
|
||||
|
@ -32,6 +25,4 @@ defmodule HTTParrot.DeleteCookiesHandler do
|
|||
defp delete_cookie(name, value, req) do
|
||||
:cowboy_req.set_resp_cookie(name, value, [path: "/", max_age: 0], req)
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -3,14 +3,7 @@ defmodule HTTParrot.DeleteHandler do
|
|||
Returns DELETE data.
|
||||
"""
|
||||
alias HTTParrot.GeneralRequestInfo
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["DELETE"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(DELETE)
|
||||
|
||||
def delete_resource(req, state) do
|
||||
{info, req} = GeneralRequestInfo.retrieve(req)
|
||||
|
@ -21,6 +14,4 @@ defmodule HTTParrot.DeleteHandler do
|
|||
defp response(info) do
|
||||
info |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.DenyHandler do
|
|||
@moduledoc """
|
||||
Returns a simple HTML page.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"text", "plain", []}, :get_plain}], req, state}
|
||||
|
@ -30,6 +23,4 @@ defmodule HTTParrot.DenyHandler do
|
|||
def get_plain(req, state) do
|
||||
{@body, req, state}
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -3,14 +3,7 @@ defmodule HTTParrot.GetHandler do
|
|||
Returns GET data.
|
||||
"""
|
||||
alias HTTParrot.GeneralRequestInfo
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"application", "json", []}, :get_json}], req, state}
|
||||
|
@ -24,6 +17,4 @@ defmodule HTTParrot.GetHandler do
|
|||
defp response(info) do
|
||||
info |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
defmodule HTTParrot.HeadersHandler do
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"application", "json", []}, :get_json}], req, state}
|
||||
|
@ -19,6 +13,4 @@ defmodule HTTParrot.HeadersHandler do
|
|||
defp response(headers) do
|
||||
[headers: headers] |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,13 +2,7 @@ defmodule HTTParrot.HiddenBasicAuthHandler do
|
|||
@moduledoc """
|
||||
404'd BasicAuth
|
||||
"""
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
@doc """
|
||||
This method should be `is_authorized`, but this handler will return 404 if the auth fails
|
||||
|
@ -34,6 +28,4 @@ defmodule HTTParrot.HiddenBasicAuthHandler do
|
|||
defp response(user) do
|
||||
[authenticated: true, user: user] |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.ImageHandler do
|
|||
@moduledoc """
|
||||
Returns an image.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"image", "png", []}, :get_png},
|
||||
|
@ -21,6 +14,4 @@ defmodule HTTParrot.ImageHandler do
|
|||
|
||||
def get_png(req, state), do: {@png, req, state}
|
||||
def get_jpeg(req, state), do: {@jpeg, req, state}
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.IPHandler do
|
|||
@moduledoc """
|
||||
Returns Origin IP
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"application", "json", []}, :get_json}], req, state}
|
||||
|
@ -24,6 +17,4 @@ defmodule HTTParrot.IPHandler do
|
|||
ip = :inet_parse.ntoa(ip) |> to_string
|
||||
[origin: ip] |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -57,6 +57,4 @@ defmodule HTTParrot.PHandler do
|
|||
defp response(info, body) do
|
||||
info ++ body |> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.RedirectHandler do
|
|||
@moduledoc """
|
||||
Redirects to the foo URL.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def malformed_request(req, state) do
|
||||
{n, req} = :cowboy_req.binding(:n, req)
|
||||
|
@ -29,6 +22,4 @@ defmodule HTTParrot.RedirectHandler do
|
|||
url = if n > 1, do: "/redirect/#{n-1}", else: "/get"
|
||||
{{true, host_url <> url}, req, nil}
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.RedirectToHandler do
|
|||
@moduledoc """
|
||||
Redirects to the foo URL.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def malformed_request(req, state) do
|
||||
{url, req} = :cowboy_req.qs_val("url", req, nil)
|
||||
|
@ -22,6 +15,4 @@ defmodule HTTParrot.RedirectToHandler do
|
|||
def moved_permanently(req, url) do
|
||||
{{true, url}, req, url}
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.RelativeRedirectHandler do
|
|||
@moduledoc """
|
||||
Redirects to the relative foo URL.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def malformed_request(req, state) do
|
||||
HTTParrot.RedirectHandler.malformed_request(req, state)
|
||||
|
@ -22,6 +15,4 @@ defmodule HTTParrot.RelativeRedirectHandler do
|
|||
url = if n > 1, do: "/redirect/#{n-1}", else: "/get"
|
||||
{{true, url}, req, nil}
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.RobotsHandler do
|
|||
@moduledoc """
|
||||
Returns a robots.txt.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"text", "plain", []}, :get_text}], req, state}
|
||||
|
@ -21,6 +14,4 @@ defmodule HTTParrot.RobotsHandler do
|
|||
"""
|
||||
|
||||
def get_text(req, state), do: {@robots, req, state}
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.SetCookiesHandler do
|
|||
@moduledoc """
|
||||
Sets one or more simple cookies.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def malformed_request(req, state) do
|
||||
{qs_vals, req} = :cowboy_req.qs_vals(req)
|
||||
|
@ -37,6 +30,4 @@ defmodule HTTParrot.SetCookiesHandler do
|
|||
defp set_cookie(name, value, req) do
|
||||
:cowboy_req.set_resp_cookie(name, value, [path: "/"], req)
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.StatusCodeHandler do
|
|||
@moduledoc """
|
||||
Returns given HTTP Status code.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{~W(GET HEAD OPTIONS), req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"application", "json", []}, :get_json}], req, state}
|
||||
|
@ -20,6 +13,4 @@ defmodule HTTParrot.StatusCodeHandler do
|
|||
{:ok, req} = :cowboy_req.reply(code, [], "", req)
|
||||
{:halt, req, state}
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -3,14 +3,7 @@ defmodule HTTParrot.StreamHandler do
|
|||
Returns GET data.
|
||||
"""
|
||||
alias HTTParrot.GeneralRequestInfo
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
@doc """
|
||||
`n` must be an integer between 1-100
|
||||
|
@ -41,6 +34,4 @@ defmodule HTTParrot.StreamHandler do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
|
@ -2,14 +2,7 @@ defmodule HTTParrot.UserAgentHandler do
|
|||
@moduledoc """
|
||||
Returns user-agent.
|
||||
"""
|
||||
|
||||
def init(_transport, _req, _opts) do
|
||||
{:upgrade, :protocol, :cowboy_rest}
|
||||
end
|
||||
|
||||
def allowed_methods(req, state) do
|
||||
{["GET"], req, state}
|
||||
end
|
||||
use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS)
|
||||
|
||||
def content_types_provided(req, state) do
|
||||
{[{{"application", "json", []}, :get_json}], req, state}
|
||||
|
@ -24,6 +17,4 @@ defmodule HTTParrot.UserAgentHandler do
|
|||
[{"user-agent", user_agent}]
|
||||
|> JSEX.encode!
|
||||
end
|
||||
|
||||
def terminate(_, _, _), do: :ok
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue