From 34a9a1a0178705fa2ebf154866ab8c2cb8d8bbb8 Mon Sep 17 00:00:00 2001 From: nixo Date: Sun, 25 Oct 2020 10:44:58 +0100 Subject: [PATCH] types, client: more utilities to have clients working --- src/types.jl | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/types.jl b/src/types.jl index 66a5eee..1f84f59 100644 --- a/src/types.jl +++ b/src/types.jl @@ -74,6 +74,14 @@ function Request(data::String) end end +function Request(protocol, host, port, path, query) + Request( + something(protocol, "gemini"), + something(host, "."), + something(port, 1965), + path, query, true, "") +end + "unescape(percent_encoded_uri)::String Replace %xx escaped chars with their single-character equivalent. @@ -107,6 +115,28 @@ function unescape(str)::String return String(unescaped[1:chars]) end +# FIXME: Is @ reserved? +# Are those the only one that are reserved? +const reserved_chars = ";/?:@&= " |> collect +isreserved(c::Char) = c in reserved_chars +function escape_uri(str) + len = length(str) + # If everything must be escaped, the string is three times longer + escaped = Vector{UInt8}(undef, len * 3) + pos = 1 + for char in str + if isreserved(char) + escaped[pos:pos+2] = string('%', string(Int(char), base=16, pad=2)) |> + collect + pos += 3 + else + escaped[pos] = char + pos += 1 + end + end + String(escaped[1:pos-1]) +end + import Base.show function show(io::IO, r::Request) # Useful for debugging @@ -115,7 +145,6 @@ function show(io::IO, r::Request) print(io, "[!invalid] ", String(r.raw)) return end - printstyled(io, r.protocol, color = :red) print(io, "://") printstyled(io, r.host, color = :black, bold = :true) @@ -127,7 +156,9 @@ function show(io::IO, r::Request) end if !isnothing(r.query) print(io, "?") - printstyled(io, r.query, color = :yellow) + # Escape it unless we are in a "terminal" + query = typeof(io) == IOBuffer ? escape_uri(r.query) : r.query + printstyled(io, query, color = :yellow) end end