From e46027d1a7730ccde29aebfee03f68e0e661a216 Mon Sep 17 00:00:00 2001 From: Sam O'Connor Date: Mon, 1 Oct 2018 20:28:36 +1000 Subject: [PATCH] Monkeypatch for https://github.com/JuliaLang/julia/issues/29451 Make AWS signing code lazy header compatible --- src/AWS4AuthRequest.jl | 10 ++++++++-- src/LazyHTTP.jl | 41 +++++++++++++++++++++++++++++++++++++++++ test/LazyHTTP.jl | 8 ++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/AWS4AuthRequest.jl b/src/AWS4AuthRequest.jl index a3feb38c8..22a87946e 100644 --- a/src/AWS4AuthRequest.jl +++ b/src/AWS4AuthRequest.jl @@ -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 @@ -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), diff --git a/src/LazyHTTP.jl b/src/LazyHTTP.jl index af0358add..115259bb3 100644 --- a/src/LazyHTTP.jl +++ b/src/LazyHTTP.jl @@ -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") diff --git a/test/LazyHTTP.jl b/test/LazyHTTP.jl index f93b0c190..973a65592 100644 --- a/test/LazyHTTP.jl +++ b/test/LazyHTTP.jl @@ -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 @@ -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 ")