Parse gemini response
This commit is contained in:
parent
6e9af773a8
commit
56fb4fe38c
|
@ -64,7 +64,13 @@ defmodule Chronoscope.Gemini.ConnectionClient do
|
||||||
defp parse_response(response, peercert) do
|
defp parse_response(response, peercert) do
|
||||||
response
|
response
|
||||||
|> Response.parse()
|
|> Response.parse()
|
||||||
|> Map.put(:cert_expiration, Certificate.expiration_date(peercert))
|
|
||||||
|> then(&{:ok, &1})
|
case Response.parse(response) do
|
||||||
|
{:ok, result} ->
|
||||||
|
{:ok, Map.put(result, :cert_expiration, Certificate.expiration_date(peercert))}
|
||||||
|
|
||||||
|
{:error, error} ->
|
||||||
|
{:error, error}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,26 @@
|
||||||
defmodule Chronoscope.Gemini.Response do
|
defmodule Chronoscope.Gemini.Response do
|
||||||
|
@response_pattern ~r/^(?<status>[0-9]{2}) (?<mime_type>.+)\r\n(?<body>(?s:.)*)/
|
||||||
|
|
||||||
def parse(response) do
|
def parse(response) do
|
||||||
# TODO
|
response
|
||||||
%{status: 20, mime_type: "text/gemini", body: to_string(response)}
|
|> to_string()
|
||||||
|
|> parse_response()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_response(response) do
|
||||||
|
case Regex.named_captures(@response_pattern, response) do
|
||||||
|
nil ->
|
||||||
|
{:error, "bad response: #{response}"}
|
||||||
|
|
||||||
|
match ->
|
||||||
|
match
|
||||||
|
|> Map.update!("status", &String.to_integer/1)
|
||||||
|
|> to_atom_keys()
|
||||||
|
|> then(&{:ok, &1})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp to_atom_keys(response) do
|
||||||
|
Map.new(response, fn {k, v} -> {String.to_atom(k), v} end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -61,7 +61,7 @@ defmodule ChronoscopeWeb.API.V1.Gemini.ConnectionController do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_response(response) do
|
defp format_response(response) do
|
||||||
Map.take(response, [:status, :mime_type, :body])
|
Map.take(response, [:status, :mime_type, :body, :cert_expiration])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp bad_request_response(conn, message) do
|
defp bad_request_response(conn, message) do
|
||||||
|
|
|
@ -53,12 +53,13 @@ defmodule Chronoscope.Gemini.ClientTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
test ":connect - not cached" do
|
test ":connect - not cached" do
|
||||||
|
response ="20 text/gemini\r\nHello!" |> to_charlist()
|
||||||
peercert = peercert()
|
peercert = peercert()
|
||||||
peercert_expiration = Certificate.expiration_date(peercert)
|
peercert_expiration = Certificate.expiration_date(peercert)
|
||||||
|
|
||||||
SSLMock
|
SSLMock
|
||||||
|> expect(:connect, fn ~c"localhost", 4444, _, _ -> {:ok, :socket} end)
|
|> expect(:connect, fn ~c"localhost", 4444, _, _ -> {:ok, :socket} end)
|
||||||
|> expect(:send, fn :socket, _ -> send_ssl_response([]) end)
|
|> expect(:send, fn :socket, _ -> send_ssl_response(response) end)
|
||||||
|> expect(:peercert, fn :socket -> {:ok, peercert} end)
|
|> expect(:peercert, fn :socket -> {:ok, peercert} end)
|
||||||
|> expect(:close, fn :socket -> :ok end)
|
|> expect(:close, fn :socket -> :ok end)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue