Add HTTP::Request#remote_address#7610
Conversation
|
Also, we should probably add some way to parse a network address that looks like "ip:port" into But again, this can be done later on. |
bcardiff
left a comment
There was a problem hiding this comment.
I second @straight-shoota input. The implementation is GTG. So after the spec is improved based on the other PR this is good to be merged
97ce8b1 to
9107eb5
Compare
9107eb5 to
52588f7
Compare
|
Rebased against master. I noticed |
|
Also thank you @omarroth for the original code, which I used almost as is. It's just that this needed a bit more forward pushing from us core team members and having it on our side makes it easier to rebase, edit, etc. |
|
No worries, thanks @asterite! |
|
I noticed today that determining remote_address "up front" for request makes this benchmark: https://gist.github.com/jgaskins/ba74aaca0ce45f714caaa7571703beba [1] 2.5% slower.(12500 -> 12225 req/s) single threaded, and about 5% slower multi-threaded. Maybe there's a way to avoid the slowdown? [1] https://forum.crystal-lang.org/t/multithreaded-crystal-initial-thoughts/1089 |
|
What exactly are you comparing? Crystal Vs Go? Different Crystal versions? |
|
Crystal versus crystal with or without this patch applied: First up any "small" HTTP server, here's one: https://gist.github.com/rdp/b136f6a998919fe0eae39dc7990d0ba0 |
This PR adds a
remote_addressproperty toHTTP::Request.Some details:
String?. It can benilif the address can't be determined, but this usually won't happen. It's also nilable because forHTTP::Clientit doesn't make sense. It'sStringand not a structured object likeIPAddressbecause a middleware might want to overwrite this property, for example using theX-Forwarded-ByorX-Real-IPheaders. Also, this field is apparently commonly used for logging so it being a String is probably fine and the most flexible option.responds_to?(:remote_address)to determine this, I'd maybe like a module that defines this as an abstract method, but we can change the implementation later.local_addressandremote_addressgetters ofOpenSSL::SSL::Socket, but only because the underlyingIOis not necessarily aIPSocket, so the type of those getters isn'tIPAddress. Maybe we can assume the underlyingIOisIPSocket, otherwise raise in this case. But this is something that we can improve later.HTTP::Request#remote_address, let me knowThe idea here is to define an API that's usable and we won't modify later on. I think adding
HTTP::Request#remote_address : String?is enough for this. We can redefine the implementation details I mention above in subsequent PRs.Fixes #453
Related to #5784
Supersedes #7302