Commit graph

230 commits

Author SHA1 Message Date
Jeremy Evans
4d03140009 [ruby/net-http] Don't invoke response block more than once due to retry
If a socket error occurs while performing a streaming download via
the response block provided to transport_request, avoid calling
the response block again as this would result in duplicate data
received by the client.

Fixes https://github.com/ruby/net-http/pull/86
Fixes https://github.com/ruby/net-http/pull/87

Fixes [Bug #11526]

114d01b092

Co-authored-by: Jeremy Stanley <jeremy@instructure.com>
2024-01-05 16:51:37 +00:00
Nobuyoshi Nakada
1b7376423d
Omit test_session_reuse_but_expire if OpenSSL 3.2.0 2023-11-26 01:18:48 +09:00
Takashi Kokubun
de37b78050 Omit a broken https test on MinGW
This started to reliably fail on MinGW at an irrelevant commit:
1899730212
1900010422
1900561380
1900584459
1900664949
1900710044
1901200064

So this failure is not detecting a new bug. Let's skip this until we fix
this test for MinGW.
2023-11-24 21:43:34 -08:00
Christian van Rensen
f109269a03 [ruby/net-http] fix a false-negative test
* no_proxy is meant to operate on the destination address, not on the name of the proxy
* if both end with `'.example'`, the test does not nail down the behaviour

bb9a5cfa3d
2023-10-30 09:27:06 +00:00
Jeremy Evans
3dec5dc346 [ruby/net-http] Force TLS version to 1.2 when using LibreSSL
This comment previously specified TLS 1.2, but actually set the
version to TLS 1.0.  LibreSSL 3.8.1 (included in OpenBSD 7.4)
dropped support for TLS 1.0/1.1 for security reasons, which
broke this test.  Switch the test to use TLS 1.2 as documented
so it will continue to work on OpenBSD 7.4+.

97be4de53a
2023-10-16 20:23:50 +00:00
Brian Hawley
9d58f93828 [ruby/net-http] Net::HTTPResponse nil checking
Fix nil handling in read_body and stream_check.

Fixes: #70

36f916ac18
2023-10-05 07:11:52 +00:00
Takashi Kokubun
23ec248e48 s/mjit/rjit/ 2023-03-06 23:44:01 -08:00
Takashi Kokubun
2e875549a9 s/MJIT/RJIT/ 2023-03-06 23:44:01 -08:00
Peter Zhu
de9e2a5ac7 [ruby/net-http] Fix test for Ruby head
The error raised for broken coderanges was changed in ruby/ruby@571d21f
and the test was fixed in that commit but not ported to this repo.

ruby/net-http@e6185dda26
2022-11-26 16:06:05 -05:00
Jeremy Evans
571d21fd4a Make String#rstrip{,!} raise Encoding::CompatibilityError for broken coderange
It's questionable whether we want to allow rstrip to work for strings
where the broken coderange occurs before the trailing whitespace and
not after, but this approach is probably simpler, and I don't think
users should expect string operations like rstrip to work on broken
strings.

In some cases, this changes rstrip to raise
Encoding::CompatibilityError instead of ArgumentError.  However, as
the problem is related to an encoding issue in the receiver, and due
not due to an issue with an argument, I think
Encoding::CompatibilityError is the more appropriate error.

Fixes [Bug #18931]
2022-11-24 18:24:42 -08:00
Jeremy Evans
cd77e71bba [ruby/net-http] Remove ENVIRONMENT_VARIABLE_IS_MULTIUSER_SAFE
This list is out of date.  At least OpenBSD since 2013 does not
allow one user to read the environment variables of a process
run by another user.

While we could try to keep the list updated, I think it's a bad
idea to not use the user/password from the environment, even if
another user on the system could read it.  If http_proxy exists
in the environment, and other users can read it, it doesn't
make it more secure for Ruby to ignore it.  You could argue that
it encourages poor security practices, but net/http should provide
mechanism, not policy.

Fixes [Bug #18908]

1e4585153d
2022-09-28 17:26:03 +09:00
Shishir Joshi
c310691dd8 [ruby/net-http] Make Net::HTTPHeader#content_range return nil on non-byte units
* Returning nil from the `content_range` method instead of raising an
  error when the unit in the content-range header is not "bytes".

Fix https://bugs.ruby-lang.org/issues/11450

0b5030dd86

Co-Authored-By: Nobuyoshi Nakada <nobu@ruby-lang.org>
2022-06-16 23:35:27 +09:00
Nobuyoshi Nakada
2223eb082a
Revert "HTTPHeader.content_range throws error on non-byte units"
This reverts commit 63546bfc15.
2022-06-16 22:10:59 +09:00
Shishir Joshi
63546bfc15
HTTPHeader.content_range throws error on non-byte units
* Added a nil check in Net::HTTPHeader#initialize_http_header for keys in the header that do not have any value
* Returning nil from the content_range method instead of raising an error when the unit in the content-range header is not bytes
* Modified initialize_http_header to match trunk

fix [Bug #11450]
fix https://github.com/ruby/ruby/pull/1018
2022-06-16 20:16:47 +09:00
Karol Bucek
cf73cf5981 [ruby/net-http] Feature detect to make net/http usable with JRuby
Handle missing session_new_cb= and do not call
session_cache_mode=, as JRuby SSL does not support
these methods.

3237ef4d8c
2022-04-20 13:01:08 +09:00
Jeremy Evans
90ccc5674a [ruby/net-http] Add ignore_eof access to HTTP and HTTPResponse
The ignore_eof setting on HTTPResponse makes it so an EOFError is
raised when reading bodies with a defined Content-Length, if the
body read was truncated due to the socket be closed.

The ignore_eof setting on HTTP sets the values used in responses
that are created by the object.

For backwards compatibility, the default is for both settings is
true.  However, unless you are specifically tested for and handling
truncated responses, it's a good idea to set ignore_eof to false so
that errors are raised for truncated responses, instead of those
errors silently being ignored.

Fixes [Bug #14972]

4d47e34995
2022-04-20 10:22:06 +09:00
Jeremy Evans
0579486f15 [ruby/net-http] Update the content-length heading when decoding bodies
Previously, the content-encoding header was removed and the body
was modified, but the content-length header was not modified,
resulting in the content-length header not matching the body
length.

Don't delete content-length before yielding inflate body, as that
causes a switch to read the entire body instead of reading in
chunks.

Fixes [Bug #16672]

58284e9710

Co-authored-by: st0012 <stan001212@gmail.com>
2022-04-14 00:11:26 +09:00
Jeremy Evans
ebb4378237 [ruby/net-http] Add HTTP#response_body_encoding for setting response body encoding
This allows for the ability to opt-in to a method to set the
encoding of response bodies.  By setting the accessor to a String
or Encoding instance, it will use the specified encoding.
Setting the value of true will try to detect the encoding of the
response body, either using the Content-Type header (assuming it
specifies charset) or by scanning for a <meta> tag in the document
that specifies the encoding.  The default is false in which case
no forcing of encoding will be done (same as before the patch).

Implements [Feature #2567]
Implements [Feature #15517]

6233e6b7c1

Co-authored-by: Yui Naruse <naruse@ruby-lang.org>
2022-04-12 00:17:34 +09:00
Jeremy Evans
7648bae4c8 [ruby/net-http] Revert "Update the content-length heading when decoding bodies"
This reverts commit a7cb30124c.

This is causing errors in Ruby's CI, will revert for now and
try again after testing a fix with Ruby's CI.

7b852b1feb
2022-04-03 06:51:07 +09:00
Jeremy Evans
58adb1636b [ruby/net-http] Update the content-length heading when decoding bodies
Previously, the content-encoding header was removed and the body
was modified, but the content-length header was not modified,
resulting in the content-length header not matching the body
length.

Fixes [Bug #16672]

a7cb30124c
2022-04-02 02:49:21 +09:00
Jeremy Evans
7529c53891 [ruby/net-http] Do not set SNI hostname if connecting to IP address
RFC 6066, section 3, explicitly disallows the use of an IP address
as an SNI server name.  So check if the connection is being made
to an IP address using the resolv regexps, and do not set an SNI
hostname in that case.

Recent changes to LibreSSL make it more strictly follow RFC 6066,
resulting an s.hostname= raising an error if passed an IP address.
When such verions of LibreSSL are used, this change not only fixes
the net/http tests, it also fixes tests for webrick and open-uri,
which both make SSL connections to 127.0.0.1 using net/http in
their tests.

Avoid warning in the openssl extension by unsetting
@ssl_context.verify_hostname if connecting to an IP address.
Make changes so that the post_connection_check still happens
when connecting to an IP address, which is necessary to keep
checking that the certificate returned includes the IP address,
which one of the tests depends on.

Revert the previous change that modified the regexp used for
checking the error message.

fa68e64bee
2022-02-03 05:10:21 +09:00
Kazuhiro NISHIYAMA
549af505c5 [ruby/net-http] Remove redundant MJIT condition
ref da0f67c038

dbeb5f1c8f
2022-01-17 17:11:27 +09:00
Kazuhiro NISHIYAMA
b9b6377401 [ruby/net-http] Fix unescaped . in Regexp
Use `include?` instead.

05022744a9
2022-01-14 08:16:37 +09:00
Hiroshi SHIBATA
9b3dcf193c [ruby/net-http] Use omit instead of skip for test-unit
843d4548de
2022-01-11 21:45:34 +09:00
Jeremy Evans
67612dadbc [ruby/net-http] Handle invalid use of IP addresses in SNI
Server Name Indication does not allow IP addresses (RFC 6066, section 3:
`Literal IPv4 and IPv6 addresses are not permitted in "HostName".`).

Recent versions of LibreSSL enforce this restriction, which raises
when setting the hostname to an IP address (s.hostname=), before
attempting to setup the SSL connection.

Since the certificate used in the test is already for localhost,
we cannot connect to localhost.  However, as all the test does is
check for a specific exception, just modify the regexp used so it
handles both cases.

6ab399db45
2022-01-04 08:25:58 +09:00
Takashi Kokubun
1a63468831
Prepare for removing RubyVM::JIT (#5262) 2021-12-13 23:07:46 -08:00
Jeremy Evans
fceaa6886b Sync test_http_request.rb after fix to net-http 2021-11-15 09:31:44 -08:00
Jeremy Evans
f1c3fc273d [ruby/net-http] Fix tests after change to allow HEAD to send Accept-Encoding
ca7bb8c51c
2021-11-16 02:21:57 +09:00
Koichi Sasada
b5531adf41 quick fix for test_initialize_HEAD
52ab9bbee9 makes a failure.

```
  1) Failure:
HTTPRequestTest#test_initialize_HEAD [/tmp/ruby/v3/src/trunk/test/net/http/test_http_request.rb:53]:
<{"accept"=>["*/*"], "user-agent"=>["Ruby"]}> expected but was
<{"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
 "accept"=>["*/*"],
 "user-agent"=>["Ruby"]}>.
```

I'm not sure it is correct, so please review later.
2021-11-11 18:04:00 +09:00
Kazuki Yamaguchi
5f2c4e344d [ruby/net-http] Reset keep_alive timer on new connection
The last_communicated timestamp is for HTTP persistent connection, to
decide whether the current TCP connection may be reused for the
subsequent requests or not. Naturally, the timer must be reset if the
connection is recreated since it is no longer relevant.

0a013de42d
2021-11-04 20:44:04 +09:00
Hiroshi SHIBATA
2bc85766c0
[ruby/net-http] Use omit instead of skip for test-unit
5bb14fd3bc
2021-09-11 10:25:34 +09:00
Hiroshi SHIBATA
f8ad51dd9a Fix test failure of 60b02db516 with Windows 2021-07-29 20:54:54 +09:00
Miguel Teixeira
60b02db516 [ruby/net-http] Enforce write timeout when body_stream is used
The existing implementation of `Net::HTTP#write_timeout` relies on
`Net::BefferedIO` to trigger the `Net::WriteTimeout` error. This commit
changes `send_request_with_body_stream` to remove the optimization that
was making `Net::HTTP#write_timeout` not work when `body_stream` is
used.

Open issue:
https://bugs.ruby-lang.org/issues/17933

a0fab1ab52
2021-07-29 15:53:54 +09:00
Kazuki Yamaguchi
364044e090 [ruby/net-http] Do not require stringio
It is not used in net/http library code since commit 15ccd0118c13
(r36473 in ruby svn trunk, 2012).

require's in test suite are also cleaned up.

996d18a43f
2021-05-06 15:58:41 +09:00
MSP-Greg
5a42ef496a Fix test/net/http/test_https.rb host naming for Windows 2021-05-04 08:53:58 +09:00
Nobuyoshi Nakada
14ee263505
Workaround failures on Windows 2021-05-04 00:59:29 +09:00
Lukas Eipert
842f00f452 [ruby/net-http] Decode user and password from env configured proxy
If someone sets an env variable defining a http_proxy, containing a
username / password with percent-encoded characters, then the resulting
base64 encoded auth header will be wrong.

For example, suppose a username is `Y\X` and the password is `R%S] ?X`.
Properly URL encoded the proxy url would be:

    http://Y%5CX:R%25S%5D%20%3FX@proxy.example:8000

The resulting proxy auth header should be: `WVxYOlIlU10gP1g=`, but the
getters defined by ruby StdLib `URI` return a username `Y%5CX` and
password `R%25S%5D%20%3FX`, resulting in `WSU1Q1g6UiUyNVMlNUQlMjAlM0ZY`.
As a result the proxy will deny the request.

Please note that this is my first contribution to the ruby ecosystem, to
standard lib especially and I am not a ruby developer.

References:

- https://gitlab.com/gitlab-org/gitlab/-/issues/289836
- https://bugs.ruby-lang.org/projects/ruby-master/repository/trunk/revisions/58461
- https://bugs.ruby-lang.org/issues/17542

e57d4f38aa
2021-04-28 11:01:23 +09:00
Kazuhiro NISHIYAMA
9de68a5221
Fix a warning
```
.../ruby/test/net/http/test_httpresponse.rb:81: warning: constant RubyVM::MJIT is deprecated
```
2021-01-23 10:52:17 +09:00
Nobuyoshi Nakada
d961f14df3 [ruby/net-http] RubyVM::MJIT is deprecated, prefer RubyVM::JIT now
abc6ea1877
2021-01-21 20:50:23 +09:00
Takashi Kokubun
e1fee7f949
Rename RubyVM::MJIT to RubyVM::JIT
because the name "MJIT" is an internal code name, it's inconsistent with
--jit while they are related to each other, and I want to discourage future
JIT implementation-specific (e.g. MJIT-specific) APIs by this rename.

[Feature #17490]
2021-01-13 22:46:51 -08:00
Benoit Daloze
b4ec4a41c2 Guard all accesses to RubyVM::MJIT with defined?(RubyVM::MJIT) &&
* Otherwise those tests, etc cannot run on alternative Ruby implementations.
2020-12-04 16:45:54 +01:00
Yusuke Endoh
0c611d7f4f test/net/http/test_https.rb: The test logic was buggy
The expected certs must be `[CA_CERT, SERVER_CERT]` before 1.1.1g and
`[SERVER_CERT]` after 1.1.1h.
2020-09-24 19:39:51 +09:00
Yusuke Endoh
1917afa34b test/net/http/test_https.rb: the order of verify_callback seems to vary
... depending upon the environment.
2020-09-24 19:34:16 +09:00
Yusuke Endoh
07786edd66 test/net/http/test_https.rb: Stop the error due to openssl 1.1.1h
On some environments that uses OpenSSL 1.1.1h, the two tests now fail.

20200924T062352Z.fail.html.gz
1159288773

```
  1) Failure:
TestNetHTTPS#test_get [/data/data/com.termux/files/home/cb/tmp/build/20200924T062352Z/ruby/test/net/http/test_https.rb:47]:
<"0\x82\x03\xED0\x82\x02\xD5\xA0\x03..."> expected but was
<"0\x82\x03\xE30\x82\x02\xCB\xA0\x03...">.
```

Not sure why, but verify_callback now seems to receive only SERVER_CERT
but not CA_CERT.
It would be good to investigate the issue furthermore, but tentatively,
I want to stop the failures.
2020-09-24 19:20:17 +09:00
Jeremy Evans
85dc570893 [ruby/net-http] Fix SSL session reuse test with LibreSSL 3.2+
5ae9620fbc
2020-08-03 18:56:14 +09:00
Jeremy Evans
20eb9e98b6 [ruby/net-http] Switch invalid server name format
invalid_servername is not a valid name in an SSL request due to
the use of the underscore, and LibreSSL 3.2.0 will raise an
exception for this.  These tests are not testing the allowed
characters in the server name, but how net/http handles cases where
the server name provided does not match the IP address you are
trying to connect to, so I think it's better to just modify the
tests to use a correct format.

While here, fix a typo in a test name, and use better code in the
ensure block so the same test doesn't issue both a failure and an
error.

0e8dc91120
2020-08-03 18:55:44 +09:00
Takashi Kokubun
574a4f0217
Skip test_read_body_block_mod for --jit-wait 2020-06-22 10:45:39 -07:00
Takashi Kokubun
7658a0f8fe
Try increasing read_timeout a little more
It failed again
http://ci.rvm.jp/results/trunk-mjit-wait@silicon-docker/3018488
2020-06-21 23:21:48 -07:00
Takashi Kokubun
eed4aaa0bf
Increase read_timeout for --jit-wait
This tries to fix a random failure like
http://ci.rvm.jp/results/trunk-mjit-wait@silicon-docker/3017837
2020-06-21 16:16:24 -07:00
Yusuke Endoh
5908914ea2 test/net/http/test_http.rb: relax open_timeout limit
The test fails randomly on the CI of OpenCSW SPARC Solaris 10:

20200321T091909Z.fail.html.gz
```
  1) Failure:
TestNetHTTP_v1_2_chunked#test_timeout_during_HTTP_session [/export/home/rubyci/unstable10s/tmp/build/20200321T091909Z/ruby/test/net/http/test_http.rb:575]:
[Net::ReadTimeout] exception expected, not #<Net::OpenTimeout: execution expired>.
```

The environment uses RUBY_TEST_SUBPROCESS_TIMEOUT_SCALE=20, so the open
timeout 0.1 sec. means 2.0 sec. for the environment, but it seems too
strict because the environment is painfully slow.
2020-03-21 23:09:17 +09:00