Skip to content
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

Resolve some TODOs #11369

Merged
merged 6 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions spec/std/big/big_float_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ describe "BigFloat" do
end

describe "#**" do
# TODO: investigate why in travis this gives ""1.79559999999999999991"
# it { ("1.34".to_big_f ** 2).to_s.should eq("1.79559999999999999994") }
it { ("1.34".to_big_f ** 2).to_s.should eq("1.79559999999999999994") }
it { ("-0.05".to_big_f ** 10).to_s.should eq("0.00000000000009765625") }
it { (0.1234567890.to_big_f ** 3).to_s.should eq("0.00188167637178915473909") }
end
Expand Down
55 changes: 37 additions & 18 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -314,24 +314,43 @@ describe "File" do
File.extname("").should eq("")
end

# TODO: these specs are redundant with path_spec.cr
pending_win32 "constructs a path from parts" do
File.join(["///foo", "bar"]).should eq("///foo/bar")
File.join(["///foo", "//bar"]).should eq("///foo//bar")
File.join(["/foo/", "/bar"]).should eq("/foo/bar")
File.join(["foo", "bar", "baz"]).should eq("foo/bar/baz")
File.join(["foo", "//bar//", "baz///"]).should eq("foo//bar//baz///")
File.join(["/foo/", "/bar/", "/baz/"]).should eq("/foo/bar/baz/")
File.join(["", "foo"]).should eq("foo")
File.join(["foo", ""]).should eq("foo/")
File.join(["", "", "foo"]).should eq("foo")
File.join(["foo", "", "bar"]).should eq("foo/bar")
File.join(["foo", "", "", "bar"]).should eq("foo/bar")
File.join(["foo", "/", "bar"]).should eq("foo/bar")
File.join(["foo", "/", "/", "bar"]).should eq("foo/bar")
File.join(["/", "/foo", "/", "bar/", "/"]).should eq("/foo/bar/")
File.join(["foo"]).should eq("foo")
File.join("foo").should eq("foo")
# There are more detailled specs for `Path#join` in path_spec.cr
it "constructs a path from parts" do
{% if flag?(:win32) %}
File.join(["///foo", "bar"]).should eq("///foo\\bar")
File.join(["///foo", "//bar"]).should eq("///foo//bar")
File.join(["/foo/", "/bar"]).should eq("/foo/bar")
File.join(["foo", "bar", "baz"]).should eq("foo\\bar\\baz")
File.join(["foo", "//bar//", "baz///"]).should eq("foo//bar//baz///")
File.join(["/foo/", "/bar/", "/baz/"]).should eq("/foo/bar/baz/")
File.join(["", "foo"]).should eq("foo")
File.join(["foo", ""]).should eq("foo\\")
File.join(["", "", "foo"]).should eq("foo")
File.join(["foo", "", "bar"]).should eq("foo\\bar")
File.join(["foo", "", "", "bar"]).should eq("foo\\bar")
File.join(["foo", "/", "bar"]).should eq("foo/bar")
File.join(["foo", "/", "/", "bar"]).should eq("foo/bar")
File.join(["/", "/foo", "/", "bar/", "/"]).should eq("/foo/bar/")
File.join(["foo"]).should eq("foo")
File.join("foo").should eq("foo")
{% else %}
File.join(["///foo", "bar"]).should eq("///foo/bar")
File.join(["///foo", "//bar"]).should eq("///foo//bar")
File.join(["/foo/", "/bar"]).should eq("/foo/bar")
File.join(["foo", "bar", "baz"]).should eq("foo/bar/baz")
File.join(["foo", "//bar//", "baz///"]).should eq("foo//bar//baz///")
File.join(["/foo/", "/bar/", "/baz/"]).should eq("/foo/bar/baz/")
File.join(["", "foo"]).should eq("foo")
File.join(["foo", ""]).should eq("foo/")
File.join(["", "", "foo"]).should eq("foo")
File.join(["foo", "", "bar"]).should eq("foo/bar")
File.join(["foo", "", "", "bar"]).should eq("foo/bar")
File.join(["foo", "/", "bar"]).should eq("foo/bar")
File.join(["foo", "/", "/", "bar"]).should eq("foo/bar")
File.join(["/", "/foo", "/", "bar/", "/"]).should eq("/foo/bar/")
File.join(["foo"]).should eq("foo")
File.join("foo").should eq("foo")
{% end %}
end

it "chown" do
Expand Down
14 changes: 5 additions & 9 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ require "http/client/response"
require "../../../support/ssl"
require "../../../support/channel"

# TODO: replace with `HTTP::Client` once it supports connecting to Unix socket (#2735)
# TODO: replace with `HTTP::Client.get` once it supports connecting to Unix socket (#2735)
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
private def unix_request(path)
UNIXSocket.open(path) do |io|
request = HTTP::Request.new("GET", "/", HTTP::Headers{"X-Unix-Socket" => path})
request.to_io(io)

HTTP::Client::Response.from_io(io).body
HTTP::Client.new(io).get(path).body
end
end

Expand Down Expand Up @@ -97,7 +94,7 @@ describe HTTP::Server do

it "binds to different ports" do
server = HTTP::Server.new do |context|
context.response.print "Test Server (#{context.request.headers["Host"]?})"
context.response.print "Test Server (#{context.request.local_address})"
end

