httpswift / swifter

Tiny http server engine written in Swift programming language.
BSD 3-Clause "New" or "Revised" License
3.87k stars 538 forks source link

`HttpRequest.address` doesn't contain the requested URL #508

Open DominikPalo opened 2 years ago

DominikPalo commented 2 years ago

I'd like to access the complete URL address inside the route callback. So here is an example code:

let server = HttpServer(0)
server["hello-world"] = { request in
    debugPrint(request.address)
    return HttpResponse.ok(.text("<html string>"))
}
server.start()

but after visiting the registered address (e.g. http://127.0.0.1:66577/hello-world?test=1), the request.path, request.headers, request.queryParams properties are parsed and set correctly, just the request.address contains a strange string - something like ::c080:d00:60:0 instead of the visited URL (http://127.0.0.1:66577/hello-world?test=1 in my case).

Although the HttpRequest.address property is not documented, I'm assuming that it should contain the complete URL of the request. Am I right? Or if the HttpRequest.address is used for something different, how can I get the visited URL?

xocialize commented 2 years ago

When starting your server you can do so like: try server.start(9080, forceIPv4: true) For me this made the request.address show the IP address of the request i.e. 127.0.0.1. If you combine with request.path you should be able to start to rebuild the request.