Reading images in HTTP doesn't seem to terminate

using HTTP

using Colors

# initialize to nothing
orig_img = nothing

using ImageMagick
HTTP.listen() do http::HTTP.Stream
    global orig_img
    @show http.message
    @show HTTP.header(http, "Content-Type")
    while !eof(http)
        orig_img = ImageMagick.readblob( readavailable(http))
    end
end

orig_img

and then I curl an image to it by

curl -X POST --data-binary @"path/to/some.png" 127.0.0.1:8081

But the above the doesn’t seem to terminate even for a small image.

When I check orig_img it looks like the image got upload if a bit grainy.

But when I terminate curl everything dies with

julia> ┌ Error: (Base.IOError("read: connection reset by peer (ECONNRESET)", -4077), Base.StackTraces.StackFrame[wait_readnb(::Sockets.TCPSocket, ::Int64) at stream.jl:350, eof at stream.jl:48 [inlined], read_to_buffer(::HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, ::Int64) at ConnectionPool.jl:251, readuntil(::HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, ::Function, ::Int64) at ConnectionPool.jl:271, readuntil at ConnectionPool.jl:269 [inlined], readheaders at Messages.jl:471 [inlined], startread(::HTTP.Streams.Stream{HTTP.Messages.Request,HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}}) at Streams.jl:155, handle_transaction(::var"#19#20", ::HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, ::HTTP.Servers.Server{Nothing,Sockets.TCPServer}; final_transaction::Bool) at Servers.jl:343, (::HTTP.Servers.var"#handle_transaction##kw")(::NamedTuple{(:final_transaction,),Tuple{Bool}}, ::typeof(HTTP.Servers.handle_transaction), ::Function, ::HTTP.ConnectionPool.Transaction{Sockets.TCPSocket}, ::HTTP.Servers.Server{Nothing,Sockets.TCPServer}) at Servers.jl:338, handle_connection(::Function, ::HTTP.ConnectionPool.Connection{Sockets.TCPSocket}, ::HTTP.Servers.Server{Nothing,Sockets.TCPServer}, ::Int64, ::Int64) at Servers.jl:299, (::HTTP.Servers.var"#8#9"{var"#19#20",HTTP.Servers.Server{Nothing,Sockets.TCPServer},Base.RefValue{Int64},Int64,Int64,Bool,HTTP.ConnectionPool.Connection{Sockets.TCPSocket}})() at task.jl:356])

I suspect I am just not understanding curl because I thought the image would have finished uploading and it will terminate.

I think I get it. I need to send a response like this so it will terminate

using HTTP

using Colors

# initialize to nothing
orig_img = nothing

using ImageMagick
HTTP.listen() do http::HTTP.Stream
    global orig_img
    @show http.message
    @show HTTP.header(http, "Content-Type")
    while !eof(http)
        orig_img = ImageMagick.readblob( readavailable(http))
    end

    HTTP.setstatus(http, 404)
    HTTP.setheader(http, "Foo-Header" => "bar")
    HTTP.startwrite(http)
    write(http, "response body")
    write(http, "more response body")
end

orig_img

Super :muscle: :pray: @xiaodai

I have all working and now with HTTP.jl with required architectural changes to the app.
It is CD/CI deployed to a container in Azure which is served via a Web App.

But now stumbling on defining the port for HTTP.jl to listen on.
Getting errors with below to examples following what docs I could cut and paste from:

HTTP.listen(host=Sockets.localhost, port=8081) do http

ERROR: InitError: UndefVarError: Sockets not defined

HTTP.listen(, port=80) do http

ERROR: LoadError: syntax: unexpected “,”

I am specifying the default port here but the purpose is to change to port 80 for production.
Surely, I am missing something obvious.

Any ideas welcome.
Eric

using Sockets

this is not valid syntax in Julia, unlike R.

Thanks again @xiaodai .

I think that one is good - not sure why it works now. Testing on full production.
HTTP.listen("127.0.0.1", 80) do http"
Getting there.