mirror of
https://github.com/edgurgel/httparrot
synced 2025-04-06 00:32:34 -04:00
29 lines
629 B
Elixir
29 lines
629 B
Elixir
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
|
|
|
|
def content_types_provided(req, state) do
|
|
{[{{"application", "json", []}, :get_json}], req, state}
|
|
end
|
|
|
|
def get_json(req, state) do
|
|
{user_agent, req} = :cowboy_req.header("user-agent", req, "null")
|
|
{response(user_agent), req, state}
|
|
end
|
|
|
|
defp response(user_agent) do
|
|
[{"user-agent", user_agent}]
|
|
|> JSEX.encode!
|
|
end
|
|
|
|
def terminate(_, _, _), do: :ok
|
|
end
|