net-http 0.3.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/{LICENSE.txt → BSDL} +3 -3
- data/COPYING +56 -0
- data/Gemfile +1 -0
- data/README.md +2 -1
- data/Rakefile +0 -7
- data/doc/net-http/examples.rdoc +2 -1
- data/doc/net-http/included_getters.rdoc +3 -0
- data/lib/net/http/exceptions.rb +1 -1
- data/lib/net/http/generic_request.rb +91 -15
- data/lib/net/http/header.rb +165 -68
- data/lib/net/http/proxy_delta.rb +1 -1
- data/lib/net/http/request.rb +50 -5
- data/lib/net/http/requests.rb +36 -1
- data/lib/net/http/response.rb +42 -22
- data/lib/net/http/responses.rb +507 -55
- data/lib/net/http/status.rb +7 -6
- data/lib/net/http.rb +1062 -498
- data/lib/net/https.rb +1 -1
- data/net-http.gemspec +9 -4
- metadata +9 -11
- data/.github/dependabot.yml +0 -6
- data/.github/workflows/test.yml +0 -22
- data/.gitignore +0 -7
- data/lib/net/http/backward.rb +0 -40
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91e4788b01779aad8b10d32588d21f857d66a5b9d752bd4226b8b624b651877d
|
4
|
+
data.tar.gz: 222d82ca817154eef246ad62f68c571deb2bfb6bc5bcfceca185066ac7773ff0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f1d6bbc6f5cea9b1b27c93dc5b2aba277e3fa6919aecc3044b69e5dd1a628b2e6a2ccc4074c3b6d3ec2bc98b59a47ce6fa6b7e340cbb7a892633804347885ff
|
7
|
+
data.tar.gz: 45aacf67fd2756870eb98199970d0bcd584547a5731e457fcce93304c93cdbe77c9c2a3a633395215e39dee657b912f542b459cccab4a05fe913af2819878952
|
data/{LICENSE.txt → BSDL}
RENAMED
@@ -4,10 +4,10 @@ Redistribution and use in source and binary forms, with or without
|
|
4
4
|
modification, are permitted provided that the following conditions
|
5
5
|
are met:
|
6
6
|
1. Redistributions of source code must retain the above copyright
|
7
|
-
notice, this list of conditions and the following disclaimer.
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
8
|
2. Redistributions in binary form must reproduce the above copyright
|
9
|
-
notice, this list of conditions and the following disclaimer in the
|
10
|
-
documentation and/or other materials provided with the distribution.
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
11
|
|
12
12
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
13
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
data/COPYING
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
|
2
|
+
You can redistribute it and/or modify it under either the terms of the
|
3
|
+
2-clause BSDL (see the file BSDL), or the conditions below:
|
4
|
+
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
6
|
+
software without restriction, provided that you duplicate all of the
|
7
|
+
original copyright notices and associated disclaimers.
|
8
|
+
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
10
|
+
you do at least ONE of the following:
|
11
|
+
|
12
|
+
a. place your modifications in the Public Domain or otherwise
|
13
|
+
make them Freely Available, such as by posting said
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
15
|
+
the author to include your modifications in the software.
|
16
|
+
|
17
|
+
b. use the modified software only within your corporation or
|
18
|
+
organization.
|
19
|
+
|
20
|
+
c. give non-standard binaries non-standard names, with
|
21
|
+
instructions on where to get the original software distribution.
|
22
|
+
|
23
|
+
d. make other distribution arrangements with the author.
|
24
|
+
|
25
|
+
3. You may distribute the software in object code or binary form,
|
26
|
+
provided that you do at least ONE of the following:
|
27
|
+
|
28
|
+
a. distribute the binaries and library files of the software,
|
29
|
+
together with instructions (in the manual page or equivalent)
|
30
|
+
on where to get the original distribution.
|
31
|
+
|
32
|
+
b. accompany the distribution with the machine-readable source of
|
33
|
+
the software.
|
34
|
+
|
35
|
+
c. give non-standard binaries non-standard names, with
|
36
|
+
instructions on where to get the original software distribution.
|
37
|
+
|
38
|
+
d. make other distribution arrangements with the author.
|
39
|
+
|
40
|
+
4. You may modify and include the part of the software into any other
|
41
|
+
software (possibly commercial). But some files in the distribution
|
42
|
+
are not written by the author, so that they are not under these terms.
|
43
|
+
|
44
|
+
For the list of those files and their copying conditions, see the
|
45
|
+
file LEGAL.
|
46
|
+
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
48
|
+
output from the software do not automatically fall under the
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
51
|
+
software.
|
52
|
+
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
+
PURPOSE.
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
|
3
3
|
Net::HTTP provides a rich library which can be used to build HTTP
|
4
4
|
user-agents. For more details about HTTP see
|
5
|
-
[
|
5
|
+
[RFC9110 HTTP Semantics](https://www.ietf.org/rfc/rfc9110.html) and
|
6
|
+
[RFC9112 HTTP/1.1](https://www.ietf.org/rfc/rfc9112.html).
|
6
7
|
|
7
8
|
Net::HTTP is designed to work closely with URI. URI::HTTP#host,
|
8
9
|
URI::HTTP#port and URI::HTTP#request_uri are designed to work with
|
data/Rakefile
CHANGED
@@ -7,11 +7,4 @@ Rake::TestTask.new(:test) do |t|
|
|
7
7
|
t.test_files = FileList["test/**/test_*.rb"]
|
8
8
|
end
|
9
9
|
|
10
|
-
task :sync_tool do
|
11
|
-
require 'fileutils'
|
12
|
-
FileUtils.cp "../ruby/tool/lib/core_assertions.rb", "./test/lib"
|
13
|
-
FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
|
14
|
-
FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
|
15
|
-
end
|
16
|
-
|
17
10
|
task :default => :test
|
data/doc/net-http/examples.rdoc
CHANGED
@@ -10,9 +10,10 @@ Many code examples here use these example websites:
|
|
10
10
|
|
11
11
|
Some examples also assume these variables:
|
12
12
|
|
13
|
-
uri = URI('https://jsonplaceholder.typicode.com')
|
13
|
+
uri = URI('https://jsonplaceholder.typicode.com/')
|
14
14
|
uri.freeze # Examples may not modify.
|
15
15
|
hostname = uri.hostname # => "jsonplaceholder.typicode.com"
|
16
|
+
path = uri.path # => "/"
|
16
17
|
port = uri.port # => 443
|
17
18
|
|
18
19
|
So that example requests may be written as:
|
data/lib/net/http/exceptions.rb
CHANGED
@@ -1,14 +1,18 @@
|
|
1
|
-
# frozen_string_literal:
|
2
|
-
# HTTPGenericRequest is the parent of the Net::HTTPRequest class.
|
3
|
-
# Do not use this directly; use a subclass of Net::HTTPRequest.
|
1
|
+
# frozen_string_literal: true
|
4
2
|
#
|
5
|
-
#
|
3
|
+
# \HTTPGenericRequest is the parent of the Net::HTTPRequest class.
|
4
|
+
#
|
5
|
+
# Do not use this directly; instead, use a subclass of Net::HTTPRequest.
|
6
|
+
#
|
7
|
+
# == About the Examples
|
8
|
+
#
|
9
|
+
# :include: doc/net-http/examples.rdoc
|
6
10
|
#
|
7
11
|
class Net::HTTPGenericRequest
|
8
12
|
|
9
13
|
include Net::HTTPHeader
|
10
14
|
|
11
|
-
def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
|
15
|
+
def initialize(m, reqbody, resbody, uri_or_path, initheader = nil) # :nodoc:
|
12
16
|
@method = m
|
13
17
|
@request_has_body = reqbody
|
14
18
|
@response_has_body = resbody
|
@@ -19,7 +23,7 @@ class Net::HTTPGenericRequest
|
|
19
23
|
raise ArgumentError, "no host component for URI" unless (hostname && hostname.length > 0)
|
20
24
|
@uri = uri_or_path.dup
|
21
25
|
host = @uri.hostname.dup
|
22
|
-
host << ":"
|
26
|
+
host << ":" << @uri.port.to_s if @uri.port != @uri.default_port
|
23
27
|
@path = uri_or_path.request_uri
|
24
28
|
raise ArgumentError, "no HTTP request path given" unless @path
|
25
29
|
else
|
@@ -53,15 +57,47 @@ class Net::HTTPGenericRequest
|
|
53
57
|
@body_data = nil
|
54
58
|
end
|
55
59
|
|
60
|
+
# Returns the string method name for the request:
|
61
|
+
#
|
62
|
+
# Net::HTTP::Get.new(uri).method # => "GET"
|
63
|
+
# Net::HTTP::Post.new(uri).method # => "POST"
|
64
|
+
#
|
56
65
|
attr_reader :method
|
66
|
+
|
67
|
+
# Returns the string path for the request:
|
68
|
+
#
|
69
|
+
# Net::HTTP::Get.new(uri).path # => "/"
|
70
|
+
# Net::HTTP::Post.new('example.com').path # => "example.com"
|
71
|
+
#
|
57
72
|
attr_reader :path
|
73
|
+
|
74
|
+
# Returns the URI object for the request, or +nil+ if none:
|
75
|
+
#
|
76
|
+
# Net::HTTP::Get.new(uri).uri
|
77
|
+
# # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
|
78
|
+
# Net::HTTP::Get.new('example.com').uri # => nil
|
79
|
+
#
|
58
80
|
attr_reader :uri
|
59
81
|
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
82
|
+
# Returns +false+ if the request's header <tt>'Accept-Encoding'</tt>
|
83
|
+
# has been set manually or deleted
|
84
|
+
# (indicating that the user intends to handle encoding in the response),
|
85
|
+
# +true+ otherwise:
|
86
|
+
#
|
87
|
+
# req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
|
88
|
+
# req['Accept-Encoding'] # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
|
89
|
+
# req.decode_content # => true
|
90
|
+
# req['Accept-Encoding'] = 'foo'
|
91
|
+
# req.decode_content # => false
|
92
|
+
# req.delete('Accept-Encoding')
|
93
|
+
# req.decode_content # => false
|
94
|
+
#
|
63
95
|
attr_reader :decode_content
|
64
96
|
|
97
|
+
# Returns a string representation of the request:
|
98
|
+
#
|
99
|
+
# Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"
|
100
|
+
#
|
65
101
|
def inspect
|
66
102
|
"\#<#{self.class} #{@method}>"
|
67
103
|
end
|
@@ -76,21 +112,45 @@ class Net::HTTPGenericRequest
|
|
76
112
|
super key, val
|
77
113
|
end
|
78
114
|
|
115
|
+
# Returns whether the request may have a body:
|
116
|
+
#
|
117
|
+
# Net::HTTP::Post.new(uri).request_body_permitted? # => true
|
118
|
+
# Net::HTTP::Get.new(uri).request_body_permitted? # => false
|
119
|
+
#
|
79
120
|
def request_body_permitted?
|
80
121
|
@request_has_body
|
81
122
|
end
|
82
123
|
|
124
|
+
# Returns whether the response may have a body:
|
125
|
+
#
|
126
|
+
# Net::HTTP::Post.new(uri).response_body_permitted? # => true
|
127
|
+
# Net::HTTP::Head.new(uri).response_body_permitted? # => false
|
128
|
+
#
|
83
129
|
def response_body_permitted?
|
84
130
|
@response_has_body
|
85
131
|
end
|
86
132
|
|
87
|
-
def body_exist?
|
133
|
+
def body_exist? # :nodoc:
|
88
134
|
warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?", uplevel: 1 if $VERBOSE
|
89
135
|
response_body_permitted?
|
90
136
|
end
|
91
137
|
|
138
|
+
# Returns the string body for the request, or +nil+ if there is none:
|
139
|
+
#
|
140
|
+
# req = Net::HTTP::Post.new(uri)
|
141
|
+
# req.body # => nil
|
142
|
+
# req.body = '{"title": "foo","body": "bar","userId": 1}'
|
143
|
+
# req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
|
144
|
+
#
|
92
145
|
attr_reader :body
|
93
146
|
|
147
|
+
# Sets the body for the request:
|
148
|
+
#
|
149
|
+
# req = Net::HTTP::Post.new(uri)
|
150
|
+
# req.body # => nil
|
151
|
+
# req.body = '{"title": "foo","body": "bar","userId": 1}'
|
152
|
+
# req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
|
153
|
+
#
|
94
154
|
def body=(str)
|
95
155
|
@body = str
|
96
156
|
@body_stream = nil
|
@@ -98,8 +158,24 @@ class Net::HTTPGenericRequest
|
|
98
158
|
str
|
99
159
|
end
|
100
160
|
|
161
|
+
# Returns the body stream object for the request, or +nil+ if there is none:
|
162
|
+
#
|
163
|
+
# req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
|
164
|
+
# req.body_stream # => nil
|
165
|
+
# require 'stringio'
|
166
|
+
# req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
|
167
|
+
# req.body_stream # => #<StringIO:0x0000027d1e5affa8>
|
168
|
+
#
|
101
169
|
attr_reader :body_stream
|
102
170
|
|
171
|
+
# Sets the body stream for the request:
|
172
|
+
#
|
173
|
+
# req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
|
174
|
+
# req.body_stream # => nil
|
175
|
+
# require 'stringio'
|
176
|
+
# req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
|
177
|
+
# req.body_stream # => #<StringIO:0x0000027d1e5affa8>
|
178
|
+
#
|
103
179
|
def body_stream=(input)
|
104
180
|
@body = nil
|
105
181
|
@body_stream = input
|
@@ -136,15 +212,15 @@ class Net::HTTPGenericRequest
|
|
136
212
|
return unless @uri
|
137
213
|
|
138
214
|
if ssl
|
139
|
-
scheme = 'https'
|
215
|
+
scheme = 'https'
|
140
216
|
klass = URI::HTTPS
|
141
217
|
else
|
142
|
-
scheme = 'http'
|
218
|
+
scheme = 'http'
|
143
219
|
klass = URI::HTTP
|
144
220
|
end
|
145
221
|
|
146
222
|
if host = self['host']
|
147
|
-
host.sub!(/:.*/m, ''
|
223
|
+
host.sub!(/:.*/m, '')
|
148
224
|
elsif host = @uri.host
|
149
225
|
else
|
150
226
|
host = addr
|
@@ -240,7 +316,7 @@ class Net::HTTPGenericRequest
|
|
240
316
|
boundary ||= SecureRandom.urlsafe_base64(40)
|
241
317
|
chunked_p = chunked?
|
242
318
|
|
243
|
-
buf = ''
|
319
|
+
buf = +''
|
244
320
|
params.each do |key, value, h={}|
|
245
321
|
key = quote_string(key, charset)
|
246
322
|
filename =
|
@@ -325,7 +401,7 @@ class Net::HTTPGenericRequest
|
|
325
401
|
if /[\r\n]/ =~ reqline
|
326
402
|
raise ArgumentError, "A Request-Line must not contain CR or LF"
|
327
403
|
end
|
328
|
-
buf =
|
404
|
+
buf = +''
|
329
405
|
buf << reqline << "\r\n"
|
330
406
|
each_capitalized do |k,v|
|
331
407
|
buf << "#{k}: #{v}\r\n"
|
data/lib/net/http/header.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# frozen_string_literal:
|
1
|
+
# frozen_string_literal: true
|
2
2
|
#
|
3
3
|
# The \HTTPHeader module provides access to \HTTP headers.
|
4
4
|
#
|
@@ -179,6 +179,8 @@
|
|
179
179
|
# - #each_value: Passes each string field value to the block.
|
180
180
|
#
|
181
181
|
module Net::HTTPHeader
|
182
|
+
MAX_KEY_LENGTH = 1024
|
183
|
+
MAX_FIELD_LENGTH = 65536
|
182
184
|
|
183
185
|
def initialize_http_header(initheader) #:nodoc:
|
184
186
|
@header = {}
|
@@ -189,6 +191,12 @@ module Net::HTTPHeader
|
|
189
191
|
warn "net/http: nil HTTP header: #{key}", uplevel: 3 if $VERBOSE
|
190
192
|
else
|
191
193
|
value = value.strip # raise error for invalid byte sequences
|
194
|
+
if key.to_s.bytesize > MAX_KEY_LENGTH
|
195
|
+
raise ArgumentError, "too long (#{key.bytesize} bytes) header: #{key[0, 30].inspect}..."
|
196
|
+
end
|
197
|
+
if value.to_s.bytesize > MAX_FIELD_LENGTH
|
198
|
+
raise ArgumentError, "header #{key} has too long field value: #{value.bytesize}"
|
199
|
+
end
|
192
200
|
if value.count("\r\n") > 0
|
193
201
|
raise ArgumentError, "header #{key} has field value #{value.inspect}, this cannot include CR/LF"
|
194
202
|
end
|
@@ -483,7 +491,7 @@ module Net::HTTPHeader
|
|
483
491
|
alias canonical_each each_capitalized
|
484
492
|
|
485
493
|
def capitalize(name)
|
486
|
-
name.to_s.split(
|
494
|
+
name.to_s.split('-'.freeze).map {|s| s.capitalize }.join('-'.freeze)
|
487
495
|
end
|
488
496
|
private :capitalize
|
489
497
|
|
@@ -691,10 +699,14 @@ module Net::HTTPHeader
|
|
691
699
|
# res.content_type # => "application/json"
|
692
700
|
#
|
693
701
|
def content_type
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
702
|
+
main = main_type()
|
703
|
+
return nil unless main
|
704
|
+
|
705
|
+
sub = sub_type()
|
706
|
+
if sub
|
707
|
+
"#{main}/#{sub}"
|
708
|
+
else
|
709
|
+
main
|
698
710
|
end
|
699
711
|
end
|
700
712
|
|
@@ -763,19 +775,38 @@ module Net::HTTPHeader
|
|
763
775
|
|
764
776
|
alias content_type= set_content_type
|
765
777
|
|
766
|
-
#
|
767
|
-
#
|
768
|
-
#
|
769
|
-
#
|
778
|
+
# Sets the request body to a URL-encoded string derived from argument +params+,
|
779
|
+
# and sets request header field <tt>'Content-Type'</tt>
|
780
|
+
# to <tt>'application/x-www-form-urlencoded'</tt>.
|
781
|
+
#
|
782
|
+
# The resulting request is suitable for HTTP request +POST+ or +PUT+.
|
783
|
+
#
|
784
|
+
# Argument +params+ must be suitable for use as argument +enum+ to
|
785
|
+
# {URI.encode_www_form}[https://docs.ruby-lang.org/en/master/URI.html#method-c-encode_www_form].
|
786
|
+
#
|
787
|
+
# With only argument +params+ given,
|
788
|
+
# sets the body to a URL-encoded string with the default separator <tt>'&'</tt>:
|
770
789
|
#
|
771
|
-
#
|
772
|
-
# application/x-www-form-urlencoded
|
790
|
+
# req = Net::HTTP::Post.new('example.com')
|
773
791
|
#
|
774
|
-
#
|
792
|
+
# req.set_form_data(q: 'ruby', lang: 'en')
|
793
|
+
# req.body # => "q=ruby&lang=en"
|
794
|
+
# req['Content-Type'] # => "application/x-www-form-urlencoded"
|
775
795
|
#
|
776
|
-
#
|
777
|
-
#
|
778
|
-
#
|
796
|
+
# req.set_form_data([['q', 'ruby'], ['lang', 'en']])
|
797
|
+
# req.body # => "q=ruby&lang=en"
|
798
|
+
#
|
799
|
+
# req.set_form_data(q: ['ruby', 'perl'], lang: 'en')
|
800
|
+
# req.body # => "q=ruby&q=perl&lang=en"
|
801
|
+
#
|
802
|
+
# req.set_form_data([['q', 'ruby'], ['q', 'perl'], ['lang', 'en']])
|
803
|
+
# req.body # => "q=ruby&q=perl&lang=en"
|
804
|
+
#
|
805
|
+
# With string argument +sep+ also given,
|
806
|
+
# uses that string as the separator:
|
807
|
+
#
|
808
|
+
# req.set_form_data({q: 'ruby', lang: 'en'}, '|')
|
809
|
+
# req.body # => "q=ruby|lang=en"
|
779
810
|
#
|
780
811
|
# Net::HTTPHeader#form_data= is an alias for Net::HTTPHeader#set_form_data.
|
781
812
|
def set_form_data(params, sep = '&')
|
@@ -787,56 +818,108 @@ module Net::HTTPHeader
|
|
787
818
|
|
788
819
|
alias form_data= set_form_data
|
789
820
|
|
790
|
-
#
|
791
|
-
#
|
792
|
-
#
|
793
|
-
#
|
794
|
-
#
|
795
|
-
#
|
796
|
-
#
|
797
|
-
#
|
798
|
-
#
|
799
|
-
#
|
800
|
-
#
|
801
|
-
#
|
802
|
-
#
|
803
|
-
#
|
804
|
-
#
|
805
|
-
#
|
806
|
-
#
|
807
|
-
#
|
808
|
-
#
|
809
|
-
#
|
810
|
-
#
|
811
|
-
#
|
812
|
-
#
|
813
|
-
#
|
814
|
-
#
|
815
|
-
#
|
816
|
-
#
|
817
|
-
#
|
818
|
-
#
|
819
|
-
#
|
820
|
-
#
|
821
|
-
#
|
822
|
-
#
|
823
|
-
#
|
824
|
-
#
|
825
|
-
#
|
826
|
-
#
|
827
|
-
#
|
828
|
-
#
|
829
|
-
#
|
830
|
-
#
|
831
|
-
#
|
832
|
-
#
|
833
|
-
#
|
834
|
-
#
|
835
|
-
#
|
836
|
-
#
|
837
|
-
#
|
838
|
-
#
|
839
|
-
#
|
821
|
+
# Stores form data to be used in a +POST+ or +PUT+ request.
|
822
|
+
#
|
823
|
+
# The form data given in +params+ consists of zero or more fields;
|
824
|
+
# each field is:
|
825
|
+
#
|
826
|
+
# - A scalar value.
|
827
|
+
# - A name/value pair.
|
828
|
+
# - An IO stream opened for reading.
|
829
|
+
#
|
830
|
+
# Argument +params+ should be an
|
831
|
+
# {Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html#module-Enumerable-label-Enumerable+in+Ruby+Classes]
|
832
|
+
# (method <tt>params.map</tt> will be called),
|
833
|
+
# and is often an array or hash.
|
834
|
+
#
|
835
|
+
# First, we set up a request:
|
836
|
+
#
|
837
|
+
# _uri = uri.dup
|
838
|
+
# _uri.path ='/posts'
|
839
|
+
# req = Net::HTTP::Post.new(_uri)
|
840
|
+
#
|
841
|
+
# <b>Argument +params+ As an Array</b>
|
842
|
+
#
|
843
|
+
# When +params+ is an array,
|
844
|
+
# each of its elements is a subarray that defines a field;
|
845
|
+
# the subarray may contain:
|
846
|
+
#
|
847
|
+
# - One string:
|
848
|
+
#
|
849
|
+
# req.set_form([['foo'], ['bar'], ['baz']])
|
850
|
+
#
|
851
|
+
# - Two strings:
|
852
|
+
#
|
853
|
+
# req.set_form([%w[foo 0], %w[bar 1], %w[baz 2]])
|
854
|
+
#
|
855
|
+
# - When argument +enctype+ (see below) is given as
|
856
|
+
# <tt>'multipart/form-data'</tt>:
|
857
|
+
#
|
858
|
+
# - A string name and an IO stream opened for reading:
|
859
|
+
#
|
860
|
+
# require 'stringio'
|
861
|
+
# req.set_form([['file', StringIO.new('Ruby is cool.')]])
|
862
|
+
#
|
863
|
+
# - A string name, an IO stream opened for reading,
|
864
|
+
# and an options hash, which may contain these entries:
|
865
|
+
#
|
866
|
+
# - +:filename+: The name of the file to use.
|
867
|
+
# - +:content_type+: The content type of the uploaded file.
|
868
|
+
#
|
869
|
+
# Example:
|
870
|
+
#
|
871
|
+
# req.set_form([['file', file, {filename: "other-filename.foo"}]]
|
872
|
+
#
|
873
|
+
# The various forms may be mixed:
|
874
|
+
#
|
875
|
+
# req.set_form(['foo', %w[bar 1], ['file', file]])
|
876
|
+
#
|
877
|
+
# <b>Argument +params+ As a Hash</b>
|
878
|
+
#
|
879
|
+
# When +params+ is a hash,
|
880
|
+
# each of its entries is a name/value pair that defines a field:
|
881
|
+
#
|
882
|
+
# - The name is a string.
|
883
|
+
# - The value may be:
|
884
|
+
#
|
885
|
+
# - +nil+.
|
886
|
+
# - Another string.
|
887
|
+
# - An IO stream opened for reading
|
888
|
+
# (only when argument +enctype+ -- see below -- is given as
|
889
|
+
# <tt>'multipart/form-data'</tt>).
|
890
|
+
#
|
891
|
+
# Examples:
|
892
|
+
#
|
893
|
+
# # Nil-valued fields.
|
894
|
+
# req.set_form({'foo' => nil, 'bar' => nil, 'baz' => nil})
|
895
|
+
#
|
896
|
+
# # String-valued fields.
|
897
|
+
# req.set_form({'foo' => 0, 'bar' => 1, 'baz' => 2})
|
898
|
+
#
|
899
|
+
# # IO-valued field.
|
900
|
+
# require 'stringio'
|
901
|
+
# req.set_form({'file' => StringIO.new('Ruby is cool.')})
|
902
|
+
#
|
903
|
+
# # Mixture of fields.
|
904
|
+
# req.set_form({'foo' => nil, 'bar' => 1, 'file' => file})
|
905
|
+
#
|
906
|
+
# Optional argument +enctype+ specifies the value to be given
|
907
|
+
# to field <tt>'Content-Type'</tt>, and must be one of:
|
908
|
+
#
|
909
|
+
# - <tt>'application/x-www-form-urlencoded'</tt> (the default).
|
910
|
+
# - <tt>'multipart/form-data'</tt>;
|
911
|
+
# see {RFC 7578}[https://www.rfc-editor.org/rfc/rfc7578].
|
912
|
+
#
|
913
|
+
# Optional argument +formopt+ is a hash of options
|
914
|
+
# (applicable only when argument +enctype+
|
915
|
+
# is <tt>'multipart/form-data'</tt>)
|
916
|
+
# that may include the following entries:
|
917
|
+
#
|
918
|
+
# - +:boundary+: The value is the boundary string for the multipart message.
|
919
|
+
# If not given, the boundary is a random string.
|
920
|
+
# See {Boundary}[https://www.rfc-editor.org/rfc/rfc7578#section-4.1].
|
921
|
+
# - +:charset+: Value is the character set for the form submission.
|
922
|
+
# Field names and values of non-file fields should be encoded with this charset.
|
840
923
|
#
|
841
924
|
def set_form(params, enctype='application/x-www-form-urlencoded', formopt={})
|
842
925
|
@body_data = params
|
@@ -852,12 +935,24 @@ module Net::HTTPHeader
|
|
852
935
|
end
|
853
936
|
end
|
854
937
|
|
855
|
-
#
|
938
|
+
# Sets header <tt>'Authorization'</tt> using the given
|
939
|
+
# +account+ and +password+ strings:
|
940
|
+
#
|
941
|
+
# req.basic_auth('my_account', 'my_password')
|
942
|
+
# req['Authorization']
|
943
|
+
# # => "Basic bXlfYWNjb3VudDpteV9wYXNzd29yZA=="
|
944
|
+
#
|
856
945
|
def basic_auth(account, password)
|
857
946
|
@header['authorization'] = [basic_encode(account, password)]
|
858
947
|
end
|
859
948
|
|
860
|
-
#
|
949
|
+
# Sets header <tt>'Proxy-Authorization'</tt> using the given
|
950
|
+
# +account+ and +password+ strings:
|
951
|
+
#
|
952
|
+
# req.proxy_basic_auth('my_account', 'my_password')
|
953
|
+
# req['Proxy-Authorization']
|
954
|
+
# # => "Basic bXlfYWNjb3VudDpteV9wYXNzd29yZA=="
|
955
|
+
#
|
861
956
|
def proxy_basic_auth(account, password)
|
862
957
|
@header['proxy-authorization'] = [basic_encode(account, password)]
|
863
958
|
end
|
@@ -867,6 +962,7 @@ module Net::HTTPHeader
|
|
867
962
|
end
|
868
963
|
private :basic_encode
|
869
964
|
|
965
|
+
# Returns whether the HTTP session is to be closed.
|
870
966
|
def connection_close?
|
871
967
|
token = /(?:\A|,)\s*close\s*(?:\z|,)/i
|
872
968
|
@header['connection']&.grep(token) {return true}
|
@@ -874,6 +970,7 @@ module Net::HTTPHeader
|
|
874
970
|
false
|
875
971
|
end
|
876
972
|
|
973
|
+
# Returns whether the HTTP session is to be kept alive.
|
877
974
|
def connection_keep_alive?
|
878
975
|
token = /(?:\A|,)\s*keep-alive\s*(?:\z|,)/i
|
879
976
|
@header['connection']&.grep(token) {return true}
|
data/lib/net/http/proxy_delta.rb
CHANGED