Skip to content

Bump HTTP compat to "1", drop support for Julia versions below 1.6 #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/jl_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
matrix:
jl_version: ["1.4", "1.6", "1.8"]
jl_version: ["1.6", "1.8"]
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
Expand Down
26 changes: 0 additions & 26 deletions CompatHelper.yml

This file was deleted.

6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ DashCoreComponents = "2.0.0"
DashHtmlComponents = "2.0.0"
DashTable = "5.0.0"
DataStructures = "0.17, 0.18"
HTTP = "0.8.10, 0.9"
HTTP = "1"
JSON = "0.21"
JSON3 = "1.9"
MD5 = "0.2"
PlotlyBase = "0.8.5, 0.8.6"
YAML = "0.4.7"
Requires = "1.3"
julia = "1.4"
YAML = "0.4.7"
julia = "1.6"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
3 changes: 1 addition & 2 deletions src/HttpHelpers/HttpHelpers.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module HttpHelpers

export state_handler, exception_handling_handler, compress_handler, Route, Router, add_route!
export state_handler, exception_handling_handler, compress_handler, Route, Router, add_route!, handle

import HTTP, CodecZlib

include("handlers.jl")
include("router.jl")

Expand Down
59 changes: 40 additions & 19 deletions src/HttpHelpers/handlers.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
struct RequestHandlerFunction <: HTTP.Handler
func::Function # func(req)
end
(x::RequestHandlerFunction)(args...) = x.func(args...)

function handle(h::RequestHandlerFunction, request::HTTP.Request, args...)
h(request, args...)
end

function handle(handler::Function, request::HTTP.Request, args...)
handler(request, args)
end

function handle(h::RequestHandlerFunction, request::HTTP.Request, state, args...)
h(request, state, args...)
end

function handle(handler::Function, request::HTTP.Request, state, args...)
handler(request, state, args)
end

function state_handler(base_handler, state)
return HTTP.RequestHandlerFunction(
function(request::HTTP.Request, args...)
response = HTTP.handle(base_handler, request, state, args...)
if response.status == 200
HTTP.defaultheader!(response, "Content-Type" => HTTP.sniff(response.body))
HTTP.defaultheader!(response, "Content-Length" => string(sizeof(response.body)))
end
return response
end
)
return RequestHandlerFunction(
function(request::HTTP.Request, args...)
response = handle(base_handler, request, state, args...)
if response.status == 200
HTTP.defaultheader!(response, "Content-Type" => HTTP.sniff(response.body))
HTTP.defaultheader!(response, "Content-Length" => string(sizeof(response.body)))
end
return response
end
)
end

state_handler(base_handler::Function, state) = state_handler(HTTP.RequestHandlerFunction(base_handler), state)
state_handler(base_handler::Function, state) = state_handler(RequestHandlerFunction(base_handler), state)

function check_mime(message::HTTP.Message, mime_list)
!HTTP.hasheader(message, "Content-Type") && return false
Expand All @@ -22,9 +43,9 @@ end

