Skip to content

Commit

Permalink
Quick hack to work around JuliaLang/julia#25472
Browse files Browse the repository at this point in the history
  • Loading branch information
samoconnor committed Jan 17, 2018
1 parent 68e3a41 commit f7cbe5f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/Pairs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Pairs

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

import ..compat_findfirst

"""
setbyfirst(collection, item) -> item
Expand All @@ -13,7 +14,7 @@ Otherwise the new `item` is inserted at the end of the `collection`.

function setbyfirst(c, item, eq = ==)
k = first(item)
if (i = findfirst(x->eq(first(x), k), c)) > 0
if (i = compat_findfirst(x->eq(first(x), k), c)) > 0
c[i] = item
else
push!(c, item)
Expand All @@ -29,7 +30,7 @@ Get `item` from collection where `first(item)` matches `key`.
"""

function getbyfirst(c, k, default=nothing, eq = ==)
i = findfirst(x->eq(first(x), k), c)
i = compat_findfirst(x->eq(first(x), k), c)
return i > 0 ? c[i] : default
end

Expand All @@ -43,7 +44,7 @@ insert the new `item` at the end of the `collection`.

function defaultbyfirst(c, item, eq = ==)
k = first(item)
if (i = findfirst(x->eq(first(x), k), c)) == 0
if (i = compat_findfirst(x->eq(first(x), k), c)) == 0
push!(c, item)
end
return
Expand All @@ -67,7 +68,7 @@ where `first(item) == key` and `value = item[2]`
"""

function getkv(c, k, default=nothing)
i = findfirst(x->first(x) == k, c)
i = compat_findfirst(x->first(x) == k, c)
return i > 0 ? c[i][2] : default
end

Expand All @@ -79,7 +80,7 @@ Remove `key` from `collection` of key/value `Pairs`.
"""

function rmkv(c, k, default=nothing)
i = findfirst(x->first(x) == k, c)
i = compat_findfirst(x->first(x) == k, c)
if i > 0
deleteat!(c, i)
end
Expand Down
5 changes: 3 additions & 2 deletions src/Parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import MbedTLS.SSLContext

import ..@debug, ..@debugshow, ..DEBUG_LEVEL
import ..@require, ..precondition_error
import ..compat_findfirst

include("consts.jl")
include("parseutils.jl")
Expand Down Expand Up @@ -465,7 +466,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#,
end
@passert p <= len + 1

write(parser.valuebuffer, view(bytes, start:p-1))
write(parser.valuebuffer, collect(view(bytes, start:p-1)))

if p_state >= s_req_http_start
parser.message.target = take!(parser.valuebuffer)
Expand Down Expand Up @@ -603,7 +604,7 @@ function parseheaders(onheader::Function #=f(::Pair{String,String}) =#,
c = lower(ch)

@debugshow 4 h
crlf = findfirst(x->(x == bCR || x == bLF), view(bytes, p:len))
crlf = compat_findfirst(x->(x == bCR || x == bLF), view(bytes, p:len))
p = crlf == 0 ? len : p + crlf - 2

p += 1
Expand Down
4 changes: 3 additions & 1 deletion src/URIs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Base.==

import ..@require, ..precondition_error
import ..@ensure, ..postcondition_error
import ..compat_search


include("urlparser.jl")
Expand Down Expand Up @@ -175,10 +176,11 @@ const uses_query = ["http", "wais", "imap", "https", "shttp", "mms", "gopher", "
const uses_fragment = ["hdfs", "ftp", "hdl", "http", "gopher", "news", "nntp", "wais", "https", "shttp", "snews", "file", "prospero"]

"checks if a `HTTP.URI` is valid"

function Base.isvalid(uri::URI)
sch = uri.scheme
isempty(sch) && throw(ArgumentError("can not validate relative URI"))
if ((sch in non_hierarchical) && (search(uri.path, '/') > 1)) || # path hierarchy not allowed
if ((sch in non_hierarchical) && (compat_search(uri.path, '/') > 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 Down
6 changes: 6 additions & 0 deletions src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
using Distributed
import Dates

compat_search(a...) = (r = search(a...); r === nothing ? 0 : r)
compat_findfirst(a...) = (r = findfirst(a...); r === nothing ? 0 : r)

else # Julia v0.6

eval(:(module Base64 end))
const Dates = Base.Dates
const Distributed = Base.Distributed

const compat_search = search
const compat_findfirst = findfirst

pairs(x) = [k => v for (k,v) in x]

Base.SubString(s) = SubString(s, 1)
Expand Down
4 changes: 3 additions & 1 deletion src/cookies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import ..Dates
import Base.==
import ..URIs.isurlchar
using ..pairs
import ..compat_search


"""
Cookie()
Expand Down Expand Up @@ -163,7 +165,7 @@ function readsetcookie(host, cookie)
parts[x] = strip(parts[x])
length(parts[x]) == 0 && continue
attr, val = parts[x], ""
j = search(parts[x], '=')
j = compat_search(parts[x], '=')
if j > 0
attr, val = attr[1:j-1], attr[j+1:end]
end
Expand Down

0 comments on commit f7cbe5f

Please sign in to comment.