Skip to content

Commit

Permalink
=most things fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox authored and quinnj committed Sep 7, 2018
1 parent 5209c35 commit b0dd3de
Show file tree
Hide file tree
Showing 26 changed files with 89 additions and 149 deletions.
5 changes: 3 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ MbedTLS = "v0.6.0"

[extras]
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
XMLDict = "228000da-037f-5747-90a9-8195ccbf91a5"
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "JSON", "XMLDict"]
test = ["Test", "JSON", "XMLDict", "Distributed"]
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.6.3
MbedTLS 0.5.11
julia 0.7
MbedTLS 0.6
IniFile
6 changes: 3 additions & 3 deletions src/ConnectionPool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export Connection, Transaction,

using ..IOExtras, ..Sockets

import ..ByteView, ..bytesavailable, ..Nothing
import ..ByteView
import ..@debug, ..@debugshow, ..DEBUG_LEVEL, ..taskid
import ..@require, ..precondition_error, ..@ensure, ..postcondition_error
using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake!
Expand Down Expand Up @@ -156,8 +156,8 @@ function Base.eof(t::Transaction)
return eof(t.c.io)
end

bytesavailable(t::Transaction) = bytesavailable(t.c)
bytesavailable(c::Connection) =
Base.bytesavailable(t::Transaction) = bytesavailable(t.c)
Base.bytesavailable(c::Connection) =
!isempty(c.excess) ? length(c.excess) : bytesavailable(c.io)

Base.isreadable(t::Transaction) = t.c.readbusy && t.c.readcount == t.sequence
Expand Down
2 changes: 1 addition & 1 deletion src/DebugRequest.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module DebugRequest

import ..Layer, ..request, ..compat_stdout
import ..Layer, ..request
using ..IOExtras
import ..ConnectionPool: ByteView, byteview

Expand Down
12 changes: 4 additions & 8 deletions src/IODebug.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import ..debug_header
io::T
end

logwrite(iod::IODebug, x) = show_io_debug(compat_stdout(), "➡️ ", x)
logread(iod::IODebug, x) = show_io_debug(compat_stdout(), "⬅️ ", x)
logunread(iod::IODebug, x) = show_io_debug(compat_stdout(), "♻️ ", x)
logwrite(iod::IODebug, x) = show_io_debug(stdout, "➡️ ", x)
logread(iod::IODebug, x) = show_io_debug(stdout, "⬅️ ", x)
logunread(iod::IODebug, x) = show_io_debug(stdout, "♻️ ", x)

else

Expand Down Expand Up @@ -57,11 +57,7 @@ IOExtras.startwrite(iod::IODebug) = startwrite(iod.io)
IOExtras.closeread(iod::IODebug) = closeread(iod.io)
IOExtras.closewrite(iod::IODebug) = closewrite(iod.io)

@static if isdefined(Base, :bytesavailable)
Base.bytesavailable(iod::IODebug) = bytesavailable(iod.io)
else
Base.nb_available(iod::IODebug) = nb_available(iod.io)
end
Base.bytesavailable(iod::IODebug) = bytesavailable(iod.io)

Base.show(io::IO, iod::IODebug) = show(io, iod.io)

Expand Down
16 changes: 6 additions & 10 deletions src/IOExtras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ Get a `Vector{UInt8}`, a vector of bytes of a string.
"""
function bytes end
bytes(s::SubArray{UInt8}) = unsafe_wrap(Array, pointer(s), length(s))
if !isdefined(Base, :CodeUnits)
const CodeUnits = Vector{UInt8}
bytes(s::String) = Vector{UInt8}(s)
bytes(s::SubString{String}) = unsafe_wrap(Array, pointer(s), length(s))
else
const CodeUnits = Union{Vector{UInt8}, Base.CodeUnits}
bytes(s::Base.CodeUnits) = bytes(String(s))
bytes(s::String) = codeunits(s)
bytes(s::SubString{String}) = codeunits(s)
end

const CodeUnits = Union{Vector{UInt8}, Base.CodeUnits}
bytes(s::Base.CodeUnits) = bytes(String(s))
bytes(s::String) = codeunits(s)
bytes(s::SubString{String}) = codeunits(s)

bytes(s::Vector{UInt8}) = s

"""
Expand Down
2 changes: 1 addition & 1 deletion src/MessageRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module MessageRequest

