JuliaWeb / HttpServer.jl

DEPRECATED! Basic, non-blocking HTTP server in Julia.

Home Page:https://github.com/JuliaWeb/HTTP.jl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pass client to the handler?

ylxdzsw opened this issue · comments

Current HttpHandler.handle receives a useless Response as the second argument, and I have seen there is a PR #79 to remove it. I want to get the client IP address in the handler but Request object does not contains the sock. Can it receive a Client object as the second arg, just like the websocket handler?

I'm currently working on an overhaul of the package; so stay tuned. The HttpHandler.handle receives a Response object as 2nd argument so that the handler can modify that response and return it, instead of having to instantiate a new Response to return (in principle, to allow persistent connections to re-use Responses), however the Response is currently no persisted between subsequent Requests, so it indeed is a little useless right now.

Thanks, glad to see this package is still maintained :)
Now I see why the second arguments is a Response, I just wonder will it receive another argument so that I can get the remote IP address in the handler.

A bit late, but I'm doing something like this using task_local_storage as a workaround:

using HttpServer
import MbedTLS

Base.getsockname(client::Client) = getsockname(
    isa(client.sock, MbedTLS.SSLContext) ? client.sock.bio : client.sock
)

handle_connect(client::Client) = task_local_storage(:inet, getsockname(client))

Then you can access the ip address/port from the task-local storage within the handler as follows:

handler = HttpHandler() do req::Request, res::Response
    ip_address, port = task_local_storage(:inet)
    ...
end
handler.events["connect"] = handle_connect

server = Server(handler)
run(server)