Skip to content

Commit b85baea

Browse files
bas0Nmergify[bot]
authored andcommitted
Fix UTF-8 encoding for trace file downloads
Set charset=utf-8 in Content-Type header to ensure proper display of Cyrillic and other Unicode characters in browsers. Fixes #13952 (cherry picked from commit 8795c5d)
1 parent 350ec81 commit b85baea

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

deps/rabbitmq_tracing/src/rabbit_tracing_wm_file.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ serve(ReqData, Context) ->
3434
Content = rabbit_tracing_util:apply_on_node(ReqData, Context,
3535
rabbit_tracing_wm_file,
3636
serve, [Name]),
37-
{Content, ReqData, Context}.
37+
ReqWithCharset = cowboy_req:set_resp_header(<<"content-type">>,
38+
<<"text/plain; charset=utf-8">>,
39+
ReqData),
40+
{Content, ReqWithCharset, Context}.
3841

3942
serve(Name) ->
4043
Path = rabbit_tracing_files:full_path(Name),

deps/rabbitmq_tracing/test/rabbit_tracing_SUITE.erl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ groups() ->
2424
[
2525
{non_parallel_tests, [], [
2626
tracing_test,
27-
tracing_validation_test
27+
tracing_validation_test,
28+
trace_file_content_type_test
2829
]}
2930
].
3031

@@ -128,6 +129,35 @@ tracing_validation_test(Config) ->
128129
http_delete(Config, Path, ?NO_CONTENT),
129130
ok.
130131

132+
trace_file_content_type_test(Config) ->
133+
case filelib:is_dir(?LOG_DIR) of
134+
true -> {ok, Files} = file:list_dir(?LOG_DIR),
135+
[ok = file:delete(?LOG_DIR ++ F) || F <- Files];
136+
_ -> ok
137+
end,
138+
139+
Args = #{format => <<"text">>,
140+
pattern => <<"#">>},
141+
http_put(Config, "/traces/%2f/test-charset", Args, ?CREATED),
142+
143+
Ch = rabbit_ct_client_helpers:open_channel(Config),
144+
amqp_channel:cast(Ch, #'basic.publish'{ exchange = <<"amq.topic">>,
145+
routing_key = <<"key">> },
146+
#amqp_msg{props = #'P_basic'{},
147+
payload = <<"Test message">>}),
148+
rabbit_ct_client_helpers:close_channel(Ch),
149+
150+
timer:sleep(100),
151+
152+
http_delete(Config, "/traces/%2f/test-charset", ?NO_CONTENT),
153+
{ok, {{_HTTP, 200, _}, Headers, _Body}} =
154+
req(Config, get, "/trace-files/test-charset.log", [auth_header("guest", "guest")]),
155+
ContentType = proplists:get_value("content-type", Headers),
156+
?assertEqual(match, re:run(ContentType, "text/plain", [{capture, none}])),
157+
?assertEqual(match, re:run(ContentType, "charset=utf-8", [{capture, none}])),
158+
http_delete(Config, "/trace-files/test-charset.log", ?NO_CONTENT),
159+
ok.
160+
131161
%%---------------------------------------------------------------------------
132162
%% TODO: Below is copied from rabbit_mgmt_test_http,
133163
%% should be moved to use rabbit_mgmt_test_util once rabbitmq_management

0 commit comments

Comments
 (0)