export body_is_a_stream, body_was_streamed, setuseragent!

import ..bytesavailable

import ..Layer, ..request
using ..IOExtras
using ..URIs
Expand Down
2 changes: 1 addition & 1 deletion src/Messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import ..@warn
using ..IOExtras
using ..Parsers
import ..@require, ..precondition_error
import ..bytes, ..bytesavailable
import ..bytes

const unknown_length = typemax(Int)

Expand Down
3 changes: 0 additions & 3 deletions src/Pairs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module Pairs

export defaultbyfirst, setbyfirst, getbyfirst, setkv, getkv, rmkv

import ..compat_findfirst
const findfirst = compat_findfirst

"""
setbyfirst(collection, item) -> item
Expand Down
4 changes: 1 addition & 3 deletions src/Parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ export Header, Headers,

include("parseutils.jl")

import ..compat_replace

const emptyss = SubString("",1,0)
const emptyheader = emptyss => emptyss
const Header = Pair{SubString{String},SubString{String}}
Expand Down Expand Up @@ -191,7 +189,7 @@ function parse_header_field(bytes::SubString{String})::Tuple{Header,SubString{St
# Finally look for obsolete line folding format:
re = obs_fold_header_field_regex
if exec(re, bytes)
unfold = SubString(strip(compat_replace(group(2, re, bytes), r"\r?\n"=>"")))
unfold = SubString(strip(replace(group(2, re, bytes), r"\r?\n"=>"")))
return (group(1, re, bytes) => unfold), nextbytes(re, bytes)
end

Expand Down
23 changes: 8 additions & 15 deletions src/Servers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ using ..Messages
using ..Parsers
using ..ConnectionPool
using ..Sockets
import ..@info, ..@warn, ..@error, ..@debug, ..@debugshow, ..DEBUG_LEVEL, ..compat_stdout
import ..@info, ..@warn, ..@error, ..@debug, ..@debugshow, ..DEBUG_LEVEL, ..stdout
using MbedTLS: SSLConfig, SSLContext, setup!, associate!, hostname!, handshake!

if !isdefined(Base, :Nothing)
const Nothing = Void
const Cvoid = Void
end

import ..Dates

@static if !isdefined(Base, :Distributed)
using Distributed
end
import Dates
using Distributed

using ..HTTP, ..Handlers

Expand Down Expand Up @@ -97,7 +90,7 @@ mutable struct Server{T <: Scheme, H <: HTTP.Handler}
out::Channel{Any}
options::ServerOptions

Server{T, H}(handler::H, logger::IO=compat_stdout(), ch=Channel(1), ch2=Channel(1),
Server{T, H}(handler::H, logger::IO=stdout, ch=Channel(1), ch2=Channel(1),
options=ServerOptions()) where {T, H} =
new{T, H}(handler, logger, ch, ch2, options)
end
Expand Down Expand Up @@ -180,9 +173,9 @@ serve(server::Server, host::AbstractString, port::Integer; verbose::Bool=true) =
serve(server::Server, host::Union{Sockets.InetAddr, AbstractString}; verbose::Bool=true) =
serve(server, host, verbose)

Server(h::Function, l::IO=compat_stdout(); cert::String="", key::String="", args...) = Server(HTTP.HandlerFunction(h), l; cert=cert, key=key, args...)
Server(h::Function, l::IO=stdout; cert::String="", key::String="", args...) = Server(HTTP.HandlerFunction(h), l; cert=cert, key=key, args...)
function Server(handler::H=HTTP.HandlerFunction(req -> HTTP.Response(200, "Hello World!")),
logger::IO=compat_stdout(),
logger::IO=stdout,
;
cert::String="",
key::String="",
Expand Down Expand Up @@ -214,7 +207,7 @@ serve(host::AbstractString, port::Integer, args...; kwargs...) = serve(parse(IPA
serve(host::AbstractString, args...; kwargs...) = serve(String(host), args...; kwargs...)
function serve(host::Union{Sockets.InetAddr, String},
handler=req -> HTTP.Response(200, "Hello World!"),
logger::IO=compat_stdout(),
logger::IO=stdout,
;
verbose::Bool=true,
args...)
Expand All @@ -224,7 +217,7 @@ end
serve(; host::IPAddr=Sockets.localhost,
port::Integer=8081,
handler=req -> HTTP.Response(200, "Hello World!"),
logger::IO=compat_stdout(),
logger::IO=stdout,
args...) =
serve(host, port, handler, logger; args...)

Expand Down
3 changes: 1 addition & 2 deletions src/Streams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import ..HTTP
using ..Sockets
using ..IOExtras
using ..Messages
import ..bytesavailable, ..compat_string
import ..ByteView
import ..Messages: header, hasheader, setheader,
writeheaders, writestartline
Expand Down Expand Up @@ -94,7 +93,7 @@ function Base.unsafe_write(http::Stream, p::Ptr{UInt8}, n::UInt)
if !http.writechunked
return unsafe_write(http.stream, p, n)
end
return write(http.stream, compat_string(n, base=16), "\r\n") +
return write(http.stream, string(n, base=16), "\r\n") +
unsafe_write(http.stream, p, n) +
write(http.stream, "\r\n")
end
Expand Down
12 changes: 6 additions & 6 deletions src/Strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Strings
export escapehtml, tocameldash, iso8859_1_to_utf8

using ..IOExtras
import ..compat_replace
import ..replace

"""
escapeHTML(i::String)
Expand All @@ -12,11 +12,11 @@ Returns a string with special HTML characters escaped: &, <, >, ", '
"""
function escapehtml(i::AbstractString)
# Refer to http://stackoverflow.com/a/7382028/3822752 for spec. links
o = compat_replace(i, "&" =>"&amp;")
o = compat_replace(o, "\""=>"&quot;")
o = compat_replace(o, "'" =>"&#39;")
o = compat_replace(o, "<" =>"&lt;")
o = compat_replace(o, ">" =>"&gt;")
o = replace(i, "&" =>"&amp;")
o = replace(o, "\""=>"&quot;")
o = replace(o, "'" =>"&#39;")
o = replace(o, "<" =>"&lt;")
o = replace(o, ">" =>"&gt;")
return o
end

Expand Down
25 changes: 12 additions & 13 deletions src/URIs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Base.==
using ..IOExtras
import ..@require, ..precondition_error
import ..@ensure, ..postcondition_error
import ..compat_search, ..compat_occursin, ..compat_parse, ..compat_string, ..compat_stdout
import ..isnumeric, ..isletter

include("parseutils.jl")
Expand Down Expand Up @@ -152,34 +151,34 @@ function ensurevalid(uri::URI)
# https://tools.ietf.org/html/rfc3986#section-3.1
# ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
if !(uri.scheme === absent ||
compat_occursin(r"^[[:alpha:]][[:alnum:]+-.]*$", uri.scheme))
occursin(r"^[[:alpha:]][[:alnum:]+-.]*$", uri.scheme))
throw(ParseError("Invalid URI scheme: $(uri.scheme)"))
end
# https://tools.ietf.org/html/rfc3986#section-3.2.2
# unreserved / pct-encoded / sub-delims
if !(uri.host === absent ||
compat_occursin(r"^[:[:alnum:]\-._~%!$&'()*+,;=]+$", uri.host))
occursin(r"^[:[:alnum:]\-._~%!$&'()*+,;=]+$", uri.host))
throw(ParseError("Invalid URI host: $(uri.host) $uri"))
end
# https://tools.ietf.org/html/rfc3986#section-3.2.3
# "port number in decimal"
if !(uri.port === absent || compat_occursin(r"^\d+$", uri.port))
if !(uri.port === absent || occursin(r"^\d+$", uri.port))
throw(ParseError("Invalid URI port: $(uri.port)"))
end

# https://tools.ietf.org/html/rfc3986#section-3.3
# unreserved / pct-encoded / sub-delims / ":" / "@"
if !(uri.path === absent ||
compat_occursin(r"^[/[:alnum:]\-._~%!$&'()*+,;=:@]*$", uri.path))
occursin(r"^[/[:alnum:]\-._~%!$&'()*+,;=:@]*$", uri.path))
throw(ParseError("Invalid URI path: $(uri.path)"))
end

# FIXME
# For compatibility with existing test/uri.jl
if !(uri.host === absent) &&
(compat_occursin("=", uri.host) ||
compat_occursin(";", uri.host) ||
compat_occursin("%", uri.host))
(occursin("=", uri.host) ||
occursin(";", uri.host) ||
occursin("%", uri.host))
throw(ParseError("Invalid URI host: $(uri.host)"))
end
end
Expand Down Expand Up @@ -242,7 +241,7 @@ showparts(io::IO, uri::URI) =
" fragment = \"", uri.fragment, "\"",
uri.fragment === absent ? " (absent)" : "", ")\n")

showparts(uri::URI) = showparts(compat_stdout(), uri)
showparts(uri::URI) = showparts(stdout, uri)

Base.print(io::IO, u::URI) = print(io, string(u))

Expand Down Expand Up @@ -302,7 +301,7 @@ const uses_fragment = ["hdfs", "ftp", "hdl", "http", "gopher", "news", "nntp", "
function Base.isvalid(uri::URI)
sch = uri.scheme
isempty(sch) && throw(ArgumentError("can not validate relative URI"))
if ((sch in non_hierarchical) && (i = compat_search(uri.path, '/'); i != nothing && i > 1)) || # path hierarchy not allowed
if ((sch in non_hierarchical) && (i = search(uri.path, '/'); i != nothing && i > 1)) || # path hierarchy not allowed
(!(sch in uses_query) && !isempty(uri.query)) || # query component not allowed
(!(sch in uses_fragment) && !isempty(uri.fragment)) || # fragment identifier component not allowed
(!(sch in uses_authority) && (!isempty(uri.host) || ("" != uri.port) || !isempty(uri.userinfo))) # authority component not allowed
Expand All @@ -323,7 +322,7 @@ utf8_chars(str::AbstractString) = (Char(c) for c in IOExtras.bytes(str))
"percent-encode a string, dict, or pair for a uri"
function escapeuri end

escapeuri(c::Char) = string('%', uppercase(compat_string(Int(c), base=16, pad=2)))
escapeuri(c::Char) = string('%', uppercase(string(Int(c), base=16, pad=2)))
escapeuri(str::AbstractString, safe::Function=issafe) =
join(safe(c) ? c : escapeuri(c) for c in utf8_chars(str))

Expand All @@ -340,7 +339,7 @@ escapeuri(query) = join((escapeuri(k, v) for (k,v) in query), "&")

"unescape a percent-encoded uri/url"
function unescapeuri(str)
compat_occursin("%", str) || return str
occursin("%", str) || return str
out = IOBuffer()
i = 1
io = IOBuffer(str)
Expand All @@ -349,7 +348,7 @@ function unescapeuri(str)
if c == '%'
c1 = read(io, Char)
c = read(io, Char)
write(out, compat_parse(UInt8, string(c1, c); base=16))
write(out, parse(UInt8, string(c1, c); base=16))
else
write(out, c)
end
Expand Down
4 changes: 2 additions & 2 deletions src/WebSockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using ..Streams
import ..ConnectionPool
using HTTP: header
import ..@debug, ..DEBUG_LEVEL, ..@require, ..precondition_error
import ..compat_string
import ..string

const WS_FINAL = 0x80
const WS_CONTINUATION = 0x00
Expand Down Expand Up @@ -292,7 +292,7 @@ function Base.show(io::IO, h::WebSocketHeader)
h.opcode == WS_PONG ? "PONG" : h.opcode,
h.final ? " | FINAL, " : ", ",
h.length > 0 ? "$(Int(h.length))-byte payload" : "",
h.hasmask ? ", mask = $(compat_string(h.mask, base=16))" : "",
h.hasmask ? ", mask = $(string(h.mask, base=16))" : "",
")")
end

Expand Down
7 changes: 0 additions & 7 deletions src/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,7 @@ function request(client::Client, method, url::URI;
body = read(body)
end

if VERSION > v"0.7.0-DEV.2338"
args = merge(args, newargs)
else
for newarg in newargs
defaultbyfirst(args, newarg)
end
end

return request(m, url, h, body; verbose=Int(verbose), args...)
end

Expand Down
10 changes: 0 additions & 10 deletions src/compat.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
using Base64
import Dates

const bytesavailable = Base.bytesavailable
const compat_findfirst = Base.findfirst
const compat_replace = Base.replace
const compat_occursin = Base.occursin
const compat_parse = Base.parse
const compat_string = Base.string

compat_stdout() = stdout

compat_search(s::AbstractString, c::Char) = Base.findfirst(isequal(c), s)
using Sockets

sprintcompact(x) = sprint(show, x; context=:compact => true)
Expand Down
Loading

0 comments on commit b0dd3de

Please sign in to comment.