Skip to content

Commit

Permalink
Monkeypatch for JuliaLang/julia#29451
Browse files Browse the repository at this point in the history
Make AWS signing code lazy header compatible
  • Loading branch information
samoconnor committed Oct 1, 2018
1 parent 460c1c1 commit e46027d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/AWS4AuthRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ using ..Dates
using MbedTLS: digest, MD_SHA256, MD_MD5
import ..Layer, ..request, ..Headers
using ..URIs
using ..Pairs: getkv, setkv, rmkv
import ..Pairs: setkv, rmkv
import ..@debug, ..DEBUG_LEVEL

using ..LazyHTTP
setkv(c::LazyHTTP.Header, k, v) = setindex!(c, v, k)
rmkv(c::LazyHTTP.Header, k) = delete!(c, k)



"""
request(AWS4AuthLayer, ::URI, ::Request, body) -> HTTP.Response
Expand Down Expand Up @@ -36,7 +42,7 @@ end

function sign_aws4!(method::String,
url::URI,
headers::Headers,
headers::Union{Headers,LazyHTTP.Header},
body::Vector{UInt8};
body_sha256::Vector{UInt8}=digest(MD_SHA256, body),
body_md5::Vector{UInt8}=digest(MD_MD5, body),
Expand Down
41 changes: 41 additions & 0 deletions src/LazyHTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,47 @@ function append_trailer(h::T, trailer) where T <: Header
end


# Monkeypatch 🙈

import Base.parseint_iterate

function Base.parseint_preamble(signed::Bool, base::Int, s::LazyHTTP.FieldValue, startpos::Int, endpos::Int)
c, i, j = parseint_iterate(s, startpos, endpos)

while isspace(c)
c, i, j = parseint_iterate(s,i,endpos)
end
(j == 0) && (return 0, 0, 0)

sgn = 1
if signed
if c == '-' || c == '+'
(c == '-') && (sgn = -1)
c, i, j = parseint_iterate(s,i,endpos)
end
end

while isspace(c)
c, i, j = parseint_iterate(s,i,endpos)
end
(j == 0) && (return 0, 0, 0)

if base == 0
if c == '0' && i <= endpos # <<<<<<<< # https://github.com/JuliaLang/julia/commit/1a1d6b6d1c636a822cf2da371dc41a5752e8c848#r30713661
c, i = iterate(s,i)::Tuple{Char, Int}
base = c=='b' ? 2 : c=='o' ? 8 : c=='x' ? 16 : 10
if base != 10
c, i, j = parseint_iterate(s,i,endpos)
end
else
base = 10
end
end
return sgn, base, j
end



include("isvalid.jl")


Expand Down
8 changes: 8 additions & 0 deletions test/LazyHTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ ifilter(a...) = Base.Iterators.filter(a...)

@testset "LazyHTTP" begin

h = RequestHeader("HEAD / HTTP/1.1\r\ncontent-Length: 0\r\n\r\n")

@test h["content-length"] == "0"

@test parse(Int, h["content-length"]) == 0

h = ResponseHeader("""
HTTP/1.1 302 FOUND\r
Connection: keep-alive\r
Expand Down Expand Up @@ -44,6 +50,8 @@ h = RequestHeader("""
@test strip(h["host"]) == "s3.ap-southeast-2.amazonaws.com"
@test strip(h["content-length"]) == "3"

@test parse(Int, h["content-length"]) == 3

@test lowercase(h["x-amz-date"]) == lowercase("20181001T011722Z")

h = RequestHeader("GET", "/http.jl.test/filez ")
Expand Down

0 comments on commit e46027d

Please sign in to comment.