Skip to content

Commit

Permalink
Get more server tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Jan 3, 2025
1 parent eae8ed9 commit 8b690aa
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 94 deletions.
1 change: 1 addition & 0 deletions src/HTTP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module HTTP
using CodecZlib, URIs, Mmap, Base64
using LibAwsCommon, LibAwsIO, LibAwsHTTP

export @logfmt_str, common_logfmt, combined_logfmt
export WebSockets

include("utils.jl")
Expand Down
20 changes: 10 additions & 10 deletions src/access_log.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ function symbol_mapping(s::Symbol)
str = string(s)
if (m = match(r"^http_(.+)$", str); m !== nothing)
hdr = replace(String(m[1]), '_' => '-')
:(HTTP.header(http.message, $hdr, "-"))
:(something(HTTP.getheader(http.request.headers, $hdr), "-"))
elseif (m = match(r"^sent_http_(.+)$", str); m !== nothing)
hdr = replace(String(m[1]), '_' => '-')
:(HTTP.header(http.message.response, $hdr, "-"))
:(something(HTTP.getheader(http.response.headers, $hdr), "-"))
elseif s === :remote_addr
:(http.stream.peerip)
:(HTTP.remote_address(http.connection))
elseif s === :remote_port
:(http.stream.peerport)
:(HTTP.remote_port(http.connection))
elseif s === :remote_user
:("-") # TODO: find from Basic auth...
elseif s === :time_iso8601
Expand All @@ -89,17 +89,17 @@ function symbol_mapping(s::Symbol)
m = symbol_mapping(:request_method)
t = symbol_mapping(:request_uri)
p = symbol_mapping(:server_protocol)
(m, " ", t, " ", p...)
(m, " ", t, " ", p)
elseif s === :request_method
:(http.message.method)
:(http.request.method)
elseif s === :request_uri
:(http.message.target)
:(http.request.target)
elseif s === :server_protocol
("HTTP/", :(http.message.version.major), ".", :(http.message.version.minor))
:(HTTP.http_version(http.connection))
elseif s === :status
:(http.message.response.status)
:(http.response.status)
elseif s === :body_bytes_sent
return :(max(0, http.nwritten))
:(HTTP.bodylen(http.response))
else
error("unknown variable in logfmt: $s")
end
Expand Down
2 changes: 1 addition & 1 deletion src/client/request.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function with_request(f::Function, client::Client, method, path, headers=nothing
setheader(h, "host", client.settings.host)
end
setheaderifabsent(h, "accept", "*/*")
setheaderifabsent(h, "user-agent", USER_AGENT[])
setheaderifabsent(h, "user-agent", something(USER_AGENT[], "-"))
if decompress === nothing || decompress
setheaderifabsent(h, "accept-encoding", "gzip")
end
Expand Down
11 changes: 9 additions & 2 deletions src/requestresponse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ function InputStream(allocator::Ptr{aws_allocator}, body::RequestBodyTypes)
end

function setinputstream!(msg::Message, body)
aws_http_message_set_body_stream(msg.ptr, C_NULL)
msg.inputstream = nothing
body === nothing && return
input_stream = InputStream(msg.allocator, body)
setfield!(msg, :inputstream, input_stream)
if input_stream.ptr != C_NULL
Expand All @@ -158,7 +161,7 @@ end
mutable struct Request <: Message
allocator::Ptr{aws_allocator}
ptr::Ptr{aws_http_message}
inputstream::InputStream # used for outgoing request body
inputstream::Union{Nothing, InputStream} # used for outgoing request body
# only set in server-side request handlers
body::Union{Nothing, Vector{UInt8}}
route::Union{Nothing, String}
Expand All @@ -181,6 +184,7 @@ mutable struct Request <: Message
end
req = new(allocator, ptr)
req.body = nothing
req.inputstream = nothing
req.route = nothing
req.params = nothing
req.cookies = nothing
Expand Down Expand Up @@ -291,7 +295,7 @@ RequestMetrics() = RequestMetrics(0, 0, 0, nothing)
mutable struct Response <: Message
allocator::Ptr{aws_allocator}
ptr::Ptr{aws_http_message}
inputstream::InputStream
inputstream::Union{Nothing, InputStream}
body::Union{Nothing, Vector{UInt8}} # only set for client-side response body when no user-provided response_body
metrics::RequestMetrics
request::Request
Expand All @@ -311,6 +315,7 @@ mutable struct Response <: Message
end
resp = new(allocator, ptr)
resp.body = nothing
resp.inputstream = nothing
body !== nothing && setinputstream!(resp, body)
return finalizer(_ -> aws_http_message_release(ptr), resp)
catch
Expand All @@ -324,6 +329,8 @@ Response(body=nothing) = Response(0, nothing, body)
Response(status::Integer, body) = Response(status, nothing, Vector{UInt8}(string(body)))
Response(status::Integer) = Response(status, nothing, nothing)

bodylen(m::Message) = isdefined(m, :inputstream) && m.inputstream !== nothing ? m.inputstream.bodylen : 0

function Base.getproperty(x::Response, s::Symbol)
if s == :status
ref = Ref{Cint}()
Expand Down
Loading

0 comments on commit 8b690aa

Please sign in to comment.