Class: Net::HTTP::Server::Stream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/net/http/server/stream.rb

Overview

Handles reading and writing to raw HTTP streams.

Since:

  • 0.2.0

Direct Known Subclasses

ChunkedStream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Stream

Creates a new stream.

Parameters:

  • socket (TCPSocket)

    The raw socket that will be read/write to.

Since:

  • 0.2.0



26
27
28
# File 'lib/net/http/server/stream.rb', line 26

def initialize(socket)
  @socket = socket
end

Instance Attribute Details

#socketObject (readonly)

The raw socket of the stream.

Since:

  • 0.2.0



16
17
18
# File 'lib/net/http/server/stream.rb', line 16

def socket
  @socket
end

Instance Method Details

#<<(data) ⇒ Object

See Also:

Since:

  • 0.2.0



108
109
110
# File 'lib/net/http/server/stream.rb', line 108

def <<(data)
  write(data)
end

#bodyString

Reads the entire body.

Returns:

  • (String)

    The complete body.

Since:

  • 0.2.0



78
79
80
81
82
83
# File 'lib/net/http/server/stream.rb', line 78

def body
  buffer = ''

  each { |chunk| buffer << chunk }
  return buffer
end

#closeObject

Closes the stream.

Since:

  • 0.2.0



117
118
# File 'lib/net/http/server/stream.rb', line 117

def close
end

#each {|chunk| ... } ⇒ Enumerator

Reads each chunk from the stream.

Yields:

  • (chunk)

    The given block will be passed each chunk.

Yield Parameters:

  • chunk (String)

    A chunk from the stream.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.

Since:

  • 0.2.0



62
63
64
65
66
67
68
# File 'lib/net/http/server/stream.rb', line 62

def each
  return enum_for unless block_given?

  while (chunk = read)
    yield chunk
  end
end

#read(length = 4096, buffer = '') ⇒ String?

Reads data from the stream.

Parameters:

  • length (Integer) (defaults to: 4096)

    The number of bytes to read.

  • buffer (#<<) (defaults to: '')

    The optional buffer to append the data to.

Returns:

  • (String, nil)

    A chunk from the stream.

Since:

  • 0.2.0



44
45
46
# File 'lib/net/http/server/stream.rb', line 44

def read(length=4096,buffer='')
  @socket.read(length,buffer)
end

#write(data) ⇒ Integer

Writes data to the stream.

Parameters:

  • data (String)

    The data to write to the stream.

Returns:

  • (Integer)

    The length of the data written.

Since:

  • 0.2.0



96
97
98
99
100
101
# File 'lib/net/http/server/stream.rb', line 96

def write(data)
  result = @socket.write(data)

  @socket.flush
  return result
end