Skip to content

Commit eae8ed9

Browse files
committed
Get sniff and handler tests passing
1 parent c98b2dc commit eae8ed9

File tree

8 files changed

+326
-77
lines changed

8 files changed

+326
-77
lines changed

src/HTTP.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include("client/stream.jl")
2020
include("client/makerequest.jl")
2121
include("websockets.jl"); using .WebSockets
2222
include("server.jl")
23-
include("handlers.jl")
23+
include("Handlers.jl"); using .Handlers
2424
include("statuses.jl")
2525

2626
struct StatusError <: Exception

src/Handlers.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Handlers
22

3-
import ..Request
3+
export Handler, Middleware, serve, serve!, Router, register!, getroute, getparams, getparam, getcookies
4+
5+
import ..Request, ..Cookies
46

57
"""
68
Handler
@@ -285,8 +287,7 @@ register!(r::Router, path, handler) = register!(r, "*", path, handler)
285287
const Params = Dict{String, String}
286288

287289
function gethandler(r::Router, req::Request)
288-
url = req.uri
289-
segments = split(url.path, '/'; keepempty=false)
290+
segments = split(req.path, '/'; keepempty=false)
290291
leaf = match(r.routes, req.method, segments, 1)
291292
params = Params()
292293
if leaf isa Leaf
@@ -329,9 +330,9 @@ function (r::Router)(req::Request)
329330
# matched the path, but method not supported
330331
return r._405(req)
331332
else
332-
req.context[:route] = route
333+
req.route = route
333334
if !isempty(params)
334-
req.context[:params] = params
335+
req.params = params
335336
end
336337
return handler(req)
337338
end
@@ -344,7 +345,7 @@ Retrieve the original route registration string for a request after its url has
344345
matched against a router. Helpful for metric logging to ignore matched variables in
345346
a path and only see the registered routes.
346347
"""
347-
getroute(req) = Base.get(req.context, :route, nothing)
348+
getroute(req) = req.route
348349

349350
"""
350351
HTTP.getparams(req) -> Dict{String, String}
@@ -354,7 +355,7 @@ If a path was registered with a router via `HTTP.register!` like
354355
"/api/widget/{id}", then the path parameters are available in the request context
355356
and can be retrieved like `id = HTTP.getparams(req)["id"]`.
356357
"""
357-
getparams(req) = Base.get(req.context, :params, nothing)
358+
getparams(req) = req.params
358359

359360
"""
360361
HTTP.getparam(req, name, default=nothing) -> String
@@ -378,8 +379,8 @@ request in the request context. Cookies can then be retrieved by calling
378379
"""
379380
function cookie_middleware(handler)
380381
function (req)
381-
if !haskey(req.context, :cookies)
382-
req.context[:cookies] = Cookies.cookies(req)
382+
if req.cookies === nothing
383+
req.cookies = Cookies.cookies(req)
383384
end
384385
return handler(req)
385386
end
@@ -393,6 +394,6 @@ are expected to be stored in the `req.context[:cookies]` of the
393394
request context as implemented in the [`HTTP.Handlers.cookie_middleware`](@ref)
394395
middleware.
395396
"""
396-
getcookies(req) = Base.get(() -> Cookie[], req.context, :cookies)
397+
getcookies(req) = req.cookies === nothing ? Cookies.Cookie[] : req.cookies::Vector{Cookies.Cookie}
397398

398399
end # module Handlers

src/requestresponse.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ mutable struct Request <: Message
159159
allocator::Ptr{aws_allocator}
160160
ptr::Ptr{aws_http_message}
161161
inputstream::InputStream # used for outgoing request body
162-
body::Union{Nothing, Vector{UInt8}} # only set in server-side request handlers
162+
# only set in server-side request handlers
163+
body::Union{Nothing, Vector{UInt8}}
164+
route::Union{Nothing, String}
165+
params::Union{Nothing, Dict{String, String}}
166+
cookies::Any # actually Union{Nothing, Vector{Cookie}}
163167

164168
function Request(method, path, headers=nothing, body=nothing, http2::Bool=false, allocator=default_aws_allocator())
165169
ptr = http2 ?
@@ -177,6 +181,9 @@ mutable struct Request <: Message
177181
end
178182
req = new(allocator, ptr)
179183
req.body = nothing
184+
req.route = nothing
185+
req.params = nothing
186+
req.cookies = nothing
180187
body !== nothing && setinputstream!(req, body)
181188
return finalizer(_ -> aws_http_message_release(ptr), req)
182189
catch
@@ -195,11 +202,12 @@ function Base.getproperty(x::Request, s::Symbol)
195202
aws_http_message_get_request_method(ptr(x), out) != 0 && return nothing
196203
return str(out[])
197204
end
198-
elseif s == :path
205+
elseif s == :path || s == :target || s == :uri
199206
out = Ref{aws_byte_cursor}()
200207
GC.@preserve out begin
201208
aws_http_message_get_request_path(ptr(x), out) != 0 && return nothing
202-
return str(out[])
209+
path = str(out[])
210+
return s == :uri ? URI(path) : path
203211
end
204212
elseif s == :headers
205213
return Headers(aws_http_message_get_headers(ptr(x)))

src/utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Ensure the first character and characters that follow a '-' are uppercase.
3838
"""
3939
function tocameldash(s::String)
4040
toUpper = UInt8('A') - UInt8('a')
41-
v = Vector{UInt8}(bytes(s))
41+
v = Vector{UInt8}(codeunits(s))
4242
upper = true
4343
for i = 1:length(v)
4444
@inbounds b = v[i]

