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