Class: Uma::HTTP::RackIO

Inherits:
Object
  • Object
show all
Defined in:
lib/uma/http.rb

Instance Method Summary collapse

Constructor Details

#initialize(env, stream) ⇒ RackIO

Returns a new instance of RackIO.



10
11
12
13
14
15
# File 'lib/uma/http.rb', line 10

def initialize(env, stream)
  @env = env
  @stream = stream
  @machine = stream.machine
  @fd = stream.fd
end

Instance Method Details

#closeObject



47
48
49
# File 'lib/uma/http.rb', line 47

def close
  @machine.close(@fd)
end

#eachObject



25
26
27
28
29
30
31
32
# File 'lib/uma/http.rb', line 25

def each(&)
  if @env['HTTP_TRANSFER_ENCODING'] == 'chunked'
    read_chunks(&)
  elsif (v = @env['CONTENT_LENGTH'])
    len = v.to_i
    yield @stream.get_string(+'', len)
  end
end

#getsObject



17
18
19
# File 'lib/uma/http.rb', line 17

def gets
  @stream.get_line(nil, 0)
end

#puts(str) ⇒ Object



51
52
53
# File 'lib/uma/http.rb', line 51

def puts(str)
  write("#{str}\n")
end

#read(len, buf = +'')) ⇒ Object



21
22
23
# File 'lib/uma/http.rb', line 21

def read(len, buf = +'')
  @stream.get_string(buf, -30)
end

#read_chunksObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/uma/http.rb', line 34

def read_chunks
  buf = +''
  while true
    line = @stream.get_line(buf, 8)
    len = line.to_i(16)
    break if len == 0

    chunk = @stream.get_string(nil, len)
    yield chunk
    @stream.skip(2)
  end
end

#write(*bufs) ⇒ Object Also known as: <<



55
56
57
# File 'lib/uma/http.rb', line 55

def write(*bufs)
  @machine.writev(@fd, *bufs)
end