-
-
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
HTTP::Request, added remote ip and peer_addr #1722
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,27 @@ class HTTP::Request | |
@method == "HEAD" | ||
end | ||
|
||
def peer_addr=(addr) | ||
@peer_addr = addr | ||
end | ||
|
||
def peer_addr | ||
@peer_addr.not_nil! | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to know :) |
||
|
||
def remote_ip(trust_headers=true) | ||
if trust_headers | ||
client_ips = ips_from("Client-Ip").reverse | ||
forwarded_ips = ips_from("X-Forwarded-For").reverse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As said I've also seen There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
ips = [forwarded_ips, client_ips, peer_addr.ip_address].flatten.compact | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mhh, |
||
|
||
first_remote_ip_address(ips) || peer_addr.ip_address | ||
else | ||
peer_addr.ip_address | ||
end | ||
end | ||
|
||
def to_io(io) | ||
io << @method << " " << resource << " " << @version << "\r\n" | ||
cookies = @cookies | ||
|
@@ -68,4 +89,21 @@ class HTTP::Request | |
private def uri | ||
(@uri ||= URI.parse(@resource)).not_nil! | ||
end | ||
|
||
private def ips_from(header) | ||
if ips = headers[header]? || headers["Http-#{header.tr("_", "-")}"]? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. But it can't lookup HTTP_X_FORWARDED_FOR and X-Forwarded-For right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So only the tr("_", "-") is redundant right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh, missed the comment, sorry. But yes, you're right and looks good now. |
||
ips.strip.split(/[,\s]+/) | ||
else | ||
[] of String | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mhh, |
||
end | ||
end | ||
|
||
private def first_remote_ip_address(ip_addresses) | ||
ip_addresses.find { |ip| !trusted_proxy?(ip) } | ||
end | ||
|
||
private def trusted_proxy?(ip) | ||
ip =~ /\A127\.0\.0\.1\Z|\A(10|172\.(1[6-9]|2[0-9]|30|31)|192\.168)\.|\A::1\Z|\Afd[0-9a-f]{2}:.+|\Alocalhost\Z|\Aunix\Z|\Aunix:/i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I also wonder given this whether we shouldn't have |
||
end | ||
|
||
end |
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.
What's the point of having two identical specs?