Class: Async::HTTP::Body::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/async/http/body/stream.rb

Overview

The input stream is an IO-like object which contains the raw HTTP POST data. When applicable, its external encoding must be “ASCII-8BIT” and it must be opened in binary mode, for Ruby 1.9 compatibility. The input stream must respond to gets, each, read and rewind.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output = Writable.new) ⇒ Stream

Returns a new instance of Stream.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
# File 'lib/async/http/body/stream.rb', line 28

def initialize(input, output = Writable.new)
  @input = input
  @output = output
  
  raise ArgumentError, "Non-writable output!" unless output.respond_to?(:write)
  
  # Will hold remaining data in `#read`.
  @buffer = nil
  @closed = false
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



39
40
41
# File 'lib/async/http/body/stream.rb', line 39

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



40
41
42
# File 'lib/async/http/body/stream.rb', line 40

def output
  @output
end

Instance Method Details

#closeObject

Close the input and output bodies.



140
141
142
143
144
145
# File 'lib/async/http/body/stream.rb', line 140

def close
  self.close_read
  self.close_write
ensure
  @closed = true
end

#close_readObject



131
132
133
# File 'lib/async/http/body/stream.rb', line 131

def close_read
  @input&.close
end

#close_writeObject



135
136
137
# File 'lib/async/http/body/stream.rb', line 135

def close_write
  @output&.close
end

#closed?Boolean

Whether the stream has been closed.

Returns:

  • (Boolean)


148
149
150
# File 'lib/async/http/body/stream.rb', line 148

def closed?
  @closed
end

#empty?Boolean

Whether there are any output chunks remaining?

Returns:

  • (Boolean)


153
154
155
# File 'lib/async/http/body/stream.rb', line 153

def empty?
  @output.empty?
end

#flushObject



128
129
# File 'lib/async/http/body/stream.rb', line 128

def flush
end

#read(size = nil, buffer = nil) ⇒ Object

read behaves like IO#read. Its signature is read([length, [buffer]]). If given, length must be a non-negative Integer (>= 0) or nil, and buffer must be a String and may not be nil. If length is given and not nil, then this method reads at most length bytes from the input stream. If length is not given or nil, then this method reads all data until EOF. When EOF is reached, this method returns nil if length is given and not nil, or “” if length is not given or is nil. If buffer is given, then the read data will be placed into buffer instead of a newly created String object.

Parameters:

  • length (Integer)

    the amount of data to read

  • buffer (String) (defaults to: nil)

    the buffer which will receive the data

Returns:

  • a buffer containing the data



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/async/http/body/stream.rb', line 49

def read(size = nil, buffer = nil)
  return '' if size == 0
  
  buffer ||= Async::IO::Buffer.new
  if @buffer
    buffer.replace(@buffer)
    @buffer = nil
  end
  
  if size
    while buffer.bytesize < size and chunk = read_next
      buffer << chunk
    end
    
    @buffer = buffer.byteslice(size, buffer.bytesize)
    buffer = buffer.byteslice(0, size)
    
    if buffer.empty?
      return nil
    else
      return buffer
    end
  else
    while chunk = read_next
      buffer << chunk
    end
    
    return buffer
  end
end

#read_nonblock(length, buffer = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/async/http/body/stream.rb', line 99

def read_nonblock(length, buffer = nil)
  @buffer ||= read_next
  chunk = nil
  
  return nil if @buffer.nil?
  
  if @buffer.bytesize > length
    chunk = @buffer.byteslice(0, length)
    @buffer = @buffer.byteslice(length, @buffer.bytesize)
  else
    chunk = @buffer
    @buffer = nil
  end
  
  if buffer
    buffer.replace(chunk)
  else
    buffer = chunk
  end
  
  return buffer
end

#read_partial(size = nil) ⇒ Object

Read at most ‘size` bytes from the stream. Will avoid reading from the underlying stream if possible.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/async/http/body/stream.rb', line 81

def read_partial(size = nil)
  if @buffer
    buffer = @buffer
    @buffer = nil
  else
    buffer = read_next
  end
  
  if buffer and size
    if buffer.bytesize > size
      @buffer = buffer.byteslice(size, buffer.bytesize)
      buffer = buffer.byteslice(0, size)
    end
  end
  
  return buffer
end

#write(buffer) ⇒ Object Also known as: write_nonblock



122
123
124
# File 'lib/async/http/body/stream.rb', line 122

def write(buffer)
  @output.write(buffer)
end