Better handling of flush, short lines

This commit is contained in:
Carlos Martín Nieto
2013-05-28 17:57:45 +02:00
parent ecb7cdfc23
commit 75e3d5416c
2 changed files with 40 additions and 22 deletions

View File

@@ -15,17 +15,21 @@ line(Text) ->
-spec parse(iolist()) -> {{want | have, geef_oid()}, binary()}. -spec parse(iolist()) -> {{want | have, geef_oid()}, binary()}.
parse(In) -> parse(In) ->
{Len, Rest} = unpack(In), case unpack(In) of
case Len of {error, ebufs} ->
0 -> {error, ebufs};
flush; {0, Rest} ->
_ -> {flush, Rest};
parse_pkt(Rest, Len - 4) {Len, Rest} ->
parse_pkt(Rest, Len)
end. end.
-spec parse_request(iolist()) -> geef_request(). -spec parse_request(iolist()) -> geef_request().
parse_request(In) -> parse_request(In) ->
{_, Line} = unpack(In), case unpack(In) of
{error, ebufs} ->
{error, ebufs};
{_, Line} ->
%% Split it into request, host, rest (should be empty) %% Split it into request, host, rest (should be empty)
[S, H, _] = binary:split(Line, <<0>>, [global]), [S, H, _] = binary:split(Line, <<0>>, [global]),
case S of case S of
@@ -35,18 +39,24 @@ parse_request(In) ->
Service = receive_pack Service = receive_pack
end, end,
<<"host=", Host/binary>> = H, <<"host=", Host/binary>> = H,
#geef_request{service=Service, path=Path, host=Host}. {ok, #geef_request{service=Service, path=Path, host=Host}}
end.
-spec unpack(iolist()) -> {non_neg_integer(), binary()}. -spec unpack(iolist()) -> {error, ebufs} | {non_neg_integer(), binary()}.
unpack(In0) -> unpack(In0) ->
In = iolist_to_binary(In0), In = iolist_to_binary(In0),
<<BLen:4/binary, Rest/binary>> = In, <<BLen:4/binary, Rest/binary>> = In,
Len = binary_to_integer(BLen, 16), Len = binary_to_integer(BLen, 16),
{Len, Rest}. if Len == 0 ->
{0, Rest};
parse_pkt(In, Len) when size(In) < Len -> Len - 4 > size(Rest) ->
{error, ebufs}; {error, ebufs};
true ->
{Len - 4, Rest}
end.
parse_pkt(In, Len0) -> parse_pkt(In, Len0) ->
io:format("~p, ~p~n", [In, Len0]),
Len1 = Len0 - 5, % "want " | "have " Len1 = Len0 - 5, % "want " | "have "
case In of case In of
<<"want ", Sha:?SHA_LEN/binary, Rest0/binary>> -> <<"want ", Sha:?SHA_LEN/binary, Rest0/binary>> ->

View File

@@ -4,7 +4,9 @@
-include("src/geef_records.hrl"). -include("src/geef_records.hrl").
flush_test() -> flush_test() ->
geef_pkt:parse("0000"). Expected = {flush, <<>>},
Actual = geef_pkt:parse("0000"),
?assertEqual(Expected, Actual).
have_test() -> have_test() ->
Expected = {{want, geef_oid:parse("e17ca7f2d877acbf8b9a9a1cb4c243ca72e86463")}, <<>>}, Expected = {{want, geef_oid:parse("e17ca7f2d877acbf8b9a9a1cb4c243ca72e86463")}, <<>>},
@@ -14,5 +16,11 @@ have_test() ->
request_test() -> request_test() ->
Line = <<"0039git-upload-pack /schacon/gitbook.git\0host=example.com\0">>, Line = <<"0039git-upload-pack /schacon/gitbook.git\0host=example.com\0">>,
Expected = #geef_request{service=upload_pack, path= <<"/schacon/gitbook.git">>, host= <<"example.com">>}, Expected = #geef_request{service=upload_pack, path= <<"/schacon/gitbook.git">>, host= <<"example.com">>},
{ok, Actual} = geef_pkt:parse_request(Line),
?assertEqual(Expected, Actual).
short_test() ->
Line = <<"0039git-upload">>,
Expected = {error, ebufs},
Actual = geef_pkt:parse_request(Line), Actual = geef_pkt:parse_request(Line),
?assertEqual(Expected, Actual). ?assertEqual(Expected, Actual).