Skip to content

Commit 6ad2600

Browse files
authored
Merge pull request #32 from maciej-szlosarczyk/replace-xmerl
Replace xmerl with erlsom
2 parents 8d46c73 + 83aae9b commit 6ad2600

8 files changed

Lines changed: 44 additions & 38 deletions

File tree

apps/epp_proxy/src/epp_xml.erl

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,40 @@
22

33
-export([find_cltrid/1, get_command/1, parse/1]).
44

5-
-include_lib("xmerl/include/xmerl.hrl").
6-
7-
%% Get command from an xmlElement. Otherwise return undefined.
8-
get_command(Record)
9-
when is_record(Record, xmlElement) ->
10-
case xmerl_xpath:string("name(/epp/*[1])", Record) of
11-
{xmlObj, string, "hello"} -> "hello";
12-
{xmlObj, string, "command"} -> get_command1(Record);
13-
{xmlObj, string, []} -> undefined
14-
end;
5+
%% We are only interested in start element of a kind. The list produced
6+
%% by this will need reversing after it is complete.
7+
%% This parsing is naive, expects command/hello element to come right
8+
%% after epp, but this should be everything we need for the purpose.
9+
-define(PARSER_FUN,
10+
fun (Event, Acc) ->
11+
case Event of
12+
{startElement, _, Name, _, _} -> [Name | Acc];
13+
_ -> Acc
14+
end
15+
end).
16+
17+
%% Get command a list of elements found by erlsom.
18+
%% Otherwise return undefined.
19+
get_command(["epp", "command", Command | _Rest]) ->
20+
Command;
21+
get_command(["epp", "hello" | _Rest]) -> "hello";
1522
get_command(_) -> undefined.
1623

17-
get_command1(Record)
18-
when is_record(Record, xmlElement) ->
19-
case xmerl_xpath:string("name(/epp/command/*[1])",
20-
Record)
21-
of
22-
{xmlObj, string, []} -> undefined;
23-
{xmlObj, string, Command} -> Command
24-
end.
25-
2624
%% xml_erl expects a list of characters, so let's give it to it.
2725
%% Otherwise return an error tuple.
2826
parse(Text) when is_list(Text) -> parse_list(Text);
2927
parse(Text) when is_binary(Text) ->
3028
List = binary_to_list(Text), parse_list(List);
3129
parse(_) -> {error, {fatal, {expected_binary_or_list}}}.
3230

33-
%% Parse a record that came from the wire and return a xmlElement record.
3431
parse_list(List) when is_list(List) ->
35-
try xmerl_scan:string(List, [{quiet, true}]) of
36-
{Record, []} when is_record(Record, xmlElement) ->
37-
{ok, Record}
32+
try erlsom:parse_sax(List, [], ?PARSER_FUN) of
33+
{ok, Result, _} ->
34+
ProperResult = lists:reverse(Result), {ok, ProperResult}
3835
catch
39-
exit:X -> {error, X}
36+
{error, Error} -> {error, Error};
37+
error:Error -> {error, Error};
38+
Error -> {error, Error}
4039
end.
4140

4241
%% The idea is that even when XML command is invalid,

apps/epp_proxy/test/epp_util_SUITE.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,5 @@ readable_ip_test_case(_Config) ->
7474
path_for_file_test_case(_Config) ->
7575
AbsoluteFilename = "/usr/bin",
7676
AbsoluteFilename = epp_util:path_for_file(AbsoluteFilename),
77-
RelativeFilename = "usr/bin",
7877
true = (AbsoluteFilename =:= epp_util:path_for_file(AbsoluteFilename)),
7978
ok.

apps/epp_proxy/test/epp_xml_SUITE.erl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
-module(epp_xml_SUITE).
22

33
-include_lib("common_test/include/ct.hrl").
4-
5-
%% This is required for parse tests.
6-
-include_lib("xmerl/include/xmerl.hrl").
4+
-include_lib("stdlib/include/assert.hrl").
75

