diff --git a/README.md b/README.md index 6320766..4ba0bc0 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ HTTP server built on top of Cowboy using (mostly) `cowboy_rest` handlers to serv ## TODO * [ ] /deflate Returns deflate-encoded data. -* [ ] /response-headers?key=val Returns given response headers. * [ ] /digest-auth/:qop/:user/:passwd Challenges HTTP Digest Auth. ## License diff --git a/lib/httparrot.ex b/lib/httparrot.ex index 9db1ace..0921f41 100644 --- a/lib/httparrot.ex +++ b/lib/httparrot.ex @@ -31,7 +31,8 @@ defmodule HTTParrot do {'/robots.txt', HTTParrot.RobotsHandler, []}, {'/base64/:value', HTTParrot.Base64Handler, []}, {'/image', HTTParrot.ImageHandler, []}, - {'/websocket', HTTParrot.WebsocketHandler, []} ] } + {'/websocket', HTTParrot.WebsocketHandler, []}, + {'/response-headers', HTTParrot.ResponseHeadersHandler, []} ] } ]) {:ok, http_port} = Application.fetch_env(:httparrot, :http_port) diff --git a/lib/httparrot/response_headers_handler.ex b/lib/httparrot/response_headers_handler.ex new file mode 100644 index 0000000..b638927 --- /dev/null +++ b/lib/httparrot/response_headers_handler.ex @@ -0,0 +1,30 @@ +defmodule HTTParrot.ResponseHeadersHandler do + @moduledoc """ + Returns given response headers. + """ + use HTTParrot.Cowboy, methods: ~w(GET HEAD OPTIONS) + + def malformed_request(req, state) do + {qs_vals, req} = :cowboy_req.qs_vals(req) + if not Enum.empty?(qs_vals) do + {false, req, qs_vals} + else + {true, req, state} + end + end + + def content_types_provided(req, state) do + {[{{"application", "json", []}, :get_json}], req, state} + end + + def get_json(req, qs_vals) do + req = Enum.reduce qs_vals, req, fn({key, value}, req) -> + :cowboy_req.set_resp_header(key, value, req) + end + {response(qs_vals), req, qs_vals} + end + + defp response(qs_vals) do + qs_vals |> JSX.encode! + end +end diff --git a/priv/index.html b/priv/index.html index 42f9218..a67d007 100644 --- a/priv/index.html +++ b/priv/index.html @@ -65,6 +65,7 @@
/ip
Returns Origin IP./user-agent
Returns user-agent./headers
Returns header dict./response-headers
Returns given response headers./get
Returns GET data./post
Returns POST data./put
Returns PUT data.