test/handlers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ using HTTP, Test
4646
HTTP.register!(r, "/api/widgets/{id}", req -> HTTP.getparam(req, "id"))
4747
@test r(HTTP.Request("GET", "/api/widgets/11")) == "11"
4848

49-
HTTP.register!(r, "/api/widgets/{name}", req -> (req.context[:params]["name"], HTTP.getroute(req)))
49+
HTTP.register!(r, "/api/widgets/{name}", req -> (HTTP.getparam(req, "name"), HTTP.getroute(req)))
5050
@test r(HTTP.Request("GET", "/api/widgets/11")) == ("11", "/api/widgets/{name}")
5151

52-
HTTP.register!(r, "/api/widgets/acme/{id:[a-z]+}", req -> req.context[:params]["id"])
52+
HTTP.register!(r, "/api/widgets/acme/{id:[a-z]+}", req -> HTTP.getparam(req, "id"))
5353
called[] = false
5454
@test r(HTTP.Request("GET", "/api/widgets/acme/11")) == 0
5555
@test !called[]

test/runtests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,28 @@ using Test, HTTP, URIs, JSON
33
const httpbin = get(ENV, "JULIA_TEST_HTTPBINGO_SERVER", "httpbingo.julialang.org")
44
isok(r) = r.status == 200
55

6+
# Core.eval(Base, :(function unsafe_copyto!(dest::Base.MemoryRef{T}, src::Base.MemoryRef{T}, n) where {T}
7+
# Base.@_terminates_globally_notaskstate_meta
8+
# n == 0 && return dest
9+
# @show n
10+
# @boundscheck Base.memoryref(dest, n), Base.memoryref(src, n)
11+
# if isbitstype(T)
12+
# tdest = Base.@_gc_preserve_begin dest
13+
# tsrc = Base.@_gc_preserve_begin src
14+
# pdest = unsafe_convert(Ptr{Cvoid}, dest)
15+
# psrc = unsafe_convert(Ptr{Cvoid}, src)
16+
# Base.memmove(pdest, psrc, Base.aligned_sizeof(T) * n)
17+
# Base.@_gc_preserve_end tdest
18+
# Base.@_gc_preserve_end tsrc
19+
# else
20+
# ccall(:jl_genericmemory_copyto, Cvoid, (Any, Ptr{Cvoid}, Any, Ptr{Cvoid}, Int), dest.mem, dest.ptr_or_offset, src.mem, src.ptr_or_offset, Int(n))
21+
# end
22+
# return dest
23+
# end))
24+
625
include("utils.jl")
26+
include("sniff.jl")
27+
include("multipart.jl")
728
include("client.jl")
829
include("handlers.jl")
930
include("server.jl")

0 commit comments

Comments
 (0)