Skip to content

Commit 35dae3f

Browse files
committed
fix: read request body correctly
1 parent d427677 commit 35dae3f

4 files changed

Lines changed: 45 additions & 7 deletions

File tree

rebar.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
{cowboy_swagger, {git, "https://github.com/emqx/cowboy_swagger", {tag, "2.7.0-emqx2"}}}
66
]}.
77

8+
{profiles, [
9+
{test, [
10+
{deps, [
11+
{hackney, {git, "https://github.com/emqx/hackney.git", {tag, "1.18.1-1"}}}
12+
]}
13+
]}
14+
]}.
15+
816
{plugins, [
917
{erlfmt, "1.6.0"}
1018
]}.

src/minirest_body.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ decoder(Request) ->
5858
end.
5959

6060
json_decoder(Request) ->
61-
{ok, Body, NRequest} = cowboy_req:read_body(Request),
61+
{ok, Body, NRequest} = binary_decoder(Request),
6262
try
6363
{ok, jsx:decode(Body, [return_maps]), NRequest}
6464
catch
@@ -107,12 +107,12 @@ read_body(Request, Acc) ->
107107
end.
108108

109109
binary_decoder(Request) ->
110-
binary_decoder(Request, <<>>).
110+
binary_decoder(Request, []).
111111

112112
binary_decoder(Request, Acc) ->
113113
case cowboy_req:read_body(Request) of
114-
{more, Bin, NewRequest} -> binary_decoder(NewRequest, <<Acc/binary, Bin/binary>>);
115-
{ok, Bin, DoneRequest} -> {ok, <<Acc/binary, Bin/binary>>, DoneRequest}
114+
{more, Bin, NewRequest} -> binary_decoder(NewRequest, [Bin | Acc]);
115+
{ok, Bin, DoneRequest} -> {ok, iolist_to_binary(lists:reverse([Bin | Acc])), DoneRequest}
116116
end.
117117

118118
%%==============================================================================================

test/minirest_handler_SUITE.erl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ all() ->
3131
t_qs_params,
3232
t_auth_meta_in_filter,
3333
t_auth_meta_in_handler,
34-
t_handler_meta_in_auth
34+
t_handler_meta_in_auth,
35+
t_post_large_body
3536
].
3637

3738
init_per_suite(Config) ->
3839
application:ensure_all_started(minirest),
40+
application:ensure_all_started(hackney),
3941
Config.
4042

4143
end_per_suite(_Config) ->
@@ -105,6 +107,15 @@ t_handler_meta_in_auth(_Config) ->
105107
httpc:request(address() ++ "/handler_meta_in_auth")
106108
).
107109

110+
t_post_large_body(_Config) ->
111+
Data100KB = iolist_to_binary([$s || _ <- lists:seq(1, 100_000)]),
112+
Data100MB = [Data100KB || _ <- lists:seq(1, 1000)],
113+
Json100MB = jsx:encode(Data100MB),
114+
URL = address() ++ "/post_large_body",
115+
Headers = [{<<"content-type">>, <<"application/json">>}],
116+
{ok, 200, _, Ref} = hackney:request(post, URL, Headers, Json100MB, []),
117+
?assertEqual({ok, <<"OK">>}, hackney:body(Ref)).
118+
108119
%%--------------------------------------------------------------------
109120
%% Helpers
110121
%%--------------------------------------------------------------------

test/minirest_test_handler.erl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
qs_params/2,
2929
auth_meta_in_filter/2,
3030
auth_meta_in_handler/2,
31-
handler_meta_in_auth/2
31+
handler_meta_in_auth/2,
32+
post_large_body/2
3233
]).
3334

3435
api_spec() ->
@@ -40,7 +41,8 @@ api_spec() ->
4041
qs_params(),
4142
auth_meta_in_filter(),
4243
auth_meta_in_handler(),
43-
handler_meta_in_auth()
44+
handler_meta_in_auth(),
45+
post_large_body()
4446
],
4547
[]
4648
}.
@@ -124,6 +126,20 @@ handler_meta_in_auth() ->
124126
},
125127
{"/handler_meta_in_auth", MetaData, handler_meta_in_auth}.
126128

129+
post_large_body() ->
130+
MetaData = #{
131+
post => #{
132+
description => "post large body",
133+
responses => text_plain_200_response(),
134+
security => [#{application => []}]
135+
}
136+
},
137+
{"/post_large_body", MetaData, post_large_body}.
138+
139+
%%--------------------------------------------------------------------
140+
%% Handlers
141+
%%--------------------------------------------------------------------
142+
127143
authorize1(_Req) ->
128144
{ok, #{message => <<"hello from authorize">>}}.
129145

@@ -157,6 +173,9 @@ auth_meta_in_handler(get, #{auth_meta := #{message := Message}}) ->
157173
handler_meta_in_auth(get, #{auth_meta := #{message := Message}}) ->
158174
{200, #{<<"content-type">> => <<"test/plain">>}, Message}.
159175

176+
post_large_body(post, #{body := _Body}) ->
177+
{200, #{<<"content-type">> => <<"test/plain">>}, <<"OK">>}.
178+
160179
%%--------------------------------------------------------------------
161180
%% Helpers
162181
%%--------------------------------------------------------------------

0 commit comments

Comments
 (0)