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

enable libxml2 "legacy" libraries #3247

Merged
merged 2 commits into from
Jun 23, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

* [CRuby] `Nokogiri::HTML5::Builder` is similar to `HTML4::Builder` but returns an `HTML5::Document`. [#3119] @flavorjones
* [CRuby] Attributes in an HTML5 document can be serialized individually, something that has always been supported by the HTML4 serializer. [#3125, #3127] @flavorjones
* [CRuby] Introduce a compile-time option, `--disable-xml2-legacy`, to remove from libxml2 its dependencies on `zlib` and `liblzma` and disable implicit `HTTP` network requests. These all remain enabled by default, and are present in the precompiled native gems. This option is a precursor for removing these libraries in a future major release, but may be interesting for the security-minded who do not need features like automatic decompression and would like to remove these dependencies. You can read more and give feedback on these plans in #3168. [#3247] @flavorjones


### Improved
Expand Down
164 changes: 90 additions & 74 deletions ext/nokogiri/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,6 @@
Look for headers in DIRECTORY.


Related to zlib:

--with-zlib-dir=DIRECTORY
Look for zlib headers and library in DIRECTORY.

--with-zlib-lib=DIRECTORY
Look for zlib library in DIRECTORY.

--with-zlib-include=DIRECTORY
Look for zlib headers in DIRECTORY.


Related to iconv:

--with-iconv-dir=DIRECTORY
Look for iconv headers and library in DIRECTORY.

--with-iconv-lib=DIRECTORY
Look for iconv library in DIRECTORY.

--with-iconv-include=DIRECTORY
Look for iconv headers in DIRECTORY.


Related to libxml2:

--with-xml2-dir=DIRECTORY
Expand All @@ -95,6 +71,10 @@
--with-xml2-source-dir=DIRECTORY
(dev only) Build libxml2 from the source code in DIRECTORY

--disable-xml2-legacy
Do not build libxml2 with zlib, liblzma, or HTTP support. This will become the default
in a future version of Nokogiri.


Related to libxslt:

Expand Down Expand Up @@ -123,6 +103,30 @@
Look for exslt headers in DIRECTORY.


Related to iconv:

--with-iconv-dir=DIRECTORY
Look for iconv headers and library in DIRECTORY.

--with-iconv-lib=DIRECTORY
Look for iconv library in DIRECTORY.

--with-iconv-include=DIRECTORY
Look for iconv headers in DIRECTORY.


Related to zlib (ignored if `--disable-xml2-legacy` is used):

--with-zlib-dir=DIRECTORY
Look for zlib headers and library in DIRECTORY.

--with-zlib-lib=DIRECTORY
Look for zlib library in DIRECTORY.

--with-zlib-include=DIRECTORY
Look for zlib headers in DIRECTORY.


Flags only used when building and using the packaged libraries:

--disable-static
Expand Down Expand Up @@ -181,6 +185,10 @@ def config_system_libraries?
end
end

def config_with_xml2_legacy?
enable_config("xml2-legacy", true)
end

def windows?
RbConfig::CONFIG["target_os"].match?(/mingw|mswin/)
end
Expand Down Expand Up @@ -707,13 +715,15 @@ def needs_darwin_linker_hack

if config_system_libraries?
message "Building nokogiri using system libraries.\n"
ensure_package_configuration(
opt: "zlib",
pc: "zlib",
lib: "z",
headers: "zlib.h",
func: "gzdopen",
)
if config_with_xml2_legacy?
ensure_package_configuration(
opt: "zlib",
pc: "zlib",
lib: "z",
headers: "zlib.h",
func: "gzdopen",
)
end
ensure_package_configuration(
opt: "xml2",
pc: "libxml-2.0",
Expand Down Expand Up @@ -757,58 +767,60 @@ def needs_darwin_linker_hack
require "yaml"
dependencies = YAML.load_file(File.join(PACKAGE_ROOT_DIR, "dependencies.yml"))

dir_config("zlib")
dir_config("zlib") if config_with_xml2_legacy?

if cross_build_p || windows?
zlib_recipe = process_recipe("zlib", dependencies["zlib"]["version"], static_p, cross_build_p) do |recipe|
recipe.files = [{
url: zlib_source(recipe.version),
sha256: dependencies["zlib"]["sha256"],
}]
if windows?
class << recipe
attr_accessor :cross_build_p

def configure
Dir.chdir(work_path) do
mk = File.read("win32/Makefile.gcc")
File.open("win32/Makefile.gcc", "wb") do |f|
f.puts "BINARY_PATH = #{path}/bin"
f.puts "LIBRARY_PATH = #{path}/lib"
f.puts "INCLUDE_PATH = #{path}/include"
mk.sub!(/^PREFIX\s*=\s*$/, "PREFIX = #{host}-") if cross_build_p
f.puts mk
if config_with_xml2_legacy?
zlib_recipe = process_recipe("zlib", dependencies["zlib"]["version"], static_p, cross_build_p) do |recipe|
recipe.files = [{
url: zlib_source(recipe.version),
sha256: dependencies["zlib"]["sha256"],
}]
if windows?
class << recipe
attr_accessor :cross_build_p

def configure
Dir.chdir(work_path) do
mk = File.read("win32/Makefile.gcc")
File.open("win32/Makefile.gcc", "wb") do |f|
f.puts "BINARY_PATH = #{path}/bin"
f.puts "LIBRARY_PATH = #{path}/lib"
f.puts "INCLUDE_PATH = #{path}/include"
mk.sub!(/^PREFIX\s*=\s*$/, "PREFIX = #{host}-") if cross_build_p
f.puts mk
end
end
end
end

def configured?
Dir.chdir(work_path) do
!!(File.read("win32/Makefile.gcc") =~ /^BINARY_PATH/)
def configured?
Dir.chdir(work_path) do
!!(File.read("win32/Makefile.gcc") =~ /^BINARY_PATH/)
end
end
end

def compile
execute("compile", "make -f win32/Makefile.gcc")
end
def compile
execute("compile", "make -f win32/Makefile.gcc")
end

def install
execute("install", "make -f win32/Makefile.gcc install")
def install
execute("install", "make -f win32/Makefile.gcc install")
end
end
end
recipe.cross_build_p = cross_build_p
else
class << recipe
def configure
env = {}
env["CFLAGS"] = concat_flags(ENV["CFLAGS"], "-fPIC", "-g")
env["CHOST"] = host
execute("configure", ["./configure", "--static", configure_prefix], { env: env })
if darwin?
# needed as of zlib 1.2.13
Dir.chdir(work_path) do
makefile = File.read("Makefile").gsub(/^AR=.*$/, "AR=#{host}-libtool")
File.open("Makefile", "w") { |m| m.write(makefile) }
recipe.cross_build_p = cross_build_p
else
class << recipe
def configure
env = {}
env["CFLAGS"] = concat_flags(ENV["CFLAGS"], "-fPIC", "-g")
env["CHOST"] = host
execute("configure", ["./configure", "--static", configure_prefix], { env: env })
if darwin?
# needed as of zlib 1.2.13
Dir.chdir(work_path) do
makefile = File.read("Makefile").gsub(/^AR=.*$/, "AR=#{host}-libtool")
File.open("Makefile", "w") { |m| m.write(makefile) }
end
end
end
end
Expand Down Expand Up @@ -901,6 +913,10 @@ def configure
cppflags = concat_flags(cppflags, "-DNOKOGIRI_PRECOMPILED_LIBRARIES")
end

if config_with_xml2_legacy?
recipe.configure_options << "--with-legacy"
end

if zlib_recipe
recipe.configure_options << "--with-zlib=#{zlib_recipe.path}"
end
Expand Down
3 changes: 3 additions & 0 deletions ext/nokogiri/nokogiri.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ Init_nokogiri(void)
rb_const_set(mNokogiri, rb_intern("LIBXSLT_COMPILED_VERSION"), NOKOGIRI_STR_NEW2(LIBXSLT_DOTTED_VERSION));
rb_const_set(mNokogiri, rb_intern("LIBXSLT_LOADED_VERSION"), NOKOGIRI_STR_NEW2(xsltEngineVersion));

rb_const_set(mNokogiri, rb_intern("LIBXML_ZLIB_ENABLED"),
xmlHasFeature(XML_WITH_ZLIB) == 1 ? Qtrue : Qfalse);

#ifdef NOKOGIRI_PACKAGED_LIBRARIES
rb_const_set(mNokogiri, rb_intern("PACKAGED_LIBRARIES"), Qtrue);
# ifdef NOKOGIRI_PRECOMPILED_LIBRARIES
Expand Down
Binary file added test/files/staff.xml.gz
Binary file not shown.
10 changes: 10 additions & 0 deletions test/xml/sax/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ def call_parse_io_with_encoding(encoding)
end
end

it "parses a compressed file" do
skip("libxml2 legacy support") unless Nokogiri.uses_libxml? && Nokogiri::LIBXML_ZLIB_ENABLED

filename = XML_FILE + ".gz"
parser.parse_file(filename)

refute_nil(parser.document.start_elements)
assert_operator(parser.document.start_elements.count, :>, 30)
end

it :test_render_parse_nil_param do
assert_raises(TypeError) { parser.parse_memory(nil) }
end
Expand Down