Invoked by IO#read
or IO#Buffer.read to read length
bytes from io
into a specified buffer
(see IO::Buffer
) at the given offset
.
The length
argument is the “minimum length to be read”. If the IO
buffer size is 8KiB, but the length
is 1024
(1KiB), up to 8KiB might be read, but at least 1KiB will be. Generally, the only case where less data than length
will be read is if there is an error reading the data.
Specifying a length
of 0 is valid and means try reading at least once and return any available data.
Suggested implementation should try to read from io
in a non-blocking manner and call io_wait
if the io
is not ready (which will yield control to other fibers).
See IO::Buffer
for an interface available to return data.
Expected to return number of bytes read, or, in case of an error, -errno
(negated number corresponding to system’s error code).
The method should be considered experimental.
Invoked by IO#pread
or IO::Buffer#pread
to read length
bytes from io
at offset from
into a specified buffer
(see IO::Buffer
) at the given offset
.
This method is semantically the same as io_read
, but it allows to specify the offset to read from and is often better for asynchronous IO
on the same file.
The method should be considered experimental.
Returns the destination encoding as an encoding object.
Returns the one-character string which cause Encoding::UndefinedConversionError
.
ec = Encoding::Converter.new("ISO-8859-1", "EUC-JP") begin ec.convert("\xa0") rescue Encoding::UndefinedConversionError puts $!.error_char.dump #=> "\xC2\xA0" p $!.error_char.encoding #=> #<Encoding:UTF-8> end
Returns the destination encoding as an encoding object.
Returns the discarded bytes when Encoding::InvalidByteSequenceError
occurs.
ec = Encoding::Converter.new("EUC-JP", "ISO-8859-1") begin ec.convert("abc\xA1\xFFdef") rescue Encoding::InvalidByteSequenceError p $! #=> #<Encoding::InvalidByteSequenceError: "\xA1" followed by "\xFF" on EUC-JP> puts $!.error_bytes.dump #=> "\xA1" puts $!.readagain_bytes.dump #=> "\xFF" end
Returns the bytes to be read again when Encoding::InvalidByteSequenceError
occurs.
Returns the destination encoding as an Encoding
object.
Returns the length of the hash value of the digest.
This method should be overridden by each implementation subclass. If not, digest_obj.digest().length() is returned.
Reads at most maxlen bytes in the non-blocking manner.
When no data can be read without blocking it raises OpenSSL::SSL::SSLError
extended by IO::WaitReadable
or IO::WaitWritable
.
IO::WaitReadable
means SSL
needs to read internally so read_nonblock
should be called again when the underlying IO
is readable.
IO::WaitWritable
means SSL
needs to write internally so read_nonblock
should be called again after the underlying IO
is writable.
OpenSSL::Buffering#read_nonblock
needs two rescue clause as follows:
# emulates blocking read (readpartial). begin result = ssl.read_nonblock(maxlen) rescue IO::WaitReadable IO.select([io]) retry rescue IO::WaitWritable IO.select(nil, [io]) retry end
Note that one reason that read_nonblock
writes to the underlying IO
is when the peer requests a new TLS/SSL handshake. See openssl the FAQ for more details. www.openssl.org/support/faq.html
By specifying a keyword argument exception to false
, you can indicate that read_nonblock
should not raise an IO::Wait*able exception, but return the symbol :wait_writable
or :wait_readable
instead. At EOF, it will return nil
instead of raising EOFError
.
Parses multipart form elements according to
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
Returns a hash of multipart form parameters with bodies of type StringIO
or Tempfile
depending on whether the multipart form element exceeds 10 KB
params[name => body]
Generate an Image Button Input element as a string.
src
is the URL of the image to use for the button. name
is the input name. alt
is the alternative text for the image.
Alternatively, the attributes can be specified as a hash.
image_button("url") # <INPUT TYPE="image" SRC="url"> image_button("url", "name", "string") # <INPUT TYPE="image" SRC="url" NAME="name" ALT="string"> image_button("SRC" => "url", "ALT" => "string") # <INPUT TYPE="image" SRC="url" ALT="string">
Generate a Form element with multipart encoding as a String
.
Multipart encoding is used for forms that include file uploads.
action
is the action to perform. enctype
is the encoding type, which defaults to “multipart/form-data”.
Alternatively, the attributes can be specified as a hash.
multipart_form{ "string" } # <FORM METHOD="post" ENCTYPE="multipart/form-data">string</FORM> multipart_form("url") { "string" } # <FORM METHOD="post" ACTION="url" ENCTYPE="multipart/form-data">string</FORM>
Generate a Password Input element as a string.
name
is the name of the input field. value
is its default value. size
is the size of the input field display. maxlength
is the maximum length of the inputted password.
Alternatively, attributes can be specified as a hash.
password_field("name") # <INPUT TYPE="password" NAME="name" SIZE="40"> password_field("name", "value") # <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="40"> password_field("password", "value", 80, 200) # <INPUT TYPE="password" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200"> password_field("NAME" => "name", "VALUE" => "value") # <INPUT TYPE="password" NAME="name" VALUE="value">
Generates a radio-button Input element.
name
is the name of the input field. value
is the value of the field if checked. checked
specifies whether the field starts off checked.
Alternatively, the attributes can be specified as a hash.
radio_button("name", "value") # <INPUT TYPE="radio" NAME="name" VALUE="value"> radio_button("name", "value", true) # <INPUT TYPE="radio" NAME="name" VALUE="value" CHECKED> radio_button("NAME" => "name", "VALUE" => "value", "ID" => "foo") # <INPUT TYPE="radio" NAME="name" VALUE="value" ID="foo">
just for compatibility
Returns a hash of the key/value pairs:
req = Net::HTTP::Get.new(uri) req.to_hash # => {"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"], "accept"=>["*/*"], "user-agent"=>["Ruby"], "host"=>["jsonplaceholder.typicode.com"]}
Stores form data to be used in a POST
or PUT
request.
The form data given in params
consists of zero or more fields; each field is:
A scalar value.
A name/value pair.
An IO
stream opened for reading.
Argument params
should be an Enumerable (method params.map
will be called), and is often an array or hash.
First, we set up a request:
_uri = uri.dup _uri.path ='/posts' req = Net::HTTP::Post.new(_uri)
Argument params
As an Array
When params
is an array, each of its elements is a subarray that defines a field; the subarray may contain:
One string:
req.set_form([['foo'], ['bar'], ['baz']])
Two strings:
req.set_form([%w[foo 0], %w[bar 1], %w[baz 2]])
When argument enctype
(see below) is given as 'multipart/form-data'
:
A string name and an IO
stream opened for reading:
require 'stringio' req.set_form([['file', StringIO.new('Ruby is cool.')]])
A string name, an IO
stream opened for reading, and an options hash, which may contain these entries:
:filename
: The name of the file to use.
:content_type
: The content type of the uploaded file.
Example:
req.set_form([['file', file, {filename: "other-filename.foo"}]]
The various forms may be mixed:
req.set_form(['foo', %w[bar 1], ['file', file]])
Argument params
As a Hash
When params
is a hash, each of its entries is a name/value pair that defines a field:
The name is a string.
The value may be:
nil
.
Another string.
An IO
stream opened for reading (only when argument enctype
– see below – is given as 'multipart/form-data'
).
Examples:
# Nil-valued fields. req.set_form({'foo' => nil, 'bar' => nil, 'baz' => nil}) # String-valued fields. req.set_form({'foo' => 0, 'bar' => 1, 'baz' => 2}) # IO-valued field. require 'stringio' req.set_form({'file' => StringIO.new('Ruby is cool.')}) # Mixture of fields. req.set_form({'foo' => nil, 'bar' => 1, 'file' => file})
Optional argument enctype
specifies the value to be given to field 'Content-Type'
, and must be one of:
'application/x-www-form-urlencoded'
(the default).
'multipart/form-data'
; see RFC 7578.
Optional argument formopt
is a hash of options (applicable only when argument enctype
is 'multipart/form-data'
) that may include the following entries:
:boundary
: The value is the boundary string for the multipart message. If not given, the boundary is a random string. See Boundary.
:charset
: Value is the character set for the form submission. Field names and values of non-file fields should be encoded with this charset.
returns a Time
that represents the Last-Modified field.