Skip to content

Server in streaming mode - never wrote a response #1156

@sairus7

Description

@sairus7

Run this example SSE code from docs: https://juliaweb.github.io/HTTP.jl/stable/examples/#Server-Sent-Events:

using HTTP, Sockets, JSON

const ROUTER = HTTP.Router()

function getItems(req::HTTP.Request)
    headers = [
        "Access-Control-Allow-Origin" => "*",
        "Access-Control-Allow-Methods" => "GET, OPTIONS"
    ]
    if HTTP.method(req) == "OPTIONS"
        return HTTP.Response(200, headers)
    end
    return HTTP.Response(200, headers, JSON.json(rand(2)))
end

function events(stream::HTTP.Stream)
    HTTP.setheader(stream, "Access-Control-Allow-Origin" => "*")
    HTTP.setheader(stream, "Access-Control-Allow-Methods" => "GET, OPTIONS")
    HTTP.setheader(stream, "Content-Type" => "text/event-stream")

    if HTTP.method(stream.message) == "OPTIONS"
        return nothing
    end

    HTTP.setheader(stream, "Content-Type" => "text/event-stream")
    HTTP.setheader(stream, "Cache-Control" => "no-cache")
    while true
        write(stream, "event: ping\ndata: $(round(Int, time()))\n\n")
        if rand(Bool)
            write(stream, "data: $(rand())\n\n")
        end
        sleep(1)
    end
    return nothing
end

HTTP.register!(ROUTER, "GET", "/api/getItems", HTTP.streamhandler(getItems))
HTTP.register!(ROUTER, "/api/events", events)

server = HTTP.serve!(ROUTER, "127.0.0.1", 8080; stream=true)

# Julia usage
resp = HTTP.get("http://localhost:8080/api/getItems") # <-- if run from chrome browser, got error together with response

If I paste http://localhost:8080/api/getItems into chrome browser, I get the responce, but there is also an error message in Julia console:

┌ Error: handle_connection handler error. 
│ 
│ ===========================
│ HTTP Error message:
│ 
│ ERROR: Server never wrote a response    
│ Stacktrace:
│  [1] error(s::String)
│    @ Base .\error.jl:35
│  [2] handle_connection(f::HTTP.Handlers.Router{typeof(HotBox.cors404), typeof(HotBox.cors405), Nothing}, c::HTTP.Connections.Connection{Sockets.TCPSocket}, listener::HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, readtimeout::Int64, access_log::Nothing)
│    @ HTTP.Servers C:\Users\gvg\.julia\packages\HTTP\1EWL3\src\Servers.jl:459
│  [3] (::HTTP.Servers.var"#16#17"{HTTP.Handlers.Router{typeof(HotBox.cors404), typeof(HotBox.cors405), Nothing}, HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, Set{HTTP.Connections.Connection}, Int64, Nothing, Base.Semaphore, HTTP.Connections.Connection{Sockets.TCPSocket}})()
│    @ HTTP.Servers C:\Users\gvg\.julia\packages\HTTP\1EWL3\src\Servers.jl:388
└ @ HTTP.Servers C:\Users\gvg\.julia\packages\HTTP\1EWL3\src\Servers.jl:470

Is that expexted behaviour? I've encountered this every time when I have streaming mode with HTTP.streamhandler middleware. Without it the message disappears.

  • Julia 1.10.
  • HTTP.jl 1.10.2
  • MbedTLS.jl 1.1.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions