【发布时间】:2021-07-05 19:44:47
【问题描述】:
我正在尝试从 http 请求中获取客户端请求和 IP 地址到我的 HTTP.jl 服务器(基于 basic server example in the docs)。
using HTTP
using Sockets
const APP = HTTP.Router()
# My request handler function can see the request's method
# and target but not the IP address it came from
HTTP.@register(APP,"GET","/",req::HTTP.Request -> begin
println("$(req.method) request to $(req.target)")
"Hello, world!"
end)
HTTP.serve(
APP,
Sockets.localhost,
8081;
# My tcpisvalid function can see the client's
# IP address but not the HTTP request
tcpisvalid=sock::Sockets.TCPSocket -> begin
host, port = Sockets.getpeername(sock)
println("Request from $host:$port")
true
end
)
我最好的猜测是有一种方法可以将 TCPSocket.buffer 解析为 HTTP 请求,但我找不到任何方法来做到这一点。
您能否建议一种从TCPSocket 获取HTTP.Request 的方法或解决此问题的不同方法?
提前致谢!
【问题讨论】:
-
听起来您可能想为 HTTP.jl 打开一个问题/功能请求。许多像这样的框架会将这种额外的元数据与请求一起发送为“REMOTE_ADDR”,也许 HTTP.jl 也应该这样做。
-
好主意,谢谢!
标签: http tcp ip julia webserver