-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HTTP::Request#remote_address #7610
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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_address
property toHTTP::Request
.Some details:
String?
. It can benil
if the address can't be determined, but this usually won't happen. It's also nilable because forHTTP::Client
it doesn't make sense. It'sString
and not a structured object likeIPAddress
because a middleware might want to overwrite this property, for example using theX-Forwarded-By
orX-Real-IP
headers. 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_address
andremote_address
getters ofOpenSSL::SSL::Socket
, but only because the underlyingIO
is not necessarily aIPSocket
, so the type of those getters isn'tIPAddress
. Maybe we can assume the underlyingIO
isIPSocket
, 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