From 3a4773c3c2bd0bbef244eb519b07208da9108e49 Mon Sep 17 00:00:00 2001 From: dcc Date: Sat, 2 Sep 2023 00:52:52 -0700 Subject: First --- lib/pleroma/web/xml.ex | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/pleroma/web/xml.ex (limited to 'lib/pleroma/web/xml.ex') diff --git a/lib/pleroma/web/xml.ex b/lib/pleroma/web/xml.ex new file mode 100644 index 0000000..380a80a --- /dev/null +++ b/lib/pleroma/web/xml.ex @@ -0,0 +1,48 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.XML do + require Logger + + def string_from_xpath(_, :error), do: nil + + def string_from_xpath(xpath, doc) do + try do + {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc) + + res = + res + |> to_string + |> String.trim() + + if res == "", do: nil, else: res + catch + _e -> + Logger.debug("Couldn't find xpath #{xpath} in XML doc") + nil + end + end + + def parse_document(text) do + try do + {doc, _rest} = + text + |> :binary.bin_to_list() + |> :xmerl_scan.string( + quiet: true, + fetch_fun: fn _, _ -> raise "Resolving external entities not supported" end + ) + + {:ok, doc} + rescue + _e -> + Logger.debug("Couldn't parse XML: #{inspect(text)}") + :error + catch + :exit, _error -> + Logger.debug("Couldn't parse XML: #{inspect(text)}") + :error + end + end +end -- cgit v1.2.3