const default_compress_mimes = ["text/plain", "text/html", "text/css", "text/xml", "application/json", "application/javascript", "application/css"]
function compress_handler(base_handler; mime_types::Vector{String} = default_compress_mimes, compress_min_size = 500)
return HTTP.RequestHandlerFunction(
return RequestHandlerFunction(
function(request::HTTP.Request, args...)
response = HTTP.handle(base_handler, request, args...)
response = handle(base_handler, request, args...)
if response.status == 200 && sizeof(response.body) >= compress_min_size &&
occursin("gzip", HTTP.header(request, "Accept-Encoding", "")) && check_mime(response, mime_types)
HTTP.setheader(response, "Content-Encoding" => "gzip")
Expand All @@ -38,14 +59,14 @@ function compress_handler(base_handler; mime_types::Vector{String} = default_com
end

function compress_handler(base_handler::Function; mime_types::Vector{String} = default_compress_mimes, compress_min_size = 500)
return compress_handler(HTTP.RequestHandlerFunction(base_handler), mime_types = mime_types, compress_min_size = compress_min_size)
return compress_handler(RequestHandlerFunction(base_handler), mime_types = mime_types, compress_min_size = compress_min_size)
end

function exception_handling_handler(ex_handling_func, base_handler)
return HTTP.RequestHandlerFunction(
return RequestHandlerFunction(
function(request::HTTP.Request, args...)
try
return HTTP.handle(base_handler, request, args...)
return handle(base_handler, request, args...)
catch e
return ex_handling_func(e)
end
Expand All @@ -55,12 +76,12 @@ function exception_handling_handler(ex_handling_func, base_handler)
end

exception_handling_handler(ex_handling_func, base_handler::Function) =
exception_handling_handler(ex_handling_func, HTTP.RequestHandlerFunction(base_handler))
exception_handling_handler(ex_handling_func, RequestHandlerFunction(base_handler))

function request_logging_handler(base_handler; exclude = Regex[])
return HTTP.RequestHandlerFunction(
return RequestHandlerFunction(
function(request::HTTP.Request, args...)
response = HTTP.handle(base_handler, request, args...)
response = handle(base_handler, request, args...)

return response
end
Expand Down
2 changes: 1 addition & 1 deletion src/HttpHelpers/router.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ end
add_route!(handler::Function, router::Router, method, url) = add_route!(router, Route(handler, method, url))
add_route!(handler::Function, router::Router, url) = add_route!(handler, router, nothing, url)

function HTTP.handle(router::Router, request::HTTP.Request, args...)
function handle(router::Router, request::HTTP.Request, args...)
path = HTTP.URI(request.target).path
return handle(router.routes, path, request, args...)
end
2 changes: 1 addition & 1 deletion src/handler/make_handler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function make_handler(app::DashApp, registry::ResourcesRegistry; check_layout =

compile_request = HTTP.Request("GET", prefix)
HTTP.setheader(compile_request, "Accept-Encoding" => "gzip")
HTTP.handle(handler, compile_request) #For handler precompilation
handle(handler, compile_request) #For handler precompilation

get_devsetting(app, :hot_reload) && start_reload_poll(state)

Expand Down
2 changes: 1 addition & 1 deletion src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function run_server(app::DashApp,
start_server = () -> begin
handler = make_handler(app);
server = Sockets.listen(get_inetaddr(host, port))
task = @async HTTP.serve(handler, host, port; server = server, verbose = true)
task = @async HTTP.serve(handler, host, port; server = server)
return (server, task)
end

Expand Down
30 changes: 15 additions & 15 deletions test/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using Dash

handler = make_handler(app)
request = HTTP.Request("GET", "/_dash-dependencies")
resp = HTTP.handle(handler, request)
resp = Dash.HttpHelpers.handle(handler, request)
deps = JSON3.read(String(resp.body))
@test deps[1].prevent_initial_call == false
@test deps[2].prevent_initial_call == false
Expand All @@ -43,7 +43,7 @@ using Dash

handler = make_handler(app)
request = HTTP.Request("GET", "/_dash-dependencies")
resp = HTTP.handle(handler, request)
resp = Dash.HttpHelpers.handle(handler, request)
deps = JSON3.read(String(resp.body))
@test deps[1].prevent_initial_call == true
@test deps[2].prevent_initial_call == false
Expand All @@ -65,7 +65,7 @@ using Dash

handler = make_handler(app)
request = HTTP.Request("GET", "/_dash-dependencies")
resp = HTTP.handle(handler, request)
resp = Dash.HttpHelpers.handle(handler, request)
deps = JSON3.read(String(resp.body))
@test deps[1].prevent_initial_call == true
@test deps[2].prevent_initial_call == true
Expand All @@ -87,7 +87,7 @@ using Dash

handler = make_handler(app)
request = HTTP.Request("GET", "/_dash-dependencies")
resp = HTTP.handle(handler, request)
resp = Dash.HttpHelpers.handle(handler, request)
deps = JSON3.read(String(resp.body))
@test deps[1].prevent_initial_call == true
@test deps[2].prevent_initial_call == false
Expand All @@ -110,7 +110,7 @@ end

handler = make_handler(app)
request = HTTP.Request("GET", "/_dash-dependencies")
resp = HTTP.handle(handler, request)
resp = Dash.HttpHelpers.handle(handler, request)
deps = JSON3.read(String(resp.body))

@test length(deps) == 1
Expand All @@ -124,7 +124,7 @@ end
handler = Dash.make_handler(app)
test_json = """{"output":"my-div.children","changedPropIds":["my-id.value"],"inputs":[{"id":"my-id","property":"value","value":"test"}]}"""
request = HTTP.Request("POST", "/_dash-update-component", [], Vector{UInt8}(test_json))
response = HTTP.handle(handler, request)
response = Dash.HttpHelpers.handle(handler, request)
@test response.status == 200
resp_obj = JSON3.read(String(response.body))
@test in(:multi, keys(resp_obj))
Expand All @@ -149,7 +149,7 @@ end
handler = Dash.make_handler(app)
test_json = """{"output":"..my-div.children...my-div2.children..","changedPropIds":["my-id.value"],"inputs":[{"id":"my-id","property":"value","value":"test"}], "state":[{"id":"my-id","property":"type","value":"state"}]}"""
request = HTTP.Request("POST", "/_dash-update-component", [], Vector{UInt8}(test_json))
response = HTTP.handle(handler, request)
response = Dash.HttpHelpers.handle(handler, request)
@test response.status == 200
resp_obj = JSON3.read(String(response.body))
@test in(:multi, keys(resp_obj))
Expand All @@ -172,7 +172,7 @@ end
handler = Dash.make_handler(app)
test_json = """{"output":"..my-div.children..","changedPropIds":["my-id.value"],"inputs":[{"id":"my-id","property":"value","value":"test"}], "state":[{"id":"my-id","property":"type","value":"state"}]}"""
request = HTTP.Request("POST", "/_dash-update-component", [], Vector{UInt8}(test_json))
response = HTTP.handle(handler, request)
response = Dash.HttpHelpers.handle(handler, request)
@test response.status == 200
resp_obj = JSON3.read(String(response.body))
@test in(:multi, keys(resp_obj))
Expand Down Expand Up @@ -350,7 +350,7 @@ end
)
test_json = JSON3.write(request)
request = HTTP.Request("POST", "/_dash-update-component", [], Vector{UInt8}(test_json))
response = HTTP.handle(handler, request)
response = Dash.HttpHelpers.handle(handler, request)

@test response.status == 200

Expand Down Expand Up @@ -389,7 +389,7 @@ end
)
test_json = JSON3.write(request)
request = HTTP.Request("POST", "/_dash-update-component", [], Vector{UInt8}(test_json))
response = HTTP.handle(handler, request)
response = Dash.HttpHelpers.handle(handler, request)

@test response.status == 200
resp_obj = JSON3.read(String(response.body))
Expand Down Expand Up @@ -441,7 +441,7 @@ end
)
test_json = JSON3.write(request)
request = HTTP.Request("POST", "/_dash-update-component", [], Vector{UInt8}(test_json))
response = HTTP.handle(handler, request)
response = Dash.HttpHelpers.handle(handler, request)
@test response.status == 200
s = String(response.body)
resp_obj = JSON3.read(s)
Expand Down Expand Up @@ -494,7 +494,7 @@ end
)
test_json = JSON3.write(request)
request = HTTP.Request("POST", "/_dash-update-component", [], Vector{UInt8}(test_json))
response = HTTP.handle(handler, request)
response = Dash.HttpHelpers.handle(handler, request)
@test response.status == 200
resp_obj = JSON3.read(String(response.body))
@test in(:multi, keys(resp_obj))
Expand Down Expand Up @@ -597,7 +597,7 @@ end

handler = make_handler(app)
request = HTTP.Request("GET", "/_dash-dependencies")
resp = HTTP.handle(handler, request)
resp = Dash.HttpHelpers.handle(handler, request)
deps = JSON3.read(String(resp.body))

@test length(deps) == 1
Expand Down Expand Up @@ -640,7 +640,7 @@ end

handler = make_handler(app)
request = HTTP.Request("GET", "/_dash-dependencies")
resp = HTTP.handle(handler, request)
resp = Dash.HttpHelpers.handle(handler, request)
deps = JSON3.read(String(resp.body))

@test length(deps) == 1
Expand All @@ -652,7 +652,7 @@ end
@test cb.clientside_function.namespace == "_dashprivate_my-div"
@test cb.clientside_function.function_name == "children"
request = HTTP.Request("GET", "/")
resp = HTTP.handle(handler, request)
resp = Dash.HttpHelpers.handle(handler, request)
body = String(resp.body)
@test occursin("clientside[\"_dashprivate_my-div\"]", body)
@test occursin("ns[\"children\"]", body)
Expand Down
26 changes: 13 additions & 13 deletions test/config_functional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,55 +81,55 @@ end
app.layout = html_div()
handler = make_handler(app)
request = HTTP.Request("GET", "/assets/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 200
request = HTTP.Request("GET", "/assets/test3.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 404
request = HTTP.Request("GET", "/images/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test startswith(HTTP.header(res, "Content-Type"), "text/html")

app = dash(url_base_pathname = "/test/")
app.layout = html_div()
handler = make_handler(app)
request = HTTP.Request("GET", "/assets/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 404
request = HTTP.Request("GET", "/test/assets/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 200
request = HTTP.Request("GET", "/images/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 404

app = dash(assets_url_path = "ass")
app.layout = html_div()
handler = make_handler(app)
request = HTTP.Request("GET", "/ass/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 200
request = HTTP.Request("GET", "/ass/test3.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 404
request = HTTP.Request("GET", "/assets/test3.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test startswith(HTTP.header(res, "Content-Type"), "text/html")
request = HTTP.Request("GET", "/images/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test startswith(HTTP.header(res, "Content-Type"), "text/html")

app = dash(assets_folder = "images")
app.layout = html_div()
handler = make_handler(app)
request = HTTP.Request("GET", "/assets/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 404
request = HTTP.Request("GET", "/assets/test_images.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test res.status == 200
request = HTTP.Request("GET", "/images/test.png")
res = HTTP.handle(handler, request)
res = Dash.HttpHelpers.handle(handler, request)
@test startswith(HTTP.header(res, "Content-Type"), "text/html")
end

Expand Down
Loading