86
-define(sampleCommandList,
97
"<epp>
@@ -59,24 +57,28 @@ parse_not_a_list_or_binary_test_case(_Config) ->
5957

6058
parse_sample_valid_xml_list_test_case(_Config) ->
6159
Input = ?sampleCommandList,
62-
{ok, Record} = epp_xml:parse(Input),
63-
true = is_record(Record, xmlElement),
60+
{ok, Result} = epp_xml:parse(Input),
61+
?assertEqual(["epp", "command", "login", "clID", "pw", "clTRID"],
62+
Result),
6463
ok.
6564

6665
parse_sample_valid_xml_binary_test_case(_Config) ->
6766
Input = list_to_binary(?sampleCommandList),
68-
{ok, Record} = epp_xml:parse(Input),
69-
true = is_record(Record, xmlElement),
67+
{ok, Result} = epp_xml:parse(Input),
68+
?assertEqual(["epp", "command", "login", "clID", "pw", "clTRID"],
69+
Result),
7070
ok.
7171

7272
parse_sample_invalid_xml_list_test_case(_Config) ->
7373
Input = "Some text",
74-
{error, {fatal, _Details}} = epp_xml:parse(Input),
74+
ExpectedResult = {error, "Malformed: Illegal character in prolog"},
75+
?assertEqual(ExpectedResult, epp_xml:parse(Input)),
7576
ok.
7677

7778
parse_sample_invalid_xml_binary_test_case(_Config) ->
78-
Input = list_to_binary("Some text"),
79-
{error, {fatal, _Details}} = epp_xml:parse(Input),
79+
Input = <<"</epp>\n">>,
80+
ExpectedResult = {error, {badmatch, []}},
81+
?assertEqual(ExpectedResult, epp_xml:parse(Input)),
8082
ok.
8183

8284
%% find_cltrid

config/docker.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%% -*- erlang -*-
12
[
23
{epp_proxy, [
34
{dev_mode, true},

config/sys.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%% -*- erlang -*-
12
[
23
{epp_proxy, [
34
%% Enables or disable TCP connections without TLS (true/false)
@@ -9,7 +10,7 @@
910
{tls_port, 700},
1011
%% When set to true, you can connect to EPP over HTTPS endpoints without
1112
%% verifying their TLS certificates.
12-
{insecure, false}
13+
{insecure, false},
1314
%% URL of EPP endpoints. Can be pointed at a web server (Apache/NGINX)
1415
%% Can contain port (https://some-host:3000/epp/session)
1516
%% Honors the prepended protocol (http / https).

config/test.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%% -*- erlang -*-
12
[
23
{epp_proxy, [{dev_mode, true},
34
{tcp_port, 1180},

rebar.config

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
{deps, [{hackney, "1.15.1"},
44
{syslog, {git, "https://github.com/Vagabond/erlang-syslog.git", {branch, "master"}}},
55
{lager, "3.7.0"},
6-
{lager_syslog, {git, "https://github.com/erlang-lager/lager_syslog.git"}}]}.
6+
{lager_syslog, {git, "https://github.com/erlang-lager/lager_syslog.git"}},
7+
{erlsom, "1.5.0"}]}.
78

89
{relx, [{release, {epp_proxy, "git"},
910
[epp_proxy,
1011
lager,
1112
lager_syslog,
1213
hackney,
1314
sasl,
14-
xmerl]},
15+
erlsom]},
1516

1617
{sys_config, "./config/sys.config"},
1718
{dev_mode, false},

rebar.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{"1.1.0",
22
[{<<"certifi">>,{pkg,<<"certifi">>,<<"2.5.1">>},1},
3+
{<<"erlsom">>,{pkg,<<"erlsom">>,<<"1.5.0">>},0},
34
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.9">>},1},
45
{<<"hackney">>,{pkg,<<"hackney">>,<<"1.15.1">>},0},
56
{<<"idna">>,{pkg,<<"idna">>,<<"6.0.0">>},1},
@@ -20,6 +21,7 @@
2021
[
2122
{pkg_hash,[
2223
{<<"certifi">>, <<"867CE347F7C7D78563450A18A6A28A8090331E77FA02380B4A21962A65D36EE5">>},
24+
{<<"erlsom">>, <<"C5A5CDD0EE0E8DCA62BCC4B13FF08DA24FDEFC16CCD8B25282A2FDA2BA1BE24A">>},
2325
{<<"goldrush">>, <<"F06E5D5F1277DA5C413E84D5A2924174182FB108DABB39D5EC548B27424CD106">>},
2426
{<<"hackney">>, <<"9F8F471C844B8CE395F7B6D8398139E26DDCA9EBC171A8B91342EE15A19963F4">>},
2527
{<<"idna">>, <<"689C46CBCDF3524C44D5F3DDE8001F364CD7608A99556D8FBD8239A5798D4C10">>},

0 commit comments

Comments
 (0)