@@ -62,35 +62,64 @@ function process_callback(app::DashApp, body::String)
62
62
63
63
end
64
64
65
- function process_assets (app:: DashApp , path)
65
+ function make_response (status, headers, body, compress:: Bool )
66
+ message_headers = headers
67
+
68
+ if compress
69
+ push! (message_headers, " Content-Encoding" => " gzip" )
70
+ body = transcode (CodecZlib. GzipCompressor, body)
71
+ end
72
+
73
+ response = HTTP. Response (status, body)
74
+ for header in message_headers
75
+ HTTP. Messages. setheader (response, header)
76
+ end
77
+ return response
78
+ end
79
+
80
+ function process_assets (app:: DashApp , path, compress:: Bool )
66
81
assets_path = " $(app. config. routes_pathname_prefix) " * strip (app. config. assets_url_path, ' /' ) * " /"
67
-
68
- filename = joinpath (app . config . assets_folder, replace (path, assets_path => " " ))
82
+ filename = joinpath (app . config . assets_folder, replace (path, assets_path => " " ))
83
+
69
84
try
70
- return HTTP. Response (200 , [], body = read (filename))
85
+ file_contents = read (filename)
86
+ mimetype = HTTP. sniff (file_contents)
87
+ use_gzip = compress && occursin (r" text|javascript" , mimetype)
88
+ headers = [" Content-Type" => mimetype]
89
+ return make_response (200 , headers, file_contents, use_gzip)
71
90
catch
72
91
return HTTP. Response (404 )
73
92
end
74
93
end
75
94
76
-
77
95
function make_handler (app:: DashApp ; debug:: Bool = false )
78
96
index_string:: String = index_page (app, debug = debug)
79
97
80
98
return function (req:: HTTP.Request )
99
+ body:: Union{Nothing, String} = nothing
81
100
uri = HTTP. URI (req. target)
101
+
102
+ # verify that the client accepts compression
103
+ accepts_gz = occursin (" gzip" , HTTP. header (req, " Accept-Encoding" ))
104
+ # verify that the server was not launched with compress=false
105
+ with_gzip = accepts_gz && app. config. compress
106
+
107
+ headers = []
108
+
82
109
ComponentPackages. @register_js_sources (uri. path, app. config. routes_pathname_prefix)
83
110
if uri. path == " $(app. config. routes_pathname_prefix) "
84
- return HTTP . Response ( 200 , index_string)
111
+ body = index_page (app, debug = debug)
85
112
end
86
113
if uri. path == " $(app. config. routes_pathname_prefix) _dash-layout"
87
- return HTTP. Response (200 , [" Content-Type" => " application/json" ], body = JSON2. write (app. layout))
114
+ body = JSON2. write (app. layout)
115
+ push! (headers, " Content-Type" => " application/json" )
88
116
end
89
117
if uri. path == " $(app. config. routes_pathname_prefix) _dash-dependencies"
90
- return HTTP. Response (200 , [" Content-Type" => " application/json" ], body = dependencies_json (app))
118
+ body = dependencies_json (app)
119
+ push! (headers, " Content-Type" => " application/json" )
91
120
end
92
121
if startswith (uri. path, " $(app. config. routes_pathname_prefix) assets/" )
93
- return process_assets (app, uri. path)
122
+ return process_assets (app, uri. path, with_gzip )
94
123
end
95
124
if uri. path == " $(app. config. routes_pathname_prefix) _dash-update-component" && req. method == " POST"
96
125
try
@@ -107,7 +136,9 @@ function make_handler(app::DashApp; debug::Bool = false)
107
136
end
108
137
end
109
138
end
139
+ if ! isnothing (body)
140
+ return make_response (200 , headers, body, with_gzip)
141
+ end
110
142
return HTTP. Response (404 )
111
- end
112
-
113
- end
143
+ end
144
+ end
0 commit comments