forked from fonsp/Pluto.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π Support data URLs in open_url (fonsp#1670)
- Loading branch information
Showing
6 changed files
with
204 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
### A Pluto.jl notebook ### | ||
# v0.17.1 | ||
|
||
using Markdown | ||
using InteractiveUtils | ||
|
||
# βββ‘ cc180e7e-46c3-11ec-3fff-05e1b5c77986 | ||
md""" | ||
# Download Data URLs | ||
""" | ||
|
||
# βββ‘ 2385dd3b-15f8-4790-907f-e0576a56c4c0 | ||
random_data = rand(UInt8, 30) | ||
|
||
# βββ‘ d8ed6d44-33cd-4c9d-828b-d237d43769f5 | ||
# try | ||
# download("asdffads") | ||
# catch e | ||
# e | ||
# end |> typeof | ||
|
||
# βββ‘ e1610184-5d16-499b-883e-7ef92f402ebb | ||
function is_inside_pluto(m::Module) | ||
if isdefined(m, :PlutoForceDisplay) | ||
return m.PlutoForceDisplay | ||
else | ||
isdefined(m, :PlutoRunner) && parentmodule(m) == Main | ||
end | ||
end | ||
|
||
# βββ‘ b987a8a2-6ab0-4e88-af3c-d7f2778af657 | ||
begin | ||
if is_inside_pluto(@__MODULE__) | ||
import Pkg | ||
|
||
# create a local environment for this notebook | ||
# used to install and load PlutoTest | ||
local_env = mktempdir() | ||
Pkg.activate(local_env) | ||
Pkg.add(name="PlutoTest", version="0.2") | ||
pushfirst!(LOAD_PATH, local_env) | ||
|
||
# activate Pluto's environment, used to load HTTP.jl | ||
Pkg.activate(Base.current_project(@__FILE__)) | ||
using PlutoTest | ||
else | ||
if !isdefined(@__MODULE__, Symbol("@test")) | ||
macro test(e...) nothing; end | ||
macro test_throws(e...) nothing; end | ||
macro test_broken(e...) nothing; end | ||
macro testset(e...) nothing; end | ||
end | ||
end | ||
import HTTP.URIs | ||
import Base64 | ||
end | ||
|
||
# βββ‘ a85c0c0b-47d0-4377-bc22-3c87239a67b3 | ||
""" | ||
```julia | ||
download_cool(url::AbstractString, [path::AbstractString = tempname()]) -> path | ||
``` | ||
The same as [`Base.download`](@ref), but also supports [Data URLs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs). | ||
""" | ||
function download_cool(url::AbstractString, path::AbstractString=tempname()) | ||
if startswith(url, "data:") | ||
comma_index = findfirst(',', url) | ||
|
||
@assert comma_index isa Int "Invalid data URL." | ||
|
||
metadata_str = url[length("data:")+1:comma_index-1] | ||
metadata_parts = split(metadata_str, ';'; limit=2) | ||
|
||
if length(metadata_parts) == 2 | ||
@assert metadata_parts[2] == "base64" "Invalid data URL." | ||
end | ||
|
||
mime = MIME(first(metadata_parts)) | ||
is_base64 = length(metadata_parts) == 2 | ||
|
||
data_str = SubString(url, comma_index+1) | ||
|
||
data = is_base64 ? | ||
Base64.base64decode(data_str) : | ||
URIs.unescapeuri(data_str) | ||
|
||
write(path, data) | ||
path | ||
else | ||
download(url, path) | ||
end | ||
end | ||
|
||
# βββ‘ 6e1dd79c-a7bf-44d6-bfa6-ced75b45170a | ||
download_cool_string(args...) = read(download_cool(args...), String) | ||
|
||
# βββ‘ 3630b4bc-ff63-426d-b95d-ae4e4f9ccd88 | ||
download_cool_data(args...) = read(download_cool(args...)) | ||
|
||
# βββ‘ 6339496d-11be-40d0-b4e5-9247e5199367 | ||
@test download_cool_string("data:,Hello%2C%20World%21") == "Hello, World!" | ||
|
||
# βββ‘ bf7b4241-9cb0-4d90-9ded-b527bf220803 | ||
@test download_cool_string("data:text/plain,Hello%2C%20World%21") == "Hello, World!" | ||
|
||
# βββ‘ d6e01532-a8e4-4173-a270-eae37c8002c7 | ||
@test download_cool_string("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==") == "Hello, World!" | ||
|
||
# βββ‘ b0ba1add-f452-4a44-ab23-becbc610e2b9 | ||
@test download_cool_string("data:;base64,SGVsbG8sIFdvcmxkIQ==") == "Hello, World!" | ||
|
||
# βββ‘ e630e261-1c2d-4117-9c44-dd49199fa3de | ||
@test download_cool_string("data:,hello") == "hello" | ||
|
||
# βββ‘ 4bb75573-09bd-4ce7-b76f-34c0249d7b88 | ||
@test download_cool_string("data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E") == "<h1>Hello, World!</h1>" | ||
|
||
# βββ‘ 301eee81-7715-4d39-89aa-37bffde3557f | ||
@test download_cool_string("data:text/html,<script>alert('hi');</script>") == "<script>alert('hi');</script>" | ||
|
||
# βββ‘ ae296e09-08dd-4ee8-87ac-eb2bf24b28b9 | ||
random_data_url = "data:asf;base64,$( | ||
Base64.base64encode(random_data) | ||
)" | ||
|
||
# βββ‘ 2eabfa58-2d8f-4479-9c00-a58b934638d9 | ||
@test download_cool_data(random_data_url) == random_data | ||
|
||
# βββ‘ 525b2cb6-b7b9-436e-898e-a951e6a1f2f1 | ||
@test occursin("reactive", download_cool_string("https://raw.githubusercontent.com/fonsp/Pluto.jl/v0.17.1/README.md")) | ||
|
||
# βββ‘ 40b48818-e191-4509-85ad-b9ff745cd0cb | ||
@test_throws Exception download_cool("data:xoxo;base10,asdfasdfasdf") | ||
|
||
# βββ‘ 1f175fcd-8b94-4f13-a912-02a21c95f8ca | ||
@test_throws Exception download_cool("data:text/plain;base10,asdfasdfasdf") | ||
|
||
# βββ‘ a4f671e6-0e23-4753-9301-048b2ef505e3 | ||
@test_throws Exception download_cool("data:asdfasdfasdf") | ||
|
||
# βββ‘ Cell order: | ||
# ββcc180e7e-46c3-11ec-3fff-05e1b5c77986 | ||
# β βa85c0c0b-47d0-4377-bc22-3c87239a67b3 | ||
# β β6339496d-11be-40d0-b4e5-9247e5199367 | ||
# β βbf7b4241-9cb0-4d90-9ded-b527bf220803 | ||
# β βd6e01532-a8e4-4173-a270-eae37c8002c7 | ||
# β βb0ba1add-f452-4a44-ab23-becbc610e2b9 | ||
# β βe630e261-1c2d-4117-9c44-dd49199fa3de | ||
# β β4bb75573-09bd-4ce7-b76f-34c0249d7b88 | ||
# β β301eee81-7715-4d39-89aa-37bffde3557f | ||
# β β2385dd3b-15f8-4790-907f-e0576a56c4c0 | ||
# β βae296e09-08dd-4ee8-87ac-eb2bf24b28b9 | ||
# β β2eabfa58-2d8f-4479-9c00-a58b934638d9 | ||
# β β525b2cb6-b7b9-436e-898e-a951e6a1f2f1 | ||
# β β6e1dd79c-a7bf-44d6-bfa6-ced75b45170a | ||
# β β3630b4bc-ff63-426d-b95d-ae4e4f9ccd88 | ||
# β β40b48818-e191-4509-85ad-b9ff745cd0cb | ||
# β β1f175fcd-8b94-4f13-a912-02a21c95f8ca | ||
# β βa4f671e6-0e23-4753-9301-048b2ef505e3 | ||
# β βd8ed6d44-33cd-4c9d-828b-d237d43769f5 | ||
# ββe1610184-5d16-499b-883e-7ef92f402ebb | ||
# β βb987a8a2-6ab0-4e88-af3c-d7f2778af657 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters