More resilient xml parsing.

This commit is contained in:
Roger Braun 2017-06-24 14:35:32 +02:00
parent 6935fc3e01
commit 39bacba280
3 changed files with 15 additions and 5 deletions

View file

@ -30,6 +30,7 @@ def handle_incoming(xml_string) do
activities = Enum.map(entries, fn (entry) -> activities = Enum.map(entries, fn (entry) ->
{:xmlObj, :string, object_type} = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry) {:xmlObj, :string, object_type} = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
{:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry) {:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
Logger.debug("Handling #{verb}")
case verb do case verb do
'http://activitystrea.ms/schema/1.0/follow' -> 'http://activitystrea.ms/schema/1.0/follow' ->

View file

@ -98,7 +98,7 @@ def finger(account, getter \\ &@httpoison.get/3) do
end end
with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response, with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response,
doc <- XML.parse_document(body), doc when doc != :error<- XML.parse_document(body),
{:ok, data} <- webfinger_from_xml(doc) do {:ok, data} <- webfinger_from_xml(doc) do
{:ok, data} {:ok, data}
else else

View file

@ -1,4 +1,7 @@
defmodule Pleroma.Web.XML do defmodule Pleroma.Web.XML do
require Logger
def string_from_xpath(xpath, :error), do: nil
def string_from_xpath(xpath, doc) do def string_from_xpath(xpath, doc) do
{:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc) {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
@ -10,10 +13,16 @@ def string_from_xpath(xpath, doc) do
end end
def parse_document(text) do def parse_document(text) do
try do
{doc, _rest} = text {doc, _rest} = text
|> :binary.bin_to_list |> :binary.bin_to_list
|> :xmerl_scan.string |> :xmerl_scan.string
doc doc
catch
:exit, error ->
Logger.debug("Couldn't parse xml: #{inspect(text)}")
:error
end
end end
end end