tcp_server = TCPServer.new("127.0.0.1", 0)
Expand Down Expand Up @@ -302,7 +299,7 @@ describe HTTP::Server do
describe "#bind_tls" do
it "binds SSL server context" do
server = HTTP::Server.new do |context|
context.response.puts "Test Server (#{context.request.headers["Host"]?})"
context.response.puts "Test Server (#{context.request.local_address})"
context.response.close
end

Expand Down Expand Up @@ -355,8 +352,7 @@ describe HTTP::Server do

begin
server = HTTP::Server.new do |context|
# TODO: Replace custom header with local_address (#5784)
context.response.print "Test Server (#{context.request.headers["X-Unix-Socket"]?})"
context.response.print "Test Server (#{context.request.local_address})"
context.response.close
end

Expand Down
10 changes: 2 additions & 8 deletions src/crystal/system/unix/lib_event2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ require "c/netdb"
{% if flag?(:openbsd) %}
@[Link("event_core")]
@[Link("event_extra")]
{% elsif compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
@[Link("event", pkg_config: "libevent")]
{% else %}
@[Link("event")]
@[Link("event", pkg_config: "libevent")]
{% end %}
{% if flag?(:preview_mt) %}
{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
@[Link("event_pthreads", pkg_config: "libevent_pthreads")]
{% else %}
@[Link("event_pthreads")]
{% end %}
@[Link("event_pthreads", pkg_config: "libevent_pthreads")]
{% end %}
lib LibEvent2
alias Int = LibC::Int
Expand Down
80 changes: 36 additions & 44 deletions src/http/request.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "./common"
require "uri"
require "http/params"
require "socket"

# An HTTP request.
#
Expand All @@ -20,47 +21,40 @@ class HTTP::Request
@query_params : URI::Params?
@uri : URI?

{% unless flag?(:win32) %}
# The network address that sent the request to an HTTP server.
#
# `HTTP::Server` will try to fill this property, and its value
# will have a format like "IP:port", but this format is not guaranteed.
# Middlewares can overwrite this value.
#
# Example:
#
# ```
# class ForwarderHandler
# include HTTP::Handler
#
# def call(context)
# if ip = context.request.headers["X-Real-IP"]? # When using a reverse proxy that guarantees this field.
# context.request.remote_address = Socket::IPAddress.new(ip, 0)
# end
# call_next(context)
# end
# end
#
# server = HTTP::Server.new([ForwarderHandler.new, HTTP::LogHandler.new])
# ```
#
# This property is not used by `HTTP::Client`.
property remote_address : Socket::Address?

# The network address of the HTTP server.
#
# `HTTP::Server` will try to fill this property, and its value
# will have a format like "IP:port", but this format is not guaranteed.
# Middlewares can overwrite this value.
#
# This property is not used by `HTTP::Client`.
property local_address : Socket::Address?
{% else %}
# TODO: Remove this once `Socket` is working on Windows

property remote_address : Nil
property local_address : Nil
{% end %}
# The network address that sent the request to an HTTP server.
#
# `HTTP::Server` will try to fill this property, and its value
# will have a format like "IP:port", but this format is not guaranteed.
# Middlewares can overwrite this value.
#
# Example:
#
# ```
# class ForwarderHandler
# include HTTP::Handler
#
# def call(context)
# if ip = context.request.headers["X-Real-IP"]? # When using a reverse proxy that guarantees this field.
# context.request.remote_address = Socket::IPAddress.new(ip, 0)
# end
# call_next(context)
# end
# end
#
# server = HTTP::Server.new([ForwarderHandler.new, HTTP::LogHandler.new])
# ```
#
# This property is not used by `HTTP::Client`.
property remote_address : Socket::Address?

# The network address of the HTTP server.
#
# `HTTP::Server` will try to fill this property, and its value
# will have a format like "IP:port", but this format is not guaranteed.
# Middlewares can overwrite this value.
#
# This property is not used by `HTTP::Client`.
property local_address : Socket::Address?

def self.new(method : String, resource : String, headers : Headers? = nil, body : String | Bytes | IO | Nil = nil, version = "HTTP/1.1")
# Duplicate headers to prevent the request from modifying data that the user might hold.
Expand Down Expand Up @@ -291,9 +285,7 @@ class HTTP::Request
host = header
else
port = port.to_i?(whitespace: false)
# TODO: Remove temporal fix when Socket::IPAddress has been ported to
# win32
unless port && {% if flag?(:win32) %}port.in?(0..UInt16::MAX){% else %}Socket::IPAddress.valid_port?(port){% end %}
unless port && Socket::IPAddress.valid_port?(port)
# what we identified as port is not valid, so use the entire header
host = header
end
Expand Down
6 changes: 1 addition & 5 deletions src/xml/libxml2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ require "./parser_options"
require "./html_parser_options"
require "./save_options"

{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
@[Link("xml2", pkg_config: "libxml-2.0")]
{% else %}
@[Link("xml2")]
{% end %}
@[Link("xml2", pkg_config: "libxml-2.0")]
lib LibXML
alias Int = LibC::Int

Expand Down
6 changes: 1 addition & 5 deletions src/yaml/lib_yaml.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require "./enums"

{% if compare_versions(Crystal::VERSION, "0.35.0-0") >= 0 %}
@[Link("yaml", pkg_config: "yaml-0.1")]
{% else %}
@[Link("yaml")]
{% end %}
@[Link("yaml", pkg_config: "yaml-0.1")]
lib LibYAML
alias Int = LibC::Int

Expand Down