Search code examples
httpjuliahttpserver

How to get client ip address in HttpHandler (Julia Language)?


I need to get client's IP address in regular HttpHandler like this:

http = HttpHandler() do req::Request, res::Response
    Response( ismatch(r"^/hello/",req.resource) ? string("Hello ", split(req.resource,'/')[3], "!") : 404 )
end

Neither req nor http.sock contain this information.


Solution

  • Thanks to waTeim's answer, but it's from 2014 and things changed in Julia. This works nicely in Julia 6.0 and probably all above:

    function ip(socket::TCPSocket) buffer = Array{UInt8}(32) bufflen::Int64 = 32 ccall(:uv_tcp_getpeername,Int64,(Ptr{Void},Ptr{UInt8},Ptr{Int64}), socket.handle, buffer, &bufflen) peername::IPv4 = IPv4(buffer[5:8]...) end