class HTTP1Adapter

HTTP 1 adapter

Constants

HTTP1_REQUEST

Public Class Methods

new(socket) click to toggle source
# File lib/polyphony/http/client/http1.rb, line 11
def initialize(socket)
  @socket = socket
  @parser = HTTP::Parser.new(self)
end

Public Instance Methods

body() click to toggle source
# File lib/polyphony/http/client/http1.rb, line 57
def body
  @waiting_for_chunk = nil
  consume_response
  @buffered_body
end
consume_response() click to toggle source
# File lib/polyphony/http/client/http1.rb, line 95
def consume_response
  while !@done && (data = @socket.readpartial(8192))
    @parser << data
  end

  raise 'Socket closed by host' unless @done
end
each_chunk(&block) click to toggle source
# File lib/polyphony/http/client/http1.rb, line 63
def each_chunk(&block)
  if (body = @buffered_body)
    @buffered_body = nil
    @waiting_for_chunk = true
    block.(body)
  end
  while !@done && (data = @socket.readpartial(8192))
    @parser << data
  end
  raise 'Socket closed by host' unless @done

  @buffered_chunks.each(&block)
end
format_headers(headers) click to toggle source
# File lib/polyphony/http/client/http1.rb, line 122
def format_headers(headers)
  headers.map { |k, v| "#{k}: #{v}\r\n" }.join
end
format_http1_request(ctx) click to toggle source
# File lib/polyphony/http/client/http1.rb, line 110
def format_http1_request(ctx)
  headers = format_headers(ctx)

  format(
    HTTP1_REQUEST,
    method:  ctx[:method],
    request: ctx[:uri].request_uri,
    host:    ctx[:uri].host,
    headers: headers
  )
end
next_body_chunk() click to toggle source
# File lib/polyphony/http/client/http1.rb, line 77
def next_body_chunk
  return nil if @done
  if @buffered_chunks && !@buffered_chunks.empty?
    return @buffered_chunks.shift
  end

  read_next_body_chunk
end
on_body(chunk) click to toggle source
# File lib/polyphony/http/client/http1.rb, line 20
def on_body(chunk)
  if @waiting_for_chunk
    @buffered_chunks ||= []
    @buffered_chunks << chunk
  elsif @buffered_body
    @buffered_body << chunk
  else
    @buffered_body = +chunk
  end
end
on_headers_complete(headers) click to toggle source
# File lib/polyphony/http/client/http1.rb, line 16
def on_headers_complete(headers)
  @headers = headers
end
on_message_complete() click to toggle source
# File lib/polyphony/http/client/http1.rb, line 31
def on_message_complete
  @done = true
end
protocol() click to toggle source
# File lib/polyphony/http/client/http1.rb, line 126
def protocol
  :http1
end
read_headers() click to toggle source
# File lib/polyphony/http/client/http1.rb, line 48
def read_headers
  @headers = nil
  while !@headers && (data = @socket.readpartial(8192))
    @parser << data
  end

  raise 'Socket closed by host' unless @headers
end
read_next_body_chunk() click to toggle source
# File lib/polyphony/http/client/http1.rb, line 86
def read_next_body_chunk
  @waiting_for_chunk = true
  while !@done && (data = @socket.readpartial(8192))
    @parser << data
    break unless @buffered_chunks.empty?
  end
  @buffered_chunks.shift
end
request(ctx) click to toggle source
# File lib/polyphony/http/client/http1.rb, line 35
def request(ctx)
  # consume previous response if not finished
  consume_response if @done == false

  @socket << format_http1_request(ctx)

  @buffered_body = nil
  @done = false

  read_headers
  Response.new(self, @parser.status_code, @headers)
end